Class: AutoC::List
- Inherits:
-
Collection
- Object
- Code
- Type
- Collection
- AutoC::List
- Defined in:
- lib/autoc/collection/list.rb
Overview
List is an ordered unidirectional sequence container. List supports submission/polling operations at one end hence it can be used as a LIFO container.
The collection’s C++ counterpart is std::forward_list<> template class.
Generated C interface
Collection management
- cols=2*
-
|=== |void ~type~Copy(Type *
dst, Type *src) | Create a new listdstfilled 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 listself.NOTE: Previous contents of
selfis overwritten.|void ~type~Dtor(Type *
self) | Destroy listself. Stored elements are destroyed as well by calling the respective destructors.|int ~type~Equal(Type *
lt, Type *rt) | Return non-zero value if listsltandrtare considered equal by contents and zero value otherwise.|size_t ~type~Identify(Type *
self) | Return hash code for listself. |===Basic operations
- cols=2*
-
|=== |int ~type~Contains(Type *
self, Ewhat) | Return non-zero value if listselfcontains (at least) one element considered equal towhatand zero value otherwise.|int ~type~Empty(Type *
self) | Return non-zero value if listselfcontains no elements and zero value otherwise.|E ~type~Find(Type *
self, Ewhat) | Return the first element of stored inselfwhich is considered equal towhat.WARNING:
selfmust contain such element otherwise the behavior is undefined. See ~type~Contains().|E ~type~Peek(Type *
self) | Return a copy of the head element ofself.WARNING:
self*must not* be empty otherwise the behavior is undefined. See ~type~Empty().|E ~type~Pop(Type *
self) | Remove head element ofselfand return it.NOTE: The function returns the element itself, not a copy.
WARNING:
self*must not* be empty otherwise the behavior is undefined. See ~type~Empty().|void ~type~Purge(Type *
self) | Remove and destroy all elements stored inself.|void ~type~Push(Type *
self, Ewhat) | Place a copy of the elementwhatto the head ofself.|int ~type~Replace(Type *
self, Ewith) | Find the first occurrence ofwithinselfand replace it with a copy of the elementwith. Replaced element is destroyed.Return non-zero value on successful replacement and zero value if no suitable element was found.
|int ~type~ReplaceAll(Type *
self, Ewith) | Find all occurrences ofwithinselfand replace them with copies of the elementwith. All replaced elements are destroyed.Return number of successful replacements.
|int ~type~ReplaceEx(Type *
self, Ewith, int count) | Find at mostcountoccurrences ofwithinselfand replace them with copies of the elementwith. Ifcountis negative, all occurrences are replaced instead. All replaced elements are destroyed.Return number of successful replacements.
|int ~type~Remove(Type *
self, Ewhat) | Remove and destroy the first occurrence of the elementwhatinself.Return non-zero value if element was removed and zero value otherwise.
|int ~type~RemoveAll(Type *
self, Ewhat) | Remove and destroy all occurrences of the elementwhatinself.Return number of elements actually removed.
|int ~type~RemoveEx(Type *
self, Ewhat, int count) | Remove and destroy at mostcountoccurrences of the elementwhatinself. Ifcountis negative, all occurrences are removed instead.Return number of elements actually removed.
|size_t ~type~Size(Type *
self) | Return number of elements stored inself. |===Iteration
- cols=2*
-
|=== |void ~it~Ctor(IteratorType *
it, Type *self) | Create a new iteratoriton listself.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
- #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, #initialize, #less
Methods inherited from Type
#abort, #assert, #calloc, #entities, #extern, #free, #initialize, #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
This class inherits a constructor from AutoC::Collection
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
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/autoc/collection/list.rb', line 183 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} #{element.type} #{peek}(#{type}*); #{declare} #{element.type} #{pop}(#{type}*); #{declare} void #{push}(#{type}*, #{element.type}); #{declare} int #{contains}(#{type}*, #{element.type}); #{declare} #{element.type} #{find}(#{type}*, #{element.type}); #define #{replace}(self, with) #{replaceEx}(self, with, 1) #define #{replaceAll}(self, with) #{replaceEx}(self, with, -1) #{declare} int #{replaceEx}(#{type}*, #{element.type}, int); #define #{remove}(self, what) #{removeEx}(self, what, 1) #define #{removeAll}(self, what) #{removeEx}(self, what, -1) #{declare} int #{removeEx}(#{type}*, #{element.type}, int); #{declare} size_t #{size}(#{type}*); #define #{empty}(self) (#{size}(self) == 0) #{declare} void #{itCtor}(#{it}*, #{type}*); #{declare} int #{itMove}(#{it}*); #{declare} #{element.type} #{itGet}(#{it}*); $ end |
#write_exported_types(stream) ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/autoc/collection/list.rb', line 157 def write_exported_types(stream) stream << %$ /*** **** #{type}<#{element.type}> (#{self.class}) ***/ $ if public? stream << %$ typedef struct #{node} #{node}; typedef struct #{type} #{type}; typedef struct #{it} #{it}; struct #{type} { #{node}* head_node; size_t node_count; }; struct #{it} { int start; #{type}* list; #{node}* this_node; }; struct #{node} { #{element.type} element; #{node}* next_node; }; $ end |
#write_implementations(stream, define) ⇒ Object
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 |
# File 'lib/autoc/collection/list.rb', line 210 def write_implementations(stream, define) stream << %$ #{define} #{element.type}* #{itGetRef}(#{it}*); #{define} void #{ctor}(#{type}* self) { #{assert}(self); self->head_node = NULL; self->node_count = 0; } #{define} void #{dtor}(#{type}* self) { #{node}* node; #{assert}(self); node = self->head_node; while(node) { #{node}* this_node = node; node = node->next_node; #{element.dtor("this_node->element")}; #{free}(this_node); } } #{define} void #{copy}(#{type}* dst, #{type}* src) { #{it} it; #{assert}(src); #{assert}(dst); #{ctor}(dst); #{itCtor}(&it, src); while(#{itMove}(&it)) { #{push}(dst, *#{itGetRef}(&it)); } } #{define} int #{equal}(#{type}* lt, #{type}* rt) { if(#{size}(lt) == #{size}(rt)) { #{it} lit, rit; #{itCtor}(&lit, lt); #{itCtor}(&rit, rt); while(#{itMove}(&lit) && #{itMove}(&rit)) { int equal; #{element.type} *le, *re; le = #{itGetRef}(&lit); re = #{itGetRef}(&rit); equal = #{element.equal("*le", "*re")}; if(!equal) return 0; } return 1; } else return 0; } #{define} size_t #{identify}(#{type}* self) { #{node}* node; size_t result = 0; #{assert}(self); for(node = self->head_node; node != NULL; node = node->next_node) { result ^= #{element.identify("node->element")}; result = AUTOC_RCYCLE(result); } return result; } #{define} void #{purge}(#{type}* self) { #{dtor}(self); #{ctor}(self); } #{define} #{element.type} #{peek}(#{type}* self) { #{element.type} result; #{assert}(self); #{assert}(!#{empty}(self)); #{element.copy("result", "self->head_node->element")}; return result; } #{define} #{element.type} #{pop}(#{type}* self) { #{node}* node; #{element.type} result; #{assert}(self); #{assert}(!#{empty}(self)); node = self->head_node; result = node->element; self->head_node = self->head_node->next_node; --self->node_count; #{free}(node); return result; } #{define} void #{push}(#{type}* self, #{element.type} element) { #{node}* node; #{assert}(self); node = (#{node}*)#{malloc}(sizeof(#{node})); #{assert}(node); #{element.copy("node->element", "element")}; node->next_node = self->head_node; self->head_node = node; ++self->node_count; } #{define} int #{contains}(#{type}* self, #{element.type} what) { #{node}* node; int found = 0; #{assert}(self); node = self->head_node; while(node) { if(#{element.equal("node->element", "what")}) { found = 1; break; } node = node->next_node; } return found; } #{define} #{element.type} #{find}(#{type}* self, #{element.type} what) { #{node}* node; #{assert}(self); #{assert}(#{contains}(self, what)); node = self->head_node; while(node) { if(#{element.equal("node->element", "what")}) { #{element.type} result; #{element.copy("result", "node->element")}; return result; } node = node->next_node; } #{abort}(); } #{define} int #{replaceEx}(#{type}* self, #{element.type} with, int count) { #{node}* node; int replaced = 0; #{assert}(self); if(count == 0) return 0; node = self->head_node; while(node) { if(#{element.equal("node->element", "with")}) { #{element.dtor("node->element")}; #{element.copy("node->element", "with")}; ++replaced; if(count > 0 && replaced >= count) break; } node = node->next_node; } return replaced; } #{define} int #{removeEx}(#{type}* self, #{element.type} what, int count) { #{node} *node, *prev_node; int removed = 0; #{assert}(self); if(count == 0) return 0; node = self->head_node; prev_node = NULL; while(node) { if(#{element.equal("node->element", "what")}) { #{node}* this_node; if(prev_node) { this_node = prev_node->next_node = node->next_node; } else { this_node = self->head_node = node->next_node; } ++removed; --self->node_count; #{element.dtor("node->element")}; #{free}(node); node = this_node; if(count > 0 && removed >= count) break; } else { prev_node = node; node = node->next_node; } } return removed; } #{define} size_t #{size}(#{type}* self) { #{assert}(self); return self->node_count; } #{define} void #{itCtor}(#{it}* self, #{type}* list) { #{assert}(self); #{assert}(list); self->start = 1; self->list = list; } #{define} int #{itMove}(#{it}* self) { #{assert}(self); if(self->start) { self->this_node = self->list->head_node; self->start = 0; } else { self->this_node = self->this_node ? self->this_node->next_node : NULL; } return self->this_node != NULL; } #{define} #{element.type} #{itGet}(#{it}* self) { #{element.type} result; #{assert}(self); #{assert}(self->this_node); #{element.copy("result", "self->this_node->element")}; return result; } #{define} #{element.type}* #{itGetRef}(#{it}* self) { #{assert}(self); #{assert}(self->this_node); return &self->this_node->element; } $ end |