LLBC_Object *LLBC_Array::Clone() const { LLBC_Array *clone = new LLBC_Array; // Clone object factory. if (_objFactory) { LLBC_ObjectFactory *cloneObjFactory = static_cast<LLBC_ObjectFactory *>(_objFactory->Clone()); clone->SetObjectFactory(cloneObjFactory); LLBC_SAFE_RELEASE(cloneObjFactory); } // Clone all array elements. ConstIter it = Begin(), endIt = End(); for (; it != endIt; it++) { LLBC_Object *cloneObj = (*it)->Clone(); clone->PushBack(cloneObj); LLBC_SAFE_RELEASE(cloneObj); } return clone; }
LLBC_Array *LLBC_Array::ObjectsAtIndexs(const LLBC_Array::IndexSet &indexs) { Obj *o = NULL; int errNo = LLBC_GetLastError(); int subErrNo = LLBC_GetSubErrorNo(); LLBC_Array *arr = new LLBC_Array; IndexSet::const_iterator iter = indexs.begin(); for (; iter != indexs.end(); iter++) { if ((o = ObjectAtIndex(*iter))) { arr->PushBack(o); } } LLBC_SetLastError(errNo); LLBC_SetSubErrorNo(subErrNo); return arr; }
void TestCase_ObjBase_Array::BasicTest() { std::cout <<"Basic test: " <<std::endl; std::cout <<"Insert 10 objects." <<std::endl; LLBC_Array *arr = new LLBC_Array; const LLBC_Array &constArr = *arr; for(int i = 0; i < 10; i ++) { LLBC_Object *obj = new TestObj; arr->Insert(arr->End(), obj); obj->Release(); } ForeachFun foreachFun; ConstForeachFun constForeachFun; std::cout <<"Done, output it: " <<std::endl; arr->Foreach(foreachFun); std::cout <<"Output it(using const foreach version method): " <<std::endl; constArr.Foreach(constForeachFun); std::cout <<"Using subscript to access array element." <<std::endl; for(int i = 0; i < 10; i ++) { std::cout <<(*arr)[i]->ToString() <<std::endl; } std::cout <<"Done" <<std::endl; std::cout <<"Using subscript to access array element(const array)." <<std::endl; for(int i = 0; i < 10; i ++) { std::cout <<constArr[i]->ToString() <<std::endl; } std::cout <<"Done" <<std::endl; std::cout <<"Fetch sub array in range[3, 5): " <<std::endl; LLBC_Array::IndexSet idxs; idxs.insert(3); idxs.insert(4); LLBC_Array *subArr = arr->ObjectsAtIndexs(idxs); std::cout <<"Done, output it: " <<std::endl; subArr->Foreach(foreachFun); delete arr; delete subArr; std::cout <<"Basic test done!" <<std::endl; }
void TestCase_ObjBase_Array::SortTest() { std::cout <<"Sort test: " <<std::endl; LLBC_Array arr; std::cout <<"Insert 10 elements: " <<std::endl; for(int i = 1; i <= 10; i ++) { LLBC_Object *obj = new TestObj; if(i % 2 == 0) { arr.PushBack(obj); } else { arr.PushFront(obj); } obj->Release(); } ForeachFun foreachFun; std::cout <<"Done, output it: " <<std::endl; arr.Foreach(foreachFun); LessSortFun lessSortFun; std::cout <<"Sort it(use less than fun object): " <<std::endl; arr.Sort(lessSortFun); std::cout <<"Done, output it: " <<std::endl; arr.Foreach(foreachFun); GreaterSortFun greaterSortFun; std::cout <<"Sort it(use greater than fun object): " <<std::endl; arr.Sort(greaterSortFun); std::cout <<"Done, output it: " <<std::endl; arr.Foreach(foreachFun); }
void TestCase_ObjBase_Array::IterTest() { std::cout <<"Iter test: " <<std::endl; // PushBack/PushFront test. LLBC_Array arr; static const int insertCnt = 10; std::cout <<"PushBack/PushFront" <<insertCnt <<" objects into array." <<std::endl; for(int i = 1; i <= insertCnt; i ++) { LLBC_Object *obj = new TestObj; arr.PushBack(obj); obj->Release(); } std::cout <<"Done, array size: " <<arr.GetSize() <<", Capacity: " <<arr.GetCapacity() <<std::endl; // Iterator test. ForeachFun foreachFun; std::cout <<"Iteration, using LLBC_Array::Iter" <<std::endl; LLBC_Array::Iter it1 = arr.Begin(); for(; it1 != arr.End(); it1 ++) { LLBC_Object *obj = *it1; std::cout <<obj->ToString() <<std::endl; } std::cout <<"Done" <<std::endl; std::cout <<"Iteration, using LLBC_Array::ReverseIter" <<std::endl; LLBC_Array::ReverseIter it2 = arr.ReverseBegin(); for(; it2 != arr.ReverseEnd(); it2 ++) { std::cout <<(*it2)->ToString() <<std::endl; } std::cout <<"Done" <<std::endl; std::cout <<"Insert new object to front" <<std::endl; LLBC_Object *obj = new TestObj; arr.Insert(arr.Begin(), obj); obj->Release(); std::cout <<"Done, ouput it: " <<std::endl; arr.Foreach(foreachFun); std::cout <<"Insert new object before position 1: " <<std::endl; obj = new TestObj; arr.Insert(++ arr.Begin(), obj); obj->Release(); std::cout <<"Done, output it: " <<std::endl; arr.Foreach(foreachFun); std::cout <<"Erase position 1's object: " <<std::endl; arr.Erase(arr.Begin() + 1); std::cout <<"Done, output it: " <<std::endl; arr.Foreach(foreachFun); std::cout <<"Erase position 0's object: " <<std::endl; arr.Erase(0); std::cout <<"Done, output it: " <<std::endl; arr.Foreach(foreachFun); std::cout <<"Erase range[begin, end): " <<std::endl; arr.Erase( arr.Begin(), arr.End() ); std::cout <<"Done, output it: " <<std::endl; arr.Foreach(foreachFun); std::cout <<"Iter test done" <<std::endl; }