Macaulay2 / M2

The primary source code repository for Macaulay2, a system for computing in commutative algebra, algebraic geometry and related fields.
https://macaulay2.com
336 stars 228 forks source link

delete for MutableList #1455

Open mahrud opened 4 years ago

mahrud commented 4 years ago

Is there a way to manipulate mutable lists without making a new copy? For instance:

A = new MutableList from {1,2,3};
append(A, 10) -- MutableList{...4...}
peek A -- MutableList{1, 2, 3}

For comparison, mutable hash tables can be manipulated in place:

B = new MutableHashTable from {a => 2}
B#b = 4
peek B
-- MutableHashTable{a => 2}
--                  b => 4
remove(B, b)
peek B -- MutableHashTable{a => 2}

I assume no copies are being made here, or at least I hope so!

DanGrayson commented 4 years ago

Yes:

Macaulay2, version 1.16
with packages: ConwayPolynomials, Elimination, IntegralClosure, InverseSystems, LLLBases,
               MinimalPrimes, PrimaryDecomposition, ReesAlgebra, TangentCone, Truncations

i1 : A = new MutableList

o1 = MutableList{}

o1 : MutableList

i2 : A#0=x

o2 = x

o2 : Symbol

i3 : A#1=y

o3 = y

o3 : Symbol

i4 : #A

o4 = 2

i5 : A##A=z -- here's how to append

o5 = z

o5 : Symbol

i6 : peek A

o6 = MutableList{x, y, z}
DanGrayson commented 4 years ago

PS: yes, when modifying a mutable hash table, no copies are made.

mahrud commented 4 years ago

Ah, thank you. How bad would it be to modify append and prepend to do this for mutable lists?

mahrud commented 3 years ago

Reopening to remember the bug with delete on a MutableList.

mahrud commented 3 years ago

Mutability of the mutable list is set to false.

i1 : L = new MutableList from {"A", "B"}

o1 = MutableList{...2...}

o1 : MutableList

i2 : mutable L

o2 = true

i3 : delete("A", L)

o3 = MutableList{...1...}

o3 : MutableList

i4 : mutable oo

o4 = false

i5 : delete(L, 1)

o5 = {0}

o5 : List