Class: AutoC::HashMap
- Inherits:
-
Collection
- Object
- Code
- Type
- Collection
- AutoC::HashMap
- Defined in:
- lib/autoc/collection/hash_map.rb
Overview
HashSet is a hash-based unordered random access container holding unique keys with each key having an element bound to it.
The collection’s C++ counterpart is std::unordered_map<> template class.
Generated C interface
Collection management
- cols=2*
-
|=== |void ~type~Copy(Type *
dst, Type *src) | Create a new mapdstfilled with the contents ofsrc. A copy operation is performed on all keys and values insrc.NOTE: Previous contents of
dstis overwritten.|void ~type~Ctor(Type *
self) | Create a new empty mapself.NOTE: Previous contents of
selfis overwritten.|void ~type~Dtor(Type *
self) | Destroy mapself. Stored keys and values are destroyed as well by calling the respective destructors.|int ~type~Equal(Type *
lt, Type *rt) | Return non-zero value if mapsltandrtare considered equal by contents and zero value otherwise.|size_t ~type~Identify(Type *
self) | Return hash code for mapself. |===Basic operations
- cols=2*
-
|=== |int ~type~ContainsKey(Type *
self, Kkey) | Return non-zero value if mapselfcontains an entry with a key considered equal to the keykeyand zero value otherwise.|int ~type~Empty(Type *
self) | Return non-zero value if mapselfcontains no entries and zero value otherwise.|E ~type~Get(Type *
self, Kkey) | Return a copy of the element inselfbound to a key which is considered equal to the keykey.WARNING:
selfmust contain such key otherwise the behavior is undefined. See ~type~Contains().|void ~type~Purge(Type *
self) | Remove and destroy all keys and elements stored inself.|int ~type~Put(Type *
self, Kkey, Evalue) | Put a copy of the elementvaluebound to a copy of the keykeyintoself*only if* there is no such key inselfwhich is considered equal tokey.Return non-zero value on successful put and zero value otherwise.
|int ~type~Replace(Type *
self, Kkey, Evalue) | Ifselfcontains a key which is considered equal to the keykey, remove and destroy that key along with an element bound to it and put a new pair built of the copies ofkeyandvalue, otherwise simply put a new key/element pair intoselfin the way of ~type~Put().Return non-zero value if the replacement was actually performed and zero value otherwise.
|int ~type~Remove(Type *
self, Kkey) | Remove and destroy a key which is considered equal to the keykey. Destroy an element bound to that key.Return non-zero value on successful key/element pair removal and zero value otherwise.
|size_t ~type~Size(Type *
self) | Return number of key/element pairs stored inself. |===Iteration
- cols=2*
-
|=== |void ~it~Ctor(IteratorType *
it, Type *self) | Create a new iteratoriton mapself.NOTE: As the map is an unordered sequence, the traversal order is unspecified.
NOTE: Previous contents of
itis overwritten.|int ~it~Move(IteratorType *
it) | Advance iterator position ofitand return non-zero value if new position is valid and zero value otherwise.|K ~it~GetKey(IteratorType *
it) | Return a copy of the key from a key/value pair pointed to by the iteratorit.WARNING: current position must be valid otherwise the behavior is undefined. See ~it~Move().
|E ~it~GetElement(IteratorType *
it) | Return a copy of the element from a key/element pair pointed to by the iteratorit.WARNING: current position must be valid otherwise the behavior is undefined. See ~it~Move().
|E ~it~Get(IteratorType *
it) | Alias for ~it~GetElement(). |===
Constant Summary
Constants inherited from Type
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
- #entities ⇒ Object
-
#initialize(type, key_type, value_type, visibility = :public) ⇒ HashMap
constructor
A new instance of HashMap.
- #write_exported_declarations(stream, declare, define) ⇒ Object
- #write_exported_types(stream) ⇒ Object
- #write_implementations(stream, define) ⇒ Object
Methods inherited from Collection
coerce, #copy, #ctor, #dtor, #equal, #identify, #less
Methods inherited from Type
#abort, #assert, #calloc, #extern, #free, #inline, #malloc, #method_missing, #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.
138 139 140 141 142 143 |
# File 'lib/autoc/collection/hash_map.rb', line 138 def initialize(type, key_type, value_type, visibility = :public) super(type, value_type, visibility) @key = Collection.coerce(key_type) @entry = UserDefinedType.new(:type => entry, :identify => entryIdentify, :equal => entryEqual, :copy => entryCopy, :dtor => entryDtor) @set = HashSet.new(set, @entry, :static) 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.
132 133 134 |
# File 'lib/autoc/collection/hash_map.rb', line 132 def key @key end |
Instance Method Details
#entities ⇒ Object
136 |
# File 'lib/autoc/collection/hash_map.rb', line 136 def entities; super + [key] end |
#write_exported_declarations(stream, declare, define) ⇒ Object
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/autoc/collection/hash_map.rb', line 172 def write_exported_declarations(stream, declare, define) stream << %$ #{declare} void #{ctor}(#{type}*); #{declare} void #{dtor}(#{type}*); #{declare} void #{copy}(#{type}*, #{type}*); #{declare} int #{equal}(#{type}*, #{type}*); #{declare} size_t #{identify}(#{type}*); #{declare} void #{purge}(#{type}*); #{declare} size_t #{size}(#{type}*); #define #{empty}(self) (#{size}(self) == 0) #{declare} int #{containsKey}(#{type}*, #{key.type}); #{declare} #{value.type} #{get}(#{type}*, #{key.type}); #{declare} int #{put}(#{type}*, #{key.type}, #{value.type}); #{declare} int #{replace}(#{type}*, #{key.type}, #{value.type}); #{declare} int #{remove}(#{type}*, #{key.type}); #{declare} void #{itCtor}(#{it}*, #{type}*); #{declare} int #{itMove}(#{it}*); #{declare} #{key.type} #{itGetKey}(#{it}*); #{declare} #{value.type} #{itGetElement}(#{it}*); #define #{itGet}(it) #{itGetElement}(it) $ end |
#write_exported_types(stream) ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/autoc/collection/hash_map.rb', line 145 def write_exported_types(stream) stream << %$ /*** **** #{type}<#{key.type},#{value.type}> (#{self.class}) ***/ $ if public? stream << %$ typedef struct #{@entry.type} #{@entry.type}; struct #{@entry.type} { #{key.type} key; #{value.type} value; unsigned flags; }; $ @set.write_exported_types(stream) stream << %$ typedef struct #{type} #{type}; typedef struct #{it} #{it}; struct #{type} { #{@set.type} entries; }; struct #{it} { #{@set.it} it; }; $ end |
#write_implementations(stream, define) ⇒ Object
195 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 381 382 383 |
# File 'lib/autoc/collection/hash_map.rb', line 195 def write_implementations(stream, define) stream << %$ #define AUTOC_VALID_VALUE 1 #define AUTOC_VALID_KEY 2 #define AUTOC_OWNED_VALUE 4 #define AUTOC_OWNED_KEY 8 static #{@entry.type} #{entryKeyOnlyRef}(#{key.type}* key) { #{@entry.type} entry; entry.key = *key; entry.flags = AUTOC_VALID_KEY; return entry; } static #{@entry.type} #{entryKeyValueRef}(#{key.type}* key, #{value.type}* value) { #{@entry.type} entry; entry.key = *key; entry.value = *value; entry.flags = (AUTOC_VALID_KEY | AUTOC_VALID_VALUE); 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}* dst, #{@entry.type}* src) { #{assert}(src->flags & AUTOC_VALID_KEY); dst->flags = (AUTOC_VALID_KEY | AUTOC_OWNED_KEY); #{key.copy("dst->key", "src->key")}; if(src->flags & AUTOC_VALID_VALUE) { dst->flags |= (AUTOC_VALID_VALUE | AUTOC_OWNED_VALUE); #{value.copy("dst->value", "src->value")}; } } #define #{entryDtor}(obj) #{entryDtorRef}(&obj) static void #{entryDtorRef}(#{@entry.type}* entry) { #{assert}(entry->flags & AUTOC_VALID_KEY); if(entry->flags & AUTOC_OWNED_KEY) #{key.dtor("entry->key")}; if(entry->flags & AUTOC_VALID_VALUE && entry->flags & AUTOC_OWNED_VALUE) #{value.dtor("entry->value")}; } $ @set.write_exported_declarations(stream, static, inline) @set.write_implementations(stream, static) stream << %$ static #{@entry.type}* #{itGetEntryRef}(#{it}*); #{define} void #{ctor}(#{type}* self) { #{assert}(self); #{@set.ctor}(&self->entries); } #{define} void #{dtor}(#{type}* self) { #{assert}(self); #{@set.dtor}(&self->entries); } static int #{putEntryRef}(#{type}* self, #{@entry.type}* entry) { int absent; #{assert}(self); #{assert}(entry); if((absent = !#{containsKey}(self, entry->key))) { #{@set.put}(&self->entries, *entry); } return absent; } #{define} void #{copy}(#{type}* dst, #{type}* src) { #{it} it; #{assert}(src); #{assert}(dst); #{ctor}(dst); #{itCtor}(&it, src); while(#{itMove}(&it)) { #{@entry.type}* e = #{itGetEntryRef}(&it); #{putEntryRef}(dst, e); } } static int #{containsAllOf}(#{type}* self, #{type}* 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; } #{define} int #{equal}(#{type}* lt, #{type}* rt) { #{assert}(lt); #{assert}(rt); return #{size}(lt) == #{size}(rt) && #{containsAllOf}(lt, rt) && #{containsAllOf}(rt, lt); } #{define} size_t #{identify}(#{type}* self) { #{assert}(self); return #{@set.identify}(&self->entries); /* TODO : make use of the values' hashes */ } #{define} void #{purge}(#{type}* self) { #{assert}(self); #{@set.purge}(&self->entries); } #{define} size_t #{size}(#{type}* self) { #{assert}(self); return #{@set.size}(&self->entries); } #{define} int #{containsKey}(#{type}* 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}* 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}* 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}* 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}* 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}* self, #{type}* map) { #{assert}(self); #{assert}(map); #{@set.itCtor}(&self->it, &map->entries); } #{define} int #{itMove}(#{it}* self) { #{assert}(self); return #{@set.itMove}(&self->it); } #{define} #{key.type} #{itGetKey}(#{it}* self) { #{@entry.type}* e; #{key.type} key; #{assert}(self); e = #{itGetEntryRef}(self); #{key.copy("key", "e->key")}; return key; } #{define} #{value.type} #{itGetElement}(#{it}* self) { #{@entry.type}* e; #{value.type} value; #{assert}(self); e = #{itGetEntryRef}(self); #{assert}(e->flags & AUTOC_VALID_VALUE); #{value.copy("value", "e->value")}; return value; } static #{@entry.type}* #{itGetEntryRef}(#{it}* self) { #{assert}(self); return #{@set.itGetRef}(&self->it); } #undef AUTOC_VALID_VALUE #undef AUTOC_VALID_KEY #undef AUTOC_OWNED_VALUE #undef AUTOC_OWNED_KEY $ end |