6 * $Date: 2012-07-16 14:23:36 +0200 (Mo, 16. Jul 2012) $
7 ***************************************************************/
10 * \brief Declaration and implementation of bounded queue class.
12 * \author Carsten Gutwenger
15 * This file is part of the Open Graph Drawing Framework (OGDF).
19 * See README.txt in the root directory of the OGDF installation for details.
22 * This program is free software; you can redistribute it and/or
23 * modify it under the terms of the GNU General Public License
24 * Version 2 or 3 as published by the Free Software Foundation;
25 * see the file LICENSE.txt included in the packaging of this file
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
35 * You should have received a copy of the GNU General Public
36 * License along with this program; if not, write to the Free
37 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
38 * Boston, MA 02110-1301, USA.
40 * \see http://www.gnu.org/copyleft/gpl.html
41 ***************************************************************/
47 #ifndef OGDF_B_QUEUE_H
48 #define OGDF_B_QUEUE_H
51 #include <ogdf/basic/basic.h>
56 template<class E
, class INDEX
> class BoundedQueue
;
59 template<class E
, class INDEX
>
60 void print(ostream
&os
, const BoundedQueue
<E
,INDEX
> &S
, char delim
= ' ');
63 //! The parameterized class \a BoundedQueue<E,INDEX> implements queues with bounded size.
65 * @tparam E is the element type.
66 * @tparam INDEX is the index type. The default index type is \c int, other possible types
67 * are \c short and <code>long long</code> (on 64-bit systems).
69 template<class E
, class INDEX
= int> class BoundedQueue
{
71 E
*m_pStart
; //! Pointer to first element of current sequence.
72 E
*m_pEnd
; //! Pointer to one past last element of current sequence.
73 E
*m_pStop
; //! Pointer to one past last element of total array.
74 E
*m_pFirst
; //! Pointer to first element of total array.
77 //! Constructs an empty bounded queue for at most \a n elements.
78 explicit BoundedQueue(INDEX n
) {
80 m_pStart
= m_pEnd
= m_pFirst
= new E
[n
+1];
81 if (m_pFirst
== 0) OGDF_THROW(InsufficientMemoryException
);
83 m_pStop
= m_pFirst
+n
+1;
86 //! Constructs a bounded queue that is a copy of \a Q.
87 BoundedQueue(const BoundedQueue
<E
> &Q
) {
92 ~BoundedQueue() { delete [] m_pFirst
; }
94 //! Returns front element.
95 const E
&top() const {
96 OGDF_ASSERT(m_pStart
!= m_pEnd
)
100 //! Returns front element.
102 OGDF_ASSERT(m_pStart
!= m_pEnd
)
106 //! Returns back element.
107 const E
&bottom() const {
108 OGDF_ASSERT(m_pStart
!= m_pEnd
)
109 if (m_pEnd
== m_pFirst
) return *(m_pStop
-1);
110 else return *(m_pEnd
-1);
113 //! Returns back element.
115 OGDF_ASSERT(m_pStart
!= m_pEnd
)
116 if (m_pEnd
== m_pFirst
) return *(m_pStop
-1);
117 else return *(m_pEnd
-1);
120 //! Returns current size of the queue.
122 return (m_pEnd
>= m_pStart
) ?
123 (m_pEnd
- m_pStart
) :
124 (m_pEnd
-m_pFirst
)+(m_pStop
-m_pStart
);
127 //! Returns the capacity of the bounded queue.
128 INDEX
capacity() const { return (m_pStop
- m_pFirst
)-1; }
130 //! Returns true iff the queue is empty.
131 bool empty() { return m_pStart
== m_pEnd
; }
133 //! Returns true iff the queue is full.
135 INDEX h
= m_pEnd
-m_pStart
137 (h
== m_pStop
-m_pFirst
-1) :
141 //! Assignment operator.
142 BoundedQueue
<E
> &operator=(const BoundedQueue
<E
> &Q
) {
148 //! Adds \a x at the end of queue.
149 void append(const E
&x
) {
151 if (m_pEnd
== m_pStop
) m_pEnd
= m_pFirst
;
152 OGDF_ASSERT(m_pStart
!= m_pEnd
)
155 //! Removes front element and returns it.
157 OGDF_ASSERT(m_pStart
!= m_pEnd
)
159 if (m_pStart
== m_pStop
) m_pStart
= m_pFirst
;
163 //! Makes the queue empty.
164 void clear() { m_pStart
= m_pEnd
= m_pFirst
; }
166 //! Prints the queue to output stream \a os.
167 void print(ostream
&os
, char delim
= ' ') const
169 for (const E
*pX
= m_pStart
; pX
!= m_pEnd
; ) {
170 if (pX
!= m_pStart
) os
<< delim
;
172 if (++pX
== m_pStop
) pX
= m_pFirst
;
177 void copy(const BoundedQueue
<E
> &Q
) {
179 m_pEnd
= m_pStart
= m_pFirst
= new E
[n
];
180 if (m_pFirst
== 0) OGDF_THROW(InsufficientMemoryException
);
182 m_pStop
= m_pStart
+ n
;
183 for (E
*pX
= Q
.m_pStart
; pX
!= Q
.m_pEnd
; ) {
185 if (pX
== Q
.m_pStop
) pX
= Q
.m_pFirst
;
188 }; // class BoundedQueue
193 template<class E
, class INDEX
>
194 ostream
&operator<<(ostream
&os
, const BoundedQueue
<E
,INDEX
> &Q
)
200 } // end namespace ogdf