CardToCardTab.mesa
Copyright Ó 1990, 1992 by Xerox Corporation. All rights reserved.
Last tweaked by Mike Spreitzer on October 16, 1990 9:37:48 am PDT
A CardToCardTab is a hash table which associates CARD keys with CARD values. CardToCardTab is like RefTab and SymTab except for the Key and Val types.
CardToCardTab: CEDAR DEFINITIONS = BEGIN
Ref: TYPE = REF CardToCardTabRep;
CardToCardTabRep: TYPE = MONITORED RECORD [impl: REF CardToCardTabImplRep];
CardToCardTabImplRep: TYPE;
Key: TYPE = CARD;
Val: TYPE = CARD;
EqualProc: TYPE = PROC [key1, key2: Key] RETURNS [BOOL];
HashProc: TYPE = PROC [key: Key] RETURNS [CARDINAL];
Create: PROC [mod: NAT ¬ 17, equal: EqualProc ¬ NIL, hash: HashProc ¬ NIL] RETURNS [Ref];
... creates new table with suggested hash size.
When equal=NIL, the keys are compared with `key1=key2'.
When hash=NIL, some reasonable hash function is used.
GetSize: PROC [x: Ref] RETURNS [INT];
... returns number of key-value pairs in table (use this to properly deal with monitored data in preference to using the size field)
Fetch: PROC [x: Ref, key: Key, default: Val ¬ Val.LAST] RETURNS [found: BOOL, val: Val];
... looks up key in table, returns associated value (if any)
if found = TRUE, val = value associated with given key
if found = FALSE, val = default
Replace: PROC [x: Ref, key: Key, val: Val] RETURNS [BOOL];
... replaces value for existing key; returns TRUE if previous value overwritten; if no previous value for key, returns FALSE without inserting new pair
Store: PROC [x: Ref, key: Key, val: Val] RETURNS [BOOL];
... stores new key-value pair, overwriting previous value for given key; returns TRUE if new value, FALSE if previous value overwritten
Insert: PROC [x: Ref, key: Key, val: Val] RETURNS [BOOL];
... inserts new key-value pair, except if previous value for given key; returns TRUE if inserted, FALSE if previous value for key
Delete: PROC [x: Ref, key: Key] RETURNS [BOOL];
... deletes key-value pair associated with given key; returns TRUE if deletion actually occurred, FALSE if no such key
Erase: PROC [x: Ref];
... deletes all key-value pairs in the table
EachPairAction: TYPE = PROC [key: Key, val: Val] RETURNS [quit: BOOL ¬ FALSE];
Pairs: PROC [x: Ref, action: EachPairAction] RETURNS [BOOL];
... enumerates pairs currently in symbol table in unspecified order; pairs inserted/deleted during enumeration may or may not be seen; applies action to each pair until action returns TRUE or no more pairs; returns TRUE if some action returns TRUE
UpdateOperation: TYPE = {none, store, delete};
UpdateAction: TYPE
= PROC [found: BOOL, val: Val]
RETURNS [op: UpdateOperation ¬ none, new: Val ¬ Val.LAST];
Update: PROC [x: Ref, key: Key, action: UpdateAction];
... atomically performs an update action; looks up key and calls action, which returns the desired operation. If op=none, the table is not changed; if op=store, the new value is stored; if op=delete, key is removed from the table.
Copy: PROC [x: Ref] RETURNS [Ref];
... atomically copies the table.
END.
Carl Hauser, January 14, 1988 2:27:48 pm PST
Folded in CedarChest7.0's RefTabExtras interface