Import HashTable.
1 The AlliedModders Template Library is a small set of C++ headers. It aims to be lightweight,
2 supporting a minimalistic set of abstractions and data structures useful for high-performance C++
3 systems code. It is the spiritual succesor to the SourceHook template library.
7 * Performance Over Generality. Rather than accomodate every use case, AMTL strives to make
8 performance explicit, and does not include features which are dangerous for performance, or
9 features that make malloc() failure detection more difficult.
11 For example, while STL provides copy constructors for most containers, AMTL does not, as these
12 operations are unsafe for fallible malloc(), and can cause large unintentional performance hits.
14 Another example is boost::shared\_ptr, which by default is atomic and threadsafe, and
15 preprocessor macros can be used to eliminate the thread safety option. AMTL opts to use distinct
16 types for threadsafe and non-threadsafe reference counting, as the cost of atomic refcounting
19 * Safety. Most code in AlliedModders does not take advantage of RAII, and does not account for
20 fallible malloc. While in general, infallible malloc is probably okay for game servers, the lack
21 of RAII leads to very complicated and bloated pointer management. A major goal of AMTL is to
22 introduce safer pointer management into its codebases, and to allow certain components to safely
23 handle malloc failures when desired.
25 * No linkage. EVerything is contained within headers so that tools don't need to link additional
26 C++ files. This could be a drawback, for example, it could increase build time or codesize
27 bloat. We may evaluate this decision in the future, but for now, it fits into our "lightweight"
30 (On some platforms, system libraries may be needed for certain headers.)
32 * No RTTI or EH. There are a few reasons for this:
33 + Both features require linkage to libstdc++. On Linux, this can cause problems with binary
36 + When using 32-bit Visual C++, try blocks are not zero-cost, as they are implemented using SEH.
38 * Fallible malloc. In many cases it's not desirable to terminate the program or throw an
39 exception when an allocation fails (for example, resizing a large vector). Thus, functions
40 like ke::Vector::append (analagous to std::vector::push\_back) will usually return bool. Whether
41 or not these functions return (i.e., abort the program or fail safely) can be specified in an
44 * Room to grow. These templates are intended to be a baseline of features needed for various
45 AlliedModders projects. They can and will grow as new features are needed, hopefully keeping the
46 above principles in mind.
50 All classes and functions reside in a "ke" namespace, and all macros are prefixed with "KE\_".
52 ### Platform Utilities (am-utilities.h)
54 This provides some common helper functions pretty much lumped together:
55 * AutoPtr<T>, RAII for calling delete on a pointer.
56 * AutoArray<T>, RAII for calling delete[] on a pointer.
57 * Detectors for add or multiply overflow, useful for computing allocation sizes.
58 * Fast Log2/Floor2 functions (also known as bitscan or count zeroes).
59 * Integer alignment (useful for allocators or code generators).
60 * KE\_DELETE and KE\_OVERRIDE which map to delete and override keywords if C++11 is available.
62 ### Thread Utilities (am-thread-utils.h)
64 Thread utils provides access to common sychronization primitives: mutexes, condition variables,
65 and threads. It also provides RAII objects for acquiring and releasing locks. More information is
66 available at the top of am-thread-utils.h.
68 ### Reference Counting (am-refcounting.h)
70 Two classes are available to help other classes implement reference counting. They are Refcounted
71 and RefcountedThreadsafe. Both must be inherited recursively, for example:
73 class MyObject : public Refcounted<MyObject>
75 The object is instantiated with a reference count of 1. A few RAII objects are provided for
76 automatically retaining and releasing references:
78 * Ref<T>. Adds one reference upon construction and releases one reference on destruction.
79 * PassRef<T>. Same as Ref<T>, except, when passed into a Ref<T> or into a PassRef<T>, the contained
80 reference is NULL'd, rather than performing an unnecessary extra AddRef()+Release(). PassRef<T>
81 is intended only for return values.
82 * Newborn<T>. If a newly allocated Refcounted object is stored into a Ref<T>, it will have one
83 extra reference. When allocating new refcounted objects, they should be immediately wrapped in a
84 Newborn<T> to prevent this.
86 RefcountedThreadsafe provides the same accessors as Refcounted. However, the reference counting is
87 performed using atomic operations, so that two threads racing do not accidentally result in a wrong
88 reference count (since a read + write is not normally atomic). This does not make the actual object
89 threadsafe, rather, it makes it safe for multiple threads to mutate the reference count.
91 ### Allocator Policies (am-allocator-policies.h)
93 ke::HashTable and ke::Vector are both parameterized on an "AllocPolicy". It is accessed whenever a
94 malloc(), free(), size overflow, or out-of-memory operation occurs. The default functionality of
95 SystemAllocatorPolicy is to abort the process on any failure, which can be changed by implementing
96 a new policy. As HashTable and Vector inherit from the AllocPolicy, it is possible to use custom
97 allocators without accessing static variables.
99 ### Vector (am-vector.h)
101 ke::Vector is roughly analagous to std::vector. A few method names are changed:
103 * push\_back() is append() and returns boolean.
104 * The underlying buffer of a vector can be "taken", which is sometimes useful for transferring
107 Vector does not have an iterator type. It should be iterated through normal sequential indexing.
109 ### HashTable (am-hashtable.h)
111 ke::HashTable is an open-addressing, linear-probing hash table. It is a bit more powerful than a
112 generic key -> value map as it is templated on a "policy", rather than keys and values. A policy
113 specifies what entries in the table look like, and how they are searched. Policies must have three
116 * Payload - the type that will be stored for each entry in the hash table.
117 * static bool matches(const T &t, Payload &p); - Must return true if |t| is equal to the entry at |p|.
118 * static uin32\_t hash(const T &t); - Must compute a hash for |t|.
120 The typename T in the policy applies for every key type that will be used in the hash table. For
121 example, a hash table might store string keys, but have faster lookup using std::string versus a
122 C-string, or even faster lookup for a string type that has its hash precomputed.
124 All key types must effectively compute the same hash; i.e., it would not be legal for Payload to
125 match both |int| and |std::string| but compute different hashes.
127 HashTable returns either a Result or Insert object when performing lookups, depending on the type
128 of lookup. A Result object is either found or not found. An Insert object, however, caches
129 information about where it found the object, so that it doesn't have to repeat the hashing and
130 lookup process for the subsequent add() call. Examples on how to use this are in am-hashmap.h, and
131 the exact rules on how Result and Insert objects can be used are in am-hashtable.h.
133 HashTables can be iterated. If the table is mutated during iteration outside of the interface
134 provided by the iterator, then the iterator may become corrupted. This is acceptable as long as the
135 iterator is abandoned. However, the table must stay alive for as long as the iterator is active.
137 ### HashMap (am-hashmap.h)
139 ke::HashMap is a key -> value map built upon HashTable. This saves you from having to implement our
140 own Policy and provides some more generic accessors. However it is still necessary to implement a
143 ### InlineList (am-inlinelist.h)
145 InlineList is a doubly-linked list that does use any calls to malloc(), with the caveat that list
146 members can be members in at most one inline list at a time. Objects must recursively inherit from
147 InlineListNode, which ensures that the object has prev/next pointers. Example:
149 class MyObject : public InlineListNode<MyObject>
151 InlineList<MyObject> list;
153 InlineList is ideal for situations where objects have already been allocated and will be a member
154 of at most one list at any time. If this is the case, the per-node malloc/free of std::list can be
157 An InlineList never claims ownership over its pointers. Its destructor is a no-op and it makes no
158 attempt to destroy members of its list (though in debug builds it will unlink members in its
159 destructor for assertion purposes).
161 ### LinkedList (am-linkedlist.h)
163 LinkedList is a doubly-linked list that requires a malloc() for each entry. It is provided for
164 backward compatibility, and it should be avoided in general, except in situations where it is
165 absolutely necessary and allocation performance is not a factor. Otherwise, use InlineList or
168 ### Strings (am-string.h)
170 The String API is still rather in flux. Currently, a simple std::string-like class is provided
171 called AString (short for ASCII String).
173 # Replacing SourceHook Includes
175 AMTL is a spiritual successor to the SourceHook template library, used in many AlliedModders
176 projects. AMTL is intended to deprecate it.
178 ### CVector (sh\_vector.h)
180 CVector can be replaced with ke::Vector. ke::Vector does not provide an iterator (yet). A few
181 method names have been changed, as CVector more closely models std::vector, however the classes are
182 pretty similar. Any missing methods could be added quite easily.
184 ### CString (sh\_string.h)
186 CString can be replaced with ke::AString, however some methods like trim() are not available yet.
187 One notable difference is that AString caches the length of the string, whereas CString does not.
188 That means AString is a larger data structure (by 4 bytes, on 32-bit), but is not prone to O(n^2)
189 operations like CString. This more closely matches the functionality of std::string.
191 ### List (sh\_list.h)
193 List can be replaced with ke::LinkedList. A few methods have changed, similar to Vector. For
194 example, push\_back() becomes append() and push\_front() becomes prepend(), and both return
197 Consider using ke::InlineList if your list contains already-allocated objects, and those objects
198 are members of at most one list, LinkedList will call malloc() and free() for each node.
200 The implementation of SourceHook::List and ke::LinkedList are basically identical underneath the
201 hood, however, copy construction and assignment are disallowed in the AMTL variant. This is because
202 they violate the memory model and also have very poor performance impliciations.
204 ### THash<K, V> (sh\_tinyhash.h)
206 TinyHash can be replaced with ke::HashMap, with minor changes. Comparators must be implemented as
207 equality operators on the key type, and the hash function is specified as a template parameter.
209 TinyHash is disadvantageous for a number of reasons. It uses separate chaining, and chains are
210 built on top of SourceHook::List, so it will use more memory on average. Inserts are also more
211 expensive as a call to malloc() is almost guaranteed. Retrieval is more expensive as well, since it
212 requires more indirection and has worse cache locality. It also has no ability to resize table
213 downward when its density is very low.
215 ### CStack (sh\_stack.h)
217 SourceHook::CStack is a stable-pointer stack. That is, if an element has been pushed, the address
218 the element is stored at is guaranteed not to change unless the element is removed. This can be an
219 important requirement in cases that involve re-entrancy. If you are relying on this feature of
220 CStack, there is no replacement in AMTL. If your CStack entries are LIFO, and tied to local
221 variables, you might consider simply linking objects together using RAII and avoiding CStack
222 entirely (though CStack will have better cache performance if you need to walk the list of entries).
224 If you don't need stable pointers, CStack can be replaced with ke::Vector.
228 All headers are licensed under a 3-clause BSD license.