summaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authorleo2004-06-10 21:17:42 +0000
committerleo2004-06-10 21:17:42 +0000
commitc117a3dc16af7346d750475e648252f5dbffce9b (patch)
tree55fdb05f3a9995972d4b0e2ecd2660ec7c125218 /common
parentb6f4b5a61c74e319ebd627f14fb4fe0b56284b2b (diff)
Pointer Array class.
git-svn-id: http://svn.leocad.org/trunk@358 c7d43263-9d01-0410-8a33-9dba5d9f93d6
Diffstat (limited to 'common')
-rwxr-xr-xcommon/array.cpp102
-rwxr-xr-xcommon/array.h30
2 files changed, 121 insertions, 11 deletions
diff --git a/common/array.cpp b/common/array.cpp
index 97eb367..0811d2d 100755
--- a/common/array.cpp
+++ b/common/array.cpp
@@ -34,15 +34,6 @@ void PtrArray<T>::Expand (int nGrow)
}
template <class T>
-void PtrArray<T>::SetSize (int nSize)
-{
- if (nSize > m_nLength)
- Expand (nSize - m_nLength);
-
- m_nLength = nSize;
-}
-
-template <class T>
T* PtrArray<T>::RemoveIndex (int nIndex)
{
T* ret = NULL;
@@ -104,16 +95,107 @@ void PtrArray<T>::InsertAt (int nIndex, T* pObj)
else
Expand (1);
+ m_nLength++;
for (int i = m_nLength - 1; i > nIndex; i--)
m_pData[i] = m_pData[i-1];
m_pData[nIndex] = pObj;
}
+// ============================================================================
+
+template <class T>
+ObjArray<T>::ObjArray(int Size, int Grow)
+{
+ m_Data = NULL;
+ m_Length = 0;
+ m_Alloc = 0;
+ m_Grow = Grow;
+
+ if (Size != 0)
+ Expand(Size);
+}
+
+template <class T>
+ObjArray<T>::~ObjArray ()
+{
+ delete[] m_Data;
+}
+
+template <class T>
+void ObjArray<T>::Expand(int Grow)
+{
+ if ((m_Length + Grow) > m_Alloc)
+ {
+ int NewSize = ((m_Length + Grow) / m_Grow + 1) * m_Grow;
+
+ T* NewData = new T[NewSize];
+
+ for (int i = 0; i < m_Length; i++)
+ NewData[i] = m_Data[i];
+
+ delete[] m_Data;
+ m_Data = NewData;
+ m_Alloc = NewSize;
+ }
+}
+
+template <class T>
+void ObjArray<T>::RemoveIndex(int Index)
+{
+ m_Length--;
+
+ for (int i = Index; i < m_Length; i++)
+ m_Data[i] = m_Data[i+1];
+}
+
+template <class T>
+void ObjArray<T>::Add(const T& Obj)
+{
+ Expand(1);
+ m_Data[m_Length++] = Obj;
+}
+
+template <class T>
+void ObjArray<T>::AddSorted (const T& Obj, LC_OBJARRAY_COMPARE_FUNC Func, void* SortData)
+{
+ int i;
+
+ for (i = 0; i < GetSize(); i++)
+ {
+ if (Func(Obj, m_Data[i], SortData) < 0)
+ {
+ InsertAt (i, Obj);
+ return;
+ }
+ }
+
+ Add(Obj);
+}
+
+template <class T>
+void ObjArray<T>::InsertAt(int Index, const T& Obj)
+{
+ if (Index >= m_Length)
+ Expand(Index - m_Length + 1);
+ else
+ Expand(1);
+
+ m_nLength++;
+ for (int i = m_Length - 1; i > Index; i--)
+ m_Data[i] = m_Data[i-1];
+
+ m_Data[Index] = Obj;
+}
+
+
+
+
+
/*
-// =============================================================================
+// ============================================================================
// ObjectArray class
ObjectArray::ObjectArray (unsigned long nSize)
diff --git a/common/array.h b/common/array.h
index 1c5d5a3..c8adfbf 100755
--- a/common/array.h
+++ b/common/array.h
@@ -10,7 +10,6 @@ class PtrArray
typedef int (*LC_PTRARRAY_COMPARE_FUNC) (T* a, T* b, void* data);
- void SetSize (int nSize);
int GetSize () const
{ return m_nLength; }
@@ -31,6 +30,35 @@ class PtrArray
int m_nAlloc;
};
+template <class T>
+class ObjArray
+{
+public:
+ ObjArray(int Size = 0, int Grow = 16);
+ ~ObjArray();
+
+ typedef int (*LC_OBJARRAY_COMPARE_FUNC)(const T& A, const T& B, void* SortData);
+
+ int GetSize() const
+ { return m_Length; }
+
+ void RemoveIndex(int Index);
+ void Add(const T& Obj);
+ void AddSorted(const T& Obj, LC_OBJARRAY_COMPARE_FUNC Func, void* SortData);
+ void InsertAt(int Index, const T& Obj);
+
+ T& operator [](int Index) const
+ { return m_Data[Index]; }
+
+protected:
+ void Expand(int Grow);
+
+ T* m_Data;
+ int m_Length;
+ int m_Alloc;
+ int m_Grow;
+};
+
#include "array.cpp"
#endif // _ARRAY_H_