Class: AutoC::Queue

Inherits:
Collection show all
Defined in:
lib/autoc/collection/queue.rb

Overview

Queue is an ordered bidirectional sequence container. Queue supports addition/removal operations at both ends. However, it is intended to be used as a FIFO container as opposed to List therefore the default submission, polling and retrieval operations are performed on the opposite ends.

This collection is a synergy of C++ std::list<> and std::queue<> template classes.

Generated C interface

Collection management

void typeCopy(Type * dst, Type * src)

Create a new queue 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 queue self.

NOTE: Previous contents of self is overwritten.

void typeDtor(Type * self)

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

int typeEqual(Type * lt, Type * rt)

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

size_t typeIdentify(Type * self)

Return hash code for queue self.

Basic operations

int typeContains(Type * self, E what)

Return non-zero value if queue self contains (at least) one element considered equal to what and zero value otherwise.

int typeEmpty(Type * self)

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

E typeFind(Type * self, E what)

Return the first element of stored in self which is considered equal to what.

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

E typePeek(Type * self)

Alias for typePeekHead().

E typePeekHead(Type * self)

Return a copy of the head element of self.

WARNING: self must not be empty otherwise the behavior is undefined. See typeEmpty().

E typePeekTail(Type * self)

Return a copy of the tail element of self.

WARNING: self must not be empty otherwise the behavior is undefined. See typeEmpty().

E typePop(Type * self)

Alias for typePopHead().

E typePopHead(Type * self)

Remove head element of self and return it.

NOTE: The function returns the element itself, not a copy.

WARNING: self must not be empty otherwise the behavior is undefined. See typeEmpty().

E typePopTail(Type * self)

Remove tail element of self and return it.

NOTE: The function returns the element itself, not a copy.

WARNING: self must not be empty otherwise the behavior is undefined. See typeEmpty().

void typePurge(Type * self)

Remove and destroy all elements stored in self.

void typePush(Type * self, E what)

Alias for typePushTail().

void typePushHead(Type * self, E what)

Place a copy of the element what to the head of self.

void typePushTail(Type * self, E what)

Place a copy of the element what to the tail of self.

int typeReplace(Type * self, E what, E with)

Find the first occurrence of what in self and replace it with a copy of the element with. Replaced element is destroyed.

Return non-zero value on successful replacement and zero value if no suitable element was found.

int typeReplaceAll(Type * self, E what, E with)

Find all occurrences of what in self and replace them with copies of the element with. All replaced elements are destroyed.

Return number of successful replacements.

int typeReplaceEx(Type * self, E what, E with, int count)

Find at most count occurrences of what in self and replace them with copies of the element with. If count is negative, all occurrences are replaced instead. All replaced elements are destroyed.

Return number of successful replacements.

int typeRemove(Type * self, E what)

Remove and destroy the first occurrence of the element what in self.

Return non-zero value if element was removed and zero value otherwise.

int typeRemoveAll(Type * self, E what)

Remove and destroy all occurrences of the element what in self.

Return number of elements actually removed.

int typeRemoveEx(Type * self, E what, int count)

Remove and destroy at most count occurrences of the element what in self. If count is negative, all occurrences are removed instead.

Return number of elements actually removed.

size_t typeSize(Type * self)

Return number of elements stored in self.

Iteration

void itCtor(IteratorType * it, Type * self)

Create a new forward iterator it on queue self.

NOTE: Previous contents of it is overwritten.

void itCtorEx(IteratorType * it, Type * self, int forward)

Create a new iterator it on queue self. Non-zero value of forward specifies a forward iterator, zero value specifies a backward iterator.

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?, #initialize

Methods inherited from Type

#==, #abort, #assert, #calloc, coerce, #comparable?, #constructible?, #copyable?, #destructible?, #entities, #extern, #free, #hash, #hashable?, #initializable?, #initialize, #inline, #malloc, #method_missing, #orderable?, #prefix, #private?, #public?, #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

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_impls(stream, define) ⇒ Object



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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
# File 'lib/autoc/collection/queue.rb', line 261

def write_impls(stream, define)
  super
  stream << %$
    static #{element.type_ref} #{itGetRef}(#{it_ref});
    #{define} #{ctor.definition} {
      #{assert}(self);
      self->head_node = self->tail_node = NULL;
      self->node_count = 0;
    }
    #{define} #{dtor.definition} {
      #{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} #{copy.definition} {
      #{it} it;
      #{assert}(src);
      #{assert}(dst);
      #{ctor}(dst);
      #{itCtor}(&it, src);
      while(#{itMove}(&it)) {
        #{element.type} element;
        #{pushTail}(dst, element = #{itGet}(&it));
        #{element.dtor("element")};
      }
    }
    #{define} #{equal.definition} {
      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} #{identify.definition} {
      #{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_ref} self) {
      #{dtor}(self);
      #{ctor}(self);
    }
    #{define} #{element.type} #{peekHead}(#{type_ref} self) {
      #{element.type} result;
      #{assert}(self);
      #{assert}(!#{empty}(self));
      #{element.copy("result", "self->head_node->element")};
      return result;
    }
    #{define} #{element.type} #{peekTail}(#{type_ref} self) {
      #{element.type} result;
      #{assert}(self);
      #{assert}(!#{empty}(self));
      #{element.copy("result", "self->tail_node->element")};
      return result;
    }
    #{define} #{element.type} #{popHead}(#{type_ref} 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->head_node->prev_node = NULL;
      --self->node_count;
      #{free}(node);
      return result;
    }
    #{define} #{element.type} #{popTail}(#{type_ref} self) {
      #{node}* node;
      #{element.type} result;
      #{assert}(self);
      #{assert}(!#{empty}(self));
      node = self->tail_node;
      result = node->element;
      self->tail_node = self->tail_node->prev_node;
      self->tail_node->next_node = NULL;
      --self->node_count;
      #{free}(node);
      return result;
    }
    #{define} void #{pushTail}(#{type_ref} self, #{element.type} element) {
      #{node}* node;
      #{assert}(self);
      node = (#{node}*)#{malloc}(sizeof(#{node})); #{assert}(node);
      #{element.copy("node->element", "element")};
      if(#{empty}(self)) {
        node->prev_node = node->next_node = NULL;
        self->tail_node = self->head_node = node;
      } else {
        node->next_node = NULL;
        node->prev_node = self->tail_node;
        self->tail_node->next_node = node;
        self->tail_node = node;
      }
      ++self->node_count;
    }
    #{define} void #{pushHead}(#{type_ref} self, #{element.type} element) {
      #{node}* node;
      #{assert}(self);
      node = (#{node}*)#{malloc}(sizeof(#{node})); #{assert}(node);
      #{element.copy("node->element", "element")};
      if(#{empty}(self)) {
        node->prev_node = node->next_node = NULL;
        self->tail_node = self->head_node = node;
      } else {
        node->prev_node = NULL;
        node->next_node = self->head_node;
        self->head_node->prev_node = node;
        self->head_node = node;
      }
      ++self->node_count;
    }
    #{define} int #{contains}(#{type_ref} 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_ref} 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_ref} 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_ref} self, #{element.type} what, int count) {
      #{node}* node;
      int removed = 0;
      #{assert}(self);
      if(count == 0) return 0;
      node = self->head_node;
      while(node) {
        if(#{element.equal("node->element", "what")}) {
          #{node}* this_node;
          if(node == self->head_node) {
            #{assert}(!node->prev_node);
            this_node = self->head_node = node->next_node;
            if(self->head_node) self->head_node->prev_node = NULL;
          } else if(node == self->tail_node) {
            #{assert}(!node->next_node);
            self->tail_node = node->prev_node;
            this_node = self->tail_node->next_node = NULL;
          } else {
            #{assert}(node->prev_node);
            #{assert}(node->next_node);
            this_node = node->next_node;
            node->next_node->prev_node = node->prev_node;
            node->prev_node->next_node = node->next_node;
          }
          ++removed;
          --self->node_count;
          #{element.dtor("node->element")};
          #{free}(node);
          node = this_node;
          if(count > 0 && removed >= count) break;
        } else {
          node = node->next_node;
        }
      }
      return removed;
    }
    #{define} size_t #{size}(#{type_ref} self) {
      #{assert}(self);
      return self->node_count;
    }
    #{define} void #{itCtorEx}(#{it_ref} self, #{type_ref} queue, int forward) {
      #{assert}(self);
      #{assert}(queue);
      self->start = 1;
      self->queue = queue;
      self->forward = forward;
    }
    #{define} int #{itMove}(#{it_ref} self) {
      #{assert}(self);
      if(self->start) {
        self->this_node = self->forward ? self->queue->head_node : self->queue->tail_node;
        self->start = 0;
      } else {
        self->this_node = self->forward ? self->this_node->next_node : self->this_node->prev_node;
      }
      return self->this_node != NULL;
    }
    #{define} #{element.type} #{itGet}(#{it_ref} self) {
      #{element.type} result;
      #{assert}(self);
      #{assert}(self->this_node);
      #{element.copy("result", "self->this_node->element")};
      return result;
    }
    static #{element.type_ref} #{itGetRef}(#{it_ref} self) {
      #{assert}(self);
      #{assert}(self->this_node);
      return &self->this_node->element;
    }
  $
end

#write_intf_decls(stream, declare, define) ⇒ Object



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
# File 'lib/autoc/collection/queue.rb', line 225

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});
    #define #{peek}(self) #{peekHead}(self)
    #{declare} #{element.type} #{peekHead}(#{type_ref});
    #{declare} #{element.type} #{peekTail}(#{type_ref});
    #define #{push}(self, element) #{pushTail}(self, element)
    #{declare} void #{pushTail}(#{type_ref}, #{element.type});
    #{declare} void #{pushHead}(#{type_ref}, #{element.type});
    #define #{pop}(self) #{popHead}(self)
    #{declare} #{element.type} #{popHead}(#{type_ref});
    #{declare} #{element.type} #{popTail}(#{type_ref});
    #{declare} int #{contains}(#{type_ref}, #{element.type});
    #{declare} #{element.type} #{find}(#{type_ref}, #{element.type});
    #define #{replace}(self, with) #{replaceEx}(self, with, 1)
    #define #{replaceAll}(self,  with) #{replaceEx}(self, with, -1)
    #{declare} int #{replaceEx}(#{type_ref}, #{element.type}, int);
    #define #{remove}(self, what) #{removeEx}(self, what, 1)
    #define #{removeAll}(self, what) #{removeEx}(self, what, -1)
    #{declare} int #{removeEx}(#{type_ref}, #{element.type}, int);
    #{declare} size_t #{size}(#{type_ref});
    #define #{empty}(self) (#{size}(self) == 0)
    #{declare} void #{itCtor}(#{it_ref}, #{type_ref});
    #define #{itCtor}(self, type) #{itCtorEx}(self, type, 1)
    #{declare} void #{itCtorEx}(#{it_ref}, #{type_ref}, int);
    #{declare} int #{itMove}(#{it_ref});
    #{declare} #{element.type} #{itGet}(#{it_ref});
  $
end

#write_intf_types(stream) ⇒ Object



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
# File 'lib/autoc/collection/queue.rb', line 196

def write_intf_types(stream)
  super
  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;
      #{node}* tail_node;
      size_t node_count;
    };
    struct #{it} {
      int start, forward;
      #{type_ref} queue;
      #{node}* this_node;
    };
    struct #{node} {
      #{element.type} element;
      #{node}* prev_node;
      #{node}* next_node;
    };
  $
end