STXXL  1.4.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
simple_vector.h
Go to the documentation of this file.
1 /***************************************************************************
2  * include/stxxl/bits/common/simple_vector.h
3  *
4  * Part of the STXXL. See http://stxxl.sourceforge.net
5  *
6  * Copyright (C) 2002 Roman Dementiev <[email protected]>
7  * Copyright (C) 2008, 2011 Andreas Beckmann <[email protected]>
8  * Copyright (C) 2013 Timo Bingmann <[email protected]>
9  *
10  * Distributed under the Boost Software License, Version 1.0.
11  * (See accompanying file LICENSE_1_0.txt or copy at
12  * http://www.boost.org/LICENSE_1_0.txt)
13  **************************************************************************/
14 
15 #ifndef STXXL_COMMON_SIMPLE_VECTOR_HEADER
16 #define STXXL_COMMON_SIMPLE_VECTOR_HEADER
17 
18 #include <algorithm>
19 #include <cstring>
20 #include <cassert>
21 #include <stxxl/bits/noncopyable.h>
22 #include <stxxl/bits/verbose.h>
24 
26 
27 //! \addtogroup support
28 //! \{
29 
30 /*!
31  * Simpler non-growing vector without initialization.
32  *
33  * simple_vector can be used a replacement for std::vector when only a
34  * non-growing array is needed. The advantages of simple_vector are that it
35  * does not initilize memory for POD types (faster), allows simpler inlines and
36  * is less error prone to copying and other problems..
37  */
38 template <typename ValueType>
39 class simple_vector : private noncopyable
40 {
41 public:
42  typedef ValueType value_type;
43  typedef size_t size_type;
44 
45 protected:
46  //! size of allocated memory
48 
49  //! pointer to allocated memory area
50  value_type* m_array;
51 
52 public:
53  // *** simple pointer iterators
54 
55  typedef value_type* iterator;
56  typedef const value_type* const_iterator;
57  typedef value_type& reference;
58  typedef const value_type& const_reference;
59 
60 public:
61  //! allocate empty simple vector
63  : m_size(0), m_array(NULL)
64  { }
65  //! allocate vector's memory
67  : m_size(sz), m_array(NULL)
68  {
69  if (m_size > 0)
70  m_array = new value_type[m_size];
71  }
72  //! swap vector with another one
73  void swap(simple_vector& obj)
74  {
75  std::swap(m_size, obj.m_size);
76  std::swap(m_array, obj.m_array);
77  }
78  //! delete vector
80  {
81  delete[] m_array;
82  }
83  //! return iterator to beginning of vector
85  {
86  return m_array;
87  }
88  //! return iterator to beginning of vector
90  {
91  return m_array;
92  }
93  //! return mutable iterator to first element
95  {
96  return m_array;
97  }
98  //! return constant iterator to first element
100  {
101  return m_array;
102  }
103  //! return constant iterator to first element
105  {
106  return begin();
107  }
108  //! return mutable iterator beyond last element
110  {
111  return m_array + m_size;
112  }
113  //! return constant iterator beyond last element
115  {
116  return m_array + m_size;
117  }
118  //! return constant iterator beyond last element
120  {
121  return end();
122  }
123  //! return number of items in vector
124  size_type size() const
125  {
126  return m_size;
127  }
128  //! return the i-th position of the vector
129  reference operator [] (size_type i)
130  {
131  assert(i < m_size);
132  return *(begin() + i);
133  }
134  //! return constant reference to the i-th position of the vector
135  const_reference operator [] (size_type i) const
136  {
137  assert(i < m_size);
138  return *(begin() + i);
139  }
140  //! resize the array to contain exactly newsize items
141  void resize(size_type newsize)
142  {
143  if (m_array)
144  {
145  STXXL_MSG("Warning: resizing non-empty simple_vector");
146  value_type* tmp = m_array;
147  m_array = new value_type[newsize];
148  memcpy((void*)m_array, (void*)tmp,
149  sizeof(value_type) * STXXL_MIN(m_size, newsize));
150  delete[] tmp;
151  m_size = newsize;
152  }
153  else
154  {
155  m_array = new value_type[newsize];
156  m_size = newsize;
157  }
158  }
159  //! Zero the whole array content.
160  void memzero()
161  {
162  memset(m_array, 0, m_size * sizeof(value_type));
163  }
164 };
165 
166 // \}
167 
169 
170 namespace std {
171 
172 template <class ValueType>
175 {
176  a.swap(b);
177 }
178 
179 } // namespace std
180 
181 #endif // !STXXL_COMMON_SIMPLE_VECTOR_HEADER
const_iterator end() const
return constant iterator beyond last element
~simple_vector()
delete vector
Definition: simple_vector.h:79
simple_vector()
allocate empty simple vector
Definition: simple_vector.h:62
const Type & STXXL_MIN(const Type &a, const Type &b)
Definition: utils.h:146
iterator data()
return iterator to beginning of vector
Definition: simple_vector.h:84
const value_type * const_iterator
Definition: simple_vector.h:56
const_iterator begin() const
return constant iterator to first element
Definition: simple_vector.h:99
const value_type & const_reference
Definition: simple_vector.h:58
size_type size() const
return number of items in vector
iterator end()
return mutable iterator beyond last element
const_iterator cend() const
return constant iterator beyond last element
void memzero()
Zero the whole array content.
iterator begin()
return mutable iterator to first element
Definition: simple_vector.h:94
value_type * m_array
pointer to allocated memory area
Definition: simple_vector.h:50
const_iterator data() const
return iterator to beginning of vector
Definition: simple_vector.h:89
void swap(simple_vector &obj)
swap vector with another one
Definition: simple_vector.h:73
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
simple_vector(size_type sz)
allocate vector&#39;s memory
Definition: simple_vector.h:66
size_type m_size
size of allocated memory
Definition: simple_vector.h:47
value_type & reference
Definition: simple_vector.h:57
Simpler non-growing vector without initialization.
Definition: simple_vector.h:39
const_iterator cbegin() const
return constant iterator to first element
#define STXXL_MSG(x)
Definition: verbose.h:73
value_type * iterator
Definition: simple_vector.h:55
#define STXXL_END_NAMESPACE
Definition: namespace.h:17
void resize(size_type newsize)
resize the array to contain exactly newsize items