2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
18 * @file FBaseColArrayListT.h
19 * @brief This is the header file for the %ArrayListT class.
21 * This header file contains the declarations of the %ArrayListT class.
23 #ifndef _FBASE_COL_ARRAY_LIST_T_H_
24 #define _FBASE_COL_ARRAY_LIST_T_H_
26 #include <FBaseResult.h>
27 #include <FBaseColIListT.h>
28 #include <FBaseColIComparerT.h>
30 namespace Tizen { namespace Base { namespace Collection
33 template< class Type > class __ArrayListEnumeratorT;
37 * @brief This class provides a template-based collection of objects that can be individually accessed by an index.
41 * The %ArrayListT class provides a template-based collection of objects that can be individually accessed by an index.
43 * For more information on the class features, see <a href="../org.tizen.native.appprogramming/html/guide/base/arraylist_linkedlist.htm">ArrayList and LinkedList</a>.
45 * The following example demonstrates how to use the %ArrayListT class to create and initialize an %ArrayListT instance and to print out its values.
50 * using namespace Tizen::Base;
51 * using namespace Tizen::Base::Collection;
54 * MyClass::ArrayListTSample(void)
56 * ArrayListT< int > list;
65 * list.Add(int1); // 1
66 * list.Add(int2); // 1,2
67 * list.Add(int3); // 1,2,3
70 * for (int i = 0; i < list.GetCount(); i++)
72 * list.GetAt(i, temp);
75 * list.InsertAt(int4, 1); // 1,4,2,3
77 * ComparerT< int >* pIntComparer = new ComparerT<int>();
78 * list.Sort(*pIntComparer); // 1,2,3,4
80 * delete pIntComparer;
82 * list.Remove(int3); // 1,2,4
84 * list.RemoveAt(0); // 2,4
86 * // Uses an enumerator to access elements in the list
87 * IEnumeratorT< int >* pEnum = list.GetEnumeratorN();
88 * while (pEnum->MoveNext() == E_SUCCESS)
90 * pEnum->GetCurrent(temp);
97 template< class Type >
99 : public IListT< Type >
104 * This is the default constructor for this class.
108 * @remarks After creating an instance of this class, one of the Construct() methods must be called explicitly to initialize this instance.
120 * This destructor overrides Tizen::Base::Object::~Object().
124 virtual ~ArrayListT(void)
126 if (__pObjArray != null)
128 delete[] __pObjArray;
133 * Initializes this instance of %ArrayListT with the specified parameter.
137 * @return An error code
138 * @param[in] capacity The initial capacity of the class @n
139 * The default capacity is @c 10.
140 * @exception E_SUCCESS The method is successful.
141 * @exception E_INVALID_ARG A specified input parameter is invalid, or
142 * the specified @c capacity is negative.
143 * @remarks If the number of elements added to the list reaches its current capacity,
144 * the capacity is automatically increased by memory reallocation.
145 * Thus, if the size of the list can be estimated,
146 * specifying the initial capacity eliminates the need to perform a number of
147 * resizing operations while adding elements to the list.
150 result Construct(int capacity = DEFAULT_CAPACITY)
152 TryReturn(capacity >= 0, E_INVALID_ARG, "[%s] The capacity(%d) MUST be greater than or equal to 0.", GetErrorMessage(E_INVALID_ARG), capacity);
154 result r = SetCapacity(capacity);
155 TryReturn(r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
161 * Initializes this instance of %ArrayListT with the specified parameter. @n
162 * The capacity of the list is the same as the number of elements copied to it.
166 * @return An error code
167 * @param[in] collection A collection of elements to add
168 * @exception E_SUCCESS The method is successful.
169 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
170 * the @c collection is modified during the operation of this method.
173 result Construct(const ICollectionT< Type >& collection)
175 result r = AddItems(collection);
176 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
181 delete[] __pObjArray;
188 * Adds the specified object to the end of the list.
192 * @return An error code
193 * @param[in] obj An object to add to the list
194 * @exception E_SUCCESS The method is successful.
195 * @exception E_OUT_OF_MEMORY The memory is insufficient.
198 virtual result Add(const Type& obj)
200 if (__count >= __capacity)
202 result r = SetCapacity(__capacity + DEFAULT_CAPACITY);
203 TryReturn(r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
206 __pObjArray[__count++] = obj;
214 * Adds the elements of the specified collection to the end of the list.
218 * @return An error code
219 * @param[in] collection A collection of elements to add to the list
220 * @exception E_SUCCESS The method is successful.
221 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
222 * the @c collection is modified during the operation of this method.
225 virtual result AddItems(const ICollectionT< Type >& collection)
227 result r = E_SUCCESS;
229 IEnumeratorT< Type >* pEnum = null;
230 int count = collection.GetCount();
233 if (count > (__capacity - __count))
235 r = SetCapacity(__count + count);
236 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
239 ICollectionT< Type >* pCol = const_cast< ICollectionT< Type >* >(&collection);
240 pEnum = pCol->GetEnumeratorN();
241 TryReturn(pEnum != null, r = GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
245 while ((r = pEnum->MoveNext()) != E_OUT_OF_RANGE)
247 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
250 r = pEnum->GetCurrent(item);
251 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
253 __pObjArray[__count++] = item;
266 * Gets the elements of the list in an instance of the IEnumeratorT derived class.
270 * @return An instance of the IEnumeratorT derived class if successful, @n
271 * else @c null if an exception occurs
272 * @exception E_SUCCESS The method is successful.
273 * @exception E_OUT_OF_MEMORY The memory is insufficient.
274 * @remarks The specific error code can be accessed using the GetLastResult() method.
275 * @see Tizen::Base::Collection::IEnumeratorT
277 virtual IEnumeratorT< Type >* GetEnumeratorN(void) const
279 result r = E_SUCCESS;
281 __ArrayListEnumeratorT< Type >* pEnum = new __ArrayListEnumeratorT< Type >(*this, __modCount);
282 TryCatch(pEnum != null, r = E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
284 SetLastResult(E_SUCCESS);
293 * Gets a bidirectional enumerator (an instance of the IBidirectionalEnumeratorT derived class) of this list.
297 * @return An instance of the IBidirectionalEnumeratorT derived class, @n
298 * else @c null if an exception occurs
299 * @exception E_SUCCESS The method is successful.
300 * @exception E_OUT_OF_MEMORY The memory is insufficient.
301 * @remarks Use this method to obtain a bidirectional enumerator (an instance of the IBidirectionalEnumeratorT derived class)
302 * to iterate over a collection (an instance of the IListT derived class).
303 * @remarks The specific error code can be accessed using the GetLastResult() method.
304 * @see Tizen::Base::Collection::IBidirectionalEnumeratorT
306 virtual IBidirectionalEnumeratorT< Type >* GetBidirectionalEnumeratorN(void) const
308 result r = E_SUCCESS;
310 __ArrayListEnumeratorT< Type >* pEnum = new __ArrayListEnumeratorT< Type >(*this, __modCount);
311 TryCatch(pEnum != null, r = E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
313 SetLastResult(E_SUCCESS);
322 * Gets the object at the specified @c index of the list.
326 * @return An error code
327 * @param[in] index The index of the object to read
328 * @param[out] obj An object to get from this list
329 * @exception E_SUCCESS The method is successful.
330 * @exception E_OUT_OF_RANGE The specified @c index is outside the bounds of the data structure, or
331 * the specified @c index is either equal to or greater than the number of elements or less than @c 0.
334 virtual result GetAt(int index, Type& obj) const
336 TryReturn(index >= 0 && index < __count, E_OUT_OF_RANGE,
337 "[%s] The index(%d) MUST be greater than or equal to 0 and less than the number of elements(%d).",
338 GetErrorMessage(E_OUT_OF_RANGE), index, __count);
340 obj = __pObjArray[index];
345 * Gets the object at the specified @c index of the list.
349 * @return An error code
350 * @param[in] index The index of the object to read
351 * @param[out] obj An object to get from this list
352 * @exception E_SUCCESS The method is successful.
353 * @exception E_OUT_OF_RANGE The specified @c index is outside the bounds of the data structure, or
354 * the specified @c index is either equal to or greater than the number of elements or less than @c 0.
357 virtual result GetAt(int index, Type& obj)
359 TryReturn(index >= 0 && index < __count, E_OUT_OF_RANGE,
360 "[%s] The index(%d) MUST be greater than or equal to 0 and less than the number of elements(%d).",
361 GetErrorMessage(E_OUT_OF_RANGE), index, __count);
363 obj = __pObjArray[index];
368 * Gets a list of a specified number of elements starting from a specified index.
372 * @return An instance of the IListT derived class within the specified range of the list, @n
373 * else @c null if an exception occurs
374 * @param[in] startIndex The index to start reading elements from
375 * @param[in] count The number of elements to read
376 * @exception E_SUCCESS The method is successful.
377 * @exception E_INVALID_ARG A specified input parameter is invalid.
378 * @exception E_OUT_OF_RANGE Either of the following conditions has occurred: @n
379 * - The specified index is outside the bounds of the data structure. @n
380 * - The specified @c startIndex is either greater than or equal to the number of elements or less than @c 0. @n
381 * - The specified @c count is either greater than the number of elements starting from @c startIndex or less than @c 0.
383 * @remarks The specific error code can be accessed using the GetLastResult() method.
385 virtual IListT< Type >* GetItemsN(int startIndex, int count) const
387 result r = E_SUCCESS;
389 ArrayListT< Type >* pList = null;
391 TryCatch(startIndex >= 0 && count >= 0, r = E_OUT_OF_RANGE,
392 "[%s] Both of the startIndex(%d) and count(%d) MUST be greater than or equal to 0.", GetErrorMessage(E_OUT_OF_RANGE), startIndex, count);
393 TryCatch(startIndex < __count, r = E_OUT_OF_RANGE,
394 "[%s] The startIndex(%d) MUST be less than the number of elements(%d).", GetErrorMessage(E_OUT_OF_RANGE), startIndex, __count);
395 TryCatch(count <= __count && (startIndex + count <= __count), r = E_OUT_OF_RANGE,
396 "[%s] The startIndex(%d) + count(%d) MUST be less than or equal to the number of elements(%d).",
397 GetErrorMessage(E_OUT_OF_RANGE), startIndex, count, __count);
399 pList = new ArrayListT< Type >();
401 r = pList->Construct(count);
402 TryCatch(r == E_SUCCESS, delete pList, "[%s] Propagating.", GetErrorMessage(r));
404 for (int i = startIndex; i < (startIndex + count); i++)
406 pList->__pObjArray[pList->__count++] = __pObjArray[i];
409 SetLastResult(E_SUCCESS);
418 * Searches for an object in this list. @n
419 * Gets the index of the object if found.
423 * @return An error code
424 * @param[in] obj The object to locate
425 * @param[out] index The index of the object
426 * @exception E_SUCCESS The method is successful.
427 * @exception E_OBJ_NOT_FOUND The specified @c obj is not found.
430 virtual result IndexOf(const Type& obj, int& index) const
432 return IndexOf(obj, 0, __count, index);
436 * Searches for an object starting from the specified @c index. @n
437 * Gets the index of the object if found.
441 * @return An error code
442 * @param[in] obj The object to locate
443 * @param[in] startIndex The starting index for the search @n
444 * It must be less than the number of elements.
445 * @param[out] index The index of the object
446 * @exception E_SUCCESS The method is successful.
447 * @exception E_OUT_OF_RANGE The specified @c index is outside the bounds of the data structure, or
448 * the specified @c startIndex is either equal to or greater than the number of elements or less than @c 0.
449 * @exception E_OBJ_NOT_FOUND The specified @c obj is not found.
452 virtual result IndexOf(const Type& obj, int startIndex, int& index) const
454 TryReturn(startIndex >= 0 && startIndex < __count, E_OUT_OF_RANGE,
455 "[%s] The startIndex(%d) MUST be greater than or equal to 0, and less than the number of elements(%d).",
456 GetErrorMessage(E_OUT_OF_RANGE), startIndex, __count);
458 return IndexOf(obj, startIndex, (__count - startIndex), index);
462 * Searches for an object within the specified range. @n
463 * Gets the index of the object if found.
467 * @return An error code
468 * @param[in] obj The object to locate
469 * @param[in] startIndex The starting index of the range
470 * @param[in] count The number of elements to read
471 * @param[out] index The index of the object
472 * @exception E_SUCCESS The method is successful.
473 * @exception E_OUT_OF_RANGE Either of the following conditions has occurred: @n
474 * - The specified @c index is outside the bounds of the data structure. @n
475 * - The specified @c startIndex is either greater than or equal to the number of elements or less than @c 0. @n
476 * - The specified @c count is either greater than the number of elements starting from @c startIndex or less than @c 0.
477 * @exception E_OBJ_NOT_FOUND The specified @c obj is not found.
480 virtual result IndexOf(const Type& obj, int startIndex, int count, int& index) const
482 TryReturn(startIndex >= 0 && count >= 0, E_OUT_OF_RANGE,
483 "[%s] Both of the startIndex(%d) and count(%d) MUST be greater than or equal to 0.", GetErrorMessage(E_OUT_OF_RANGE), startIndex, count);
484 TryReturn(startIndex < __count, E_OUT_OF_RANGE,
485 "[%s] The startIndex(%d) MUST be less than the number of elements(%d).", GetErrorMessage(E_OUT_OF_RANGE), startIndex, __count);
486 TryReturn(count <= __count && (startIndex + count <= __count), E_OUT_OF_RANGE,
487 "[%s] The startIndex(%d) + count(%d) MUST be less than or equal to the number of elements(%d).",
488 GetErrorMessage(E_OUT_OF_RANGE), startIndex, count, __count);
490 int arrayListCount = startIndex + count;
491 for (int i = startIndex; i < arrayListCount; i++)
493 if (obj == __pObjArray[i])
500 return E_OBJ_NOT_FOUND;
504 * Inserts an object at a specified location.
508 * @return An error code
509 * @param[in] obj The object to insert
510 * @param[in] index The index at which the object must be inserted
511 * @exception E_SUCCESS The method is successful.
512 * @exception E_OUT_OF_RANGE The specified @c index is outside the bounds of the data structure, or
513 * the @c index is greater than the number of elements or less than @c 0.
514 * @remarks The elements that follow the insertion point move down to accommodate the new element.
515 * If the @c index equals the number of elements in the list, the new element
516 * is added at the end of the list.
520 virtual result InsertAt(const Type& obj, int index)
522 TryReturn(index >= 0 && index <= __count, E_OUT_OF_RANGE,
523 "[%s] The index(%d) MUST be greater than or equal to 0, and less than or equal to the number of elements(%d).",
524 GetErrorMessage(E_OUT_OF_RANGE), index, __count);
526 result r = E_SUCCESS;
528 if (__count >= __capacity)
530 r = SetCapacity(__capacity + DEFAULT_CAPACITY);
531 TryReturn(r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
534 for (int i = __count; i > index; i--)
536 __pObjArray[i] = __pObjArray[i - 1];
541 __pObjArray[index] = obj;
547 * Inserts the elements of a collection at a specified location.
551 * @return An error code
552 * @param[in] collection The collection to insert
553 * @param[in] startIndex The index from which the collection must be inserted
554 * @exception E_SUCCESS The method is successful.
555 * @exception E_OUT_OF_RANGE The specified index is outside the bounds of the data structure, or
556 * the @c startIndex is greater than the number of elements or less than @c 0.
557 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
558 * the @c collection is modified during the operation of this method.
559 * @remarks The elements that follow the insertion point move down to accommodate the new elements.
560 * If the @c startIndex equals the number of elements in the list, the new elements
561 * are added at the end of the list.
565 virtual result InsertItemsFrom(const ICollectionT< Type >& collection, int startIndex)
567 TryReturn(startIndex >= 0 && startIndex <= __count, E_OUT_OF_RANGE,
568 "[%s] The startIndex(%d) MUST be greater than or equal to 0, and less than or equal to the number of elements(%d).",
569 GetErrorMessage(E_OUT_OF_RANGE), startIndex, __count);
571 result r = E_SUCCESS;
573 IEnumeratorT< Type >* pEnum = null;
574 int count = collection.GetCount();
578 if (count > (__capacity - __count))
580 r = SetCapacity(__count + count);
581 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
585 for (int i = (__count - 1); i >= (startIndex + count); i--)
587 __pObjArray[i] = __pObjArray[i - count];
590 ICollectionT< Type >* pCol = const_cast< ICollectionT< Type >* >(&collection);
591 pEnum = pCol->GetEnumeratorN();
592 TryReturn(pEnum != null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
596 while ((r = pEnum->MoveNext()) != E_OUT_OF_RANGE)
600 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
602 r = pEnum->GetCurrent(item);
603 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
605 __pObjArray[startIndex++] = item;
618 * Searches for the last occurrence of an object in this list. @n
619 * Gets the index of the object if found.
623 * @return An error code
624 * @param[in] obj The object to locate
625 * @param[out] index The index of the last occurrence of the specified object
626 * @exception E_SUCCESS The method is successful.
627 * @exception E_OBJ_NOT_FOUND The specified @c obj is not found.
630 virtual result LastIndexOf(const Type& obj, int& index) const
632 for (int i = (__count - 1); i >= 0; i--)
634 if (obj == __pObjArray[i])
641 return E_OBJ_NOT_FOUND;
645 * Removes the first occurrence of a specified object.
649 * @return An error code
650 * @param[in] obj The object to remove
651 * @exception E_SUCCESS The method is successful.
652 * @exception E_OBJ_NOT_FOUND The specified @c obj is not found.
657 virtual result Remove(const Type& obj)
660 result r = IndexOf(obj, index);
663 return E_OBJ_NOT_FOUND;
667 return RemoveAt(index);
672 * Removes all the elements of a specified collection from the list.
676 * @return An error code
677 * @param[in] collection The collection to remove from this list
678 * @exception E_SUCCESS The method is successful.
679 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
680 * the @c collection is modified during the operation of this method.
684 virtual result RemoveItems(const ICollectionT< Type >& collection)
686 result r = E_SUCCESS;
687 int oldCount = __count;
689 ICollectionT< Type >* pCol = const_cast< ICollectionT< Type >* >(&collection);
690 IEnumeratorT< Type >* pEnum = pCol->GetEnumeratorN();
691 TryCatch(pEnum != null, r = GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
693 while ((r = pEnum->MoveNext()) != E_OUT_OF_RANGE)
697 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
699 r = pEnum->GetCurrent(item);
700 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
703 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
706 if (__count < oldCount)
720 * Removes an object from a specified location.
724 * @return An error code
725 * @param[in] index The index of the object that is to remove
726 * @exception E_SUCCESS The method is successful.
727 * @exception E_OUT_OF_RANGE The specified @c index is outside the bounds of the data structure, or
728 * the specified @c index is greater than or equal to the number of elements or less than @c 0.
729 * @remarks The elements that follow the deleted object move up the list to occupy the empty location.
733 virtual result RemoveAt(int index)
735 TryReturn(index < __count && index >= 0, E_OUT_OF_RANGE,
736 "[%s] The index MUST be greater than or equal to 0, and less than the number of elements(%d).",
737 GetErrorMessage(E_OUT_OF_RANGE), index, __count);
742 for (int i = index; i < __count; i++)
744 __pObjArray[i] = __pObjArray[i + 1];
753 * Removes all the elements within a specified range.
757 * @return An error code
758 * @param[in] startIndex The starting index of the range
759 * @param[in] count The number of elements to remove
760 * @exception E_SUCCESS The method is successful.
761 * @exception E_OUT_OF_RANGE Either of the following conditions has occurred: @n
762 * - The specified index is outside the bounds of the data structure. @n
763 * - The specified @c startIndex is either greater than or equal to the number of elements or less than @c 0. @n
764 * - The specified @c count is either greater than the number of elements starting from @c startIndex or less than @c 0.
765 * @remarks The elements that follow the deleted elements move up the list to occupy the empty locations.
767 * @see InsertItemsFrom()
769 virtual result RemoveItems(int startIndex, int count)
771 TryReturn(startIndex >= 0 && count >= 0, E_OUT_OF_RANGE,
772 "[%s] Both of the startIndex(%d) and count(%d) MUST be greater than or equal to 0.", GetErrorMessage(E_OUT_OF_RANGE), startIndex, count);
773 TryReturn(startIndex < __count, E_OUT_OF_RANGE,
774 "[%s] The startIndex(%d) MUST be less than the number of elements(%d).", GetErrorMessage(E_OUT_OF_RANGE), startIndex, __count);
775 TryReturn(count <= __count && (startIndex + count <= __count), E_OUT_OF_RANGE,
776 "[%s] The startIndex(%d) + count(%d) MUST be less than or equal to the number of elements(%d).",
777 GetErrorMessage(E_OUT_OF_RANGE), startIndex, count, __count);
781 Type* newArray = new Type[__capacity];
782 TryReturn(newArray != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
787 for (int i = 0; i < startIndex; i++)
789 newArray[i] = __pObjArray[i];
791 for (int i = startIndex; i < __count; i++)
793 newArray[i] = __pObjArray[i + count];
796 delete[] __pObjArray;
797 __pObjArray = newArray;
804 * Removes all elements in the list.
808 virtual void RemoveAll(void)
812 delete[] __pObjArray;
824 * Sets the object at a specified @c index of the current instance of ByteBuffer with the specified object.
828 * @return An error code
829 * @param[in] obj The object to set
830 * @param[in] index The index at which the object must be set
831 * @exception E_SUCCESS The method is successful.
832 * @exception E_OUT_OF_RANGE The specified @c index is outside the bounds of the data structure, or
833 * the specified @c index is either equal to or greater than the number of elements or less than @c 0.
836 virtual result SetAt(const Type& obj, int index)
838 TryReturn(index >= 0 && index < __count, E_OUT_OF_RANGE,
839 "[%s] The index(%d) MUST be greater than or equal to 0, less than the number of elements(%d).",
840 GetErrorMessage(E_OUT_OF_RANGE), index, __count);
844 __pObjArray[index] = obj;
850 * Sets the capacity of the list to a specified value.
854 * @return An error code
855 * @param[in] newCapacity The new capacity to set for the list
856 * @exception E_SUCCESS The method is successful.
857 * @exception E_INVALID_ARG A specified input parameter is invalid, or
858 * the @c newCapacity is negative.
859 * @remarks If the new capacity is less than the current capacity, the memory
860 * is truncated and the elements within the truncated memory are destroyed.
865 virtual result SetCapacity(int newCapacity)
867 TryReturn(newCapacity >= 0, E_INVALID_ARG, "[%s] The newCapacity(%d) MUST be greater than or equal to 0.",
868 GetErrorMessage(E_INVALID_ARG), newCapacity);
870 result r = E_SUCCESS;
871 if (__capacity != newCapacity)
873 Type* newArray = null;
876 newArray = new Type[newCapacity];
877 TryCatch(newArray != null, r = E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
879 if (__pObjArray != null)
881 int count = __count < newCapacity ? __count : newCapacity;
882 for (int i = 0; i < count; i++)
884 newArray[i] = __pObjArray[i];
888 if (__pObjArray != null)
890 delete[] __pObjArray;
892 if (__count > newCapacity)
895 __count = newCapacity;
897 __pObjArray = newArray;
898 __capacity = newCapacity;
908 * Sorts the elements in the list using a comparer.
912 * @return An error code
913 * @param[in] comparer A pointer to IComparerT
914 * @exception E_SUCCESS The method is successful.
915 * @exception E_INVALID_ARG A specified input parameter is invalid, or
916 * the @c comparer is not valid.
918 virtual result Sort(const IComparerT< Type >& comparer)
923 __pComparer = const_cast< IComparerT< Type >* >(&comparer);
924 result r = QuickSort(0, (__count - 1));
927 AppLogException("[%s] Propagating.", GetErrorMessage(r));
937 * Trims the capacity of a list to the actual number of elements in the list.
941 * @return An error code
942 * @exception E_SUCCESS The method is successful.
943 * @exception E_OUT_OF_MEMORY The memory is insufficient.
944 * @remarks The specific error code can be accessed using the GetLastResult() method.
947 virtual void Trim(void)
954 result r = SetCapacity(__count);
957 AppLogException("[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
962 * Gets the current capacity of the list.
966 * @return The current capacity of the list
969 int GetCapacity(void) const
975 * Gets the number of objects currently stored in the list.
979 * @return The number of objects stored in the list
981 virtual int GetCount(void) const
987 * Checks whether a list contains the specified object.
991 * @return @c true if the object is present in the list, @n
993 * @param[in] obj The object to locate
996 virtual bool Contains(const Type& obj) const
1003 for (int i = 0; i < __count; i++)
1005 if (obj == __pObjArray[i])
1015 * Checks whether the list contains all the elements of the specified @c collection.
1019 * @return An error code
1020 * @param[in] collection The collection to check for in the list
1021 * @param[out] out @c true if the list contains all the elements of the specified @c collection, @n
1023 * @exception E_SUCCESS The method is successful.
1024 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
1025 * the @c collection is modified during the operation of this method.
1026 * @remarks If the given @c collection is empty, the @c out parameter will be set to @c true.
1029 virtual result ContainsAll(const ICollectionT< Type >& collection, bool& out) const
1031 result r = E_SUCCESS;
1034 if (collection.GetCount() == 0)
1040 ICollectionT< Type >* pCol = const_cast< ICollectionT< Type >* >(&collection);
1041 IEnumeratorT< Type >* pEnum = pCol->GetEnumeratorN();
1042 TryReturn(pEnum != null, r = GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1044 while ((r = pEnum->MoveNext()) != E_OUT_OF_RANGE)
1048 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
1050 r = pEnum->GetCurrent(item);
1051 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
1053 if (false == Contains(item))
1075 * Compares two instances of the %ArrayListT class.
1079 * @return @c true if the two instances match, @n
1081 * @param[in] obj The object to compare with the current instance
1082 * @remarks This method returns @c true if and only if the two instances contain the same elements in the same order.
1084 virtual bool Equals(const Tizen::Base::Object& obj) const
1086 const ArrayListT< Type >* other = dynamic_cast< const ArrayListT< Type >* >(&obj);
1091 else if (other == this)
1095 else if (__count != other->__count)
1101 for (int i = 0; i < __count; i++)
1103 if (__pObjArray[i] != other->__pObjArray[i])
1114 * Gets the hash value of the current instance.
1118 * @return The hash value of the current instance
1119 * @remarks The two Tizen::Base::Object::Equals() instances must return the same hash value. For better performance, @n
1120 * the used hash function must generate a random distribution for all inputs.
1122 virtual int GetHashCode(void) const
1125 for (int i = 0; i < __count; i++)
1127 if (&(__pObjArray[i]) != null)
1129 hash += reinterpret_cast< int >(&(__pObjArray[i]));
1137 * The implementation of this copy constructor is intentionally blank and declared as private to prohibit copying of objects.
1139 * @param[in] list The instance of the %ArrayListT class to copy from
1141 ArrayListT(const ArrayListT< Type >& list);
1144 * The implementation of this copy assignment operator is intentionally blank and declared as private to prohibit copying of objects.
1146 * @param[in] list An instance of %ArrayListT
1148 ArrayListT< Type >& operator =(const ArrayListT< Type >& list);
1151 * Sorts a section of a list using a comparer.
1153 * @return An error code
1154 * @param[in] startIndex The start index of the section of the list
1155 * @param[in] endIndex The end index of the section of the list
1156 * @exception E_SUCCESS The method is successful.
1157 * @exception E_INVALID_ARG A specified input parameter is invalid, or
1158 * the comparer has failed to compare the elements.
1160 result QuickSort(int startIndex, int endIndex)
1162 result r = E_SUCCESS;
1164 if (startIndex < endIndex)
1167 int i = startIndex - 1;
1168 int j = endIndex + 1;
1171 int compareResult = 1;
1172 while ((compareResult > 0) && (j > static_cast< int >(startIndex)))
1176 r = __pComparer->Compare(__pObjArray[j], __pObjArray[startIndex], compareResult);
1177 TryReturn(r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
1181 while ((compareResult < 0) && (i < static_cast< int >(endIndex)))
1185 r = __pComparer->Compare(__pObjArray[i], __pObjArray[startIndex], compareResult);
1186 TryReturn(r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
1191 Type temp = __pObjArray[j];
1192 __pObjArray[j] = __pObjArray[i];
1193 __pObjArray[i] = temp;
1202 r = QuickSort(startIndex, middleIndex);
1203 TryReturn(r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
1205 r = QuickSort(middleIndex + 1, endIndex);
1206 TryReturn(r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
1216 IComparerT< Type >* __pComparer;
1217 static const int DEFAULT_CAPACITY = 10;
1219 friend class __ArrayListEnumeratorT< Type >;
1224 // @class __ArrayListEnumeratorT
1225 // @brief This class is an implementation of the IEnumeratorT interface for the %ArrayListT class.
1228 template< class Type >
1229 class __ArrayListEnumeratorT
1230 : public IBidirectionalEnumeratorT< Type >
1231 , public Tizen::Base::Object
1234 __ArrayListEnumeratorT(const ArrayListT< Type >& list, int modCount)
1236 , __modCount(modCount)
1241 virtual ~__ArrayListEnumeratorT(void)
1245 virtual result GetCurrent(Type& obj) const
1247 TryReturn((__modCount == __list.__modCount), E_INVALID_OPERATION,
1248 "[%s] The source collection is modified after the creation of this enumerator.", GetErrorMessage(E_INVALID_OPERATION));
1249 TryReturn((__position > -1) && (__position < static_cast< int >(__list.__count)), E_INVALID_OPERATION,
1250 "[%s] Current position(%d) is before the first element or past the last element.", GetErrorMessage(E_INVALID_OPERATION), __position);
1252 obj = __list.__pObjArray[__position];
1256 virtual result MoveNext(void)
1258 TryReturn((__modCount == __list.__modCount), E_INVALID_OPERATION,
1259 "[%s] The source collection is modified after the creation of this enumerator.", GetErrorMessage(E_INVALID_OPERATION));
1261 if ((__position + 1) >= static_cast< int >(__list.__count))
1263 return E_OUT_OF_RANGE;
1273 virtual result Reset(void)
1275 TryReturn((__modCount == __list.__modCount), E_INVALID_OPERATION,
1276 "[%s] The source collection is modified after the creation of this enumerator.", GetErrorMessage(E_INVALID_OPERATION));
1282 virtual result MovePrevious(void)
1284 TryReturn(__modCount == __list.__modCount, E_INVALID_OPERATION,
1285 "[%s] The source collection is modified after the creation of this enumerator.", GetErrorMessage(E_INVALID_OPERATION));
1287 TryReturn(__position > 0, E_OUT_OF_RANGE, "[%s] Reached start of the list, no previous element", GetErrorMessage(E_OUT_OF_RANGE));
1294 virtual result ResetLast(void)
1296 TryReturn(__modCount == __list.__modCount, E_INVALID_OPERATION,
1297 "[%s] The source collection is modified after the creation of this enumerator.", GetErrorMessage(E_INVALID_OPERATION));
1299 __position = __list.__count;
1304 const ArrayListT< Type >& __list;
1308 }; //__ArrayListEnumeratorT
1310 } } } // Tizen::Base::Collection
1312 #endif // _FBASE_COL_ARRAY_LIST_T_H_