Stxxl  1.3.2
simple_vector.h
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  *
9  * Distributed under the Boost Software License, Version 1.0.
10  * (See accompanying file LICENSE_1_0.txt or copy at
11  * http://www.boost.org/LICENSE_1_0.txt)
12  **************************************************************************/
13 
14 #ifndef STXXL_SIMPLE_VECTOR_HEADER
15 #define STXXL_SIMPLE_VECTOR_HEADER
16 
17 #include <algorithm>
18 #include <stxxl/bits/noncopyable.h>
19 #include <stxxl/bits/common/types.h>
20 
21 
22 __STXXL_BEGIN_NAMESPACE
23 
24 template <class _Tp /*, class _Alloc=__STL_DEFAULT_ALLOCATOR(_Tp) */>
25 class simple_vector : private noncopyable
26 {
27 public:
28  typedef unsigned_type size_type;
29  typedef _Tp value_type;
30 // typedef simple_alloc<_Tp, _Alloc> _data_allocator;
31 
32 protected:
33  size_type _size;
34  value_type * _array;
35 
36 public:
37  typedef value_type * iterator;
38  typedef const value_type * const_iterator;
39  typedef value_type & reference;
40  typedef const value_type & const_reference;
41 
42  simple_vector(size_type sz) : _size(sz), _array(NULL)
43  {
44  // _array = _data_allocator.allocate(sz);
45  if (size() > 0)
46  _array = new _Tp[size()];
47  }
48  void swap(simple_vector & obj)
49  {
50  std::swap(_size, obj._size);
51  std::swap(_array, obj._array);
52  }
53  ~simple_vector()
54  {
55  // _data_allocator.deallocate(_array,_size);
56  delete[] _array;
57  }
58  iterator begin()
59  {
60  return _array;
61  }
62  const_iterator begin() const
63  {
64  return _array;
65  }
66  const_iterator cbegin() const
67  {
68  return begin();
69  }
70  iterator end()
71  {
72  return _array + _size;
73  }
74  const_iterator end() const
75  {
76  return _array + _size;
77  }
78  const_iterator cend() const
79  {
80  return end();
81  }
82  size_type size() const
83  {
84  return _size;
85  }
86  reference operator [] (size_type i)
87  {
88  return *(begin() + i);
89  }
90  const_reference operator [] (size_type i) const
91  {
92  return *(begin() + i);
93  }
94 };
95 __STXXL_END_NAMESPACE
96 
97 namespace std
98 {
99  template <class Tp_>
100  void swap(stxxl::simple_vector<Tp_> & a,
101  stxxl::simple_vector<Tp_> & b)
102  {
103  a.swap(b);
104  }
105 }
106 
107 #endif // !STXXL_SIMPLE_VECTOR_HEADER