1 // Scintilla source code edit control
\r
3 ** A simple expandable vector.
\r
5 // Copyright 1998-2001 by Neil Hodgson <neilh@hare.net.au>
\r
6 // The License.txt file describes the conditions under which this software may be distributed.
\r
11 #ifdef SCI_NAMESPACE
\r
12 namespace Scintilla {
\r
16 * A simple expandable integer vector.
\r
17 * Storage not allocated for elements until an element is used.
\r
18 * This makes it very lightweight unless used so is a good match for optional features.
\r
21 enum { allocSize = 4000 };
\r
23 int *v; ///< The vector
\r
24 unsigned int size; ///< Number of elements allocated
\r
25 unsigned int len; ///< Number of elements used in vector
\r
26 bool allocFailure; ///< A memory allocation call has failed
\r
28 /** Internally allocate more elements than the user wants
\r
29 * to avoid thrashing the memory allocator. */
\r
30 void SizeTo(int newSize) {
\r
31 if (newSize < allocSize)
\r
32 newSize += allocSize;
\r
34 newSize = (newSize * 3) / 2;
\r
35 int* newv = new int[newSize];
\r
37 allocFailure = true;
\r
42 for (; i<len; i++) {
\r
45 for (; i<size; i++) {
\r
54 allocFailure = false;
\r
62 /// Constructor from another vector.
\r
63 SVector(const SVector &other) {
\r
64 allocFailure = false;
\r
68 if (other.Length() > 0) {
\r
69 SizeTo(other.Length());
\r
70 if (!allocFailure) {
\r
71 for (int i=0;i<other.Length();i++)
\r
73 len = other.Length();
\r
77 /// Copy constructor.
\r
78 SVector &operator=(const SVector &other) {
\r
79 if (this != &other) {
\r
81 allocFailure = false;
\r
85 if (other.Length() > 0) {
\r
86 SizeTo(other.Length());
\r
87 if (!allocFailure) {
\r
88 for (int i=0;i<other.Length();i++)
\r
91 len = other.Length();
\r
96 /** @brief Accessor.
\r
97 * Allows to access values from the list, and grows it if accessing
\r
98 * outside the current bounds. The returned value in this case is 0. */
\r
99 int &operator[](unsigned int i) {
\r
115 /** @brief Grow vector size.
\r
116 * Doesn't allow a vector to be shrinked. */
\r
117 void SetLength(unsigned int newLength) {
\r
118 if (newLength > len) {
\r
119 if (newLength >= size) {
\r
125 /// Get the current length (number of used elements) of the vector.
\r
126 int Length() const {
\r
131 #ifdef SCI_NAMESPACE
\r