vhacdSArray.h
Go to the documentation of this file.
1 /* Copyright (c) 2011 Khaled Mamou (kmamou at gmail dot com)
2  All rights reserved.
3 
4 
5  Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6 
7  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
8 
9  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
10 
11  3. The names of the contributors may not be used to endorse or promote products derived from this software without specific prior written permission.
12 
13  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14  */
15 #pragma once
16 #ifndef VHACD_SARRAY_H
17 #define VHACD_SARRAY_H
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 
22 #define SARRAY_DEFAULT_MIN_SIZE 16
23 
24 namespace VHACD {
26 template <typename T, size_t N = 64>
27 class SArray {
28 public:
29  T& operator[](size_t i)
30  {
31  T* const data = Data();
32  return data[i];
33  }
34  const T& operator[](size_t i) const
35  {
36  const T* const data = Data();
37  return data[i];
38  }
39  size_t Size() const
40  {
41  return m_size;
42  }
43  T* const Data()
44  {
45  return (m_maxSize == N) ? m_data0 : m_data;
46  }
47  const T* const Data() const
48  {
49  return (m_maxSize == N) ? m_data0 : m_data;
50  }
51  void Clear()
52  {
53  m_size = 0;
54  delete[] m_data;
55  m_data = 0;
56  m_maxSize = N;
57  }
58  void PopBack()
59  {
60  --m_size;
61  }
62  void Allocate(size_t size)
63  {
64  if (size > m_maxSize) {
65  T* temp = new T[size];
66  memcpy(temp, Data(), m_size * sizeof(T));
67  delete[] m_data;
68  m_data = temp;
69  m_maxSize = size;
70  }
71  }
72  void Resize(size_t size)
73  {
74  Allocate(size);
75  m_size = size;
76  }
77 
78  void PushBack(const T& value)
79  {
80  if (m_size == m_maxSize) {
81  size_t maxSize = (m_maxSize << 1);
82  T* temp = new T[maxSize];
83  memcpy(temp, Data(), m_maxSize * sizeof(T));
84  delete[] m_data;
85  m_data = temp;
86  m_maxSize = maxSize;
87  }
88  T* const data = Data();
89  data[m_size++] = value;
90  }
91  bool Find(const T& value, size_t& pos)
92  {
93  T* const data = Data();
94  for (pos = 0; pos < m_size; ++pos)
95  if (value == data[pos])
96  return true;
97  return false;
98  }
99  bool Insert(const T& value)
100  {
101  size_t pos;
102  if (Find(value, pos))
103  return false;
104  PushBack(value);
105  return true;
106  }
107  bool Erase(const T& value)
108  {
109  size_t pos;
110  T* const data = Data();
111  if (Find(value, pos)) {
112  for (size_t j = pos + 1; j < m_size; ++j)
113  data[j - 1] = data[j];
114  --m_size;
115  return true;
116  }
117  return false;
118  }
119  void operator=(const SArray& rhs)
120  {
121  if (m_maxSize < rhs.m_size) {
122  delete[] m_data;
123  m_maxSize = rhs.m_maxSize;
124  m_data = new T[m_maxSize];
125  }
126  m_size = rhs.m_size;
127  memcpy(Data(), rhs.Data(), m_size * sizeof(T));
128  }
129  void Initialize()
130  {
131  m_data = 0;
132  m_size = 0;
133  m_maxSize = N;
134  }
135  SArray(const SArray& rhs)
136  {
137  m_data = 0;
138  m_size = 0;
139  m_maxSize = N;
140  *this = rhs;
141  }
143  {
144  Initialize();
145  }
147  {
148  delete[] m_data;
149  }
150 
151 private:
152  T m_data0[N];
153  T* m_data;
154  size_t m_size;
155  size_t m_maxSize;
156 };
157 }
158 #endif
bool Insert(const T &value)
Definition: vhacdSArray.h:99
T *const Data()
Definition: vhacdSArray.h:43
void Initialize()
Definition: vhacdSArray.h:129
SArray(const SArray &rhs)
Definition: vhacdSArray.h:135
void PopBack()
Definition: vhacdSArray.h:58
void PushBack(const T &value)
Definition: vhacdSArray.h:78
SArray.
Definition: vhacdSArray.h:27
bool Erase(const T &value)
Definition: vhacdSArray.h:107
bool Find(const T &value, size_t &pos)
Definition: vhacdSArray.h:91
void Resize(size_t size)
Definition: vhacdSArray.h:72
void Allocate(size_t size)
Definition: vhacdSArray.h:62
const T *const Data() const
Definition: vhacdSArray.h:47
void operator=(const SArray &rhs)
Definition: vhacdSArray.h:119
Definition: vhacdCircularList.h:19
T & operator[](size_t i)
Definition: vhacdSArray.h:29
void Clear()
Definition: vhacdSArray.h:51
size_t Size() const
Definition: vhacdSArray.h:39
~SArray()
Definition: vhacdSArray.h:146
const T & operator[](size_t i) const
Definition: vhacdSArray.h:34
SArray()
Definition: vhacdSArray.h:142