sarray.h

Go to the documentation of this file.
00001 //    sampalib.org ESL library and tools
00002 //    Copyright (C) 2007  Thierry Grellier
00003 //
00004 //    This program is free software; you can redistribute it and/or modify
00005 //    it under the terms of the GNU General Public License version 2 as
00006 //    published by the Free Software Foundation.
00007 //
00008 //    This program is distributed in the hope that it will be useful,
00009 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 //    GNU General Public License for more details.
00012 //
00013 //    You should have received a copy of the GNU General Public License along
00014 //    with this program; if not, write to the Free Software Foundation, Inc.,
00015 //    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00016 //
00017 //    contact: www.sampalib.org
00018 
00019 #ifndef SAMPA_SARRAY_H
00020 #  define SAMPA_SARRAY_H
00021 
00022 #include "sampa/core/persistence.h"
00023 #if SAMPA_CONFIG_COLD_RESTART == SAMPA_ENABLED
00024 #  include "array.h"
00025 #  include "sampa/core/triggerable.h"
00026 #endif
00027 
00028 namespace Sampa {
00029 
00030   template<typename Element>
00031   class SAMPA_PERSISTENT_CLASS(Array) {
00032   public:
00033 #if SAMPA_CONFIG_COLD_RESTART == SAMPA_ENABLED
00034     Element& operator[](size_t idx)            
00035       { return m_elements->body()[idx]; }
00036     const Element& operator[](size_t idx) const           
00037       { return m_elements->body()[idx]; }
00038     Element* begin() { return m_elements->body(); }
00039 #else
00040     Element& operator[](size_t idx)            
00041       { return m_elements[idx]; }
00042     const Element& operator[](size_t idx) const            
00043       { return m_elements[idx]; }
00044     Element* begin() { return m_elements; }
00045 #endif
00046     Array(size_t capacity, const char* name = 0);
00047     ~Array();
00048   private:  
00049     // Disabled
00050     Array(const Array&);
00051     Array& operator=(const Array&);
00052 #if SAMPA_CONFIG_COLD_RESTART == SAMPA_ENABLED
00053     array_of_scalar<Element>* m_elements;
00054 #else
00055     Element* m_elements;
00056 #endif
00057     SAMPA_PERSISTENT(Array);
00058   };  
00059 
00060   class SAMPA_PERSISTENT_CLASS(PtrQueueBase) {
00061   public:
00062     bool   is_empty() const { return m_size == 0; }
00063     size_t get_size() const { return m_size; }
00064     void   clear()          { m_size = 0;    }
00065     int increase_capacity(int begin, int old_size, int n);
00066   protected:
00067 #if SAMPA_CONFIG_COLD_RESTART == SAMPA_ENABLED
00068     typedef object*  Element;
00069     typedef Element* Iterator;
00070     Iterator begin()        { return m_elements->body(); }
00071     Iterator end()          { return m_elements->body()+m_size; }
00072     Element operator[](size_t idx) const           
00073       { return m_elements->body()[idx]; }
00074     void set(int idx, const Element& e) { m_elements->body()[idx] = e; }
00075     void push_back(const Element& e) 
00076     { size_t capacity = m_elements->get_size(); 
00077       if (m_size == capacity)
00078         m_elements = reallocate(capacity);
00079       m_elements->body()[m_size++] = e;
00080     }
00081     ArrayOfObject* reallocate(size_t capacity);
00082     void push_back_no_check(const Element& e) 
00083     {
00084       m_elements->body()[m_size++] = e;
00085     }
00086 #else
00087     typedef void*    Element;
00088     typedef Element* Iterator;
00089     Iterator begin()        { return m_elements; }
00090     Iterator end()          { return m_elements+m_size; }
00091     Element operator[](size_t idx) const           
00092       { return m_elements[idx]; } 
00093     void set(int idx, const Element& e) { m_elements[idx] = e; }
00094     void push_back(const Element& e) 
00095     {
00096       if (m_size == m_capacity) {
00097         m_capacity <<= 1;
00098         m_elements = (void**) realloc(m_elements, sizeof(Element)*m_capacity);
00099       }
00100       m_elements[m_size++] = e;
00101     }
00102     void push_back_no_check(const Element& e) 
00103     {
00104       m_elements[m_size++] = e;
00105     }
00106 #endif
00107     PtrQueueBase(size_t capacity);
00108     PtrQueueBase(DefaultCtrTag);
00109     ~PtrQueueBase();
00110     void remove(const Element& r) {
00111       for(Iterator i = begin(), e = end(); i < e; i++) {
00112         if (*i == r) {
00113           *i = *(e-1); 
00114           m_size--;
00115           return;
00116         }
00117       }
00118     }
00119   private:  
00120     // Disabled
00121     PtrQueueBase(const PtrQueueBase&);
00122     PtrQueueBase& operator=(const PtrQueueBase&);
00123 #if SAMPA_CONFIG_COLD_RESTART == SAMPA_ENABLED
00124     ArrayOfObject* m_elements;
00125     size_t         m_size;
00126 #else
00127     Element*       m_elements;
00128     size_t         m_size;
00129     size_t         m_capacity;
00130 #endif
00131     SAMPA_PERSISTENT(PtrQueueBase);
00132   };
00133 
00134   template<class T>
00135   class PtrQueue : public PtrQueueBase {
00136   public:
00137     typedef T*       Element;
00138     typedef Element* Iterator;
00139     Element operator[](size_t idx) const           
00140       { return static_cast<Element>(PtrQueueBase::operator[](idx)); }
00141     void set(int idx, const Element& e) { PtrQueueBase::set(idx, e); }
00142     Iterator begin() { return reinterpret_cast<Iterator>(PtrQueueBase::begin()); }
00143     Iterator end()   { return reinterpret_cast<Iterator>(PtrQueueBase::end()); }
00144     void push_back(const Element& e) { PtrQueueBase::push_back(e); }
00145     void push_back_no_check(const Element& e) { PtrQueueBase::push_back_no_check(e); }
00146     void remove(const Element& e) { PtrQueueBase::remove(e); }
00147     PtrQueue(size_t capacity) : PtrQueueBase(capacity) { }
00148   private:  
00149     PtrQueue(const PtrQueue&);
00150     PtrQueue& operator=(const PtrQueue&);
00151     SAMPA_PERSISTENT(PtrQueue);
00152   };
00153 
00154 #if SAMPA_CONFIG_COLD_RESTART == SAMPA_ENABLED
00155   template<>
00156   class SAMPA_PERSISTENT_CLASS(PtrQueue<Triggerable>) {
00157   public:
00158     typedef Triggerable* Element;
00159     typedef Element*     Iterator;
00160     bool   is_empty() const { return m_size == 0; }
00161     size_t get_size() const { return m_size; }
00162     void   clear()          { m_size = 0;    }
00163     Iterator begin()        { return m_elements->body(); }
00164     Iterator end()          { return m_elements->body()+m_size; }
00165     Element operator[](size_t idx) const           
00166       { return m_elements->body()[idx]; }
00167     void push_back(const Element& e) 
00168     { size_t capacity = m_elements->get_size(); 
00169       if (m_size == capacity)
00170         m_elements = reallocate(capacity);
00171       m_elements->body()[m_size++] = e;
00172     }
00173     void push_back_no_check(const Element& e) 
00174     {
00175       m_elements->body()[m_size++] = e;
00176     }
00177 
00178     array_of_ptr<Triggerable>* reallocate(size_t);
00179 
00180     PtrQueue(size_t capacity);
00181     PtrQueue(DefaultCtrTag);
00182     ~PtrQueue();
00183 
00184     void remove(const Element& r) {
00185       for(Iterator i = begin(), e = end(); i < e; i++) {
00186         if (*i == r) {
00187           *i = *(e-1); 
00188           m_size--;
00189           return;
00190         }
00191       }
00192     }
00193   private:  
00194     PtrQueue(const PtrQueue&);
00195     PtrQueue& operator=(const PtrQueue&);
00196     array_of_ptr<Triggerable>* m_elements;
00197     size_t                     m_size;
00198     SAMPA_PERSISTENT(PtrQueue);
00199   };
00200 
00201 #endif
00202   
00203 }
00204 
00205 #endif
00206 

Generated on Sat Feb 16 16:23:15 2008 for Sampa by  doxygen 1.5.3