Macaulay2 » Documentation
Packages » Macaulay2Doc » The Macaulay2 language » lists and sequences » MutableList
next | previous | forward | backward | up | index | toc

MutableList -- the class of all mutable lists

Description

For an overview of lists and sequences, see lists and sequences.

Normally the entries in a mutable list are not printed, to prevent infinite loops in the printing routines. To print them out, use peek.

i1 : s = new MutableList from {a,b,c};
i2 : s#2 = 1234

o2 = 1234
i3 : s

o3 = MutableList{...3...}

o3 : MutableList
i4 : peek s

o4 = MutableList{a, b, 1234}

To construct a mutable list with a given length and whose entries are all null, provide an integer.

i5 : s = new MutableList from 6;
i6 : peek s

o6 = MutableList{null, null, null, null, null, null}

If a value is assigned at a position that is not yet present in a mutable list, then it grows accordingly, and any intermediate positions are filled with null.

i7 : t = new MutableList from {};
i8 : t#4 = e

o8 = e

o8 : Symbol
i9 : peek t

o9 = MutableList{null, null, null, null, e}

Functions that iterate over a mutable list, such as apply, scan, select, any, and for loops, work with a snapshot of its entries taken when the iteration begins. So if the list is modified during the iteration, e.g., by growing or shrinking it, the iteration still visits exactly the original entries, while the modifications take effect in the list itself.

i10 : u = new MutableList from {1, 2, 3};
i11 : for i in u list (u##u = 10 * i; i)

o11 = {1, 2, 3}

o11 : List
i12 : peek u

o12 = MutableList{1, 2, 3, 10, 20, 30}

A mutable list may have room for more entries than it currently holds; the number of entries it has room for is its capacity, which is reported by size(BasicList). When an assignment makes the list longer than its capacity, the capacity is doubled until the list fits, so appending entries one at a time takes linear time overall. When the list becomes shorter than a quarter of its capacity, e.g., after using remove, the capacity is halved (but never below 4).

i13 : s = new MutableList;
i14 : for i to 8 list (s#i = i; (#s, size s))

o14 = {(1, 4), (2, 4), (3, 4), (4, 4), (5, 8), (6, 8), (7, 8), (8, 8), (9,
      -----------------------------------------------------------------------
      16)}

o14 : List
i15 : for i to 8 list (remove(s, 0); (#s, size s))

o15 = {(8, 16), (7, 16), (6, 16), (5, 16), (4, 16), (3, 8), (2, 8), (1, 4),
      -----------------------------------------------------------------------
      (0, 4)}

o15 : List

See also

Menu

Types of mutable list:

  • Bag -- the class of all bags

Methods that use a mutable list:

  • new MutableList from ZZ
  • positions(MutableList,Function) -- see positions -- which elements of a list satisfy a condition
  • remove(MutableList,ZZ) -- see remove -- remove an entry from a mutable hash table, list, or database
  • shuffle(MutableList) -- see shuffle -- shuffle a list randomly

For the programmer

The object MutableList is a type, with ancestor classes BasicList < Thing.


The source of this document is in /build/reproducible-path/macaulay2-1.26.11+ds/M2/Macaulay2/packages/Macaulay2Doc/ov_lists.m2:853:0.