ORDTABLE
signature
Ordered tables are tables which are ordered by their keys.
This interface is nearly identical to
TABLE
except for the ordered table
functions starting with first
,
Key
now ascribing to
ORDKEY
, and
Set
now ascribing to
ORDSET
.
structure Key : ORDKEY
structure Seq : SEQUENCE
type α t
type α table = α t
exception Order
structure Set : ORDSET where Key = Key and Seq = Seq
val size : α table → int
val domain : α table → Set.t
val range : α table → α Seq.t
val toString : (α → string) → α table → string
val toSeq : α table → (Key.t * α) Seq.t
val find : α table → Key.t → α option
val insert : α table * (Key.t * α) → α table
val insertWith : (α * α → α) → α table * (Key.t * α) → α table
val delete : α table * Key.t → α table
val empty : unit → α table
val singleton : Key.t * α → α table
val tabulate : (Key.t → α) → Set.t → α table
val collect : (Key.t * α) Seq.t → α Seq.t table
val fromSeq : (Key.t * α) Seq.t → α table
val map : (α → β) → α table → β table
val mapKey : (Key.t * α → β) → α table → β table
val filter : (α → bool) → α table → α table
val filterKey : (Key.t * α → bool) → α table → α table
val reduce : (α * α → α) → α → α table → α
val iterate : (β * α → β) → β → α table → β
val iteratePrefixes : (β * α → β) → β → α table → (β table * β)
val union : (α * α → α) → α table * α table → α table
val intersection : (α * β → γ) → α table * β table → γ table
val difference : α table * β table → α table
val restrict : α table * Set.t → α table
val subtract : α table * Set.t → α table
val $ : (Key.t * α) → α table
val first : α table → (Key.t * α) option
val last : α table → (Key.t * α) option
val prev : α table * Key.t → (Key.t * α) option
val next : α table * Key.t → (Key.t * α) option
val split : α table * Key.t → α table * α option * α table
val join : α table * α table → α table
val getRange : α table → Key.t * Key.t → α table
val rank : α table * Key.t → int
val select : α table * int → (Key.t * α) option
val splitRank : α table * int → α table * α table
structure Key :
ORDKEY
Key
substructure defines the key type of
tables, providing equality and other useful functions.
structure Seq :
SEQUENCE
Seq
substructure defines the sequence type to
and from which tables can be converted.
structure Set :
ORDSET where Key = Key and Seq = Seq
Set
substructure contains operations on
sets with elements of type Key.t
.
type α t
type α table =
α t
α t
, for readability.
exception Order
Order
is raised whenever table operations would violate the ordering invariant.val size :
α table → int
val domain :
α table → Set.t
val range :
α table → α Seq.t
val toString :
(α → string) → α table → string
(toString f t)
evaluates to a string representation of t
where each key is converted to a string with Key.toString
and each value is converted with f. For example, the table
{(1↦2),(3↦42)} might be represented by the string
“{(1->2),(3->42)}
“. The ordering of key-value
pairs in the resulting string is sorted by key.
val toSeq :
α table → (Key.t * α) Seq.t
val find :
α table → Key.t → α option
(find t k)
returns (SOME
v) if
(k↦v)∈t, and NONE
otherwise.
val insert :
α table * (Key.t * α) → α table
(insert (t, (k, v)))
returns the table
t∪{(k↦v)} where v is given precedence over v′ if
there already exists a (k↦v′)∈t.
val insertWith :
(α * α → α) → α table * (Key.t * α) → α table
(insertWith f (t, (k, v)))
returns the table
t∪{(k↦v)} if k is not already a member of t,
otherwise it returns t∪{(k↦f(v′,v))} where
(k↦v′)∈t.
val delete :
α table * Key.t → α table
(delete (t, k))
deletes k only if it is present in t.
That is, it evaluates to t∖{(k↦v)}
if (k↦v)∈t. Otherwise, it evaluates to t.
val empty :
unit → α table
val singleton :
Key.t * α → α table
(singleton (k, v))
evaluates to the singleton table
{(k↦v)}.
val tabulate :
(Key.t → α) → Set.t → α table
(tabulate f x)
evaluates to the table
{(k↦f(k)):k∈x}.
val collect :
(Key.t * α) Seq.t → α Seq.t table
(collect s)
is logically equivalent to
(fromSeq (Seq.collect Key.compare s))
.
val fromSeq :
(Key.t * α) Seq.t → α table
val map :
(α → β) → α table → β table
(map f t)
returns the table
{(k↦f(v)):(k↦v)∈t}.
val mapKey :
(Key.t * α → β) → α table → β table
(mapKey f t)
returns the table
{(k↦f(k,v)):(k↦v)∈t}.
val filter :
(α → bool) → α table → α table
(filter p t)
returns the table containing every
(k↦v)∈t which satisfies p(v).
val filterKey :
(Key.t * α → bool) → α table → α table
(filterKey p t)
returns the table containing every
(k↦v)∈t which satisfies p(k,v).
val reduce :
(α * α → α) → α → α table → α
(reduce f b t)
is logically equivalent to
(Seq.reduce f b (range t))
.
val iterate :
(β * α → β) → β → α table → β
(iterate f b t)
is logically equivalent to
(Seq.iterate f b (range t))
.
val iteratePrefixes :
(β * α → β) → β → α table → (β table * β)
(iteratePrefixes f b t)
is logically equivalent to
(fromSeq p, x)
where
(p,x) = (Seq.iteratePrefixes f b (range t))
.
val union :
(α * α → α) → α table * α table → α table
(union f (a, b))
evaluates to a∪b. For keys k where
(k↦v)∈a and (k↦w)∈b, we have
(k↦f(v,w)) in the resulting table.
val intersection :
(α * β → γ) → α table * β table → γ table
(intersection f (a, b))
evaluates to the table a∩b.
Every intersecting key k is mapped to f(v,w), where
(k↦v)∈a and (k↦w)∈b.
val difference :
α table * β table → α table
(difference (a, b))
evaluates to the table a∖b.
val restrict :
α table * Set.t → α table
restrict
is basically an alias for intersection
in that it restricts a table's domain by the elements of a set.
The name is motivated by the interpretation of a table as a function,
and therefore (restrict (t, x))
evaluates to t|x.
val subtract :
α table * Set.t → α table
subtract
is basically an alias for difference
in that it removes all mappings whose keys are given in a set. The name
is once again motivated by the interpretation of a table as a function,
where (subtract (t, x))
performs the domain subtraction
of t by x. This is also sometimes referred to as the domain
anti-restriction.
val $ :
(Key.t * α) → α table
singleton
.
val first :
α table → (Key.t * α) option
NONE
if the table is empty.
val last :
α table → (Key.t * α) option
NONE
if the table is empty.
val prev :
α table * Key.t → (Key.t * α) option
(prev (t, k))
evaluates to the maximum key (paired with its
associated value) in t which is strictly less than k,
or NONE
if there is no such key.
val next :
α table * Key.t → (Key.t * α) option
(next (t, k))
evaluates to the minimum key (paired with its
associated value) in t which is strictly greater than k,
or NONE
if there is no such key.
val split :
α table * Key.t → α table * α option * α table
(split (t, k))
evaluates to (l,v,r), where
l={(k′↦v′)∈t | k′<k},
r={(k′↦v′)∈t | k′>k}, and v is k's associated
value, or NONE
if k∉t.
val join :
α table * α table → α table
(join (x, y))
evaluates to x∪y.
val getRange :
α table → Key.t * Key.t → α table
(getRange t (a, b))
evaluates to
{(k↦v)∈t | a≤k≤b}.
val rank :
α table * Key.t → int
(rank (t, k))
evaluates to
|{(k′↦v′)∈t | k′<k}|.
val select :
α table * int → (Key.t * α) option
(select (t, i))
evaluates to the ith largest
key (paired with its associated value) in t, or NONE
if
either i<0 or i≥|t|.
val splitRank :
α table * int → α table * α table
(splitRank (t, i))
evaluates to (l,r) such that
|l|=i, |r|=|t|−i, every key in l is strictly less than
every key in r, and their union is t.
Raises Fail
if i<0 or i≥|t|.