*** empty log message ***
[anjuta-git-plugin.git] / scintilla / SVector.h
blobc8edb513bc3fdca89e6ef43f7341ee4a73f5ed13
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 /**
12 * A simple expandable integer vector.
13 * Storage not allocated for elements until an element is used.
14 * This makes it very lightweight unless used so is a good match for optional features.
16 class SVector {
17 enum { allocSize = 4000 };
19 int *v; ///< The vector
20 unsigned int size; ///< Number of elements allocated
21 unsigned int len; ///< Number of elements used in vector
22 bool allocFailure; ///< A memory allocation call has failed
24 /** Internally allocate more elements than the user wants
25 * to avoid thrashing the memory allocator. */
26 void SizeTo(int newSize) {
27 if (newSize < allocSize)
28 newSize += allocSize;
29 else
30 newSize = (newSize * 3) / 2;
31 int* newv = new int[newSize];
32 if (!newv) {
33 allocFailure = true;
34 return;
36 size = newSize;
37 unsigned int i=0;
38 for (; i<len; i++) {
39 newv[i] = v[i];
41 for (; i<size; i++) {
42 newv[i] = 0;
44 delete []v;
45 v = newv;
48 public:
49 SVector() {
50 allocFailure = false;
51 v = 0;
52 len = 0;
53 size = 0;
55 ~SVector() {
56 Free();
58 /// Constructor from another vector.
59 SVector(const SVector &other) {
60 allocFailure = false;
61 v = 0;
62 len = 0;
63 size = 0;
64 if (other.Length() > 0) {
65 SizeTo(other.Length());
66 if (!allocFailure) {
67 for (int i=0;i<other.Length();i++)
68 v[i] = other.v[i];
69 len = other.Length();
73 /// Copy constructor.
74 SVector &operator=(const SVector &other) {
75 if (this != &other) {
76 delete []v;
77 allocFailure = false;
78 v = 0;
79 len = 0;
80 size = 0;
81 if (other.Length() > 0) {
82 SizeTo(other.Length());
83 if (!allocFailure) {
84 for (int i=0;i<other.Length();i++)
85 v[i] = other.v[i];
87 len = other.Length();
90 return *this;
92 /** @brief Accessor.
93 * Allows to access values from the list, and grows it if accessing
94 * outside the current bounds. The returned value in this case is 0. */
95 int &operator[](unsigned int i) {
96 if (i >= len) {
97 if (i >= size) {
98 SizeTo(i);
100 len = i+1;
102 return v[i];
104 /// Reset vector.
105 void Free() {
106 delete []v;
107 v = 0;
108 size = 0;
109 len = 0;
111 /** @brief Grow vector size.
112 * Doesn't allow a vector to be shrinked. */
113 void SetLength(unsigned int newLength) {
114 if (newLength > len) {
115 if (newLength >= size) {
116 SizeTo(newLength);
119 len = newLength;
121 /// Get the current length (number of used elements) of the vector.
122 int Length() const {
123 return len;
127 #endif