STXXL  1.4.0
 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 
25 
27 
28 /*!
29  * Simpler non-growing vector without initialization.
30  *
31  * simple_vector can be used a replacement for std::vector when only a
32  * non-growing array is needed. The advantages of simple_vector are that it
33  * does not initilize memory for POD types (faster), allows simpler inlines and
34  * is less error prone to copying and other problems..
35  */
36 template <typename ValueType>
37 class simple_vector : private noncopyable
38 {
39 public:
40  typedef ValueType value_type;
41  typedef size_t size_type;
42 
43 protected:
44  //! size of allocated memory
46 
47  //! pointer to allocated memory area
49 
50 public:
51  // *** simple pointer iterators
52 
53  typedef value_type* iterator;
54  typedef const value_type* const_iterator;
56  typedef const value_type& const_reference;
57 
58 public:
59  //! allocate empty simple vector
61  : m_size(0), m_array(NULL)
62  { }
63  //! allocate vector's memory
65  : m_size(sz), m_array(NULL)
66  {
67  if (m_size > 0)
68  m_array = new value_type[m_size];
69  }
70  //! swap vector with another one
71  void swap(simple_vector& obj)
72  {
73  std::swap(m_size, obj.m_size);
74  std::swap(m_array, obj.m_array);
75  }
76  //! delete vector
78  {
79  delete[] m_array;
80  }
81  //! return iterator to beginning of vector
83  {
84  return m_array;
85  }
86  //! return iterator to beginning of vector
88  {
89  return m_array;
90  }
91  //! return mutable iterator to first element
93  {
94  return m_array;
95  }
96  //! return constant iterator to first element
98  {
99  return m_array;
100  }
101  //! return constant iterator to first element
103  {
104  return begin();
105  }
106  //! return mutable iterator beyond last element
108  {
109  return m_array + m_size;
110  }
111  //! return constant iterator beyond last element
113  {
114  return m_array + m_size;
115  }
116  //! return constant iterator beyond last element
118  {
119  return end();
120  }
121  //! return number of items in vector
122  size_type size() const
123  {
124  return m_size;
125  }
126  //! return the i-th position of the vector
127  reference operator [] (size_type i)
128  {
129  assert(i < m_size);
130  return *(begin() + i);
131  }
132  //! return constant reference to the i-th position of the vector
133  const_reference operator [] (size_type i) const
134  {
135  assert(i < m_size);
136  return *(begin() + i);
137  }
138  //! resize the array to contain exactly newsize items
139  void resize(size_type newsize)
140  {
141  if (m_array)
142  {
143  STXXL_MSG("Warning: resizing non-empty simple_vector");
144  value_type* tmp = m_array;
145  m_array = new value_type[newsize];
146  memcpy((void*)m_array, (void*)tmp,
147  sizeof(value_type) * STXXL_MIN(m_size, newsize));
148  delete[] tmp;
149  m_size = newsize;
150  }
151  else
152  {
153  m_array = new value_type[newsize];
154  m_size = newsize;
155  }
156  }
157  //! Zero the whole array content.
158  void memzero()
159  {
160  memset(m_array, 0, m_size * sizeof(value_type));
161  }
162 };
164 
165 namespace std {
166 
167 template <class ValueType>
170 {
171  a.swap(b);
172 }
173 
174 } // namespace std
175 
176 #endif // !STXXL_COMMON_SIMPLE_VECTOR_HEADER
const_iterator end() const
return constant iterator beyond last element
const Tp & STXXL_MIN(const Tp &a, const Tp &b)
Definition: utils.h:147
~simple_vector()
delete vector
Definition: simple_vector.h:77
simple_vector()
allocate empty simple vector
Definition: simple_vector.h:60
iterator data()
return iterator to beginning of vector
Definition: simple_vector.h:82
const value_type * const_iterator
Definition: simple_vector.h:54
const_iterator begin() const
return constant iterator to first element
Definition: simple_vector.h:97
High-performance smart pointer used as a wrapping reference counting pointer.
Definition: counting_ptr.h:48
const value_type & const_reference
Definition: simple_vector.h:56
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:92
value_type * m_array
pointer to allocated memory area
Definition: simple_vector.h:48
const_iterator data() const
return iterator to beginning of vector
Definition: simple_vector.h:87
void swap(simple_vector &obj)
swap vector with another one
Definition: simple_vector.h:71
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
simple_vector(size_type sz)
allocate vector&#39;s memory
Definition: simple_vector.h:64
size_type m_size
size of allocated memory
Definition: simple_vector.h:45
value_type & reference
Definition: simple_vector.h:55
Simpler non-growing vector without initialization.
Definition: simple_vector.h:37
const_iterator cbegin() const
return constant iterator to first element
#define STXXL_MSG(x)
Definition: verbose.h:72
value_type * iterator
Definition: simple_vector.h:53
#define STXXL_END_NAMESPACE
Definition: namespace.h:17
void resize(size_type newsize)
resize the array to contain exactly newsize items