• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

DynArray.h

Go to the documentation of this file.
00001 /** @file
00002 
00003   Dynamic Array Implementation used by Regex.cc
00004 
00005   @section license License
00006 
00007   Licensed to the Apache Software Foundation (ASF) under one
00008   or more contributor license agreements.  See the NOTICE file
00009   distributed with this work for additional information
00010   regarding copyright ownership.  The ASF licenses this file
00011   to you under the Apache License, Version 2.0 (the
00012   "License"); you may not use this file except in compliance
00013   with the License.  You may obtain a copy of the License at
00014 
00015       http://www.apache.org/licenses/LICENSE-2.0
00016 
00017   Unless required by applicable law or agreed to in writing, software
00018   distributed under the License is distributed on an "AS IS" BASIS,
00019   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00020   See the License for the specific language governing permissions and
00021   limitations under the License.
00022  */
00023 
00024 #ifndef __DYN_ARRAY_H__
00025 #define __DYN_ARRAY_H__
00026 
00027 template<class T> class DynArray {
00028 public:
00029   DynArray(const T * val = 0, intptr_t initial_size = 0);
00030   ~DynArray();
00031 
00032 #ifndef __GNUC__
00033   operator  const T *() const;
00034 #endif
00035   operator  T *();
00036   T & operator[](intptr_t idx);
00037   T & operator()(intptr_t idx);
00038   T *detach();
00039   T defvalue() const;
00040   intptr_t length();
00041   void clear();
00042   void set_length(intptr_t i);
00043 
00044 private:
00045   void resize(intptr_t new_size);
00046 
00047 private:
00048   T * data;
00049   const T *default_val;
00050   int size;
00051   int pos;
00052 };
00053 
00054 
00055 template<class T> inline DynArray<T>::DynArray(const T * val, intptr_t initial_size)
00056   :
00057 data(NULL),
00058 default_val(val),
00059 size(0),
00060 pos(-1)
00061 {
00062   if (initial_size > 0) {
00063     int i = 1;
00064 
00065     while (i < initial_size)
00066       i <<= 1;
00067 
00068     resize(i);
00069   }
00070 
00071 }
00072 
00073 template<class T> inline DynArray<T>::~DynArray()
00074 {
00075   if (data) {
00076     delete[]data;
00077   }
00078 }
00079 
00080 #ifndef __GNUC__
00081 template<class T> inline DynArray<T>::operator  const T *()
00082 const
00083 {
00084   return
00085     data;
00086 }
00087 #endif
00088 
00089 template <
00090   class
00091   T >
00092   inline
00093   DynArray <
00094   T >::operator
00095 T * ()
00096 {
00097   return data;
00098 }
00099 
00100 template<class T> inline T & DynArray<T>::operator [](intptr_t idx) {
00101   return data[idx];
00102 }
00103 
00104 template<class T> inline T & DynArray<T>::operator ()(intptr_t idx) {
00105   if (idx >= size) {
00106     intptr_t new_size;
00107 
00108     if (size == 0) {
00109       new_size = 64;
00110     } else {
00111       new_size = size * 2;
00112     }
00113 
00114     if (idx >= new_size) {
00115       new_size = idx + 1;
00116     }
00117 
00118     resize(new_size);
00119   }
00120 
00121   if (idx > pos) {
00122     pos = idx;
00123   }
00124 
00125   return data[idx];
00126 }
00127 
00128 template<class T> inline T * DynArray<T>::detach()
00129 {
00130   T *d;
00131 
00132   d = data;
00133   data = NULL;
00134 
00135   return d;
00136 }
00137 
00138 template<class T> inline T DynArray<T>::defvalue() const
00139 {
00140   return *default_val;
00141 }
00142 
00143 template<class T> inline intptr_t DynArray<T>::length()
00144 {
00145   return pos + 1;
00146 }
00147 
00148 template<class T> inline void DynArray<T>::clear()
00149 {
00150   if (data) {
00151     delete[]data;
00152     data = NULL;
00153   }
00154 
00155   size = 0;
00156   pos = -1;
00157 }
00158 
00159 template<class T> inline void DynArray<T>::set_length(intptr_t i)
00160 {
00161   pos = i - 1;
00162 }
00163 
00164 template<class T> inline void DynArray<T>::resize(intptr_t new_size)
00165 {
00166   if (new_size > size) {
00167     T *new_data;
00168     intptr_t i;
00169 
00170     new_data = new T[new_size];
00171 
00172     for (i = 0; i < size; i++) {
00173       new_data[i] = data[i];
00174     }
00175 
00176     for (; i < new_size; i++) {
00177       if (default_val)
00178         new_data[i] = (T) * default_val;
00179     }
00180 
00181     if (data) {
00182       delete[]data;
00183     }
00184     data = new_data;
00185     size = new_size;
00186   }
00187 }
00188 
00189 
00190 #endif /* __DYN_ARRAY_H__ */
00191 

Generated by  doxygen 1.7.1