summaryrefslogtreecommitdiff
path: root/common/array.cpp
diff options
context:
space:
mode:
authorleo2005-07-14 05:10:57 +0000
committerleo2005-07-14 05:10:57 +0000
commite3b7ec77734b8f415f9c756f0d7bf90ff202e71e (patch)
tree741fd8c82177a499308de47bd94453d9bdc5e229 /common/array.cpp
parent7c16eadee05cfe3115ac0a69a822c66f1b3e292f (diff)
Changed the pieces list to a tree sorted by categories.
git-svn-id: http://svn.leocad.org/trunk@414 c7d43263-9d01-0410-8a33-9dba5d9f93d6
Diffstat (limited to 'common/array.cpp')
-rwxr-xr-xcommon/array.cpp143
1 files changed, 88 insertions, 55 deletions
diff --git a/common/array.cpp b/common/array.cpp
index 3d58c55..76c9a30 100755
--- a/common/array.cpp
+++ b/common/array.cpp
@@ -1,105 +1,132 @@
//
-// Simple array class
+// Simple array classes
//
#include <stdlib.h>
#include <string.h>
template <class T>
-PtrArray<T>::PtrArray (int nSize)
+PtrArray<T>::PtrArray(int nSize)
{
- m_pData = NULL;
- m_nLength = 0;
- m_nAlloc = 0;
+ m_pData = NULL;
+ m_nLength = 0;
+ m_nAlloc = 0;
- if (nSize != 0)
- Expand (nSize);
+ if(nSize != 0)
+ Expand(nSize);
}
template <class T>
-PtrArray<T>::~PtrArray ()
+PtrArray<T>::~PtrArray()
{
- free (m_pData);
+ free(m_pData);
}
template <class T>
-void PtrArray<T>::Expand (int nGrow)
+void PtrArray<T>::Expand(int nGrow)
{
- if ((m_nLength + nGrow) > m_nAlloc)
- {
- m_pData = (T**)realloc (m_pData, (m_nLength + nGrow) * sizeof (T*));
- memset (m_pData + m_nLength, 0, nGrow * sizeof (T*));
- m_nAlloc = m_nLength + nGrow;
- }
+ if((m_nLength + nGrow) > m_nAlloc)
+ {
+ m_pData =(T**)realloc(m_pData,(m_nLength + nGrow) * sizeof(T*));
+ memset(m_pData + m_nLength, 0, nGrow * sizeof(T*));
+ m_nAlloc = m_nLength + nGrow;
+ }
}
template <class T>
-T* PtrArray<T>::RemoveIndex (int nIndex)
+void PtrArray<T>::RemoveAll()
{
- T* ret = NULL;
+ m_nLength = 0;
+}
- if (nIndex < m_nLength)
- {
- ret = m_pData[nIndex];
+template <class T>
+T* PtrArray<T>::RemoveIndex(int nIndex)
+{
+ T* ret = NULL;
- if (nIndex != m_nLength - 1)
- memmove (m_pData + nIndex, m_pData + nIndex + 1,
- sizeof (T*) * (m_nLength - nIndex - 1));
-
- m_nLength--;
- m_pData[m_nLength] = NULL;
- }
+ if(nIndex < m_nLength)
+ {
+ ret = m_pData[nIndex];
- return ret;
+ if(nIndex != m_nLength - 1)
+ memmove(m_pData + nIndex, m_pData + nIndex + 1, sizeof(T*) *(m_nLength - nIndex - 1));
+
+ m_nLength--;
+ m_pData[m_nLength] = NULL;
+ }
+
+ return ret;
}
template <class T>
-T* PtrArray<T>::RemovePointer (T* pObj)
+T* PtrArray<T>::RemovePointer(T* pObj)
{
- int i;
+ int i;
- for (i = 0; i < m_nLength; i++)
- if (m_pData[i] == pObj)
- return RemoveIndex (i);
+ for(i = 0; i < m_nLength; i++)
+ if(m_pData[i] == pObj)
+ return RemoveIndex(i);
- return NULL;
+ return NULL;
}
template <class T>
-void PtrArray<T>::Add (T* pObj)
+void PtrArray<T>::Add(T* pObj)
{
- Expand (1);
- m_pData[m_nLength++] = pObj;
+ Expand(1);
+ m_pData[m_nLength] = pObj;
+ m_nLength++;
}
template <class T>
-void PtrArray<T>::AddSorted (T* pObj, LC_PTRARRAY_COMPARE_FUNC pFunc, void* pData)
+void PtrArray<T>::AddSorted(T* pObj, LC_PTRARRAY_COMPARE_FUNC pFunc, void* pData)
{
- int i;
+ int i;
- for (i = 0; i < GetSize (); i++)
- if (pFunc (pObj, m_pData[i], pData) < 0)
- {
- InsertAt (i, pObj);
- return;
- }
+ for(i = 0; i < GetSize(); i++)
+ {
+ if(pFunc(pObj, m_pData[i], pData) < 0)
+ {
+ InsertAt(i, pObj);
+ return;
+ }
+ }
- Add (pObj);
+ Add(pObj);
}
template <class T>
-void PtrArray<T>::InsertAt (int nIndex, T* pObj)
+void PtrArray<T>::InsertAt(int nIndex, T* pObj)
{
- if (nIndex >= m_nLength)
- Expand (nIndex - m_nLength + 1);
- else
- Expand (1);
+ if(nIndex >= m_nLength)
+ Expand(nIndex - m_nLength + 1);
+ else
+ Expand(1);
m_nLength++;
- for (int i = m_nLength - 1; i > nIndex; i--)
- m_pData[i] = m_pData[i-1];
+ for(int i = m_nLength - 1; i > nIndex; i--)
+ m_pData[i] = m_pData[i-1];
+
+ m_pData[nIndex] = pObj;
+}
+
+template <class T>
+int PtrArray<T>::FindIndex(T* Obj) const
+{
+ for (int i = 0; i < m_nLength; i++)
+ if (m_pData[i] == Obj)
+ return i;
- m_pData[nIndex] = pObj;
+ return -1;
+}
+
+template <class T>
+PtrArray<T>& PtrArray<T>::operator=(const PtrArray<T>& Array)
+{
+ m_nLength = Array.m_nLength;
+ m_nAlloc = Array.m_nAlloc;
+ m_pData =(T**)realloc(m_pData, (m_nAlloc) * sizeof(T*));
+ memcpy(m_pData, Array.m_pData, (m_nAlloc) * sizeof(T*));
}
// ============================================================================
@@ -141,6 +168,12 @@ void ObjArray<T>::Expand(int Grow)
}
template <class T>
+void ObjArray<T>::RemoveAll()
+{
+ m_Length = 0;
+}
+
+template <class T>
void ObjArray<T>::RemoveIndex(int Index)
{
m_Length--;