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