Operations

AssociativeArray(): -> Assoc
Create the null associative array with no index universe. The first assignment to the array will determine its index universe.
AssociativeArray(I): Str -> Assoc
Create the empty associative array with index universe I.
A[x] := y : Assoc, Elt, Elt ->
Set the value in A associated with index x to be y. If x is not coercible into the current index universe I of A, then an attempt is first made to lift the index universe of A to contain both I and x.
A[x] : Assoc, Elt -> Elt
Given an index x coercible into the index universe I of A, return the value associated with x. If x is not in the keys of A, then an error is raised.
IsDefined(A, x) : Assoc, Elt -> Bool, Elt
Given an index x coercible into the index universe I of A, return whether x is currently in the keys of A and if so, return also the value A[x].
Remove(~A, x) : Assoc, Elt ->
(Procedure.) Destructively remove the value indexed by x from the array A. If x is not present as an index, then nothing happens (i.e., an error is not raised).
Universe(A): Assoc -> Str
Given an associative array A, return the index universe I of A, in which the keys of A currently lie.
# A: Assoc -> RngIntElt
Given an associative array A, return the number of items stored in A.
Keys(A): Assoc -> SetEnum
Given an associative array A, return the current keys of A as a set. Warning: this constructs a new copy of the set of keys, so should only be called when that is needed. It is not meant to be used as a quick access function.

Example Assoc_InvarField1 (H14E1)

This example shows simple use of associative arrays. First we create an array indexed by rationals.
> A := AssociativeArray();
> A[1/2] := 7;
> A[3/8] := "abc";
> A[3] := 3/8;
> A[1/2];
7
> IsDefined(A, 3);
true 3/8
> IsDefined(A, 4);
false
> IsDefined(A, 3/8);
true abc
> Keys(A);
{ 3/8, 1/2, 3 }
> for x in Keys(A) do x, A[x]; end for;
1/2 7
3/8 abc
3 3/8
> Remove(~A, 3/8);
> IsDefined(A, 3/8);
false
> Keys(A);
{ 1/2, 3 }
> Universe(A);
Rational Field
We repeat that an associative array can be indexed by elements of any structure. We now index an array by elements of the symmetric group S3.
> G := Sym(3);
> A := AssociativeArray(G);
> v := 1; for x in G do A[x] := v; v +:= 1; end for;
> A;
Associative Array with index universe GrpPerm: G, Degree 3, Order 2 * 3
> Keys(A);
{
    (1, 3, 2),
    (2, 3),
    (1, 3),
    (1, 2, 3),
    (1, 2),
    Id(G)
}
> A[G!(1,3,2)];
3
V2.28, 13 July 2023