Class: AutoC::HashMap
- Inherits:
-
Collection
- Object
- Code
- Type
- Collection
- AutoC::HashMap
- Includes:
- Maps
- Defined in:
- lib/autoc/collection/hash_map.rb
Overview
HashMap< K ⇒ E > is a hash-based unordered random access container holding unique keys with each key having an element bound to it.
HashMap is backed by HashSet container.
The collection’s C++ counterpart is std::unordered_map<> template class.
Generated C interface
Collection management
void typeCopy(Type * dst, Type * src) |
Create a new map dst filled with the contents of src. A copy operation is performed on all keys and values in src. NOTE: Previous contents of dst is overwritten. |
void typeCtor(Type * self) |
Create a new empty map self. NOTE: Previous contents of self is overwritten. |
void typeDtor(Type * self) |
Destroy map self. Stored keys and values are destroyed as well by calling the respective destructors. |
int typeEqual(Type * lt, Type * rt) |
Return non-zero value if maps lt and rt are considered equal by contents and zero value otherwise. |
size_t typeIdentify(Type * self) |
Return hash code for map self. |
Basic operations
int typeContainsKey(Type * self, K key) |
Return non-zero value if map self contains an entry with a key considered equal to the key key and zero value otherwise. |
int typeEmpty(Type * self) |
Return non-zero value if map self contains no entries and zero value otherwise. |
E typeGet(Type * self, K key) |
Return a copy of the element in self bound to a key which is considered equal to the key key. WARNING: self must contain such key otherwise the behavior is undefined. See typeContainsKey(). |
void typePurge(Type * self) |
Remove and destroy all keys and elements stored in self. |
int typePut(Type * self, K key, E value) |
Put a copy of the element value bound to a copy of the key key into self only if there is no such key in self which is considered equal to key. Return non-zero value on successful put and zero value otherwise. |
int typeReplace(Type * self, K key, E value) |
If self contains a key which is considered equal to the key key, remove and destroy that key along with an element bound to it and put a new pair built of the copies of key and value, otherwise no nothing. Return non-zero value if the replacement was actually performed and zero value otherwise. |
int typeRemove(Type * self, K key) |
Remove and destroy a key which is considered equal to the key key. Destroy an element bound to that key. Return non-zero value on successful key/element pair removal and zero value otherwise. |
size_t typeSize(Type * self) |
Return number of key/element pairs stored in self. |
Iteration
void itCtor(IteratorType * it, Type * self) |
Create a new iterator it on map self. NOTE: As the map is an unordered sequence, the traversal order is unspecified. NOTE: Previous contents of it is overwritten. |
int itMove(IteratorType * it) |
Advance iterator position of it and return non-zero value if new position is valid and zero value otherwise. |
K itGetKey(IteratorType * it) |
Return a copy of the key from a key/value pair pointed to by the iterator it. WARNING: current position must be valid otherwise the behavior is undefined. See itMove(). |
E itGetElement(IteratorType * it) |
Return a copy of the element from a key/element pair pointed to by the iterator it. WARNING: current position must be valid otherwise the behavior is undefined. See itMove(). |
E itGet(IteratorType * it) |
Alias for itGetElement(). |
Instance Attribute Summary collapse
-
#key ⇒ Object
readonly
Returns the value of attribute key.
Attributes inherited from Collection
Attributes inherited from Type
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #comparable? ⇒ Boolean
- #copyable? ⇒ Boolean
- #entities ⇒ Object
- #hash ⇒ Object
- #hashable? ⇒ Boolean
-
#initialize(type, key_type, value_type, visibility = :public) ⇒ HashMap
constructor
A new instance of HashMap.
- #write_impls(stream, define) ⇒ Object
- #write_intf_decls(stream, declare, define) ⇒ Object
- #write_intf_types(stream) ⇒ Object
Methods inherited from Collection
#constructible?, #destructible?, #initializable?
Methods inherited from Type
#abort, #assert, #calloc, coerce, #constructible?, #destructible?, #extern, #free, #initializable?, #inline, #malloc, #method_missing, #orderable?, #prefix, #private?, #public?, #sortable?, #static, #static?, #write_decls, #write_defs, #write_intf
Methods inherited from Code
#attach, #priority, #source_size, #write_decls, #write_defs, #write_intf
Constructor Details
#initialize(type, key_type, value_type, visibility = :public) ⇒ HashMap
Returns a new instance of HashMap
151 152 153 154 155 156 157 158 |
# File 'lib/autoc/collection/hash_map.rb', line 151 def initialize(type, key_type, value_type, visibility = :public) super(type, value_type, visibility) @key = Type.coerce(key_type) @entry = UserDefinedType.new(:type => entry, :identify => entryIdentify, :equal => entryEqual, :copy => entryCopy, :dtor => entryDtor) @set = HashSet.new(set, @entry, :static) element_requirement(value) key_requirement(key) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class AutoC::Type
Instance Attribute Details
#key ⇒ Object (readonly)
Returns the value of attribute key
139 140 141 |
# File 'lib/autoc/collection/hash_map.rb', line 139 def key @key end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
145 |
# File 'lib/autoc/collection/hash_map.rb', line 145 def ==(other) super && key == other.key end |
#comparable? ⇒ Boolean
162 |
# File 'lib/autoc/collection/hash_map.rb', line 162 def comparable?; super && key.comparable? end |
#copyable? ⇒ Boolean
160 |
# File 'lib/autoc/collection/hash_map.rb', line 160 def copyable?; super && key.copyable? end |
#entities ⇒ Object
149 |
# File 'lib/autoc/collection/hash_map.rb', line 149 def entities; super << key end |
#hash ⇒ Object
143 |
# File 'lib/autoc/collection/hash_map.rb', line 143 def hash; super ^ key.hash end |
#hashable? ⇒ Boolean
164 |
# File 'lib/autoc/collection/hash_map.rb', line 164 def hashable?; super && key.hashable? end |
#write_impls(stream, define) ⇒ Object
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 |
# File 'lib/autoc/collection/hash_map.rb', line 196 def write_impls(stream, define) super stream << %$ #define #{validValue} 1 #define #{validKey} 2 #define #{ownedValue} 4 #define #{ownedKey} 8 static #{@entry.type} #{entryKeyOnlyRef}(#{key.type_ref} key) { #{@entry.type} entry; entry.key = *key; entry.flags = #{validKey}; return entry; } static #{@entry.type} #{entryKeyValueRef}(#{key.type_ref} key, #{value.type_ref} value) { #{@entry.type} entry; entry.key = *key; entry.value = *value; entry.flags = (#{validKey} | #{validValue}); return entry; } #define #{entryIdentify}(obj) #{entryIdentifyRef}(&obj) static size_t #{entryIdentifyRef}(#{@entry.type}* entry) { return #{key.identify("entry->key")}; } #define #{entryEqual}(lt, rt) #{entryEqualRef}(<, &rt) static int #{entryEqualRef}(#{@entry.type}* lt, #{@entry.type}* rt) { return #{key.equal("lt->key", "rt->key")}; } #define #{entryCopy}(dst, src) #{entryCopyRef}(&dst, &src) static void #{entryCopyRef}(#{@entry.type_ref} dst, #{@entry.type_ref} src) { #{assert}(src->flags & #{validKey}); dst->flags = (#{validKey} | #{ownedKey}); #{key.copy("dst->key", "src->key")}; if(src->flags & #{validValue}) { dst->flags |= (#{validValue} | #{ownedValue}); #{value.copy("dst->value", "src->value")}; } } #define #{entryDtor}(obj) #{entryDtorRef}(&obj) static void #{entryDtorRef}(#{@entry.type}* entry) { #{assert}(entry->flags & #{validKey}); if(entry->flags & #{ownedKey}) #{key.dtor("entry->key")}; if(entry->flags & #{validValue} && entry->flags & #{ownedValue}) #{value.dtor("entry->value")}; } static #{@entry.type_ref} #{itGetEntryRef}(#{it_ref}); static int #{containsAllOf}(#{type_ref} self, #{type_ref} other) { #{it} it; #{itCtor}(&it, self); while(#{itMove}(&it)) { int found = 0; #{@entry.type}* e = #{itGetEntryRef}(&it); if(#{containsKey}(other, e->key)) { #{value.type} other_value = #{get}(other, e->key); found = #{value.equal("e->value", "other_value")}; #{value.dtor("other_value")}; } if(!found) return 0; } return 1; } $ @set.write_intf_decls(stream, static, inline) @set.write_impls(stream, static) stream << %$ #{define} #{ctor.definition} { #{assert}(self); #{@set.ctor}(&self->entries); } #{define} #{dtor.definition} { #{assert}(self); #{@set.dtor}(&self->entries); } static int #{putEntryRef}(#{type_ref} self, #{@entry.type_ref} entry) { int absent; #{assert}(self); #{assert}(entry); absent = !#{containsKey}(self, entry->key); if(absent) #{@set.put}(&self->entries, *entry); return absent; } #{define} #{copy.definition} { #{it} it; #{assert}(src); #{assert}(dst); #{ctor}(dst); #{itCtor}(&it, src); while(#{itMove}(&it)) { #{@entry.type}* e = #{itGetEntryRef}(&it); #{putEntryRef}(dst, e); } } #{define} #{equal.definition} { #{assert}(lt); #{assert}(rt); return #{size}(lt) == #{size}(rt) && #{containsAllOf}(lt, rt) && #{containsAllOf}(rt, lt); } #{define} #{identify.definition} { #{assert}(self); return #{@set.identify}(&self->entries); /* TODO : make use of the values' hashes */ } #{define} void #{purge}(#{type_ref} self) { #{assert}(self); #{@set.purge}(&self->entries); } #{define} size_t #{size}(#{type_ref} self) { #{assert}(self); return #{@set.size}(&self->entries); } #{define} int #{containsKey}(#{type_ref} self, #{key.type} key) { int result; #{@entry.type} entry; #{assert}(self); result = #{@set.contains}(&self->entries, entry = #{entryKeyOnlyRef}(&key)); #{@entry.dtor("entry")}; return result; } #{define} #{value.type} #{get}(#{type_ref} self, #{key.type} key) { #{value.type} result; #{@entry.type} entry, existing_entry; #{assert}(self); #{assert}(#{containsKey}(self, key)); existing_entry = #{@set.get}(&self->entries, entry = #{entryKeyOnlyRef}(&key)); #{value.copy("result", "existing_entry.value")}; #{@entry.dtor("existing_entry")}; #{@entry.dtor("entry")}; return result; } #{define} int #{put}(#{type_ref} self, #{key.type} key, #{value.type} value) { int result; #{@entry.type} entry; #{assert}(self); entry = #{entryKeyValueRef}(&key, &value); result = #{putEntryRef}(self, &entry); #{@entry.dtor("entry")}; return result; } #{define} int #{replace}(#{type_ref} self, #{key.type} key, #{value.type} value) { int result; #{@entry.type} entry; #{assert}(self); entry = #{entryKeyValueRef}(&key, &value); result = #{@set.replace}(&self->entries, entry); #{@entry.dtor("entry")}; return result; } #{define} int #{remove}(#{type_ref} self, #{key.type} key) { int result; #{@entry.type} entry; #{assert}(self); result = #{@set.remove}(&self->entries, entry = #{entryKeyOnlyRef}(&key)); #{@entry.dtor("entry")}; return result; } #{define} void #{itCtor}(#{it_ref} self, #{type_ref} map) { #{assert}(self); #{assert}(map); #{@set.itCtor}(&self->it, &map->entries); } #{define} int #{itMove}(#{it_ref} self) { #{assert}(self); return #{@set.itMove}(&self->it); } #{define} #{key.type} #{itGetKey}(#{it_ref} self) { #{@entry.type_ref} e; #{key.type} key; #{assert}(self); e = #{itGetEntryRef}(self); #{key.copy("key", "e->key")}; return key; } #{define} #{value.type} #{itGetElement}(#{it_ref} self) { #{@entry.type_ref} e; #{value.type} value; #{assert}(self); e = #{itGetEntryRef}(self); #{assert}(e->flags & #{validValue}); #{value.copy("value", "e->value")}; return value; } static #{@entry.type_ref} #{itGetEntryRef}(#{it_ref} self) { #{assert}(self); return #{@set.itGetRef}(&self->it); } $ end |
#write_intf_decls(stream, declare, define) ⇒ Object
189 190 191 192 193 194 |
# File 'lib/autoc/collection/hash_map.rb', line 189 def write_intf_decls(stream, declare, define) super stream << %$ #{declare} void #{itCtor}(#{it_ref}, #{type_ref}); $ end |
#write_intf_types(stream) ⇒ Object
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/autoc/collection/hash_map.rb', line 166 def write_intf_types(stream) super stream << %$ typedef struct #{@entry.type} #{@entry.type}; struct #{@entry.type} { #{key.type} key; #{value.type} value; unsigned flags; }; $ @set.write_intf_types(stream) stream << %$ typedef struct #{type} #{type}; typedef struct #{it} #{it}; struct #{type} { #{@set.type} entries; }; struct #{it} { #{@set.it} it; }; $ end |