📄 array.h
字号:
/*////////////////////////////////////////////////////////////////////////////////// INTEL CORPORATION PROPRIETARY INFORMATION// This software is supplied under the terms of a license agreement or// nondisclosure agreement with Intel Corporation and may not be copied// or disclosed except in accordance with the terms of that agreement.// Copyright(c) 2003-2005 Intel Corporation. All Rights Reserved.//*/#ifndef __UMC_SIMPLE_ARRAY_H__#define __UMC_SIMPLE_ARRAY_H__#include <string.h> // fore memmove function#include <stdlib.h> // for realloc/free functions#include "vm_debug.h"#include "umc_malloc.h"#include "umc_dynamic_cast.h"template <class T>class SimpleArray{ DYNAMIC_CAST_DECL_BASE(SimpleArray)public: T* m_pArray; unsigned int m_uiSize; unsigned int m_uiAllocSize; // Constructors & destructors SimpleArray() : m_pArray(NULL), m_uiSize(0), m_uiAllocSize(0) {} ~SimpleArray() { DeleteAll(); } // Main functionality inline unsigned int GetSize() const { return m_uiSize; } bool Insert(const unsigned int& ruiIndex, T t) { assert(ruiIndex <= m_uiSize); if(m_uiSize == m_uiAllocSize) { T* pT; unsigned int uiNewAllocSize = (m_uiAllocSize == 0) ? 1 : (2 * m_uiSize); pT = (T*)realloc(m_pArray, uiNewAllocSize * sizeof(T)); if(pT == NULL) { return false; } m_uiAllocSize = uiNewAllocSize; m_pArray = pT; } m_uiSize++; if (m_uiSize - 1 > ruiIndex) { memmove((void*)&m_pArray[ruiIndex + 1], (void*)&m_pArray[ruiIndex], (m_uiSize - ruiIndex - 1) * sizeof(T)); } SetAtIndex(ruiIndex, t); return true; } bool Add(T& t) { return Insert(m_uiSize, t); } bool Delete(T& t) { int iIndex = Find(t); if (-1 == iIndex) { return false; } return DeleteAt(iIndex); } bool DeleteAt(unsigned int uiIndex) { if ((m_uiSize - 1) != uiIndex) { m_pArray[uiIndex].~T(); memmove((void*)&m_pArray[uiIndex], (void*)&m_pArray[uiIndex + 1], (m_uiSize - (uiIndex + 1)) * sizeof(T)); } m_uiSize--; return true; } void DeleteAll() { if(m_pArray != NULL) { for (unsigned int i = 0; i < m_uiSize; i++) { m_pArray[i].~T(); } free(m_pArray); m_pArray = NULL; } m_uiAllocSize = 0; m_uiSize = 0; } T& operator[] (unsigned int uiIndex) const { assert(uiIndex < m_uiSize); return m_pArray[uiIndex]; } T* GetArray() const { return m_pArray; } // Implementation class Wrapper { public: Wrapper(T& _t): t(_t) {} template <class _Tw> #undef new // To avoid redefining umc_malloc's operator new void* operator new(size_t, _Tw* p) { return p; } template <class _Tw> void operator delete(void* /*p*/, _Tw* /*pBuffer*/){} T t; }; void SetAtIndex(int nIndex, T& t) { assert((nIndex >= 0) && (nIndex < (int) m_uiSize)); new(&m_pArray[nIndex]) Wrapper(t); } int Seek(T& t) const { for (int i = 0; i < m_uiSize; i++) { if (m_pArray[i] == t) { return i; } } return -1; // not found }};#endif // __UMC_SIMPLE_ARRAY_H__
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -