replicode
vector.h
Go to the documentation of this file.
1 // vector.h
2 //
3 // Author: Eric Nivel
4 //
5 // BSD license:
6 // Copyright (c) 2010, Eric Nivel
7 // All rights reserved.
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions are met:
10 //
11 // - Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // - Redistributions in binary form must reproduce the above copyright
14 // notice, this list of conditions and the following disclaimer in the
15 // documentation and/or other materials provided with the distribution.
16 // - Neither the name of Eric Nivel nor the
17 // names of their contributors may be used to endorse or promote products
18 // derived from this software without specific prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
21 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 // DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
24 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 #ifndef r_code_vector_h
32 #define r_code_vector_h
33 
34 #include <vector>
35 #include <cstddef>
36 
37 namespace r_code {
38 
39 // auto-resized vector
40 template<typename T> class vector {
41 public:
42  vector() : m_vector(new std::vector<T>) {}
43  ~vector() { delete m_vector; }
44  size_t size() const {
45  return m_vector->size();
46  }
47  T &operator [](size_t i) {
48  if (i >= size())
49  m_vector->resize(i + 1);
50  return m_vector->at(i);
51  }
52  T &operator [](size_t i) const {
53  return m_vector->at(i);
54  }
55  void push_back(T t) {
56  m_vector->push_back(t);
57  }
58  std::vector<T> *as_std() const {
59  return m_vector;
60  }
61 private:
62  std::vector<T> *m_vector;
63 };
64 
65 }
66 
67 
68 #endif
std::vector< T > * m_vector
Definition: vector.h:62
Definition: atom.cpp:36
std::vector< T > * as_std() const
Definition: vector.h:58
size_t size() const
Definition: vector.h:44
Definition: vector.h:40
~vector()
Definition: vector.h:43
T & operator[](size_t i)
Definition: vector.h:47
vector()
Definition: vector.h:42
void push_back(T t)
Definition: vector.h:55