casa  5.7.0-16
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Queue.h
Go to the documentation of this file.
1 //# Queue.h: A First-In-First-Out (FIFO) data structure.
2 //# Copyright (C) 1995,1999
3 //# Associated Universities, Inc. Washington DC, USA.
4 //#
5 //# This library is free software; you can redistribute it and/or modify it
6 //# under the terms of the GNU Library General Public License as published by
7 //# the Free Software Foundation; either version 2 of the License, or (at your
8 //# option) any later version.
9 //#
10 //# This library is distributed in the hope that it will be useful, but WITHOUT
11 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13 //# License for more details.
14 //#
15 //# You should have received a copy of the GNU Library General Public License
16 //# along with this library; if not, write to the Free Software Foundation,
17 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18 //#
19 //# Correspondence concerning AIPS++ should be addressed as follows:
20 //# Internet email: aips2-request@nrao.edu.
21 //# Postal address: AIPS++ Project Office
22 //# National Radio Astronomy Observatory
23 //# 520 Edgemont Road
24 //# Charlottesville, VA 22903-2475 USA
25 //#
26 //# $Id$
27 
28 #ifndef CASA_QUEUE_H
29 #define CASA_QUEUE_H
30 
31 #ifndef AIPS_USE_DEPRECATED
32 #error "Queue.h is deprecated; use -DBUILD_DEPRECATED=ON to use it"
33 #endif
34 
35 #include <casacore/casa/aips.h>
37 
38 namespace casacore { //# NAMESPACE CASACORE - BEGIN
39 
40 //
41 
42 // <summary>
43 // A First-In-First-Out (FIFO) data structure.
44 // </summary>
45 //
46 // <reviewed reviewer="Gareth Hunt" date="94Jan06" tests="tQueue" demos="">
47 // </reviewed>
48 //
49 // <synopsis>
50 // A Queue as implemented here is a simple container which can grow with time,
51 // and which returns its elements (only) in the order they are inserted. That
52 // is, the fundamental operations are to insert an element in the queue (at the
53 // "back") and retrieve an element (from the "front").
54 //
55 // <srcblock>
56 // Queue<Int> queue;
57 // Int x;
58 // queue(1); // enqueue
59 // queue.enqueue(2); // enqueue
60 // queue(3); // enqueue
61 // queue(4); // enqueue
62 // queue.dequeue(x); // dequeue
63 // cout << x << endl;
64 // ...
65 // while (queue.nelements() > 0)
66 // cout << queue() << " "; // dequeue
67 // cout << endl;
68 // </srcblock>
69 //
70 // Presently the implementation is rather simple. It stores the elements in
71 // a Block<T> which resizes (exponentially to avoid quadratic behaviour) when
72 // necessary. New elements are added to the end of the block, old elements are
73 // pulled off the front of the Block. The positions at the beginning are only
74 // reclaimed when the queue is empty or the compress() member is called.
75 // This implementation is reasonably time
76 // efficient, but not necessarily space efficient. A more sophisticated
77 // implementation may be necessary eventually.
78 //
79 // To be used in a Queue, a class must have a default constructor, assignment
80 // operator, and copy constructor.
81 // </synopsis>
82 //
83 // <motivation>
84 // This class was written for an application which thought it needed to queue
85 // up some Glish events while it processed other Glish events. In fact that
86 // application (Clean) was simplified so that it doesn't presently operate that
87 // way.
88 // </motivation>
89 //
90 // <todo asof="28OCT94">
91 // <li> It is conceivable that an iterator might be useful for this class.
92 // <li> If this class is ever heavily used, a more space efficient
93 // implementation may be necessary.
94 // </todo>
95 
96 template<class T> class Queue
97 {
98 public:
99  // Create a Queue with no elements.
100  Queue();
101 
102  // Create a queue which is a copy of other. Compresses unused heap storage.
103  Queue(const Queue<T> &other);
104 
105  ~Queue();
106 
107  // Create a queue which is a copy of other. Compresses unused heap storage.
108  Queue<T> &operator=(const Queue<T> &other);
109 
110  // Place an element in the queue. After calling this,
111  // nelements() is increaed by one.
112  // <group>
113  void enqueue(const T &value);
114  // Short-hand for enqueue();
115  void operator()(const T &value);
116  // </group>
117 
118  // Remove an element from the head of the queue and decrease
119  // nelements() by one. If called when nelements() is zero, an
120  // exception is thrown.
121  // <group>
122  void dequeue(T &value);
123  // Short-hand for dequeue.
124  T operator()();
125  // </group>
126 
127  // Delete all the elements from the queue, and free up any resources.
128  void clear();
129 
130  // Leave this queue logically unchanged, but remove unused storage.
131  // With the present Block<T> based implementation, removes
132  // the unused entries at the beginning of the block.
133  void compress();
134 
135  // How many elements are in the queue?
136  uInt nelements() const {return next_p - first_p;}
137 private:
141 };
142 
143 
144 } //# NAMESPACE CASACORE - END
145 
146 #ifndef CASACORE_NO_AUTO_TEMPLATES
147 #include <casacore/casa/Containers/Queue.tcc>
148 #endif //# CASACORE_NO_AUTO_TEMPLATES
149 #endif
int Int
Definition: aipstype.h:50
void dequeue(T &value)
Remove an element from the head of the queue and decrease nelements() by one.
void enqueue(const T &value)
Place an element in the queue.
T operator()()
Short-hand for dequeue.
void compress()
Leave this queue logically unchanged, but remove unused storage.
void clear()
Delete all the elements from the queue, and free up any resources.
Block< T > data_p
Definition: Queue.h:140
A First-In-First-Out (FIFO) data structure.
Definition: Queue.h:96
uInt nelements() const
How many elements are in the queue?
Definition: Queue.h:136
simple 1-D array
Queue()
Create a Queue with no elements.
LatticeExprNode value(const LatticeExprNode &expr)
This function returns the value of the expression without a mask.
Queue< T > & operator=(const Queue< T > &other)
Create a queue which is a copy of other.
unsigned int uInt
Definition: aipstype.h:51
#define casacore
&lt;X11/Intrinsic.h&gt; #defines true, false, casacore::Bool, and String.
Definition: X11Intrinsic.h:42