19. The collections library
To choose the structure that fits what you are doing: the stack for the last thing in, the queue for the first thing in, and the set for membership without repetition.
Explanation
Section titled “Explanation”- After
استورد مجموعات(import collections), you build the structure from a list:كومة_من(stack from),طابور_من(queue from) andمجموعة_من(set from). The element type is inferred from the list, because they are generic structs written in Meem (since 0.17.0). - What only reads takes the structure itself (
اعلىtop,طول_كومةstack length,يحوي_عنصرcontains), and what changes it takes a pointer to it (ادفعpush,اسحبpop,اضف_اليهاadd). That is why it is built withجديد(...)(new) and read withمحتوى(...)(contents). - Taking from an empty structure returns
لاشيء(nothing), so the result ofاسحبandاعلىhas the typeن؟: give it a fallback withوالا(else). - Freeing (since 0.17.2):
حرر_كومة,حرر_طابورandحرر_مجموعةgive the structure’s memory back, so it is not used afterwards. For one allocated withجديد, free its contents and then the slot: writeخاتمه حرر(ك)and thenخاتمه مجموعات.حرر_كومة(محتوى(ك))next to the line that builds it;خاتمهruns in reverse order.
The examples here were run with the --debug-mem option, and the line «كل الخانات حُرّرت» (all slots were freed) at their end says that nothing was left.
The stack: the last thing in is the first thing out.
# الكومة: آخر ما يدخل أول ما يخرج، كالتراجع في محرراستورد مجموعات
خطوات = جديد(مجموعات.كومة_من(["كتب «م»", "كتب «ي»"]))خاتمه حرر(خطوات)خاتمه مجموعات.حرر_كومة(محتوى(خطوات))مجموعات.ادفع(خطوات, "حذف سطراً")اطبع("آخر خطوة: ", مجموعات.اعلى(محتوى(خطوات)) والا "لا شيء")طالما مجموعات.طول_كومة(محتوى(خطوات)) > 0: خ = مجموعات.اسحب(خطوات) والا "" اطبع("تراجع عن: ", خ)The queue: the first thing in is the first thing out.
# الطابور: أول من يصل أول من يُخدماستورد مجموعات
دور = جديد(مجموعات.طابور_من(["ليلى", "عمر"]))خاتمه حرر(دور)خاتمه مجموعات.حرر_طابور(محتوى(دور))مجموعات.ادفع_خلفاً(دور, "هند")اطبع("في الطابور: ", مجموعات.طول_طابور(محتوى(دور)))طالما مجموعات.طول_طابور(محتوى(دور)) > 0: ز = مجموعات.اسحب_اماماً(دور) والا "" اطبع("يُخدم الآن: ", ز)The set: each element appears once, and يحوي_عنصر answers: is it in there?
# المجموعة: عضوية بلا تكراراستورد مجموعات
زوار = جديد(مجموعات.مجموعة_من(["سلمى", "خالد", "سلمى"]))خاتمه حرر(زوار)خاتمه مجموعات.حرر_مجموعة(محتوى(زوار))اطبع("زوار مختلفون: ", مجموعات.طول_مجموعة(محتوى(زوار)))مجموعات.اضف_اليها(زوار, "نور")مجموعات.اضف_اليها(زوار, "خالد")اطبع("بعد الإضافة: ", مجموعات.طول_مجموعة(محتوى(زوار)))اطبع("هل زار خالد؟ ", مجموعات.يحوي_عنصر(محتوى(زوار), "خالد"))اطبع("حُذفت سلمى؟ ", مجموعات.احذف_منها(زوار, "سلمى"))اطبع("هل زارت سلمى؟ ", مجموعات.يحوي_عنصر(محتوى(زوار), "سلمى"))Exercise
Section titled “Exercise”Count the different words in a sentence with تقسيم (split) and then مجموعة_من. Then keep the squares a player passed through on a board in a set of a struct مربع (square) that implements قابل_للتجزئة, and ask whether the player passed through a given square. Free everything you built, so that --debug-mem prints no report (the parts from تقسيم go in an arena, given back at once).
Show the solution
# الكلمات المختلفة في جملة، ومربعات زارها لاعب على لوحاستورد مجموعات
هيكل مربع يحقق قابل_للتجزئة: صف عدد64 عمود عدد64
س_ = ساحة(الافتراضي)خاتمه اعد(س_)كلمات = تقسيم("ميم لغة سهلة ميم لغة عربية", " ", س_)مختلفة = مجموعات.مجموعة_من(كلمات)خاتمه مجموعات.حرر_مجموعة(مختلفة)اطبع("كل الكلمات: ", طول(كلمات), "، والمختلفة: ", مجموعات.طول_مجموعة(مختلفة))
مسار قائمة[مربع] = [مربع(0, 0), مربع(0, 1), مربع(0, 0), مربع(1, 1)]زارها = مجموعات.مجموعة_من(مسار)خاتمه مجموعات.حرر_مجموعة(زارها)اطبع("مربعات مختلفة: ", مجموعات.طول_مجموعة(زارها))اطبع("زار (1, 1)؟ ", مجموعات.يحوي_عنصر(زارها, مربع(1, 1)))اطبع("زار (2, 2)؟ ", مجموعات.يحوي_عنصر(زارها, مربع(2, 2)))حرر(مسار)Common mistakes
Section titled “Common mistakes”A set of a struct that does not implement قابل_للتجزئة (E0521):
استورد مجموعات
هيكل نقطة: س عدد64 ص عدد64
زيارات = مجموعات.مجموعة_من([نقطة(1, 2), نقطة(1, 2)])Exit code: 2
In the error catalog: E0521
The fix: هيكل نقطة يحقق قابل_للتجزئة:, so the compiler derives hashing and equality from the fields.
See also
Section titled “See also”The collections module in the reference · Generics and traits · Lists