{"id":10468,"date":"2024-12-15T12:30:52","date_gmt":"2024-12-15T11:30:52","guid":{"rendered":"https:\/\/oopm.org\/?page_id=10468"},"modified":"2025-02-04T13:13:17","modified_gmt":"2025-02-04T12:13:17","slug":"4-4-details-of-collection-classes","status":"publish","type":"page","link":"https:\/\/oopm.org\/?page_id=10468","title":{"rendered":"21. Details of collection classes"},"content":{"rendered":"<div class=\"pdfprnt-buttons pdfprnt-buttons-page pdfprnt-top-right\"><a href=\"https:\/\/oopm.org\/index.php?rest_route=wpv2pages10468&print=pdf\" class=\"pdfprnt-button pdfprnt-button-pdf\" target=\"_blank\"><img decoding=\"async\" src=\"https:\/\/oopm.org\/wp-content\/plugins\/pdf-print\/images\/pdf.png\" alt=\"image_pdf\" title=\"View PDF\" \/><\/a><a href=\"https:\/\/oopm.org\/index.php?rest_route=wpv2pages10468&print=print\" class=\"pdfprnt-button pdfprnt-button-print\" target=\"_blank\"><img decoding=\"async\" src=\"https:\/\/oopm.org\/wp-content\/plugins\/pdf-print\/images\/print.png\" alt=\"image_print\" title=\"Print Content\" \/><\/a><\/div>\n<p class=\"wp-block-paragraph\">In this section, we describe the details of the collection classes <code>Set<\/code> and <code>OrderedList<\/code>. There are other collection classes in the qBeta library, but they are not described here. We do not provide details of class <code>Array<\/code>, since <code>Array<\/code> is a built-in mechanism of qBeta.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Class Collection<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Class <code>Collection<\/code> is a general supercars of all collection classes in the qBeta library.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>class<\/strong> Collection(ElmType:&lt; Object):\n   insert(e: <strong>ref<\/strong> ElmType):&lt;\n      inner(insert)\n   has(e: <strong>ref<\/strong> ElmType) -&gt; B: <strong>var<\/strong> boolean:&lt;\n      inner(has)\n   isEmpty -&gt; b: <strong>var<\/strong> boolean:&lt;\n      inner(has)\n   remove(e: <strong>ref<\/strong> ElmType):&lt;\n      inner(remove)\n   size -&gt; v: <strong>var<\/strong> integer:&lt;\n      inner(size)\n   clear:&lt;\n      inner(clear)\n   inner(Collection)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Class <code>Collection<\/code> has the following attributes:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li> A virtual class parameter, <code>ElmType<\/code>, which specifies the type of the elements that may be inserted into the&nbsp;<code>Collection<\/code>.<\/li>\n\n\n\n<li>A method <code>insert<\/code> for inserting an element in the <code>Collection<\/code>.<\/li>\n\n\n\n<li>A method <code>has<\/code> that returns true if the parameter <code>e<\/code> is member of the <code>Collection<\/code>, false otherwise.<\/li>\n\n\n\n<li>A method <code>has<\/code> that returns true if there are no members in the  <code>Collection<\/code>, false otherwise.<\/li>\n\n\n\n<li>A method <code>remove<\/code> for removing an element <code>e<\/code> for the <code>Collection<\/code>.<\/li>\n\n\n\n<li>A method <code>size<\/code> that returns the number of elements in the <code>Collection<\/code>.<\/li>\n\n\n\n<li>A method <code>clear<\/code>, that remove all members of the <code>Collection<\/code>.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Class Set<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Class <code>Set<\/code> is a subclass of <code>Collection<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It has a local class <code>link<\/code> that is used to represent the elements of the list and a local reference, and a variable <code>head<\/code> that refers to the first element of the list:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>class<\/strong> Set: Collection\n   insert::&lt;\n       head:= link(e,head) \n   :::\n   <strong>class<\/strong> link(e: <strong>ref<\/strong> ElmType, next: <strong>ref<\/strong> link):\n      ...\n   head: <strong>ref<\/strong> link<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The representation of a <code>Set<\/code> objects is a so-called <em>linked list<\/em>. Each element in the list is a <code>link<\/code> object. Each <code>link<\/code> object contains a reference, e being an element in the <code>Set<\/code> and a reference <code>next<\/code> referring to the next element in the linked list. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The variable <code>head<\/code> refers to the first object in the linked list. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The virtual binding of <code>insert<\/code> creates a new <code>Link<\/code> object where the first parameter is <code>e<\/code>, the element to be inserted and the second parameter is the reference head referring to the current list representing the <code>Set<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Consider the following example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>s: <strong>obj<\/strong> Set(Record)\n<strong>class<\/strong> Record(id: <strong>var<\/strong> String): \n   ...\ns.insert(Record(\"Peter\"))\ns.insert(Record(\"Paul\"))\ns.insert(Record(\"Mary\"))<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">After execution of the above code, the following snapshot shows the situation of the <code>Set<\/code> <code>s<\/code> and its internal representation:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img fetchpriority=\"high\" decoding=\"async\" width=\"716\" height=\"287\" src=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/12\/setlink.jpg\" alt=\"\" class=\"wp-image-10683\" srcset=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/12\/setlink.jpg 716w, https:\/\/oopm.org\/wp-content\/uploads\/2024\/12\/setlink-300x120.jpg 300w\" sizes=\"(max-width: 716px) 100vw, 716px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">A <code>Set<\/code> also has an iterator <code>scan<\/code> that executes an <code>inner<\/code> for each element of the <code>Set<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>class<\/strong> Set: Collection\n   :::      \n   scan:\n      current: <strong>ref<\/strong> ElmType\n      nxt: <strong>ref<\/strong> link\n      scanTail:\n         :::\n      nxt := head\n      loop: <strong>do<\/strong>\n         if (nxt =\/= none) :then\n            current:= nxt.e\n            inner(scan)\n            nxt:= nxt.next\n            restart(loop)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">As can be seen, the <code>loop<\/code> object of <code>scan<\/code> goes through the linked list starting represented by <code>link<\/code> objects starting with <code>head<\/code> and for each <code>link<\/code> object it assigns its <code>e<\/code> reference to <code>current<\/code> and executes an <code>inner<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>Scan<\/code> has a local control pattern <code>scanTail<\/code>, which at a given point during a <code>scan<\/code> iterates over the rest of the elements in the <code>Set<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The full implementation of <code>Set<\/code> is shown below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>class<\/strong> Set: Collection\n   insert::&lt;\n       head:= link(e,head) \n   has::&lt;\n      b := false\n      scan\n         if (current == e) :then\n            b := true\n            leave(has)\n   isEmpty -> b: var Boolean::&lt;\n       b := head == none  \n   remove::&lt;\n      ...  \n   size::&lt;\n      scan\n         v := v + 1\n   clear::&lt;\n      head := none      \n   scan:\n      current: <strong>ref<\/strong> ElmType\n      nxt: <strong>ref<\/strong> link\n      nxt := head\n      scanTail:\n         current: <strong>ref<\/strong> ElmType\n         nxtt: <strong>ref<\/strong> link\n         nxtt := this(scan).nxt.next\n         loop: <strong>do<\/strong>\n            if (nxtt =\/= none) :then\n               current := nxtt.e\n               inner(scanTail)\n               nxtt := nxtt.next\n               restart(loop)\n      loop: <strong>do<\/strong>\n         if (nxt =\/= none) :then\n            current:= nxt.e\n            inner(scan)\n            nxt:= nxt.next\n            restart(loop)\n         :else\n            nxt:= nxt\n   %private\n   <strong>class<\/strong> link(e: <strong>ref<\/strong> ElmType next: <strong>ref<\/strong> link):\n      ...\n   head: <strong>ref<\/strong> link\n   inner(Set)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Note that class <code>Link<\/code> and head are <code>%private<\/code> attributes of <code>OrderedList<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Class OrderedList<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The implementation of class <code>Orderedlist<\/code> is similar the implementation of class <code>Set<\/code>. It has a class <code>link<\/code> and a reference head to the first <code>link<\/code> object holding the members of an <code>OrderedList<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In addition it has a reference variable <code>tail<\/code> referring to the last member of the linked list. The reason is a new member is inserted at the end of the linked list and not at the beginning as for class <code>Set<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> <strong>class<\/strong> OrderedList: Collection\n    :::\n    insert::\n       if (head == none) :then\n          head := Link(e):next(none)\n          tail := head\n       :else\n          t: <strong>ref<\/strong> Link:next\n          t := link(e):next(none)\n          tail.next := t\n          tail := t\n   :::\n   link(e: <strong>ref<\/strong> ElmType):next(next: b Link:next):\n        ...\n   head: <strong>ref<\/strong> link:next\n   tail: <strong>ref<\/strong> Link:next<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">When the first element is inserted (<code>head == none<\/code>) in an <code>OrderedList<\/code>, a <code>link<\/code> object is generated and <code>head<\/code> and <code>tail<\/code> are set to refer to this object.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When additional members are inserted (<code>head =\/= none<\/code>), a link object is generated and <code>tail.next<\/code> and <code>tail<\/code> is set to refer to this object.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Below we show a modified version of the above example using <code>Set<\/code>, but using an <code>OrderedList<\/code>. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>s: <strong>obj<\/strong> OrderedList(Record)\n<strong>class<\/strong> Record(id: <strong>var<\/strong> String): \n   ...\nr: <strong>ref<\/strong> Record\ns.insert(Record(\"Peter\"))\nr := Record(\"Paul\")\ns.insert(r)\ns.insert(Record(\"Mary\"))\ns.insert(r)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">We insert the object <code>Record(\"Paul\") <\/code>as referred to by <code>r<\/code> twice to illustrate that an <code>OrderedList<\/code> may have the same element more than once, which is not the case for <code>Set<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">After execution of the above code, the following snapshot shows the situation of the <code>OrderedList<\/code> <code>s<\/code> and its internal representation:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"948\" height=\"341\" src=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/12\/setlink-2.jpg\" alt=\"\" class=\"wp-image-10711\" srcset=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/12\/setlink-2.jpg 948w, https:\/\/oopm.org\/wp-content\/uploads\/2024\/12\/setlink-2-300x108.jpg 300w, https:\/\/oopm.org\/wp-content\/uploads\/2024\/12\/setlink-2-768x276.jpg 768w\" sizes=\"(max-width: 948px) 100vw, 948px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">As for <code>Set<\/code>, <code>OrderedList<\/code> also has a <code>scan<\/code> method.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The full implementation of&nbsp;<code>OrderedList<\/code>&nbsp;is shown below: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>OrderedList: <strong>obj<\/strong>\n   <strong>class<\/strong> OrderedList: Collection   \n      insert::\n         if (head == none) :then\n            head := Link(e):next(none)\n            tail := head\n         :else\n            t: <strong>ref<\/strong> Link:next\n            t := link(e):next(none)\n            tail.next := t\n            tail := t\n      has::&lt;\n         b := false\n         scan\n            if (current == e) :then\n                b := true\n                leave(has)\t\t\n      isEmpty::&lt;\n          b := head == none\n      remove::&lt;\n         ...\n      size::&lt;\n         scan\n             v := v + 1\n      clear::&lt;\n          head := none\n          tail := none\t\n      scan:\n         current: <strong>ref<\/strong> ElmType\n         nxt : <strong>ref<\/strong> Link:next\n         nxt := head\n         loop: <strong>do<\/strong>\n            if (nxt =\/= none) :then\n               current := nxt.e\n               inner(scan)\n               nxt := nxt.next\n               restart(loop)\n      %private\t       \n      link(E: <strong>ref<\/strong> ElmType):next(next: Link:next):\n         ...\n      head: <strong>ref<\/strong> link:next\n      tail: <strong>ref<\/strong> Link:next<\/code><\/pre>\n<div style=\"display:flex; gap:10px;justify-content:center\" class=\"wps-pgfw-pdf-generate-icon__wrapper-frontend\">\n\t\t<a  href=\"https:\/\/oopm.org?action=genpdf&amp;id=10468\" class=\"pgfw-single-pdf-download-button\" ><img src=\"https:\/\/oopm.org\/wp-content\/plugins\/pdf-generator-for-wp\/admin\/src\/images\/PDF_Tray.svg\" title=\"Generate PDF\" style=\"width:auto; height:45px;\"><\/a>\n\t\t<\/div>","protected":false},"excerpt":{"rendered":"<p>In this section, we describe the details of the collection classes Set and OrderedList. There are other collection classes in the qBeta library, but they are not described here. We do not provide details of class Array, since Array is a built-in mechanism of qBeta. Class Collection Class Collection is a general supercars of all [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":21,"menu_order":21,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-10468","page","type-page","status-publish","hentry"],"mb":[],"mfb_rest_fields":["title","gutenberg_elementor_mode"],"_links":{"self":[{"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/10468","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10468"}],"version-history":[{"count":28,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/10468\/revisions"}],"predecessor-version":[{"id":11340,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/10468\/revisions\/11340"}],"up":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/21"}],"wp:attachment":[{"href":"https:\/\/oopm.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10468"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}