Class: AutoC::HashSet
- Inherits:
-
Collection
- Object
- Code
- Type
- Collection
- AutoC::HashSet
- Defined in:
- lib/autoc/collection/hash_set.rb
Overview
HashSet 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
- cols=2*
-
|=== |void ~type~Copy(Type *
dst, Type *src) | Create a new setdstfilled with the contents ofsrc. A copy operation is performed on every element insrc.NOTE: Previous contents of
dstis overwritten.|void ~type~Ctor(Type *
self) | Create a new empty setself.NOTE: Previous contents of
selfis overwritten.|void ~type~Dtor(Type *
self) | Destroy setself. Stored elements are destroyed as well by calling the respective destructors.|int ~type~Equal(Type *
lt, Type *rt) | Return non-zero value if setsltandrtare considered equal by contents and zero value otherwise.|size_t ~type~Identify(Type *
self) | Return hash code for setself. |===Basic operations
- cols=2*
-
|=== |int ~type~Contains(Type *
self, Ewhat) | Return non-zero value if setselfcontains an element considered equal to the elementwhatand zero value otherwise.|int ~type~Empty(Type *
self) | Return non-zero value if setselfcontains no elements and zero value otherwise.|E ~type~Get(Type *
self, Ewhat) | Return a copy of the element inselfconsidered equal to the elementwhat.WARNING:
selfmust contain such element otherwise the behavior is undefined. See ~type~Contains().|void ~type~Purge(Type *
self) | Remove and destroy all elements stored inself.|int ~type~Put(Type *
self, Ewhat) | Put a copy of the elementwhatintoself*only if* there is no such element inselfwhich is considered equal towhat.Return non-zero value on successful element put and zero value otherwise.
|int ~type~Replace(Type *
self, Ewith) | Ifselfcontains an element which is considered equal to the elementwith, replace that element with a copy ofwith, otherwise do nothing. Replaced element is destroyed.Return non-zero value if the replacement was actually performed and zero value otherwise.
|int ~type~Remove(Type *
self, Ewhat) | Remove and destroy an element inselfwhich is considered equal to the elementwhat.Return non-zero value on successful element removal and zero value otherwise.
|size_t ~type~Size(Type *
self) | Return number of elements stored inself. |===Logical operations
- cols=2*
-
|=== |void ~type~Exclude(Type *
self, Type *other) | Perform the difference operation that isselfwill retain only the elements not contained inother.Removed elements are destroyed. |void ~type~Include(Type *
self, Type *other) | Perform the union operation that isselfwill contain the elements from bothselfandother.selfreceives the copies of extra elements inother.|void ~type~Invert(Type *
self, Type *other) | Perform the symmetric difference operation that isselfwill retain the elements contained in eitherselforother, but not in both.Removed elements are destroyed, extra elements are copied.
|void ~type~Retain(Type *
self, Type *other) | Perform the intersection operation that isselfwill retain only the elements contained in bothselfandother.Removed elements are destroyed. |===
Iteration
- cols=2*
-
|=== |void ~it~Ctor(IteratorType *
it, Type *self) | Create a new iteratoriton setself.NOTE: As the set 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.|E ~it~Get(IteratorType *
it) | Return a copy of current element pointed to by the iteratorit.WARNING: current position must be valid otherwise the behavior is undefined. See ~it~Move(). |===
Constant Summary
Constants inherited from Type
Instance Attribute Summary
Attributes inherited from Collection
Attributes inherited from Type
Instance Method Summary collapse
-
#initialize(*args) ⇒ HashSet
constructor
A new instance of HashSet.
- #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, #entities, #equal, #identify, #less
Methods inherited from Type
#abort, #assert, #calloc, #entities, #extern, #free, #inline, #malloc, #method_missing, #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.
148 149 150 151 |
# File 'lib/autoc/collection/hash_set.rb', line 148 def initialize(*args) super @list = List.new(list, element, :static) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class AutoC::Type
Instance Method Details
#write_exported_declarations(stream, declare, define) ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/autoc/collection/hash_set.rb', line 177 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} int #{contains}(#{type}*, #{element.type}); #{declare} #{element.type} #{get}(#{type}*, #{element.type}); #{declare} size_t #{size}(#{type}*); #define #{empty}(self) (#{size}(self) == 0) #{declare} int #{put}(#{type}*, #{element.type}); #{declare} int #{replace}(#{type}*, #{element.type}); #{declare} int #{remove}(#{type}*, #{element.type}); #{declare} void #{exclude}(#{type}*, #{type}*); #{declare} void #{retain}(#{type}*, #{type}*); #{declare} void #{include}(#{type}*, #{type}*); #{declare} void #{invert}(#{type}*, #{type}*); #{declare} void #{itCtor}(#{it}*, #{type}*); #{declare} int #{itMove}(#{it}*); #{declare} #{element.type} #{itGet}(#{it}*); $ end |
#write_exported_types(stream) ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/autoc/collection/hash_set.rb', line 153 def write_exported_types(stream) stream << %$ /*** **** #{type}<#{element.type}> (#{self.class}) ***/ $ if public? @list.write_exported_types(stream) stream << %$ typedef struct #{type} #{type}; typedef struct #{it} #{it}; struct #{type} { #{@list.type}* 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}* set; int bucket_index; #{@list.it} it; }; $ end |
#write_implementations(stream, define) ⇒ Object
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 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 |
# File 'lib/autoc/collection/hash_set.rb', line 202 def write_implementations(stream, define) @list.write_exported_declarations(stream, static, inline) @list.write_implementations(stream, static) stream << %$ #{define} #{element.type}* #{itGetRef}(#{it}*); static void #{rehash}(#{type}* self) { #{@list.type}* 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}*)#{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} void #{ctor}(#{type}* self) { #{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} void #{dtor}(#{type}* self) { size_t i; #{assert}(self); for(i = 0; i < self->bucket_count; ++i) { #{@list.dtor}(&self->buckets[i]); } #{free}(self->buckets); } #{define} void #{copy}(#{type}* dst, #{type}* src) { #{it} it; #{assert}(src); #{assert}(dst); #{ctor}(dst); #{itCtor}(&it, src); while(#{itMove}(&it)) #{put}(dst, *#{itGetRef}(&it)); } static int #{containsAllOf}(#{type}* self, #{type}* other) { #{it} it; #{itCtor}(&it, self); while(#{itMove}(&it)) { int found = 0; if(#{contains}(other, *#{itGetRef}(&it))) found = 1; 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) { #{it} it; size_t result = 0; #{assert}(self); #{itCtor}(&it, self); while(#{itMove}(&it)) { #{element.type}* e = #{itGetRef}(&it); result ^= #{element.identify("*e")}; result = AUTOC_RCYCLE(result); } return result; } #{define} void #{purge}(#{type}* self) { #{assert}(self); #{dtor}(self); self->buckets = NULL; #{rehash}(self); } #{define} int #{contains}(#{type}* self, #{element.type} element) { #{assert}(self); return #{@list.contains}(&self->buckets[#{element.identify("element")} % self->bucket_count], element); } #{define} #{element.type} #{get}(#{type}* 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} size_t #{size}(#{type}* self) { #{assert}(self); return self->size; } #{define} int #{put}(#{type}* self, #{element.type} element) { #{@list.type}* 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}* self, #{element.type} element) { #{@list.type}* bucket; #{assert}(self); bucket = &self->buckets[#{element.identify("element")} % self->bucket_count]; return #{@list.replace}(bucket, element); } #{define} int #{remove}(#{type}* self, #{element.type} element) { #{@list.type}* 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 #{exclude}(#{type}* self, #{type}* other) { #{it} it; #{assert}(self); #{assert}(other); #{itCtor}(&it, other); while(#{itMove}(&it)) #{remove}(self, *#{itGetRef}(&it)); } #{define} void #{include}(#{type}* self, #{type}* other) { #{it} it; #{assert}(self); #{assert}(other); #{itCtor}(&it, other); while(#{itMove}(&it)) #{put}(self, *#{itGetRef}(&it)); } #{define} void #{retain}(#{type}* self, #{type}* other) { #{it} it; #{type} set; #{assert}(self); #{assert}(other); #{ctor}(&set); #{itCtor}(&it, self); while(#{itMove}(&it)) { #{element.type}* e = #{itGetRef}(&it); if(#{contains}(other, *e)) #{put}(&set, *e); } #{dtor}(self); *self = set; } #{define} void #{invert}(#{type}* self, #{type}* other) { #{it} it; #{type} set; #{assert}(self); #{assert}(other); #{ctor}(&set); #{itCtor}(&it, self); while(#{itMove}(&it)) { #{element.type}* e = #{itGetRef}(&it); if(!#{contains}(other, *e)) #{put}(&set, *e); } #{itCtor}(&it, other); while(#{itMove}(&it)) { #{element.type}* e = #{itGetRef}(&it); if(!#{contains}(self, *e)) #{put}(&set, *e); } #{dtor}(self); *self = set; } #{define} void #{itCtor}(#{it}* self, #{type}* set) { #{assert}(self); self->set = set; self->bucket_index = -1; } #{define} int #{itMove}(#{it}* self) { #{assert}(self); if(self->bucket_index < 0) #{@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}* self) { #{assert}(self); return #{@list.itGet}(&self->it); } #{define} #{element.type}* #{itGetRef}(#{it}* self) { #{assert}(self); return #{@list.itGetRef}(&self->it); } $ end |