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

Constant Summary

Instance Attribute Summary (collapse)

Attributes inherited from Collection

#element, #it_ref

Attributes inherited from Type

#type, #type_ref

Instance Method Summary (collapse)

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?, #static, #static?, #write_decls, #write_defs, #write_intf

Methods inherited from Code

#attach, #priority, #source_size, #write_decls, #write_defs, #write_intf

Constructor Details

- (HashMap) initialize(type, key_type, value_type, visibility = :public)

Returns a new instance of HashMap



144
145
146
147
148
149
150
151
# File 'lib/autoc/collection/hash_map.rb', line 144

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

- (Object) key (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

- (Object) ==(other) Also known as: eql?



138
# File 'lib/autoc/collection/hash_map.rb', line 138

def ==(other) super && key == other.key end

- (Boolean) comparable?

Returns:

  • (Boolean)


155
# File 'lib/autoc/collection/hash_map.rb', line 155

def comparable?; super && key.comparable? end

- (Boolean) copyable?

Returns:

  • (Boolean)


153
# File 'lib/autoc/collection/hash_map.rb', line 153

def copyable?; super && key.copyable? end

- (Object) entities



142
# File 'lib/autoc/collection/hash_map.rb', line 142

def entities; super << key end

- (Object) hash



136
# File 'lib/autoc/collection/hash_map.rb', line 136

def hash; super ^ key.hash end

- (Boolean) hashable?

Returns:

  • (Boolean)


157
# File 'lib/autoc/collection/hash_map.rb', line 157

def hashable?; super && key.hashable? end

- (Object) write_impls(stream, define)



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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# File 'lib/autoc/collection/hash_map.rb', line 211

def write_impls(stream, define)
  super
  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_ref} key) {
      #{@entry.type} entry;
      entry.key = *key;
      entry.flags = AUTOC_VALID_KEY;
      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 = (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_ref} dst, #{@entry.type_ref} 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")};
    }
    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 & AUTOC_VALID_VALUE);
      #{value.copy("value", "e->value")};
      return value;
    }
    static #{@entry.type_ref} #{itGetEntryRef}(#{it_ref} 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

- (Object) write_intf_decls(stream, declare, define)



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/autoc/collection/hash_map.rb', line 187

def write_intf_decls(stream, declare, define)
  super
  stream << %$
    #{declare} #{ctor.declaration};
    #{declare} #{dtor.declaration};
    #{declare} #{copy.declaration};
    #{declare} #{equal.declaration};
    #{declare} #{identify.declaration};
    #{declare} void #{purge}(#{type_ref});
    #{declare} size_t #{size}(#{type_ref});
    #define #{empty}(self) (#{size}(self) == 0)
    #{declare} int #{containsKey}(#{type_ref}, #{key.type});
    #{declare} #{value.type} #{get}(#{type_ref}, #{key.type});
    #{declare} int #{put}(#{type_ref}, #{key.type}, #{value.type});
    #{declare} int #{replace}(#{type_ref}, #{key.type}, #{value.type});
    #{declare} int #{remove}(#{type_ref}, #{key.type});
    #{declare} void #{itCtor}(#{it_ref}, #{type_ref});
    #{declare} int #{itMove}(#{it_ref});
    #{declare} #{key.type} #{itGetKey}(#{it_ref});
    #{declare} #{value.type} #{itGetElement}(#{it_ref});
    #define #{itGet}(it) #{itGetElement}(it)
  $
end

- (Object) write_intf_types(stream)



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/autoc/collection/hash_map.rb', line 159

def write_intf_types(stream)
  super
  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_intf_types(stream)
  stream << %$
    typedef struct #{type} #{type};
    typedef struct #{it} #{it};
    struct #{type} {
      #{@set.type} entries;
    };
    struct #{it} {
      #{@set.it} it;
    };
  $
end