]> git.saurik.com Git - wxWidgets.git/blob - src/stc/scintilla/src/SVector.h
12a7d5d401fb8ef6a5c7058e5d0836102bc20c36
[wxWidgets.git] / src / stc / scintilla / src / SVector.h
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.
7
8 #ifndef SVECTOR_H
9 #define SVECTOR_H
10
11 #ifdef SCI_NAMESPACE
12 namespace Scintilla {
13 #endif
14
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.
19 */
20 class SVector {
21 enum { allocSize = 4000 };
22
23 int *v; ///< The vector
24 unsigned int size; ///< Number of elements allocated
25 unsigned int len; ///< Number of elements used in vector
26
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];
39 }
40 for (; i<size; i++) {
41 newv[i] = 0;
42 }
43 delete []v;
44 v = newv;
45 }
46
47 public:
48 SVector() {
49 v = 0;
50 len = 0;
51 size = 0;
52 }
53 ~SVector() {
54 Free();
55 }
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();
66 }
67 }
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();
80 }
81 }
82 return *this;
83 }
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);
91 }
92 len = i+1;
93 }
94 return v[i];
95 }
96 /// Reset vector.
97 void Free() {
98 delete []v;
99 v = 0;
100 size = 0;
101 len = 0;
102 }
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);
109 }
110 }
111 len = newLength;
112 }
113 /// Get the current length (number of used elements) of the vector.
114 int Length() const {
115 return len;
116 }
117 };
118
119 #ifdef SCI_NAMESPACE
120 }
121 #endif
122
123 #endif