sample3-inl.h 5.24 KB
Newer Older
shiqian's avatar
shiqian committed
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
// Copyright 2005, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//     * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

// A sample program demonstrating using Google C++ testing framework.
//
// Author: wan@google.com (Zhanyong Wan)

#ifndef GTEST_SAMPLES_SAMPLE3_INL_H_
#define GTEST_SAMPLES_SAMPLE3_INL_H_

#include <stddef.h>


// Queue is a simple queue implemented as a singled-linked list.
//
// The element type must support copy constructor.
template <typename E>  // E is the element type
class Queue;

// QueueNode is a node in a Queue, which consists of an element of
// type E and a pointer to the next node.
template <typename E>  // E is the element type
class QueueNode {
  friend class Queue<E>;

 public:
  // Gets the element in this node.
54
  const E& element() const { return element_; }
shiqian's avatar
shiqian committed
55
56

  // Gets the next node in the queue.
57
58
  QueueNode* next() { return next_; }
  const QueueNode* next() const { return next_; }
shiqian's avatar
shiqian committed
59
60
61
62

 private:
  // Creates a node with a given element value.  The next pointer is
  // set to NULL.
63
  explicit QueueNode(const E& an_element) : element_(an_element), next_(NULL) {}
shiqian's avatar
shiqian committed
64
65

  // We disable the default assignment operator and copy c'tor.
66
67
  const QueueNode& operator = (const QueueNode&);
  QueueNode(const QueueNode&);
shiqian's avatar
shiqian committed
68
69

  E element_;
70
  QueueNode* next_;
shiqian's avatar
shiqian committed
71
72
73
74
};

template <typename E>  // E is the element type.
class Queue {
75
 public:
shiqian's avatar
shiqian committed
76
77
78
79
80
81
82
83
84
85
  // Creates an empty queue.
  Queue() : head_(NULL), last_(NULL), size_(0) {}

  // D'tor.  Clears the queue.
  ~Queue() { Clear(); }

  // Clears the queue.
  void Clear() {
    if (size_ > 0) {
      // 1. Deletes every node.
86
87
      QueueNode<E>* node = head_;
      QueueNode<E>* next = node->next();
shiqian's avatar
shiqian committed
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
      for (; ;) {
        delete node;
        node = next;
        if (node == NULL) break;
        next = node->next();
      }

      // 2. Resets the member variables.
      head_ = last_ = NULL;
      size_ = 0;
    }
  }

  // Gets the number of elements.
  size_t Size() const { return size_; }

  // Gets the first element of the queue, or NULL if the queue is empty.
105
106
  QueueNode<E>* Head() { return head_; }
  const QueueNode<E>* Head() const { return head_; }
shiqian's avatar
shiqian committed
107
108

  // Gets the last element of the queue, or NULL if the queue is empty.
109
110
  QueueNode<E>* Last() { return last_; }
  const QueueNode<E>* Last() const { return last_; }
shiqian's avatar
shiqian committed
111
112
113
114
115

  // Adds an element to the end of the queue.  A copy of the element is
  // created using the copy constructor, and then stored in the queue.
  // Changes made to the element in the queue doesn't affect the source
  // object, and vice versa.
116
117
  void Enqueue(const E& element) {
    QueueNode<E>* new_node = new QueueNode<E>(element);
shiqian's avatar
shiqian committed
118
119
120
121
122
123
124
125
126
127
128
129
130

    if (size_ == 0) {
      head_ = last_ = new_node;
      size_ = 1;
    } else {
      last_->next_ = new_node;
      last_ = new_node;
      size_++;
    }
  }

  // Removes the head of the queue and returns it.  Returns NULL if
  // the queue is empty.
131
  E* Dequeue() {
shiqian's avatar
shiqian committed
132
133
134
135
    if (size_ == 0) {
      return NULL;
    }

136
    const QueueNode<E>* const old_head = head_;
shiqian's avatar
shiqian committed
137
138
139
140
141
142
    head_ = head_->next_;
    size_--;
    if (size_ == 0) {
      last_ = NULL;
    }

143
    E* element = new E(old_head->element());
shiqian's avatar
shiqian committed
144
145
146
147
148
149
150
151
152
    delete old_head;

    return element;
  }

  // Applies a function/functor on each element of the queue, and
  // returns the result in a new queue.  The original queue is not
  // affected.
  template <typename F>
153
154
155
  Queue* Map(F function) const {
    Queue* new_queue = new Queue();
    for (const QueueNode<E>* node = head_; node != NULL; node = node->next_) {
shiqian's avatar
shiqian committed
156
157
158
159
160
161
162
      new_queue->Enqueue(function(node->element()));
    }

    return new_queue;
  }

 private:
163
164
  QueueNode<E>* head_;  // The first node of the queue.
  QueueNode<E>* last_;  // The last node of the queue.
shiqian's avatar
shiqian committed
165
166
167
  size_t size_;  // The number of elements in the queue.

  // We disallow copying a queue.
168
169
  Queue(const Queue&);
  const Queue& operator = (const Queue&);
170
};
shiqian's avatar
shiqian committed
171
172

#endif  // GTEST_SAMPLES_SAMPLE3_INL_H_