CUBRID Engine  latest
extensible_array.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2008 Search Solution Corporation
3  * Copyright 2016 CUBRID Corporation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 /* extensible_array.hpp -
20  *
21  * This header file defines the extensible_array class template.
22  */
23 
24 #ifndef EXTENSIBLE_ARRAY_HPP_
25 #define EXTENSIBLE_ARRAY_HPP_
26 
27 #include "mem_block.hpp"
28 
29 #include <memory>
30 
31 namespace cubmem
32 {
33  template <size_t Size>
35  {
36  private:
38 
39  public:
40 
42  appendible_block (const block_allocator &alloc);
43 
44  inline void append (const char *source, size_t length); // append at the end of existing data
45  inline void copy (const char *source, size_t length); // overwrite entire array
46 
47  template <typename T>
48  inline void append (const T &obj);
49 
50  inline std::size_t get_size () const;
51 
52  private:
53  inline void reset ();
54 
55  size_t m_size; // current size
56  };
57 
58  template <typename T, size_t Size>
59  class extensible_array : protected extensible_stack_block<sizeof (T) * Size>
60  {
61  private:
63 
64  public:
65  void extend_by (size_t count);
66  void extend_to (size_t count);
67 
68  const T *get_array (void) const;
69 
70  protected:
71  T *get_data_ptr ();
72 
73  size_t get_memsize_for_count (size_t count) const;
74  };
75 
76  template <typename T, size_t S>
77  class appendable_array : public extensible_array<T, S>
78  {
79  private:
81 
82  public:
83  appendable_array (void);
84  appendable_array (const block_allocator &allocator);
85  ~appendable_array ();
86 
87  inline void append (const T &source);
88  inline void append (const T *source, size_t length); // append at the end of existing data
89  inline void copy (const T *source, size_t length); // overwrite entire array
90  void erase (size_t index); // remove at index; does not preserve order
91 
92  inline size_t get_size (void) const; // get current size
93 
94  size_t get_memsize () const;
95 
96  private:
97  inline void reset (void); // reset array
98  inline T *get_append_ptr ();
99 
100  size_t m_size; // current size
101  };
102 } // namespace cubmem
103 
104 /************************************************************************/
105 /* Implementation */
106 /************************************************************************/
107 
108 #include <cassert>
109 #include <cstring>
110 
111 namespace cubmem
112 {
113  //
114  // appendible_block
115  //
116  template <size_t Size>
118  : extensible_stack_block<Size> ()
119  , m_size (0)
120  {
121  }
122 
123  template <size_t Size>
125  : extensible_stack_block<Size> (alloc)
126  , m_size (0)
127  {
128  }
129 
130  template <size_t Size>
131  std::size_t
133  {
134  return m_size;
135  }
136 
137  template <size_t Size>
138  void
140  {
141  m_size = 0;
142  }
143 
144  template <size_t Size>
145  void
146  appendible_block<Size>::append (const char *source, size_t length)
147  {
148  base_type::extend_to (m_size + length);
149  std::memcpy (base_type::get_ptr () + m_size, source, length);
150  m_size += length;
151  }
152 
153  template <size_t Size>
154  template <typename T>
155  void
157  {
158  append (reinterpret_cast<const char *> (&obj), sizeof (obj));
159  }
160 
161  template <size_t Size>
162  void
163  appendible_block<Size>::copy (const char *source, size_t length)
164  {
165  reset ();
166  append (source, length);
167  }
168 
169  //
170  // extensible_array
171  //
172  template <typename T, size_t Size>
173  void
175  {
176  base_type::extend_by (get_memsize_for_count (count));
177  }
178 
179  template <typename T, size_t Size>
180  void
182  {
183  base_type::extend_to (get_memsize_for_count (count));
184  }
185 
186  template <typename T, size_t Size>
187  const T *
189  {
190  return reinterpret_cast<const T *> (base_type::get_read_ptr ());
191  }
192 
193  template <typename T, size_t Size>
194  T *
196  {
197  return reinterpret_cast<T *> (base_type::get_ptr ());
198  }
199 
200  template <typename T, size_t Size>
201  size_t
203  {
204  return count * sizeof (T);
205  }
206 
207  //
208  // appendable_array
209  //
210  template <typename T, size_t Size>
212  : base_type ()
213  , m_size (0)
214  {
215  //
216  }
217 
218  template <typename T, size_t Size>
220  : base_type (allocator)
221  , m_size (0)
222  {
223  // empty
224  }
225 
226  template <typename T, size_t Size>
228  {
229  // empty
230  }
231 
232  template <typename T, size_t Size>
233  T *
235  {
236  return base_type::get_data_ptr () + m_size;
237  }
238 
239  template <typename T, size_t Size>
240  void
242  {
243  append (&source, 1);
244  }
245 
246  template <typename T, size_t Size>
247  void
248  appendable_array<T, Size>::append (const T *source, size_t length)
249  {
250  // make sure memory is enough
251  base_type::extend_to (m_size + length);
252 
253  // copy data at the end of the array
254  std::memcpy (base_type::get_data_ptr () + m_size, source, length * sizeof (T));
255  m_size += length;
256  }
257 
258  template <typename T, size_t Size>
259  void
260  appendable_array<T, Size>::copy (const T *source, size_t length)
261  {
262  // copy = reset + append
263  reset ();
264  append (source, length);
265  }
266 
267  template <typename T, size_t Size>
268  void
270  {
271  if (index >= m_size)
272  {
273  // no op
274  return;
275  }
276  if (index < m_size - 1)
277  {
278  // overwrite with last
280  }
281  --m_size;
282  }
283 
284  template <typename T, size_t Size>
285  size_t
287  {
288  return m_size;
289  }
290 
291  template<typename T, size_t S>
292  size_t
294  {
296  }
297 
298  template <typename T, size_t Size>
299  void
301  {
302  // set size to zero
303  m_size = 0;
304  }
305 
306 } // namespace cubmem
307 
308 #endif /* !EXTENSIBLE_ARRAY_HPP_ */
void extend_to(size_t count)
void extend_to(size_t total_bytes)
size_t get_size(void) const
size_t get_memsize_for_count(size_t count) const
std::size_t get_size() const
void copy(const char *source, size_t length)
void append(const char *source, size_t length)
int count(int &result, const cub_regex_object &reg, const std::string &src, const int position, const INTL_CODESET codeset)
void extend_by(size_t count)
void extend_by(size_t additional_bytes)
void append(const T &source)
void copy(const T *source, size_t length)
const T * get_array(void) const