casa
$Rev:20696$
|
00001 //# Queue.h: A First-In-First-Out (FIFO) data structure. 00002 //# Copyright (C) 1995,1999 00003 //# Associated Universities, Inc. Washington DC, USA. 00004 //# 00005 //# This library is free software; you can redistribute it and/or modify it 00006 //# under the terms of the GNU Library General Public License as published by 00007 //# the Free Software Foundation; either version 2 of the License, or (at your 00008 //# option) any later version. 00009 //# 00010 //# This library is distributed in the hope that it will be useful, but WITHOUT 00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00012 //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 00013 //# License for more details. 00014 //# 00015 //# You should have received a copy of the GNU Library General Public License 00016 //# along with this library; if not, write to the Free Software Foundation, 00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA. 00018 //# 00019 //# Correspondence concerning AIPS++ should be addressed as follows: 00020 //# Internet email: aips2-request@nrao.edu. 00021 //# Postal address: AIPS++ Project Office 00022 //# National Radio Astronomy Observatory 00023 //# 520 Edgemont Road 00024 //# Charlottesville, VA 22903-2475 USA 00025 //# 00026 //# $Id: Queue.h 20652 2009-07-06 05:04:32Z Malte.Marquarding $ 00027 00028 #ifndef CASA_QUEUE_H 00029 #define CASA_QUEUE_H 00030 00031 #include <casa/aips.h> 00032 #include <casa/Containers/Block.h> 00033 00034 namespace casa { //# NAMESPACE CASA - BEGIN 00035 00036 // 00037 00038 // <summary> 00039 // A First-In-First-Out (FIFO) data structure. 00040 // </summary> 00041 // 00042 // <reviewed reviewer="Gareth Hunt" date="94Jan06" tests="tQueue" demos=""> 00043 // </reviewed> 00044 // 00045 // <synopsis> 00046 // A Queue as implemented here is a simple container which can grow with time, 00047 // and which returns its elements (only) in the order they are inserted. That 00048 // is, the fundamental operations are to insert an element in the queue (at the 00049 // "back") and retrieve an element (from the "front"). 00050 // 00051 // <srcblock> 00052 // Queue<Int> queue; 00053 // Int x; 00054 // queue(1); // enqueue 00055 // queue.enqueue(2); // enqueue 00056 // queue(3); // enqueue 00057 // queue(4); // enqueue 00058 // queue.dequeue(x); // dequeue 00059 // cout << x << endl; 00060 // ... 00061 // while (queue.nelements() > 0) 00062 // cout << queue() << " "; // dequeue 00063 // cout << endl; 00064 // </srcblock> 00065 // 00066 // Presently the implementation is rather simple. It stores the elements in 00067 // a Block<T> which resizes (exponentially to avoid quadratic behaviour) when 00068 // necessary. New elements are added to the end of the block, old elements are 00069 // pulled off the front of the Block. The positions at the beginning are only 00070 // reclaimed when the queue is empty or the compress() member is called. 00071 // This implementation is reasonably time 00072 // efficient, but not necessarily space efficient. A more sophisticated 00073 // implementation may be necessary eventually. 00074 // 00075 // To be used in a Queue, a class must have a default constructor, assignment 00076 // operator, and copy constructor. 00077 // </synopsis> 00078 // 00079 // <motivation> 00080 // This class was written for an application which thought it needed to queue 00081 // up some Glish events while it processed other Glish events. In fact that 00082 // application (Clean) was simplified so that it doesn't presently operate that 00083 // way. 00084 // </motivation> 00085 // 00086 // <todo asof="28OCT94"> 00087 // <li> It is conceivable that an iterator might be useful for this class. 00088 // <li> If this class is ever heavily used, a more space efficient 00089 // implementation may be necessary. 00090 // </todo> 00091 00092 template<class T> class Queue 00093 { 00094 public: 00095 // Create a Queue with no elements. 00096 Queue(); 00097 00098 // Create a queue which is a copy of other. Compresses unused heap storage. 00099 Queue(const Queue<T> &other); 00100 00101 ~Queue(); 00102 00103 // Create a queue which is a copy of other. Compresses unused heap storage. 00104 Queue<T> &operator=(const Queue<T> &other); 00105 00106 // Place an element in the queue. After calling this, 00107 // nelements() is increaed by one. 00108 // <group> 00109 void enqueue(const T &value); 00110 // Short-hand for enqueue(); 00111 void operator()(const T &value); 00112 // </group> 00113 00114 // Remove an element from the head of the queue and decrease 00115 // nelements() by one. If called when nelements() is zero, an 00116 // exception is thrown. 00117 // <group> 00118 void dequeue(T &value); 00119 // Short-hand for dequeue. 00120 T operator()(); 00121 // </group> 00122 00123 // Delete all the elements from the queue, and free up any resources. 00124 void clear(); 00125 00126 // Leave this queue logically unchanged, but remove unused storage. 00127 // With the present Block<T> based implementation, removes 00128 // the unused entries at the beginning of the block. 00129 void compress(); 00130 00131 // How many elements are in the queue? 00132 uInt nelements() const {return next_p - first_p;} 00133 private: 00134 Int first_p; 00135 Int next_p; 00136 Block<T> data_p; 00137 }; 00138 00139 00140 } //# NAMESPACE CASA - END 00141 00142 #ifndef CASACORE_NO_AUTO_TEMPLATES 00143 #include <casa/Containers/Queue.tcc> 00144 #endif //# CASACORE_NO_AUTO_TEMPLATES 00145 #endif