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 <dementiev@mpi-sb.mpg.de>
7 * Copyright (C) 2008, 2011 Andreas Beckmann <beckmann@cs.uni-frankfurt.de>
8 * Copyright (C) 2013 Timo Bingmann <tb@panthema.net>
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>
23 #include <stxxl/bits/common/utils.h>
24
25 STXXL_BEGIN_NAMESPACE
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
47 size_type m_size;
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
simple_vector()62 simple_vector()
63 : m_size(0), m_array(NULL)
64 { }
65 //! allocate vector's memory
simple_vector(size_type sz)66 simple_vector(size_type sz)
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
swap(simple_vector & obj)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
~simple_vector()79 ~simple_vector()
80 {
81 delete[] m_array;
82 }
83 //! return iterator to beginning of vector
data()84 iterator data()
85 {
86 return m_array;
87 }
88 //! return iterator to beginning of vector
data()89 const_iterator data() const
90 {
91 return m_array;
92 }
93 //! return mutable iterator to first element
begin()94 iterator begin()
95 {
96 return m_array;
97 }
98 //! return constant iterator to first element
begin()99 const_iterator begin() const
100 {
101 return m_array;
102 }
103 //! return constant iterator to first element
cbegin()104 const_iterator cbegin() const
105 {
106 return begin();
107 }
108 //! return mutable iterator beyond last element
end()109 iterator end()
110 {
111 return m_array + m_size;
112 }
113 //! return constant iterator beyond last element
end()114 const_iterator end() const
115 {
116 return m_array + m_size;
117 }
118 //! return constant iterator beyond last element
cend()119 const_iterator cend() const
120 {
121 return end();
122 }
123 //! return number of items in vector
size()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
resize(size_type newsize)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.
memzero()160 void memzero()
161 {
162 memset(m_array, 0, m_size * sizeof(value_type));
163 }
164 };
165
166 // \}
167
168 STXXL_END_NAMESPACE
169
170 namespace std {
171
172 template <class ValueType>
swap(stxxl::simple_vector<ValueType> & a,stxxl::simple_vector<ValueType> & b)173 void swap(stxxl::simple_vector<ValueType>& a,
174 stxxl::simple_vector<ValueType>& b)
175 {
176 a.swap(b);
177 }
178
179 } // namespace std
180
181 #endif // !STXXL_COMMON_SIMPLE_VECTOR_HEADER
182