📄 core.h
字号:
}/**获取缓存区大小*/template <class T>inline int C_Array<T>::size() const{ return m_nSize;}/**获取缓存区内对象个数*/template <class T>inline int C_Array<T>::count() const{ return m_nCount;}/**判断是否为空*/template <class T>inline bool C_Array<T>::isEmpty() const{ return m_nCount <=0;}/**在索引处置入对象*/template <class T>bool C_Array<T>::setAt(int i_nIndex, const T& i_yElement){ if (i_nIndex > m_nCount-1 || i_nIndex < 0) return false; *(m_pBuf + i_nIndex) = i_yElement; return true;}/**在头置入对象*/template <class T>bool C_Array<T>::setFirst(const T& i_yElement){ if (m_nCount <= 0) return false; *m_pBuf = i_yElement; return true;}/**在尾置入对象*/template <class T>bool C_Array<T>::setLast(const T& i_yElement){ if (m_nCount <= 0) return false; *(m_pBuf + m_nCount -1) = i_yElement; return true;}/**获取索引对象引用,重载[].*/template <class T>inline T& C_Array<T>::operator[](int i_nIndex){ return *(m_pBuf + i_nIndex);}/**获取索引对象引用,重载[].*/template <class T>inline T C_Array<T>::operator[](int i_nIndex) const{ return *(m_pBuf + i_nIndex);}/**获取索引处对象引用*/template <class T>bool C_Array<T>::at(int i_nIndex, T& o_yElement){ if (i_nIndex >= m_nCount || i_nIndex < 0) return false; o_yElement = *(m_pBuf + i_nIndex); return true;}/**获取索引处对象引用*/template <class T>T& C_Array<T>::at(int i_nIndex){ assert (i_nIndex < m_nCount && i_nIndex >= 0); return *(m_pBuf + i_nIndex);}/**获取索引处对象*/template <class T>T C_Array<T>::at(int i_nIndex) const{ assert (i_nIndex < m_nCount && i_nIndex >= 0); return *(m_pBuf + i_nIndex);}/**获取索引处对象指针*/template <class T>T* C_Array<T>::pAt(int i_nIndex){ if (i_nIndex >= m_nCount || i_nIndex < 0) return NULL; return m_pBuf + i_nIndex;}/**获取头对象引用*/template <class T>inline bool C_Array<T>::first(T& o_yElement){ if (m_nCount == 0) return false; o_yElement = *m_pBuf; return true;}/**获取头对象引用*/template <class T>inline T& C_Array<T>::first(){ assert (m_nCount != 0); return *m_pBuf;}/**获取头对象*/template <class T>inline T C_Array<T>::first() const{ assert (m_nCount != 0); return *m_pBuf;}/**获取头对象指针*/template <class T>inline T* C_Array<T>::pFirst(){ if (m_nCount == 0) return NULL; return m_pBuf;}/**获取尾对象引用*/template <class T>inline bool C_Array<T>::last(T& o_yElement){ if (m_nCount == 0) return false; o_yElement = *(m_pBuf + m_nCount - 1); return true;}/**获取尾对象引用*/template <class T>inline T& C_Array<T>::last(){ assert (m_nCount != 0); return *(m_pBuf + m_nCount - 1);}/**获取尾对象*/template <class T>inline T C_Array<T>::last() const{ assert (m_nCount != 0); return *(m_pBuf + m_nCount - 1);}/**获取尾对象指针*/template <class T>inline T* C_Array<T>::pLast(){ if (m_nCount == 0) return NULL; return m_pBuf + m_nCount - 1;}/**增加或减少数组元素个数*/template <class T>bool C_Array<T>::addCount(int i_nNumAdd){ int len = m_nCount + i_nNumAdd; if (i_nNumAdd == 0) return true; // 未改变 if (len < 0) return false; if (i_nNumAdd < 0) // 减少元素个数 { m_nCount += i_nNumAdd; return true; } if (m_nSize < len) // 若容积小于增加后的元素个数时 addSize(len); // 增长 m_nCount += i_nNumAdd; return true;}/**增加或减少缓存大小*/template <class T>bool C_Array<T>::addSize(int i_nSizeAdd){ T* temp; int len = 0; if (i_nSizeAdd == 0) return true; // 未改变 len = m_nSize + i_nSizeAdd; if (len > 0) { // 开辟一个新长度的缓冲区 temp = new T[len]; assert (temp != NULL); // 将原数组元素拷入新开辟的缓冲区中 if (m_nCount > 0) // 原有元素 { if (i_nSizeAdd < 0) { for (int i=0; i<len; ++i) { temp[i] = m_pBuf[i]; } } else { for (int i=0; i<m_nSize; ++i) { temp[i] = m_pBuf[i]; } } } delete []m_pBuf; // 删除原缓冲区 // 将新缓冲区首指针赋组缓冲区指针,并改变缓冲区长度值 m_pBuf = temp; m_nSize += i_nSizeAdd; if (m_nCount > m_nSize) m_nCount = m_nSize; } else { delete []m_pBuf; m_pBuf = NULL; m_nSize = 0; m_nCount = 0; } return true;}/**在索引对象之前增加对象\sa addAfter*/template <class T>bool C_Array<T>::addBefore(int i_nIndex, const T& i_yElement){ if (i_nIndex > m_nCount || i_nIndex < 0) return false; int moveNum = m_nCount - i_nIndex; addCount(); for (int i=moveNum; i>0; --i) { m_pBuf[i_nIndex+i] = m_pBuf[i_nIndex+i-1]; } m_pBuf[i_nIndex] = i_yElement; return true;}/**在索引对象之后增加对象\sa addBefore*/template <class T>bool C_Array<T>::addAfter(int i_nIndex, const T& i_yElement){ if (i_nIndex >= m_nCount || i_nIndex < 0) return false; int moveNum = m_nCount - i_nIndex - 1; addCount(); for (int i=moveNum; i>0; --i) { m_pBuf[i_nIndex+i+1] = m_pBuf[i_nIndex+i]; } m_pBuf[i_nIndex+1] = i_yElement; return true;}/**在最前端增加对象*/template <class T>inline bool C_Array<T>::addFirst(const T& i_yElement){ return addBefore(0, i_yElement);}/**在最后端增加对象*/template <class T>inline bool C_Array<T>::addLast(const T& i_yElement){ return addBefore(m_nCount, i_yElement);}/**删除索引处对象*/template <class T>bool C_Array<T>::removeAt(int i_nIndex){ if (i_nIndex > m_nCount-1 || i_nIndex < 0) return false; for (int i=0; i<m_nCount-i_nIndex-1; ++i) { m_pBuf[i_nIndex+i] = m_pBuf[i_nIndex+i+1]; } addCount(-1); return true;}/**删除头对象*/template <class T>inline bool C_Array<T>::removeFirst(){ return removeAt(0);}/**删除尾对象*/template <class T>inline bool C_Array<T>::removeLast(){ return removeAt(m_nCount-1);}/**清空对象*/template <class T>inline void C_Array<T>::clear(){ m_nCount = 0;}/**清空多余缓存区*/template <class T>inline bool C_Array<T>::freeExtra(){ return addSize(m_nCount-m_nSize);}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -