dynamic-queue.hpp
Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ 00002 /* 00003 * Main authors: 00004 * Christian Schulte <schulte@gecode.org> 00005 * 00006 * Copyright: 00007 * Christian Schulte, 2009 00008 * 00009 * Last modified: 00010 * $Date: 2009-09-08 21:10:29 +0200 (Tue, 08 Sep 2009) $ by $Author: schulte $ 00011 * $Revision: 9692 $ 00012 * 00013 * This file is part of Gecode, the generic constraint 00014 * development environment: 00015 * http://www.gecode.org 00016 * 00017 * Permission is hereby granted, free of charge, to any person obtaining 00018 * a copy of this software and associated documentation files (the 00019 * "Software"), to deal in the Software without restriction, including 00020 * without limitation the rights to use, copy, modify, merge, publish, 00021 * distribute, sublicense, and/or sell copies of the Software, and to 00022 * permit persons to whom the Software is furnished to do so, subject to 00023 * the following conditions: 00024 * 00025 * The above copyright notice and this permission notice shall be 00026 * included in all copies or substantial portions of the Software. 00027 * 00028 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 00029 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00030 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00031 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 00032 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 00033 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 00034 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00035 * 00036 */ 00037 00038 namespace Gecode { namespace Support { 00039 00045 template<class T, class A> 00046 class DynamicQueue { 00047 private: 00049 A& a; 00051 int limit; 00053 int fst; 00055 int lst; 00057 T* q; 00059 void resize(void); 00061 void move(int& i); 00062 public: 00064 DynamicQueue(A& a); 00066 ~DynamicQueue(void); 00067 00069 bool empty(void) const; 00070 00072 void reset(void); 00073 00075 T pop(void); 00077 void push(const T& x); 00078 private: 00080 static void* operator new(size_t s) throw() { (void) s; return NULL; } 00082 static void operator delete(void* p) { (void) p; }; 00084 DynamicQueue(const DynamicQueue& s) : a(s.a) {} 00086 const DynamicQueue& operator =(const DynamicQueue&) { return *this; } 00087 }; 00088 00089 00090 template<class T, class A> 00091 forceinline void 00092 DynamicQueue<T,A>::move(int& i) { 00093 i = (i+1) & (limit - 1); 00094 } 00095 00096 template<class T, class A> 00097 void 00098 DynamicQueue<T,A>::resize(void) { 00099 assert(fst == lst); 00100 T* nq = a.template alloc<T>(limit << 1); 00101 int j=0; 00102 for (int i = fst; i<limit; i++) 00103 nq[j++] = q[i]; 00104 for (int i = 0; i<lst; i++) 00105 nq[j++] = q[i]; 00106 a.template free<T>(q,limit); 00107 q = nq; 00108 fst = 0; 00109 lst = limit; 00110 limit <<= 1; 00111 } 00112 00113 template<class T, class A> 00114 forceinline 00115 DynamicQueue<T,A>::DynamicQueue(A& a0) 00116 : a(a0), limit(8), fst(0), lst(0), q(a.template alloc<T>(limit)) {} 00117 00118 template<class T, class A> 00119 forceinline 00120 DynamicQueue<T,A>::~DynamicQueue(void) { 00121 a.free(q,limit); 00122 } 00123 00124 template<class T, class A> 00125 forceinline bool 00126 DynamicQueue<T,A>::empty(void) const { 00127 return fst == lst; 00128 } 00129 00130 template<class T, class A> 00131 forceinline void 00132 DynamicQueue<T,A>::reset(void) { 00133 fst = lst = 0; 00134 } 00135 00136 template<class T, class A> 00137 forceinline T 00138 DynamicQueue<T,A>::pop(void) { 00139 assert(!empty()); 00140 T t = q[fst]; 00141 move(fst); 00142 return t; 00143 } 00144 00145 template<class T, class A> 00146 forceinline void 00147 DynamicQueue<T,A>::push(const T& x) { 00148 q[lst] = x; 00149 move(lst); 00150 if (fst == lst) 00151 resize(); 00152 } 00153 00154 }} 00155 00156 // STATISTICS: support-any