updated Scintilla to 2.29
[TortoiseGit.git] / ext / scintilla / src / SVector.h
blob7d1a8a8b4f02cef3e63670e736d67ef4c1411ebb
1 // Scintilla source code edit control
2 /** @file SVector.h
3 ** A simple expandable vector.
4 **/
5 // Copyright 1998-2001 by Neil Hodgson <neilh@hare.net.au>
6 // The License.txt file describes the conditions under which this software may be distributed.
8 #ifndef SVECTOR_H
9 #define SVECTOR_H
11 #ifdef SCI_NAMESPACE
12 namespace Scintilla {
13 #endif
15 /**
16 * A simple expandable integer vector.
17 * Storage not allocated for elements until an element is used.
18 * This makes it very lightweight unless used so is a good match for optional features.
20 class SVector {
21 enum { allocSize = 4000 };
23 int *v; ///< The vector
24 unsigned int size; ///< Number of elements allocated
25 unsigned int len; ///< Number of elements used in vector
27 /** Internally allocate more elements than the user wants
28 * to avoid thrashing the memory allocator. */
29 void SizeTo(int newSize) {
30 if (newSize < allocSize)
31 newSize += allocSize;
32 else
33 newSize = (newSize * 3) / 2;
34 int *newv = new int[newSize];
35 size = newSize;
36 unsigned int i=0;
37 for (; i<len; i++) {
38 newv[i] = v[i];
40 for (; i<size; i++) {
41 newv[i] = 0;
43 delete []v;
44 v = newv;
47 public:
48 SVector() {
49 v = 0;
50 len = 0;
51 size = 0;
53 ~SVector() {
54 Free();
56 /// Constructor from another vector.
57 SVector(const SVector &other) {
58 v = 0;
59 len = 0;
60 size = 0;
61 if (other.Length() > 0) {
62 SizeTo(other.Length());
63 for (int i=0; i<other.Length(); i++)
64 v[i] = other.v[i];
65 len = other.Length();
68 /// Copy constructor.
69 SVector &operator=(const SVector &other) {
70 if (this != &other) {
71 delete []v;
72 v = 0;
73 len = 0;
74 size = 0;
75 if (other.Length() > 0) {
76 SizeTo(other.Length());
77 for (int i=0; i<other.Length(); i++)
78 v[i] = other.v[i];
79 len = other.Length();
82 return *this;
84 /** @brief Accessor.
85 * Allows to access values from the list, and grows it if accessing
86 * outside the current bounds. The returned value in this case is 0. */
87 int &operator[](unsigned int i) {
88 if (i >= len) {
89 if (i >= size) {
90 SizeTo(i);
92 len = i+1;
94 return v[i];
96 /// Reset vector.
97 void Free() {
98 delete []v;
99 v = 0;
100 size = 0;
101 len = 0;
103 /** @brief Grow vector size.
104 * Doesn't allow a vector to be shrinked. */
105 void SetLength(unsigned int newLength) {
106 if (newLength > len) {
107 if (newLength >= size) {
108 SizeTo(newLength);
111 len = newLength;
113 /// Get the current length (number of used elements) of the vector.
114 int Length() const {
115 return len;
119 #ifdef SCI_NAMESPACE
121 #endif
123 #endif