weak tables - Maple Help
For the best experience, we recommend viewing online help using Google Chrome or Microsoft Edge.

Online Help

All Products    Maple    MapleSim


table

  

weak

  

create a weak table

 

Calling Sequence

Description

Thread Safety

Examples

Compatibility

Calling Sequence

table[weak](...)

table[weak_key](...)

table[weak_value](...)

Description

• 

An entry in a weak table will automatically be removed when the key and/or value is no longer in use.  

• 

An entry in a weak_key table will be kept alive as long as something else in the system refers to the same key expression.  More specifically, an entry will be removed from a weak_key table if any of the following conditions are true:

– 

The key is a small integer

– 

The key is a sequence and contains one or more objects that are not in use

– 

The key is a sequence and contains only small integers

– 

The key is not a sequence is not in use

• 

An entry in a weak_value table will be kept alive as long as something else in the system refers to the same entry expression.  More specifically, an entry will be removed from a weak_value table if any of the following conditions are true:

– 

The value is a small integer

– 

The value is not in use

• 

The third kind of weak table incorporates both of the above conditions, and is constructed via table['weak'](...).  Index/entry pairs will be discarded when both the weak_key and weak_value criteria are satisfied.  That is, the key/value pair will remain in the table as long as either the key or value is in use somewhere else in the system.

• 

The key conditions treat a sequence specially because of the way indexing is implemented in Maple.  When you index T[X,Y], a temporary (X,Y) sequence is created behind the scenes.  This (X,Y) pair is not likely to be referred to anywhere else, so the pair itself not being in use is not part of the criteria for discarding the entry.  On the other hand, when you insert T[i] := (X,Y) it is more explicit, and the entire assigned entry alone is considered.  

• 

Maple represents small integers as hardware-integer references, which do not require an object header or extra memory allocation to wrap them.  Therefore they are not tracked by the garbage collector, and it cannot be determined whether they are in use or not somewhere else in the system.  For this reason, small integers are considered as not in use for the purpose of determining whether a key or value should be discarded from a weak table.  On 64-bit systems, a "small integer" in this context is one that is in the range -2^62+1 .. 2^62-1.  On 32-bit systems, the range is -2^30+1 .. 2^30-1.

• 

It can be difficult to predict when a particular entry will have no other references to it in Maple's memory space.  References could be held by ditto, in a remember table, on the stack, or may be conservatively kept alive because there happens to be a matching bit-pattern in the heap that Maple walks.  Weak references will only be discarded after a garbage collection sweep.

• 

A weak table will behave exactly like a regular table in every way -- creation, indexing, and operations.  A weak table, in fact, is a regular table, except that after garbage collection, it is inspected to clean up references that are no longer in use.

Thread Safety

• 

The table[weak] command is thread-safe as of Maple 2019.

• 

For more information on thread safety, see index/threadsafe.

Examples

alivez2

alivez2

(1)

Ttableweak_key1=x2,z2=2

Ttable1=x2,z2=2

(2)

T1

x2

(3)

Tz2

2

(4)

Do some other things, then trigger a collection

1:2:3:int1x412,x:

gc

The T[1] entry has been removed.  The T[z^2] entry's key is referenced in the variable alive, so it stays in the table.

entriesT,pairs

z2=2

(5)

Because table indexing can return unevaluated, it is a common pattern in code to do something like this:

ifassignedTithenvalTiendif:

With weak-tables, the entry at T[i] could disappear between the assigned() check and the fetch to assign to val, possibly leading to strange and hard to reproduce errors.  There are several ways to safely work around this.

Pattern#1: check assigned and get the value all at once:

ifassignedTi,valthenendif:

Pattern#2: compare against evaln

valTi:ifvalevalnTithenendif:

Pattern#3: use a sparse table

Ttableweak_keysparse=_NOTFOUND:

valTi:

ifval_NOTFOUNDthenendif:

Pattern#4: check that the value is an expected type

valTi:

ifval::Notindexedthenendif:

Compatibility

• 

The table[weak] command was introduced in Maple 2019.

• 

For more information on Maple 2019 changes, see Updates in Maple 2019.

See Also

assigned

evaln

gc

indexed

indexfcn

indices

option,system

selection

sequence

sparse

table

type/table