sample3-inl.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Copyright 2005, Google Inc.
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. // * Neither the name of Google Inc. nor the names of its
  15. // contributors may be used to endorse or promote products derived from
  16. // this software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. // A sample program demonstrating using Google C++ testing framework.
  30. //
  31. // Author: wan@google.com (Zhanyong Wan)
  32. #ifndef GTEST_SAMPLES_SAMPLE3_INL_H_
  33. #define GTEST_SAMPLES_SAMPLE3_INL_H_
  34. #include <stddef.h>
  35. // Queue is a simple queue implemented as a singled-linked list.
  36. //
  37. // The element type must support copy constructor.
  38. template <typename E> // E is the element type
  39. class Queue;
  40. // QueueNode is a node in a Queue, which consists of an element of
  41. // type E and a pointer to the next node.
  42. template <typename E> // E is the element type
  43. class QueueNode {
  44. friend class Queue<E>;
  45. public:
  46. // Gets the element in this node.
  47. const E& element() const { return element_; }
  48. // Gets the next node in the queue.
  49. QueueNode* next() { return next_; }
  50. const QueueNode* next() const { return next_; }
  51. private:
  52. // Creates a node with a given element value. The next pointer is
  53. // set to NULL.
  54. explicit QueueNode(const E& an_element) : element_(an_element), next_(NULL) {}
  55. // We disable the default assignment operator and copy c'tor.
  56. const QueueNode& operator = (const QueueNode&);
  57. QueueNode(const QueueNode&);
  58. E element_;
  59. QueueNode* next_;
  60. };
  61. template <typename E> // E is the element type.
  62. class Queue {
  63. public:
  64. // Creates an empty queue.
  65. Queue() : head_(NULL), last_(NULL), size_(0) {}
  66. // D'tor. Clears the queue.
  67. ~Queue() { Clear(); }
  68. // Clears the queue.
  69. void Clear() {
  70. if (size_ > 0) {
  71. // 1. Deletes every node.
  72. QueueNode<E>* node = head_;
  73. QueueNode<E>* next = node->next();
  74. for (; ;) {
  75. delete node;
  76. node = next;
  77. if (node == NULL) break;
  78. next = node->next();
  79. }
  80. // 2. Resets the member variables.
  81. head_ = last_ = NULL;
  82. size_ = 0;
  83. }
  84. }
  85. // Gets the number of elements.
  86. size_t Size() const { return size_; }
  87. // Gets the first element of the queue, or NULL if the queue is empty.
  88. QueueNode<E>* Head() { return head_; }
  89. const QueueNode<E>* Head() const { return head_; }
  90. // Gets the last element of the queue, or NULL if the queue is empty.
  91. QueueNode<E>* Last() { return last_; }
  92. const QueueNode<E>* Last() const { return last_; }
  93. // Adds an element to the end of the queue. A copy of the element is
  94. // created using the copy constructor, and then stored in the queue.
  95. // Changes made to the element in the queue doesn't affect the source
  96. // object, and vice versa.
  97. void Enqueue(const E& element) {
  98. QueueNode<E>* new_node = new QueueNode<E>(element);
  99. if (size_ == 0) {
  100. head_ = last_ = new_node;
  101. size_ = 1;
  102. } else {
  103. last_->next_ = new_node;
  104. last_ = new_node;
  105. size_++;
  106. }
  107. }
  108. // Removes the head of the queue and returns it. Returns NULL if
  109. // the queue is empty.
  110. E* Dequeue() {
  111. if (size_ == 0) {
  112. return NULL;
  113. }
  114. const QueueNode<E>* const old_head = head_;
  115. head_ = head_->next_;
  116. size_--;
  117. if (size_ == 0) {
  118. last_ = NULL;
  119. }
  120. E* element = new E(old_head->element());
  121. delete old_head;
  122. return element;
  123. }
  124. // Applies a function/functor on each element of the queue, and
  125. // returns the result in a new queue. The original queue is not
  126. // affected.
  127. template <typename F>
  128. Queue* Map(F function) const {
  129. Queue* new_queue = new Queue();
  130. for (const QueueNode<E>* node = head_; node != NULL; node = node->next_) {
  131. new_queue->Enqueue(function(node->element()));
  132. }
  133. return new_queue;
  134. }
  135. private:
  136. QueueNode<E>* head_; // The first node of the queue.
  137. QueueNode<E>* last_; // The last node of the queue.
  138. size_t size_; // The number of elements in the queue.
  139. // We disallow copying a queue.
  140. Queue(const Queue&);
  141. const Queue& operator = (const Queue&);
  142. };
  143. #endif // GTEST_SAMPLES_SAMPLE3_INL_H_