aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/SVector.h
diff options
context:
space:
mode:
authornyamatongwe <unknown>2000-03-08 01:43:56 +0000
committernyamatongwe <unknown>2000-03-08 01:43:56 +0000
commitc196d2fc7c3ece7ccb7d89c425499a75ead7e59b (patch)
tree3ea3c536f04e88499b86ed82e8a9a457f96b4978 /src/SVector.h
parent7fbd8e2a34d2f5084ce26ad95d7c70ae4de6a233 (diff)
downloadscintilla-mirror-c196d2fc7c3ece7ccb7d89c425499a75ead7e59b.tar.gz
Initial revision
Diffstat (limited to 'src/SVector.h')
-rw-r--r--src/SVector.h110
1 files changed, 110 insertions, 0 deletions
diff --git a/src/SVector.h b/src/SVector.h
new file mode 100644
index 000000000..7bc948738
--- /dev/null
+++ b/src/SVector.h
@@ -0,0 +1,110 @@
+// Scintilla source code edit control
+// SVector.h - a simple expandable vector
+// Copyright 1998-1999 by Neil Hodgson <neilh@hare.net.au>
+// The License.txt file describes the conditions under which this software may be distributed.
+
+#ifndef SVECTOR_H
+#define SVECTOR_H
+
+// A simple expandable vector.
+// T must support assignment.
+// Storage not allocated for elements until an element is used.
+// This makes it very lightweight unless used so is a good match for optional features.
+template<class T, int sizeIncrement>
+class SVector {
+ T *v;
+ unsigned int size; // Number of elements allocated
+ unsigned int len; // Number of elements in vector
+ bool allocFailure; // A memory allocation call has failed
+
+ // Internally allocate more elements than the user wants to avoid
+ // thrashng the memory allocator
+ void SizeTo(int newSize) {
+ if (newSize < sizeIncrement)
+ newSize += sizeIncrement;
+ else
+ newSize = (newSize * 3) / 2;
+ T* newv = new T[newSize];
+ if (!newv) {
+ allocFailure = true;
+ return;
+ }
+ size = newSize;
+ for (int i=0; i<len; i++) {
+ newv[i] = v[i];
+ }
+ delete []v;
+ v = newv;
+ }
+
+public:
+ SVector() {
+ allocFailure = false;
+ v = 0;
+ len = 0;
+ size = 0;
+ }
+ ~SVector() {
+ Free();
+ }
+ SVector(const SVector &other) {
+ allocFailure = false;
+ v = 0;
+ len = 0;
+ size = 0;
+ if (other.Length() > 0) {
+ SizeTo(other.Length());
+ if (!allocFailure) {
+ for (int i=0;i<other.Length();i++)
+ v[i] = other.v[i];
+ len = other.Length();
+ }
+ }
+ }
+ SVector &operator=(const SVector &other) {
+ if (this != &other) {
+ delete []v;
+ allocFailure = false;
+ v = 0;
+ len = 0;
+ size = 0;
+ if (other.Length() > 0) {
+ SizeTo(other.Length());
+ if (!allocFailure) {
+ for (int i=0;i<other.Length();i++)
+ v[i] = other.v[i];
+ }
+ len = other.Length();
+ }
+ }
+ return *this;
+ }
+ T &operator[](unsigned int i) {
+ if (i >= len) {
+ if (i >= size) {
+ SizeTo(i);
+ }
+ len = i+1;
+ }
+ return v[i];
+ }
+ void Free() {
+ delete []v;
+ v = 0;
+ size = 0;
+ len = 0;
+ }
+ void SetLength(int newLen) {
+ if (newLength > len) {
+ if (newLength >= size) {
+ SizeTo(newLength);
+ }
+ }
+ len = newLen;
+ }
+ int Length() const {
+ return len;
+ }
+};
+
+#endif