]> git.saurik.com Git - wxWidgets.git/blob - src/stc/scintilla/src/SVector.h
49fc376ddacbe0ddc8b8bed3e6c6c6276a35985d
[wxWidgets.git] / src / stc / scintilla / src / SVector.h
1 // Scintilla source code edit control
2 // SVector.h - a simple expandable vector
3 // Copyright 1998-1999 by Neil Hodgson <neilh@hare.net.au>
4 // The License.txt file describes the conditions under which this software may be distributed.
5
6 #ifndef SVECTOR_H
7 #define SVECTOR_H
8
9 // A simple expandable vector.
10 // T must support assignment.
11 // Storage not allocated for elements until an element is used.
12 // This makes it very lightweight unless used so is a good match for optional features.
13 template<class T, int sizeIncrement>
14 class SVector {
15 T *v;
16 unsigned int size; // Number of elements allocated
17 unsigned int len; // Number of elements in vector
18 bool allocFailure; // A memory allocation call has failed
19
20 // Internally allocate more elements than the user wants to avoid
21 // thrashng the memory allocator
22 void SizeTo(int newSize) {
23 if (newSize < sizeIncrement)
24 newSize += sizeIncrement;
25 else
26 newSize = (newSize * 3) / 2;
27 T* newv = new T[newSize];
28 if (!newv) {
29 allocFailure = true;
30 return;
31 }
32 size = newSize;
33 for (int i=0; i<len; i++) {
34 newv[i] = v[i];
35 }
36 delete []v;
37 v = newv;
38 }
39
40 public:
41 SVector() {
42 allocFailure = false;
43 v = 0;
44 len = 0;
45 size = 0;
46 }
47 ~SVector() {
48 Free();
49 }
50 SVector(const SVector &other) {
51 allocFailure = false;
52 v = 0;
53 len = 0;
54 size = 0;
55 if (other.Length() > 0) {
56 SizeTo(other.Length());
57 if (!allocFailure) {
58 for (int i=0;i<other.Length();i++)
59 v[i] = other.v[i];
60 len = other.Length();
61 }
62 }
63 }
64 SVector &operator=(const SVector &other) {
65 if (this != &other) {
66 delete []v;
67 allocFailure = false;
68 v = 0;
69 len = 0;
70 size = 0;
71 if (other.Length() > 0) {
72 SizeTo(other.Length());
73 if (!allocFailure) {
74 for (int i=0;i<other.Length();i++)
75 v[i] = other.v[i];
76 }
77 len = other.Length();
78 }
79 }
80 return *this;
81 }
82 T &operator[](unsigned int i) {
83 if (i >= len) {
84 if (i >= size) {
85 SizeTo(i);
86 }
87 len = i+1;
88 }
89 return v[i];
90 }
91 void Free() {
92 delete []v;
93 v = 0;
94 size = 0;
95 len = 0;
96 }
97 void SetLength(int newLen) {
98 if (newLen > len) {
99 if (newLen >= size) {
100 SizeTo(newLen);
101 }
102 }
103 len = newLen;
104 }
105 int Length() const {
106 return len;
107 }
108 };
109
110 #endif