From c196d2fc7c3ece7ccb7d89c425499a75ead7e59b Mon Sep 17 00:00:00 2001 From: nyamatongwe Date: Wed, 8 Mar 2000 01:43:56 +0000 Subject: Initial revision --- src/SVector.h | 110 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 src/SVector.h (limited to 'src/SVector.h') 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 +// 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 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 0) { + SizeTo(other.Length()); + if (!allocFailure) { + for (int i=0;i 0) { + SizeTo(other.Length()); + if (!allocFailure) { + for (int i=0;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 -- cgit v1.2.3