sficl  Current version
SfiVectorLite.h
Go to the documentation of this file.
1 // Copyright (c) 2007-2011 Jabiru Ventures LLC
2 // Licensing questions should be addressed to jvlicense@jabiruventures.com
3 //
4 // Permission is hereby granted, free of charge, to any person
5 // obtaining a copy of this software and associated documentation
6 // files (the "Software"), to deal in the Software without
7 // restriction, including without limitation the rights to use,
8 // copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the
10 // Software is furnished to do so, subject to the following
11 // conditions:
12 //
13 // The above copyright notice and this permission notice shall be
14 // included in all copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 // OTHER DEALINGS IN THE SOFTWARE.
24 //-----------------------------------------------------------------------------
25 //
26 // SfiVectorLite.h - A light version of std::vector
27 //
28 //-----------------------------------------------------------------------------
29 #ifndef SfiVectorLite_INCLUDED
30 #define SfiVectorLite_INCLUDED
31 
32 #include <vector>
33 
34 //------------------------------------------------------------------------------
35 //
36 // SfiVectorLite
37 //
38 //------------------------------------------------------------------------------
39 /// A light version of std::vector that allows push_back and clear without unnecessary memory operations.
40 /// The intended purpose is a vector that grows by calling push_back() and then clears by calling clear().
41 /// In the regular vector, the clear operation releases allocated memory. In this implementation,
42 /// only the perceived size of the vector is changed, while any memory remains and will be reused on
43 /// subsequent calls to push_back(). The memory can be released by calling pack().
44 template <typename T>
46 {
47 private:
48  /// The storage for internal data.
49  vector<T> m_data;
50  /// The number of elements in the vector.
51  size_t m_count;
52 
53 public:
54 
55  /// Initializes the vector to the given size.
56  explicit SfiVectorLite(int size = 0) : m_count(size)
57  {
58  m_data.resize(size);
59  }
60 
61  /// Copy constructor.
63  {
64  *this = vec;
65  }
66 
67 
69  {
70  }
71 
72  /// Returns the i-th element of the vector. Can be used as an l-value.
73  T& operator[](int i)
74  {
75  return m_data[i];
76  }
77  /// Returns the i-th element of the vector.
78  const T& operator[](int i) const
79  {
80  return m_data[i];
81  }
82 
83 
84  //-------------------------------------------------
85  // Size management
86  //-------------------------------------------------
87 
88  /// Returns the size of the vector.
89  int size() const
90  {
91  return m_count;
92  }
93  /// Returns the size of the vector.
94  int length() const
95  {
96  return m_count;
97  }
98 
99  /// Returns the vector's storage capacity. If the vector is attached to external storage,
100  /// returns the external storage capacity.
101  int capacity() const
102  {
103  return m_data.capacity();
104  }
105 
106  /// Resizes the underlying vector, but does not change the perceived size.
107  void reserve(size_t size)
108  {
109  if (size > m_data.size())
110  {
111  m_data.resize(size);
112  }
113  }
114  /// Resizes the vector storage and adds/removes the elements appropriately.
115  void resize(size_t size)
116  {
117  m_data.resize(size);
118  m_count = size;
119  }
120 
121 
122  /// Resizes the vector to the current number of elements. Can be used to free unused space
123  /// after removing elements or resizing the vector down.
124  void pack()
125  {
126  m_data.resize(m_count);
127  }
128 
129  /// Sets the number of elements to zero without releasing the memory. Use pack() to also release the memory.
130  void clear()
131  {
132  m_count = 0;
133  }
134 
135  /// Appends t to the end of the vector.
136  void push_back(const T& t)
137  {
138  if (m_count + 1 > m_data.size())
139  {
140  m_data.push_back(t);
141  m_count++;
142  }
143  else
144  {
145  m_data[m_count++] = t;
146  }
147  }
148 
149  /// Copies the contents of vec into the vector.
151  {
152  m_data = vec.m_data;
153  m_count = vec.m_count;
154  return *this;
155  }
156 
157 
158 };
159 
160 #endif
161 
void resize(size_t size)
Resizes the vector storage and adds/removes the elements appropriately.
void reserve(size_t size)
Resizes the underlying vector, but does not change the perceived size.
SfiVectorLite(int size=0)
Initializes the vector to the given size.
Definition: SfiVectorLite.h:56
int length() const
Returns the size of the vector.
Definition: SfiVectorLite.h:94
SfiVectorLite< T > & operator=(const SfiVectorLite< T > &vec)
Copies the contents of vec into the vector.
SfiVectorLite(const SfiVectorLite< T > &vec)
Copy constructor.
Definition: SfiVectorLite.h:62
int capacity() const
Returns the vector&#39;s storage capacity.
void pack()
Resizes the vector to the current number of elements.
const T & operator[](int i) const
Returns the i-th element of the vector.
Definition: SfiVectorLite.h:78
int size() const
Returns the size of the vector.
Definition: SfiVectorLite.h:89
void clear()
Sets the number of elements to zero without releasing the memory. Use pack() to also release the memo...
void push_back(const T &t)
Appends t to the end of the vector.
vector< T > m_data
The storage for internal data.
Definition: SfiVectorLite.h:49
T & operator[](int i)
Returns the i-th element of the vector. Can be used as an l-value.
Definition: SfiVectorLite.h:73
A light version of std::vector that allows push_back and clear without unnecessary memory operations...
Definition: SfiVectorLite.h:45
size_t m_count
The number of elements in the vector.
Definition: SfiVectorLite.h:51