Class: AutoC::HashMap

Inherits:
Collection show all
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

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 typeContains().

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 simply put a new key/element pair into self in the way of typePut().

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

Attributes inherited from Collection

#element

Attributes inherited from Type

#type

Instance Method Summary collapse

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

#keyObject (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

#entitiesObject



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}(&lt, &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