# 9.80 Table¶

The Table constructor provides a general structure for associative storage. This type provides hash tables in which data objects can be saved according to keys of any type. For a given table, specific types must be chosen for the keys and entries.

In this example the keys to the table are polynomials with integer coefficients. The entries in the table are strings.

t: Table(Polynomial Integer, String) := table()


 table()

Type: Table(Polynomial Integer,String)

To save an entry in the table, the seteltseteltTable operation is used. This can be called directly, giving the table a key and an entry.

setelt(t, x^2 - 1, "Easy to factor")


 “Easytofactor”

Type: String

Alternatively, you can use assignment syntax.

t(x^3 + 1) := "Harder to factor"


 “Hardertofactor”

Type: String

t(x) := "The easiest to factor"


 “Theeasiesttofactor”

Type: String

elt(t, x)


 “Theeasiesttofactor”

Type: String

This operation is called when a table is applied to a key using this or the following syntax.

t.x


 “Theeasiesttofactor”

Type: String

t x


 “Theeasiesttofactor”

Type: String

Parentheses are used only for grouping. They are needed if the key is an infixed expression.

t.(x^2 - 1)


 “Easytofactor”

Type: String

Note that the elteltTable operation is used only when the key is known to be in the table—otherwise an error is generated.

t (x^3 + 1)


 “Hardertofactor”

Type: String

You can get a list of all the keys to a table using the keyskeysTable operation.

keys t


 [x,x3+1,x2-1]

Type: List Polynomial Integer

If you wish to test whether a key is in a table, the searchsearchTable operation is used. This operation returns either an entry or “failed”.

search(x, t)


 “Theeasiesttofactor”

Type: Union(String,...)

search(x^2, t)


 “failed”

Type: Union(“failed”,...)

The return type is a union so the success of the search can be tested using case.

search(x^2, t) case "failed"


 true

Type: Boolean

The removeremoveTable operation is used to delete values from a table.

remove!(x^2-1, t)


 “Easytofactor”

Type: Union(String,...)

If an entry exists under the key, then it is returned. Otherwise removeremoveTable returns “failed”.

remove!(x-1, t)


 “failed”

Type: Union(“failed”,...)

The number of key-entry pairs can be found using the # #Table operation.

#t


 2

Type: PositiveInteger

Just as keyskeysTable returns a list of keys to the table, a list of all the entries can be obtained using the membersmembersTable operation.

members t


 [“Theeasiesttofactor”,”Hardertofactor”]

Type: List String

A number of useful operations take functions and map them on to the table to compute the result. Here we count the entries which have Hard as a prefix.

count(s: String +-> prefix?("Hard", s), t)


 1

Type: PositiveInteger

Other table types are provided to support various needs. \indent

AssociationList gives a list with a table view. This allows new entries to be appended onto the front of the list to cover up old entries. This is useful when table entries need to be stacked or when frequent list traversals are required. See AssociationListXmpPage for more information.

EqTable gives tables in which keys are considered equal only when they are in fact the same instance of a structure. See EqTableXmpPage for more information.

StringTable should be used when the keys are known to be strings. See StringTableXmpPage for more information.

SparseTable provides tables with default entries, so lookup never fails. The GeneralSparseTable constructor can be used to make any table type behave this way. See SparseTableXmpPage for more information.

KeyedAccessFile allows values to be saved in a file, accessed as a table. See KeyedAccessFileXmpPage for more information.