forked from douglascraigschmidt/CPlusPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLQueue.cpp
More file actions
570 lines (467 loc) · 13.4 KB
/
Copy pathLQueue.cpp
File metadata and controls
570 lines (467 loc) · 13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
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
182
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
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
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
#ifndef _LQUEUE_CPP
#define _LQUEUE_CPP
#include <memory>
#include "LQueue.h"
// It's fine to use "this" in base-member initializations.
#pragma warning(disable:4355)
/**
* @class LQueue_Node
* @brief Defines a node in the @a LQueue that's implemented as a circular linked list.
*/
template <class T>
class LQueue_Node
{
friend class LQueue<T>;
friend class LQueue_Iterator<T>;
friend class LQueue_Const_Iterator<T>;
public:
/// Constructor.
LQueue_Node (const T &item,
LQueue_Node<T> *next = 0);
/// Constructor.
LQueue_Node (LQueue_Node<T> *next);
/// Default constructor that doesn't initialize <item_>.
LQueue_Node (void);
/// Allocate a new <LQueue_Node>, trying first from the @a free_list_
/// and if that's empty try from the global @a ::operator new.
void *operator new (size_t bytes);
/// Return @a ptr to the @a free_list_.
void operator delete (void *ptr);
/// Return the next node to which this node points.
LQueue_Node *next (void);
/// Preallocate n @a LQueue_Nodes and store them on the @a
/// free_list_.
static void free_list_allocate (size_t n);
/// Returns all dynamic memory on the free list to the free store.
static void free_list_release (void);
private:
/// Head of the free list, which is a stack of @a LQueue_Nodes
/// used to speed up allocation.
static LQueue_Node<T> *free_list_;
/// Item in this node.
T item_;
/// Pointer to the next node.
LQueue_Node<T> *next_;
};
/* static */
template <class T> LQueue_Node<T> *
LQueue_Node<T>::free_list_ = 0;
// Allocate a new <LQueue_Node>, trying first from the
// <free_list_> and if that's empty try from the global <::operator
// new>.
template <class T> void *
LQueue_Node<T>::operator new (size_t)
{
// extract element from the free_list_ if there is one left
if (LQueue_Node<T>::free_list_ != 0)
{
// get the top element of the list
LQueue_Node<T>* new_node = LQueue_Node<T>::free_list_;
// "remove" the element from the list and pass it to the caller
LQueue_Node<T>::free_list_ = new_node->next_;
return new_node;
}
return ::operator new(sizeof (LQueue_Node<T>));
}
// Return <ptr> to the <free_list_>.
template <class T> void
LQueue_Node<T>::operator delete (void *ptr)
{
// do nothing on a null pointer
if (ptr != 0)
{
// cast to a node pointer
LQueue_Node<T>* node = static_cast<LQueue_Node<T>*> (ptr);
// put the node back into the list
node->next_ = LQueue_Node<T>::free_list_;
LQueue_Node<T>::free_list_ = node;
}
}
// Returns the next node to which this node points.
template <class T> LQueue_Node<T> *
LQueue_Node<T>::next (void)
{
return next_;
}
// Returns all dynamic memory on the free list to the free store.
template <class T> void
LQueue_Node<T>::free_list_release (void)
{
// delete free list element by element
while (LQueue_Node<T>::free_list_ != 0)
{
LQueue_Node<T>* node = LQueue_Node<T>::free_list_;
LQueue_Node<T>::free_list_ = node->next_;
::operator delete (node);
}
}
// Preallocate <n> <LQueue_Nodes> and store them on the
// <free_list_>.
template <class T> void
LQueue_Node<T>::free_list_allocate (size_t n)
{
// add a new element to the stack n times
for (size_t node_number = 0; node_number < n; ++node_number)
{
// create a new element avoiding the overwritten new operator
LQueue_Node<T>* new_node =
reinterpret_cast<LQueue_Node<T>*> (
::operator new (sizeof (LQueue_Node<T>)));
new_node->next_ = LQueue_Node<T>::free_list_;
// make the new element the top of the list
LQueue_Node<T>::free_list_ = new_node;
}
}
template <class T>
LQueue_Node<T>::LQueue_Node (const T &item,
LQueue_Node<T> *next)
: item_ (item),
next_ (next)
{
}
template <class T>
LQueue_Node<T>::LQueue_Node (LQueue_Node<T> *next)
: next_ (next)
{
}
// This method is helpful to implement the dummy node in a concise
// way.
template <class T>
LQueue_Node<T>::LQueue_Node (void)
: next_ (this)
{
}
// Returns the current size.
template <class T> size_t
LQueue<T>::size (void) const
{
return count_;
}
// Constructor.
template <class T>
LQueue<T>::LQueue (size_t size_hint)
// Initialize fields here.
: tail_ (0),
count_ (0)
{
// use the size_hint to preallocate memory for nodes
LQueue_Node<T>::free_list_allocate (size_hint);
// create the dummy node
tail_ = new LQueue_Node<T> ();
}
// Copy constructor.
template <class T>
LQueue<T>::LQueue (const LQueue<T> &rhs)
// Initialize fields here.
: tail_ (0),
count_ (0) // count_ will be set correctly by copy_list
{
// insert a dummy node first and keep it as an auto_ptr for exception
// safety issues
std::auto_ptr <LQueue_Node<T> > tail (new LQueue_Node<T> ());
tail_ = tail.get ();
// copy_list has strong exception safety, so no try catch block
// is necessary here
copy_list (rhs);
// from here on, the auto_ptr should not try to delete the
// tail pointer anymore.
tail.release ();
}
// Copy a linked list of nodes
template <class T> void
LQueue<T>::copy_list (const LQueue<T> &rhs)
{
LQueue<T> temp;
// enqueue the elements into the temporary list
for (typename LQueue<T>::const_iterator it = rhs.begin ();
it != rhs.end ();
++it)
{
temp.enqueue (*it);
}
// we only swap the lists if the temporary list has been successfully
// created. This ensures strong exception guarantees.
std::swap (tail_, temp.tail_);
// swap the counts too
std::swap (count_, temp.count_);
}
// Delete a linked list of nodes
template <class T> void
LQueue<T>::delete_list ()
{
// we do not delete the dummy node here. This will be done in the destructor
// we dequeue all elements until the queue is empty again
while (!is_empty ())
{
dequeue_i ();
}
}
// Assignment operator.
template <class T> LQueue<T> &
LQueue<T>::operator= (const LQueue<T> &rhs)
{
// test for self assignment first
if (this != &rhs)
{
// delete old data of the rhs
delete_list ();
// copy new data
copy_list (rhs);
}
return *this;
}
// Perform actions needed when queue goes out of scope.
template <class T>
LQueue<T>::~LQueue (void)
{
// delete all elements of the list
delete_list ();
// delete the last dummy node here
delete tail_;
}
// Compare this queue with <rhs> for equality. Returns true if the
// size()'s of the two queues are equal and all the elements from 0
// .. size() are equal, else false.
template <class T> bool
LQueue<T>::operator== (const LQueue<T> &rhs) const
{
return (size () == rhs.size ()) &&
std::equal (begin (), end (), rhs.begin ());
}
// Compare this queue with <rhs> for inequality such that <*this> !=
// <s> is always the complement of the boolean return value of
// <*this> == <s>.
template <class T> bool
LQueue<T>::operator!= (const LQueue<T> &rhs) const
{
return !(*this == rhs);
}
// Place a <new_item> at the tail of the queue. Throws
// the <Overflow> exception if the queue is full.
template <class T> void
LQueue<T>::enqueue (const T &new_item)
{
try
{
// assign the value to the tail element before the new allocation
// to make sure that exceptions thrown in the assignment operator
// of T does leave the queue structure altered
tail_->item_ = new_item;
// integrate the new node into the list
tail_->next_ = new LQueue_Node<T> (tail_->next_);
tail_ = tail_->next_;
// increment the element count
++count_;
}
catch (const std::bad_alloc&)
{
// we transform a bad_alloc excption into an overflow exception,
// because it basically means, that it is no longer possible
// to enqueue new elements
throw Overflow ();
}
}
// Remove and return the front item on the queue.
// Throws the <Underflow> exception if the queue is empty.
template <class T> T
LQueue<T>::dequeue (void)
{
// check for empty queue first
if (is_empty ())
{
throw Underflow ();
}
// extract the value of the head node. This is done before we actually
// remove the element for exceptions could be thrown in the assignment
// operator.
T item = tail_->next_->item_;
// call actual dequeue implementation
dequeue_i ();
return item;
}
template <class T> void
LQueue<T>::dequeue_i (void)
{
// remember the current queue head
LQueue_Node<T>* head = tail_->next_;
// remove the head from the queue
tail_->next_ = head->next_;
// decrement the element count
--count_;
// delete the old head node
delete head;
}
// Returns the front queue item without removing it.
// Throws the <Underflow> exception if the queue is empty.
template <class T> T
LQueue<T>::front (void) const
{
// check for empty queue first
if (is_empty())
throw Underflow ();
// return the item in head
return tail_->next_->item_;
}
// Returns true if the queue is empty, otherwise returns false.
template <class T> bool
LQueue<T>::is_empty (void) const
{
return count_ == 0;
}
// Returns true if the queue is full, otherwise returns false.
template <class T> bool
LQueue<T>::is_full (void) const
{
// there is no upper limit for the queue
return false;
}
// Get an iterator to the begining of the queue
template <typename T> typename LQueue<T>::iterator
LQueue<T>::begin (void)
{
// iterator starts at the head element
return typename LQueue<T>::iterator (*this, tail_->next_);
}
// Get an iterator pointing past the end of the queue
template <typename T> typename LQueue<T>::iterator
LQueue<T>::end (void)
{
// iterator starts at the tail element
return typename LQueue<T>::iterator (*this, tail_);
}
// Get an iterator to the begining of the queue
template <typename T> typename LQueue<T>::const_iterator
LQueue<T>::begin (void) const
{
// iterator starts at the head element
return typename LQueue<T>::const_iterator (*this, tail_->next_);
}
// Get an iterator pointing past the end of the queue
template <typename T> typename LQueue<T>::const_iterator
LQueue<T>::end (void) const
{
// iterator starts at the tail element
return typename LQueue<T>::const_iterator (*this, tail_);
}
template <typename T> T &
LQueue_Iterator<T>::operator* (void)
{
return pos_->item_;
}
template <typename T> const T &
LQueue_Iterator<T>::operator* (void) const
{
return pos_->item_;
}
template <typename T> LQueue_Iterator<T> &
LQueue_Iterator<T>::operator++ (void)
{
// advance to the next position
pos_ = pos_->next_;
return *this;
}
// Post-increment.
template <typename T> LQueue_Iterator<T>
LQueue_Iterator<T>::operator++ (int)
{
// keep copy of the original iterator
LQueue_Iterator<T> copy = *this;
// advance to the next position
pos_ = pos_->next_;
// return original iterator
return copy;
}
template <typename T> bool
LQueue_Iterator<T>::operator== (const LQueue_Iterator<T> &rhs) const
{
// check if the iterator points to the same position in the same queue
// (we could even omit the check for queue equality, because it is
// very unlikely that two queues share the same node pointer)
return (pos_ == rhs.pos_);
}
template <typename T> bool
LQueue_Iterator<T>::operator!= (const LQueue_Iterator<T> &rhs) const
{
// implement != in terms of ==
return !(*this == rhs);
}
template <typename T>
LQueue_Iterator<T>::LQueue_Iterator (LQueue<T> &queue,
size_t pos)
: queue_ (queue),
pos_ (queue.tail_->next_)
{
// iterator over the queue unto the right position
// we save iterations for values > count_ by doing modulo calculations
for (pos = pos % (queue_.count_ -1);
pos > 0;
--pos)
{
// advance one position each time
pos_ = pos_->next_;
}
}
template <typename T>
LQueue_Iterator<T>::LQueue_Iterator (LQueue<T> &queue,
LQueue_Node<T> *pos)
: queue_ (queue),
pos_ (pos)
{
}
template <typename T> const T &
LQueue_Const_Iterator<T>::operator* (void) const
{
return pos_->item_;
}
template <typename T> const LQueue_Const_Iterator<T> &
LQueue_Const_Iterator<T>::operator++ (void) const
{
// advance to the next position
pos_ = pos_->next_;
return *this;
}
template <typename T> LQueue_Const_Iterator<T>
LQueue_Const_Iterator<T>::operator++ (int) const
{
// keep copy of the original iterator
LQueue_Const_Iterator<T> copy = *this;
// advance to the next position
pos_ = pos_->next_;
// return original iterator
return copy;
}
template <typename T> bool
LQueue_Const_Iterator<T>::operator== (const LQueue_Const_Iterator<T> &rhs) const
{
// check if the iterator points to the same position in the same queue
return (pos_ == rhs.pos_);
}
template <typename T> bool
LQueue_Const_Iterator<T>::operator!= (const LQueue_Const_Iterator<T> &rhs) const
{
return !(*this == rhs);
}
template <typename T>
LQueue_Const_Iterator<T>::LQueue_Const_Iterator (const LQueue<T> &queue,
size_t pos)
: queue_ (queue),
pos_ (queue.tail_->next_)
{
// iterator over the queue unto the right position
// we save iterations for values > count_ by doing modulo calculations
for (pos = pos % (queue_.count_ -1);
pos > 0;
--pos)
{
// advance one position each time
pos_ = pos_->next_;
}
}
template <typename T>
LQueue_Const_Iterator<T>::LQueue_Const_Iterator (const LQueue<T> &queue,
LQueue_Node<T> *pos)
: queue_ (queue),
pos_ (pos)
{
}
#endif /* _LQUEUE_CPP */