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