| | 1 | #charset "us-ascii" |
| | 2 | |
| | 3 | /* |
| | 4 | * Copyright (c) 2000, 2006 Michael J. Roberts |
| | 5 | * |
| | 6 | * This file is part of TADS 3. |
| | 7 | * |
| | 8 | * The header defines the LookupTable and WeakRefLookupTable intrinsic |
| | 9 | * classes, as well as LookupTableIterator, the special Iterator type for |
| | 10 | * these classes. |
| | 11 | */ |
| | 12 | |
| | 13 | #ifndef _LOOKUP_H_ |
| | 14 | #define _LOOKUP_H_ |
| | 15 | |
| | 16 | /* include our base class definitions */ |
| | 17 | #include "systype.h" |
| | 18 | |
| | 19 | /* |
| | 20 | * The LookupTable intrinsic class provides a general-purpose hash table |
| | 21 | * implementation. LookupTable can be used syntactically as though it were |
| | 22 | * a list, but the index values are arbitrary hash key values rather than |
| | 23 | * being limited to sequential integers. |
| | 24 | */ |
| | 25 | intrinsic class LookupTable 'lookuptable/030002': Collection |
| | 26 | { |
| | 27 | /* |
| | 28 | * Determine if a given key is present in the table. Returns true if |
| | 29 | * the key is present, nil if not. |
| | 30 | */ |
| | 31 | isKeyPresent(key); |
| | 32 | |
| | 33 | /* |
| | 34 | * Remove an entry from the table. Removes the key/value pair |
| | 35 | * associated with the given key, and returns the value that was |
| | 36 | * associated with the key. If the key isn't present in the table, |
| | 37 | * the return value is nil, and the method has no other effect. |
| | 38 | */ |
| | 39 | removeElement(key); |
| | 40 | |
| | 41 | /* |
| | 42 | * Apply the given function to each entry, and replace the value of |
| | 43 | * the entry with the return value of the function. The callback is |
| | 44 | * invoked with the key and value as arguments for each entry: |
| | 45 | * func(key, value). No return value. |
| | 46 | */ |
| | 47 | applyAll(func); |
| | 48 | |
| | 49 | /* |
| | 50 | * Invoke the given function with each entry in the table. The |
| | 51 | * function is invoked with value of an entry as its argument: |
| | 52 | * func(value). Any return value of the function is ignored. No |
| | 53 | * return value. |
| | 54 | */ |
| | 55 | forEach(func); |
| | 56 | |
| | 57 | /* |
| | 58 | * Get the number of buckets (i.e., slots for unique hash values). |
| | 59 | * The number of buckets doesn't vary over the life of the table, so |
| | 60 | * this simply returns the number of buckets that was specified in the |
| | 61 | * constructor when the table was created. This can be used to create |
| | 62 | * a new table with the same parameters as an existing table. |
| | 63 | */ |
| | 64 | getBucketCount(); |
| | 65 | |
| | 66 | /* |
| | 67 | * Get the number of entries. This returns the number of key/value |
| | 68 | * pairs stored in the table. Note that this is not the same as the |
| | 69 | * initial capacity specified in the constructor when the table was |
| | 70 | * created; this is the number of entries actually stored in the |
| | 71 | * table. |
| | 72 | */ |
| | 73 | getEntryCount(); |
| | 74 | |
| | 75 | /* |
| | 76 | * Invoke the given function with each entry in the table, passing the |
| | 77 | * key and value to the callback. The function is invoked with key |
| | 78 | * and value of an entry as its arguments: func(key, value). Any |
| | 79 | * return value of the function is ignored. No return value. |
| | 80 | */ |
| | 81 | forEachAssoc(func); |
| | 82 | |
| | 83 | /* |
| | 84 | * Make a list of all of my keys or values. The return value is a |
| | 85 | * list, in arbitrary order, of all of the keys or values in the table. |
| | 86 | */ |
| | 87 | keysToList(); |
| | 88 | valsToList(); |
| | 89 | } |
| | 90 | |
| | 91 | /* |
| | 92 | * WeakRefLookupTable is a "weak reference" version of the basic lookup |
| | 93 | * table. This is similar to the regular LookupTable, and has the same |
| | 94 | * methods; the only difference is that this type of table references its |
| | 95 | * values "weakly." A value that is reachable only through weak references |
| | 96 | * is subject to deletion by the garbage collector. A weak-reference |
| | 97 | * lookup table is useful when you don't want a value's presence in the |
| | 98 | * table to force the value to stay active, such as when the lookup table |
| | 99 | * is merely a fast index to a set of values that must be otherwise |
| | 100 | * reachable to be useful. When the garbage collector deletes one of our |
| | 101 | * values, the key/value pair for the value is automatically deleted from |
| | 102 | * the table. |
| | 103 | */ |
| | 104 | intrinsic class WeakRefLookupTable 'weakreflookuptable/030000': LookupTable |
| | 105 | { |
| | 106 | } |
| | 107 | |
| | 108 | /* |
| | 109 | * LookupTable iterator - this type of iterator is used for LookupTable and |
| | 110 | * WeakRefLookupTable instances. |
| | 111 | */ |
| | 112 | intrinsic class LookupTableIterator 'lookuptable-iterator/030000': Iterator |
| | 113 | { |
| | 114 | } |
| | 115 | |
| | 116 | #endif /* _LOOKUP_H_ */ |