]> git.saurik.com Git - wxWidgets.git/blob - src/stc/scintilla/src/SVector.h
Moved from Scintilla version 1.25 to 1.32
[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 integer vector.
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.
12
13 class SVector {
14 int *v;
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) {
22 if (newSize < 4000)
23 newSize += 4000;
24 else
25 newSize = (newSize * 3) / 2;
26 int* newv = new int[newSize];
27 if (!newv) {
28 allocFailure = true;
29 return;
30 }
31 size = newSize;
32 unsigned int i=0;
33 for (; i<len; i++) {
34 newv[i] = v[i];
35 }
36 for (; i<size; i++) {
37 newv[i] = 0;
38 }
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 }
85 int &operator[](unsigned int i) {
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 }
100 void SetLength(unsigned int newLength) {
101 if (newLength > len) {
102 if (newLength >= size) {
103 SizeTo(newLength);
104 }
105 }
106 len = newLength;
107 }
108 int Length() const {
109 return len;
110 }
111 };
112
113 #endif