]>
Commit | Line | Data |
---|---|---|
9ce192d4 RD |
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 | ||
d134f170 | 9 | // A simple expandable integer vector. |
9ce192d4 RD |
10 | // Storage not allocated for elements until an element is used. |
11 | // This makes it very lightweight unless used so is a good match for optional features. | |
d134f170 | 12 | |
9ce192d4 | 13 | class SVector { |
d134f170 | 14 | int *v; |
9ce192d4 RD |
15 | unsigned int size; // Number of elements allocated |
16 | unsigned int len; // Number of elements in vector | |
17 | bool allocFailure; // A memory allocation call has failed | |
18 | ||
19 | // Internally allocate more elements than the user wants to avoid | |
20 | // thrashng the memory allocator | |
21 | void SizeTo(int newSize) { | |
d134f170 RD |
22 | if (newSize < 4000) |
23 | newSize += 4000; | |
9ce192d4 RD |
24 | else |
25 | newSize = (newSize * 3) / 2; | |
d134f170 | 26 | int* newv = new int[newSize]; |
9ce192d4 RD |
27 | if (!newv) { |
28 | allocFailure = true; | |
29 | return; | |
30 | } | |
31 | size = newSize; | |
d134f170 RD |
32 | unsigned int i=0; |
33 | for (; i<len; i++) { | |
9ce192d4 RD |
34 | newv[i] = v[i]; |
35 | } | |
d134f170 RD |
36 | for (; i<size; i++) { |
37 | newv[i] = 0; | |
38 | } | |
9ce192d4 RD |
39 | delete []v; |
40 | v = newv; | |
41 | } | |
42 | ||
43 | public: | |
44 | SVector() { | |
45 | allocFailure = false; | |
46 | v = 0; | |
47 | len = 0; | |
48 | size = 0; | |
49 | } | |
50 | ~SVector() { | |
51 | Free(); | |
52 | } | |
53 | SVector(const SVector &other) { | |
54 | allocFailure = false; | |
55 | v = 0; | |
56 | len = 0; | |
57 | size = 0; | |
58 | if (other.Length() > 0) { | |
59 | SizeTo(other.Length()); | |
60 | if (!allocFailure) { | |
61 | for (int i=0;i<other.Length();i++) | |
62 | v[i] = other.v[i]; | |
63 | len = other.Length(); | |
64 | } | |
65 | } | |
66 | } | |
67 | SVector &operator=(const SVector &other) { | |
68 | if (this != &other) { | |
69 | delete []v; | |
70 | allocFailure = false; | |
71 | v = 0; | |
72 | len = 0; | |
73 | size = 0; | |
74 | if (other.Length() > 0) { | |
75 | SizeTo(other.Length()); | |
76 | if (!allocFailure) { | |
77 | for (int i=0;i<other.Length();i++) | |
78 | v[i] = other.v[i]; | |
79 | } | |
80 | len = other.Length(); | |
81 | } | |
82 | } | |
83 | return *this; | |
84 | } | |
d134f170 | 85 | int &operator[](unsigned int i) { |
9ce192d4 RD |
86 | if (i >= len) { |
87 | if (i >= size) { | |
88 | SizeTo(i); | |
89 | } | |
90 | len = i+1; | |
91 | } | |
92 | return v[i]; | |
93 | } | |
94 | void Free() { | |
95 | delete []v; | |
96 | v = 0; | |
97 | size = 0; | |
98 | len = 0; | |
99 | } | |
f6bcfd97 BP |
100 | void SetLength(unsigned int newLength) { |
101 | if (newLength > len) { | |
102 | if (newLength >= size) { | |
103 | SizeTo(newLength); | |
9ce192d4 RD |
104 | } |
105 | } | |
f6bcfd97 | 106 | len = newLength; |
9ce192d4 RD |
107 | } |
108 | int Length() const { | |
109 | return len; | |
110 | } | |
111 | }; | |
112 | ||
113 | #endif |