sat-solver/Queue.h
00001 /*****************************************************************************************[Queue.h]
00002 MiniSat -- Copyright (c) 2003-2006, Niklas Een, Niklas Sorensson
00003 
00004 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
00005 associated documentation files (the "Software"), to deal in the Software without restriction,
00006 including without limitation the rights to use, copy, modify, merge, publish, distribute,
00007 sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
00008 furnished to do so, subject to the following conditions:
00009 
00010 The above copyright notice and this permission notice shall be included in all copies or
00011 substantial portions of the Software.
00012 
00013 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
00014 NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00015 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00016 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
00017 OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00018 **************************************************************************************************/
00019 
00020 #ifndef Queue_h
00021 #define Queue_h
00022 
00023 #include "Vec.h"
00024 
00025 //=================================================================================================
00026 
00027 
00028 template <class T>
00029 class Queue {
00030     vec<T>  elems;
00031     int     first;
00032 
00033 public:
00034     Queue(void) : first(0) { }
00035 
00036     void insert(T x)   { elems.push(x); }
00037     T    peek  () const { return elems[first]; }
00038     void pop   () { first++; }
00039 
00040     void clear(bool dealloc = false)   { elems.clear(dealloc); first = 0; }
00041     int  size(void)    { return elems.size() - first; }
00042 
00043     //bool has(T x) { for (int i = first; i < elems.size(); i++) if (elems[i] == x) return true; return false; }
00044 
00045     const T& operator [] (int index) const  { return elems[first + index]; }
00046 
00047 };
00048 
00049 //template<class T>
00050 //class Queue {
00051 //    vec<T>  buf;
00052 //    int     first;
00053 //    int     end;
00054 //
00055 //public:
00056 //    typedef T Key;
00057 //
00058 //    Queue() : buf(1), first(0), end(0) {}
00059 //
00060 //    void clear () { buf.shrinkTo(1); first = end = 0; }
00061 //    int  size  () { return (end >= first) ? end - first : end - first + buf.size(); }
00062 //
00063 //    T    peek  () { assert(first != end); return buf[first]; }
00064 //    void pop   () { assert(first != end); first++; if (first == buf.size()) first = 0; }
00065 //    void insert(T elem) {   // INVARIANT: buf[end] is always unused
00066 //        buf[end++] = elem;
00067 //        if (end == buf.size()) end = 0;
00068 //        if (first == end){  // Resize:
00069 //            vec<T>  tmp((buf.size()*3 + 1) >> 1);
00070 //            //**/printf("queue alloc: %d elems (%.1f MB)\n", tmp.size(), tmp.size() * sizeof(T) / 1000000.0);
00071 //            int     i = 0;
00072 //            for (int j = first; j < buf.size(); j++) tmp[i++] = buf[j];
00073 //            for (int j = 0    ; j < end       ; j++) tmp[i++] = buf[j];
00074 //            first = 0;
00075 //            end   = buf.size();
00076 //            tmp.moveTo(buf);
00077 //        }
00078 //    }
00079 //};
00080 
00081 //=================================================================================================
00082 #endif