#include "FITKElementList.h" #include "FITKAbstractElement.h" namespace Interface { FITKElementList::~FITKElementList() { //释放全部单元内存 this->removeAllElement(); } void FITKElementList::appendElement(FITKAbstractElement* element) { //已经存在的单元不追加 if (element == nullptr) return; // if (_elementList.contains(element)) return; _elementList.append(element); const int id = element->getEleID(); const int index = _elementList.size() - 1; //建立ID与index的映射关系 _idIndexMap.insert(id, index); _eleMaxID = _eleMaxID > id ? _eleMaxID : id; } int FITKElementList::getEleIDByIndex(const int index) { if (index < 0 || index >= _elementList.size()) return -1; FITKAbstractElement* ele = _elementList[index]; if (ele == nullptr) return -1; return ele->getEleID(); } int FITKElementList::getEleIndexByID(const int id) const { return _idIndexMap.value(id); } FITKAbstractElement* FITKElementList::getElementAt(const int index) { //越界返回空指针 if (index < 0 || index >= _elementList.size()) return nullptr; return _elementList.at(index); } Interface::FITKAbstractElement* FITKElementList::getElementByID(const int id) { //首先查到ID const int index = _idIndexMap.value(id); if (index < 0 || index >= _elementList.size()) return nullptr; return _elementList.at(index); } int FITKElementList::getElementCount() { return _elementList.size(); } void FITKElementList::reverseEleByID(const int id) { auto ele = this->getElementByID(id); if (ele) ele->reverse(); } void FITKElementList::reverseEleByIndex(const int index) { auto ele = this->getElementAt(index); if (ele) ele->reverse(); } int FITKElementList::getElementMaxID() { return _eleMaxID; } void FITKElementList::removeAllElement() { for (auto ele : _elementList) delete ele; //清空列表 _elementList.clear(); _idIndexMap.clear(); _eleMaxID = 0; } }