forked from douglascraigschmidt/CPlusPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLStack.cpp
More file actions
593 lines (487 loc) · 13.9 KB
/
Copy pathLStack.cpp
File metadata and controls
593 lines (487 loc) · 13.9 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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
#ifndef _LSTACK_CPP_
#define _LSTACK_CPP_
#include <memory>
#include "LStack.h"
// It's fine to use "this" in base-member initializations.
#pragma warning(disable:4355)
/**
* @class LStack_Node
* @brief Defines a node in the @a LStack that's implemented as a linked list.
*/
template <class T>
class LStack_Node
{
friend class LStack<T>;
friend class LStack_Iterator<T>;
friend class LStack_Const_Iterator<T>;
public:
// = Initialization methods
LStack_Node (const T &item,
LStack_Node<T> *next = 0);
LStack_Node (LStack_Node<T> *next);
LStack_Node (void);
// Default constructor that doesn't initialize <item_>.
void *operator new (size_t bytes);
// Allocate a new <LStack_Node>, trying first from the
// <free_list_> and if that's empty try from the global <::operator
// new>.
void operator delete (void *ptr);
// Return <ptr> to the <free_list_>.
LStack_Node *next (void);
// Return the next node to which this node points.
// This method is added to support the Scoped_List class below.
static void free_list_allocate (size_t n);
// Preallocate <n> <LStack_Nodes> and store them on the
// <free_list_>.
static void free_list_release (void);
// Returns all dynamic memory on the free list to the free store.
private:
static LStack_Node<T> *free_list_;
// Head of the "free list", which is a stack of
// <LStack_Nodes> used to speed up allocation.
T item_;
// Item in this node.
LStack_Node<T> *next_;
// Pointer to the next node.
};
/* static */
template <class T> LStack_Node<T> *
LStack_Node<T>::free_list_ = 0;
// Allocate a new <LStack_Node>, trying first from the
// <free_list_> and if that's empty try from the global <::operator
// new>.
template <class T> void *
LStack_Node<T>::operator new (size_t)
{
// extract element from the free_list_ if there is one left
if (LStack_Node<T>::free_list_ != 0)
{
// get the top element of the list
LStack_Node<T>* new_node = LStack_Node<T>::free_list_;
// "remove" the element from the list and pass it to the caller
LStack_Node<T>::free_list_ = new_node->next_;
return new_node;
}
return ::operator new (sizeof (LStack_Node<T>));
}
// Return <ptr> to the <free_list_>.
template <class T> void
LStack_Node<T>::operator delete (void *ptr)
{
// do nothing on a null pointer
if (ptr != 0)
{
// cast to a node pointer
LStack_Node<T>* node = static_cast<LStack_Node<T>*> (ptr);
// put the node back into the list
node->next_ = LStack_Node<T>::free_list_;
LStack_Node<T>::free_list_ = node;
}
}
// Returns the next node to which this node points.
template <class T> LStack_Node<T> *
LStack_Node<T>::next (void)
{
return next_;
}
// Returns all dynamic memory on the free list to the free store.
template <class T> void
LStack_Node<T>::free_list_release (void)
{
// delete free list element by element
while (LStack_Node<T>::free_list_ != 0)
{
LStack_Node<T>* node = LStack_Node<T>::free_list_;
LStack_Node<T>::free_list_ = node->next_;
::operator delete (node);
}
}
// Preallocate <n> <LStack_Nodes> and store them on the
// <free_list_>.
template <class T> void
LStack_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
LStack_Node<T>* new_node =
reinterpret_cast<LStack_Node<T>*> (
::operator new (sizeof (LStack_Node<T>)));
new_node->next_ = LStack_Node<T>::free_list_;
// make the new element the top of the list
LStack_Node<T>::free_list_ = new_node;
}
}
template <class T>
LStack_Node<T>::LStack_Node (const T &item,
LStack_Node<T> *next)
: item_ (item),
next_ (next)
{
}
template <class T>
LStack_Node<T>::LStack_Node (LStack_Node<T> *next)
: next_ (next)
{
}
// This method is helpful to implement the dummy node in a concise
// way.
template <class T>
LStack_Node<T>::LStack_Node (void)
: next_ (this)
{
}
// Returns the current size.
template <class T> size_t
LStack<T>::size (void) const
{
return count_;
}
// Constructor.
template <class T>
LStack<T>::LStack (size_t size_hint)
// Initialize fields here.
: head_ (0),
count_ (0)
{
// use the size_hint to preallocate memory for nodes
LStack_Node<T>::free_list_allocate (size_hint);
}
// Copy constructor.
template <class T>
LStack<T>::LStack (const LStack<T> &rhs)
// Initialize fields here.
: head_ (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 <LStack_Node<T> > tail (new LStack_Node<T> ());
head_ = 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
LStack<T>::copy_list (const LStack<T> &rhs)
{
LStack<T> temp;
LStack_Node<T>* prev = 0;
// Iterate along the list of stack nodes in <s>, creating a new
// corresponding node and chaining them along. Note that we
// can't use push() to insert at the head since it will reverse
// the stack.
std::auto_ptr <LStack_Node<T> > new_node;
for (typename LStack<T>::const_iterator it = rhs.begin ();
it != rhs.end ();
++it)
{
new_node.reset (new LStack_Node<T> (*it));
if (it == rhs.begin ())
{
// special case for the first iteration: set the head element of
// temporary stack
temp.head_ = new_node.release ();
prev = temp.head_;
}
else
{
// standard case: add one element to prev
prev->next_ = new_node.release ();
prev = prev->next_;
}
// make sure that the element count of temp stays correct
++temp.count_;
}
// we only swap the lists if the temporary list has been successfully
// created. This ensures strong exception guarantees.
std::swap (head_, temp.head_);
// set the counts correctly
std::swap (count_, temp.count_);
}
// Delete a linked list of nodes
template <class T> void
LStack<T>::delete_list ()
{
// we do not delete the dummy node here. This will be done in the destructor
// we pop all elements until the queue is empty again
while (!is_empty ())
{
pop_i ();
}
}
// Delete a linked list of nodes
template <class T> void
LStack<T>::erase (void)
{
delete_list ();
}
// Assignment operator.
template <class T> LStack<T> &
LStack<T>::operator= (const LStack<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>
LStack<T>::~LStack (void)
{
// delete all elements of the list
delete_list ();
}
// 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
LStack<T>::operator== (const LStack<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
LStack<T>::operator!= (const LStack<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
LStack<T>::push (const T &new_item)
{
try
{
// create a temporary new node for exception safety reasons
std::auto_ptr <LStack_Node<T> > temp (new LStack_Node<T> (new_item, head_));
// integrate the new node into the list
head_ = temp.release ();
// 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 push 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
LStack<T>::pop (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 = head_->item_;
// call actual pop implementation
pop_i ();
return item;
}
template <class T> void
LStack<T>::pop_i (void)
{
// remember the current queue head
LStack_Node<T>* head = head_;
// remove the head from the queue
head_ = 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
LStack<T>::top (void) const
{
// check for empty queue first
if (is_empty())
throw Underflow ();
// return the item in head
return head_->item_;
}
// Returns true if the queue is empty, otherwise returns false.
template <class T> bool
LStack<T>::is_empty (void) const
{
return count_ == 0;
}
// Returns true if the queue is full, otherwise returns false.
template <class T> bool
LStack<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 LStack<T>::iterator
LStack<T>::begin (void)
{
// iterator starts at the head element
return typename LStack<T>::iterator (*this, head_);
}
// Get an iterator pointing past the end of the queue
template <typename T> typename LStack<T>::iterator
LStack<T>::end (void)
{
// iterator starts at the tail element
return typename LStack<T>::iterator (*this, (LStack_Node<T>*) 0);
}
// Get an iterator to the begining of the queue
template <typename T> typename LStack<T>::const_iterator
LStack<T>::begin (void) const
{
// iterator starts at the head element
return typename LStack<T>::const_iterator (*this, head_);
}
// Get an iterator pointing past the end of the queue
template <typename T> typename LStack<T>::const_iterator
LStack<T>::end (void) const
{
// iterator starts at the tail element
return typename LStack<T>::const_iterator (*this, (LStack_Node<T>*) 0);
}
template <typename T> T &
LStack_Iterator<T>::operator* (void)
{
return pos_->item_;
}
template <typename T> const T &
LStack_Iterator<T>::operator* (void) const
{
return pos_->item_;
}
template <typename T> LStack_Iterator<T> &
LStack_Iterator<T>::operator++ (void)
{
// advance to the next position
pos_ = pos_->next_;
return *this;
}
// Post-increment.
template <typename T> LStack_Iterator<T>
LStack_Iterator<T>::operator++ (int)
{
// keep copy of the original iterator
LStack_Iterator<T> copy = *this;
// advance to the next position
pos_ = pos_->next_;
// return original iterator
return copy;
}
template <typename T> bool
LStack_Iterator<T>::operator== (const LStack_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
LStack_Iterator<T>::operator!= (const LStack_Iterator<T> &rhs) const
{
// implement != in terms of ==
return !(*this == rhs);
}
template <typename T>
LStack_Iterator<T>::LStack_Iterator (LStack<T> &stack,
size_t pos)
: stack_ (stack),
pos_ (stack.head_)
{
// iterator over the stack unto the right position
// we save iterations for values > count_ by doing modulo calculations
for (pos = pos % (stack_.count_ -1);
pos > 0;
--pos)
{
// advance one position each time
pos_ = pos_->next_;
}
}
template <typename T>
LStack_Iterator<T>::LStack_Iterator (LStack<T> &stack,
LStack_Node<T> *pos)
: stack_ (stack),
pos_ (pos)
{
}
template <typename T> const T &
LStack_Const_Iterator<T>::operator* (void) const
{
return pos_->item_;
}
template <typename T> const LStack_Const_Iterator<T> &
LStack_Const_Iterator<T>::operator++ (void) const
{
// advance to the next position
pos_ = pos_->next_;
return *this;
}
template <typename T> LStack_Const_Iterator<T>
LStack_Const_Iterator<T>::operator++ (int) const
{
// keep copy of the original iterator
LStack_Const_Iterator<T> copy = *this;
// advance to the next position
pos_ = pos_->next_;
// return original iterator
return copy;
}
template <typename T> bool
LStack_Const_Iterator<T>::operator== (const LStack_Const_Iterator<T> &rhs) const
{
// check if the iterator points to the same position in the same stack
return (pos_ == rhs.pos_);
}
template <typename T> bool
LStack_Const_Iterator<T>::operator!= (const LStack_Const_Iterator<T> &rhs) const
{
return !(*this == rhs);
}
template <typename T>
LStack_Const_Iterator<T>::LStack_Const_Iterator (const LStack<T> &stack,
size_t pos)
: stack_ (stack),
pos_ (stack.head_)
{
// iterator over the stack unto the right position
// we save iterations for values > count_ by doing modulo calculations
for (pos = pos % (stack_.count_ -1);
pos > 0;
--pos)
{
// advance one position each time
pos_ = pos_->next_;
}
}
template <typename T>
LStack_Const_Iterator<T>::LStack_Const_Iterator (const LStack<T> &stack,
LStack_Node<T> *pos)
: stack_ (stack),
pos_ (pos)
{
}
#endif /* _LSTACK_CPP_ */