this repo has no description
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ 2/* 3 * Main authors: 4 * Christian Schulte <schulte@gecode.org> 5 * 6 * Copyright: 7 * Christian Schulte, 2009 8 * 9 * This file is part of Gecode, the generic constraint 10 * development environment: 11 * http://www.gecode.org 12 * 13 * Permission is hereby granted, free of charge, to any person obtaining 14 * a copy of this software and associated documentation files (the 15 * "Software"), to deal in the Software without restriction, including 16 * without limitation the rights to use, copy, modify, merge, publish, 17 * distribute, sublicense, and/or sell copies of the Software, and to 18 * permit persons to whom the Software is furnished to do so, subject to 19 * the following conditions: 20 * 21 * The above copyright notice and this permission notice shall be 22 * included in all copies or substantial portions of the Software. 23 * 24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 28 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 29 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 30 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 31 * 32 */ 33 34namespace Gecode { namespace Support { 35 36 /** 37 * \brief Queue with arbitrary number of elements 38 * 39 * \ingroup FuncSupport 40 */ 41 template<class T, class A> 42 class DynamicQueue { 43 private: 44 /// Memory allocator 45 A& a; 46 /// Current size of queue (must be power of two) 47 int limit; 48 /// First element in queue 49 int fst; 50 /// Last element in queue 51 int lst; 52 /// Elements in queue 53 T* q; 54 /// Resize queue (double size) 55 void resize(void); 56 /// Move index \a i to next element 57 void move(int& i); 58 public: 59 /// Initialize queue 60 DynamicQueue(A& a); 61 /// Release memory 62 ~DynamicQueue(void); 63 64 /// Test whether queue is empty 65 bool empty(void) const; 66 67 /// Reset queue to be empty 68 void reset(void); 69 70 /// Pop element added first from queue and return it 71 T pop(void); 72 /// Push element \a x to queue 73 void push(const T& x); 74 75 /// Allocate memory from heap (disabled) 76 static void* operator new(size_t s) = delete; 77 /// Free memory allocated from heap (disabled) 78 static void operator delete(void* p) = delete; 79 /// Copy constructor (disabled) 80 DynamicQueue(const DynamicQueue& s) = delete; 81 /// Assignment operator (disabled) 82 const DynamicQueue& operator =(const DynamicQueue&) = delete; 83 }; 84 85 86 template<class T, class A> 87 forceinline void 88 DynamicQueue<T,A>::move(int& i) { 89 i = (i+1) & (limit - 1); 90 } 91 92 template<class T, class A> 93 void 94 DynamicQueue<T,A>::resize(void) { 95 assert(fst == lst); 96 T* nq = a.template alloc<T>(limit << 1); 97 int j=0; 98 for (int i = fst; i<limit; i++) 99 nq[j++] = q[i]; 100 for (int i = 0; i<lst; i++) 101 nq[j++] = q[i]; 102 a.template free<T>(q,limit); 103 q = nq; 104 fst = 0; 105 lst = limit; 106 limit <<= 1; 107 } 108 109 template<class T, class A> 110 forceinline 111 DynamicQueue<T,A>::DynamicQueue(A& a0) 112 : a(a0), limit(8), fst(0), lst(0), q(a.template alloc<T>(limit)) {} 113 114 template<class T, class A> 115 forceinline 116 DynamicQueue<T,A>::~DynamicQueue(void) { 117 a.free(q,limit); 118 } 119 120 template<class T, class A> 121 forceinline bool 122 DynamicQueue<T,A>::empty(void) const { 123 return fst == lst; 124 } 125 126 template<class T, class A> 127 forceinline void 128 DynamicQueue<T,A>::reset(void) { 129 fst = lst = 0; 130 } 131 132 template<class T, class A> 133 forceinline T 134 DynamicQueue<T,A>::pop(void) { 135 assert(!empty()); 136 T t = q[fst]; 137 move(fst); 138 return t; 139 } 140 141 template<class T, class A> 142 forceinline void 143 DynamicQueue<T,A>::push(const T& x) { 144 q[lst] = x; 145 move(lst); 146 if (fst == lst) 147 resize(); 148 } 149 150}} 151 152// STATISTICS: support-any