Class: AutoC::HashSet

Inherits:
Collection show all
Includes:
Iterators::Unidirectional, Sets
Defined in:
lib/autoc/collection/hash_set.rb

Overview

HashSet< E > is a hash-based unordered container holding unique elements.

The collection’s C++ counterpart is std::unordered_set<> template class.

Generated C interface

Collection management

void typeCopy(Type * dst, Type * src)

Create a new set dst filled with the contents of src. A copy operation is performed on every element in src.

NOTE: Previous contents of dst is overwritten.

void typeCtor(Type * self)

Create a new empty set self.

NOTE: Previous contents of self is overwritten.

void typeDtor(Type * self)

Destroy set self. Stored elements are destroyed as well by calling the respective destructors.

int typeEqual(Type * lt, Type * rt)

Return non-zero value if sets lt and rt are considered equal by contents and zero value otherwise.

size_t typeIdentify(Type * self)

Return hash code for set self.

Basic operations

int typeContains(Type * self, E what)

Return non-zero value if set self contains an element considered equal to the element what and zero value otherwise.

int typeEmpty(Type * self)

Return non-zero value if set self contains no elements and zero value otherwise.

E typeGet(Type * self, E what)

Return a copy of the element in self considered equal to the element what.

WARNING: self must contain such element otherwise the behavior is undefined. See typeContains().

void typePurge(Type * self)

Remove and destroy all elements stored in self.

int typePut(Type * self, E what)

Put a copy of the element what into self only if there is no such element in self which is considered equal to what.

Return non-zero value on successful element put (that is there was not such element in self) and zero value otherwise.

int typeReplace(Type * self, E with)

If self contains an element which is considered equal to the element with, replace that element with a copy of with, otherwise do nothing. Replaced element is destroyed.

Return non-zero value if the replacement was actually performed and zero value otherwise.

int typeRemove(Type * self, E what)

Remove and destroy an element in self which is considered equal to the element what.

Return non-zero value on successful element removal and zero value otherwise.

size_t typeSize(Type * self)

Return number of elements stored in self.

Logical operations

void typeExclude(Type * self, Type * other)

Perform the difference operation that is self will retain only the elements not contained in other.

Removed elements are destroyed.

void typeInclude(Type * self, Type * other)

Perform the union operation that is self will contain the elements from both self and other.

self receives the copies of extra elements in other.

void typeInvert(Type * self, Type * other)

Perform the symmetric difference operation that is self will retain the elements contained in either self or other, but not in both.

Removed elements are destroyed, extra elements are copied.

void typeRetain(Type * self, Type * other)

Perform the intersection operation that is self will retain only the elements contained in both self and other.

Removed elements are destroyed.

Iteration

void itCtor(IteratorType * it, Type * self)

Create a new iterator it on set self.

NOTE: As the set 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.

E itGet(IteratorType * it)

Return a copy of current element pointed to by the iterator it.

WARNING: current position must be valid otherwise the behavior is undefined. See itMove().

Instance Attribute Summary

Attributes inherited from Collection

#element, #it_ref

Attributes inherited from Type

#type, #type_ref

Instance Method Summary collapse

Methods inherited from Collection

#==, #comparable?, #constructible?, #copyable?, #destructible?, #entities, #hash, #hashable?, #initializable?

Methods inherited from Type

#==, #abort, #assert, #calloc, coerce, #comparable?, #constructible?, #copyable?, #destructible?, #entities, #extern, #free, #hash, #hashable?, #initializable?, #inline, #malloc, #method_missing, #orderable?, #prefix, #private?, #public?, #sortable?, #static, #static?, #write_decls, #write_defs, #write_intf

Methods inherited from Code

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

Constructor Details

#initialize(*args) ⇒ HashSet

Returns a new instance of HashSet.



155
156
157
158
159
# File 'lib/autoc/collection/hash_set.rb', line 155

def initialize(*args)
  super
  @list = List.new(list, element, :static)
  key_requirement(element)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class AutoC::Type

Instance Method Details

#write_impls(stream, define) ⇒ Object



185
186
187
188
189
190
191
192
193
194
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
# File 'lib/autoc/collection/hash_set.rb', line 185

def write_impls(stream, define)
  @list.write_intf_decls(stream, static, inline)
  @list.write_impls(stream, static)
  super
  stream << %$
    static void #{rehash}(#{type_ref} self) {
      #{@list.type_ref} buckets;
      size_t index, bucket_count, size, fill;
      #{assert}(self);
      #{assert}(self->min_fill > 0);
      #{assert}(self->max_fill > 0);
      #{assert}(self->min_fill < self->max_fill);
      #{assert}(self->min_bucket_count > 0);
      if(self->buckets) {
        if(self->min_size < self->size && self->size < self->max_size) return;
        fill = (size_t)((float)self->size/self->bucket_count*100);
        if(fill > self->max_fill) {
          bucket_count = (size_t)((float)self->bucket_count/100*self->capacity_multiplier);
        } else
        if(fill < self->min_fill && self->bucket_count > self->min_bucket_count) {
          bucket_count = (size_t)((float)self->bucket_count/self->capacity_multiplier*100);
          if(bucket_count < self->min_bucket_count) bucket_count = self->min_bucket_count;
        } else
          return;
        size = self->size;
        self->min_size = (size_t)((float)self->min_fill/100*size);
        self->max_size = (size_t)((float)self->max_fill/100*size);
      } else {
        bucket_count = self->min_bucket_count;
        size = 0;
      }
      buckets = (#{@list.type_ref})#{malloc}(bucket_count*sizeof(#{@list.type})); #{assert}(buckets);
      for(index = 0; index < bucket_count; ++index) {
        #{@list.ctor}(&buckets[index]);
      }
      if(self->buckets) {
        #{it} it;
        #{itCtor}(&it, self);
        while(#{itMove}(&it)) {
          #{@list.type}* bucket;
          #{element.type} element = #{itGet}(&it);
          bucket = &buckets[#{element.identify("element")} % bucket_count];
          #{@list.push}(bucket, element);
          #{element.dtor("element")};
        }
        #{dtor}(self);
      }
      self->buckets = buckets;
      self->bucket_count = bucket_count;
      self->size = size;
    }
    #{define} #{ctor.definition} {
      #{assert}(self);
      self->min_bucket_count = 16;
      self->min_fill = 20;
      self->max_fill = 80;
      self->min_size = (size_t)((float)self->min_fill/100*self->min_bucket_count);
      self->max_size = (size_t)((float)self->max_fill/100*self->min_bucket_count);
      self->capacity_multiplier = 200;
      self->buckets = NULL;
      #{rehash}(self);
    }
    #{define} #{dtor.definition} {
      size_t i;
      #{assert}(self);
      for(i = 0; i < self->bucket_count; ++i) {
        #{@list.dtor}(&self->buckets[i]);
      }
      #{free}(self->buckets);
    }
    #{define} void #{purge}(#{type_ref} self) {
      #{assert}(self);
      #{dtor}(self);
      self->buckets = NULL;
      #{rehash}(self);
    }
    #{define} int #{contains}(#{type_ref} self, #{element.type} element) {
      #{assert}(self);
      return #{@list.contains}(&self->buckets[#{element.identify("element")} % self->bucket_count], element);
    }
    #{define} #{element.type} #{get}(#{type_ref} self, #{element.type} element) {
      #{element.type} result;
      #{assert}(self);
      #{assert}(#{contains}(self, element));
      result = #{@list.find}(&self->buckets[#{element.identify("element")} % self->bucket_count], element);
      return result;
    }
    #{define} int #{put}(#{type_ref} self, #{element.type} element) {
      #{@list.type_ref} bucket;
      #{assert}(self);
      bucket = &self->buckets[#{element.identify("element")} % self->bucket_count];
      if(!#{@list.contains}(bucket, element)) {
        #{@list.push}(bucket, element);
        ++self->size;
        #{rehash}(self);
        return 1;
      }
      return 0;
    }
    #{define} int #{replace}(#{type_ref} self, #{element.type} element) {
      #{@list.type_ref} bucket;
      #{assert}(self);
      bucket = &self->buckets[#{element.identify("element")} % self->bucket_count];
      return #{@list.replace}(bucket, element);
    }
    #{define} int #{remove}(#{type_ref} self, #{element.type} element) {
      #{@list.type_ref} bucket;
      #{assert}(self);
      bucket = &self->buckets[#{element.identify("element")} % self->bucket_count];
      if(#{@list.remove}(bucket, element)) {
        --self->size;
        #{rehash}(self);
        return 1;
      }
      return 0;
    }
    #{define} void #{itCtor}(#{it_ref} self, #{type_ref} set) {
      #{assert}(self);
      self->set = set;
      self->bucket_index = self->set->bucket_count;
    }
    #{define} int #{itMove}(#{it_ref} self) {
      #{assert}(self);
      if(self->bucket_index >= self->set->bucket_count) #{@list.itCtor}(&self->it, &self->set->buckets[self->bucket_index = 0]);
      if(#{@list.itMove}(&self->it)) return 1;
      while(++self->bucket_index < self->set->bucket_count) {
        #{@list.itCtor}(&self->it, &self->set->buckets[self->bucket_index]);
        if(#{@list.itMove}(&self->it)) return 1;
      }
      return 0;
    }
    #{define} #{element.type} #{itGet}(#{it_ref} self) {
      #{assert}(self);
      return #{@list.itGet}(&self->it);
    }
    static #{element.type_ref} #{itGetRef}(#{it_ref} self) {
      #{assert}(self);
      return #{@list.itGetRef}(&self->it);
    }
  $
end

#write_intf_decls(stream, declare, define) ⇒ Object



161
162
163
# File 'lib/autoc/collection/hash_set.rb', line 161

def write_intf_decls(stream, declare, define)
  super
end

#write_intf_types(stream) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/autoc/collection/hash_set.rb', line 165

def write_intf_types(stream)
  super
  @list.write_intf_types(stream)
  stream << %$
    typedef struct #{type} #{type};
    typedef struct #{it} #{it};
    struct #{type} {
      #{@list.type_ref} buckets;
      size_t bucket_count, min_bucket_count;
      size_t size, min_size, max_size;
      unsigned min_fill, max_fill, capacity_multiplier; /* ?*1e-2 */
    };
    struct #{it} {
      #{type_ref} set;
      size_t bucket_index;
      #{@list.it} it;
    };
  $
end