2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
19 * @file FBaseColArrayListT.h
20 * @brief This is the header file for the %ArrayListT class.
22 * This header file contains the declarations of the %ArrayListT class.
24 #ifndef _FBASE_COL_ARRAY_LIST_T_H_
25 #define _FBASE_COL_ARRAY_LIST_T_H_
27 #include <FBaseResult.h>
28 #include <FBaseColIListT.h>
29 #include <FBaseColIComparerT.h>
32 namespace Tizen { namespace Base { namespace Collection
35 template< class Type > class __ArrayListEnumeratorT;
39 * @brief This class provides a template-based collection of objects that can be individually accessed by an index.
43 * The %ArrayListT class provides a template-based collection of objects that can be individually accessed by an index.
45 * For more information on the class features, see <a href="../org.tizen.native.appprogramming/html/guide/base/arraylist_linkedlist.htm">ArrayList and LinkedList</a>.
47 * The following example demonstrates how to use the %ArrayListT class to create and initialize an %ArrayListT instance and to print out its values.
52 * using namespace Tizen::Base;
53 * using namespace Tizen::Base::Collection;
56 * MyClass::ArrayListTSample(void)
58 * ArrayListT<int> list;
67 * list.Add(int1); // 1
68 * list.Add(int2); // 1,2
69 * list.Add(int3); // 1,2,3
72 * for (int i = 0; i < list.GetCount(); i++)
74 * list.GetAt(i, temp);
77 * list.InsertAt(int4, 1); // 1,4,2,3
79 * ComparerT<int>* pIntComparer = new ComparerT<int>();
80 * list.Sort(*pIntComparer); // 1,2,3,4
82 * delete pIntComparer;
84 * list.Remove(int3); // 1,2,4
86 * list.RemoveAt(0); // 2,4
88 * // Uses an enumerator to access elements in the list
89 * IEnumeratorT<int>* pEnum = list.GetEnumeratorN();
90 * while (pEnum->MoveNext() == E_SUCCESS)
92 * pEnum->GetCurrent(temp);
99 template< class Type >
101 : public IListT< Type >
106 * This is the default constructor for this class.
110 * @remarks After creating an instance of this class, one of the Construct() methods must be called explicitly to initialize this instance.
122 * This destructor overrides Tizen::Base::Object::~Object().
126 virtual ~ArrayListT(void)
128 if (__pObjArray != null)
130 delete[] __pObjArray;
135 * Initializes this instance of %ArrayListT with the specified parameter.
139 * @return An error code
140 * @param[in] capacity The initial capacity of the class @n
141 * The default capacity is @c 10.
142 * @exception E_SUCCESS The method is successful.
143 * @exception E_INVALID_ARG A specified input parameter is invalid, or
144 * the specified @c capacity is negative.
145 * @remarks If the number of elements added to the list reaches its current capacity,
146 * the capacity is automatically increased by memory reallocation.
147 * Thus, if the size of the list can be estimated,
148 * specifying the initial capacity eliminates the need to perform a number of
149 * resizing operations while adding elements to the list.
152 result Construct(int capacity = DEFAULT_CAPACITY)
154 TryReturn(capacity >= 0, E_INVALID_ARG, "[%s] The capacity(%d) MUST be greater than or equal to 0.", GetErrorMessage(E_INVALID_ARG), capacity);
156 result r = SetCapacity(capacity);
157 TryReturn(r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
163 * Initializes this instance of %ArrayListT with the specified parameter. @n
164 * The capacity of the list is the same as the number of elements copied to it.
168 * @return An error code
169 * @param[in] collection A collection of elements to add
170 * @exception E_SUCCESS The method is successful.
171 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
172 * the @c collection is modified during the operation of this method.
175 result Construct(const ICollectionT< Type >& collection)
177 result r = AddItems(collection);
178 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
183 delete[] __pObjArray;
190 * Adds the specified object to the end of the list.
194 * @return An error code
195 * @param[in] obj An object to add to the list
196 * @exception E_SUCCESS The method is successful.
197 * @exception E_OUT_OF_MEMORY The memory is insufficient.
200 virtual result Add(const Type& obj)
202 if (__count >= __capacity)
204 result r = SetCapacity(__capacity + DEFAULT_CAPACITY);
205 TryReturn(r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
208 __pObjArray[__count++] = obj;
216 * Adds the elements of the specified collection to the end of the list.
220 * @return An error code
221 * @param[in] collection A collection of elements to add to the list
222 * @exception E_SUCCESS The method is successful.
223 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
224 * the @c collection is modified during the operation of this method.
227 virtual result AddItems(const ICollectionT< Type >& collection)
229 result r = E_SUCCESS;
231 IEnumeratorT< Type >* pEnum = null;
232 int count = collection.GetCount();
235 if (count > (__capacity - __count))
237 r = SetCapacity(__count + count);
238 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
241 ICollectionT< Type >* pCol = const_cast< ICollectionT< Type >* >(&collection);
242 pEnum = pCol->GetEnumeratorN();
243 TryReturn(pEnum != null, r = GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
247 while ((r = pEnum->MoveNext()) != E_OUT_OF_RANGE)
249 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
252 r = pEnum->GetCurrent(item);
253 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
255 __pObjArray[__count++] = item;
268 * Gets the elements of the list in an instance of the IEnumeratorT derived class.
272 * @return An instance of the IEnumeratorT derived class if successful, @n
273 * else @c null if an exception occurs
274 * @exception E_SUCCESS The method is successful.
275 * @exception E_OUT_OF_MEMORY The memory is insufficient.
276 * @remarks The specific error code can be accessed using the GetLastResult() method.
277 * @see Tizen::Base::Collection::IEnumeratorT
279 virtual IEnumeratorT< Type >* GetEnumeratorN(void) const
281 result r = E_SUCCESS;
283 __ArrayListEnumeratorT< Type >* pEnum = new __ArrayListEnumeratorT< Type >(*this, __modCount);
284 TryCatch(pEnum != null, r = E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
286 SetLastResult(E_SUCCESS);
295 * Gets a bidirectional enumerator (an instance of the IBidirectionalEnumeratorT derived class) of this list.
299 * @return An instance of the IBidirectionalEnumeratorT derived class, @n
300 * else @c null if an exception occurs
301 * @exception E_SUCCESS The method is successful.
302 * @exception E_OUT_OF_MEMORY The memory is insufficient.
303 * @remarks Use this method to obtain a bidirectional enumerator (an instance of the IBidirectionalEnumeratorT derived class)
304 * to iterate over a collection (an instance of the IListT derived class).
305 * @remarks The specific error code can be accessed using the GetLastResult() method.
306 * @see Tizen::Base::Collection::IBidirectionalEnumeratorT
308 virtual IBidirectionalEnumeratorT< Type >* GetBidirectionalEnumeratorN(void) const
310 result r = E_SUCCESS;
312 __ArrayListEnumeratorT< Type >* pEnum = new __ArrayListEnumeratorT< Type >(*this, __modCount);
313 TryCatch(pEnum != null, r = E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
315 SetLastResult(E_SUCCESS);
324 * Gets the object at the specified index of the list.
328 * @return An error code
329 * @param[in] index The index of the object to read
330 * @param[out] obj An object to get from this list
331 * @exception E_SUCCESS The method is successful.
332 * @exception E_OUT_OF_RANGE The specified index is outside the bounds of the data structure, or
333 * the specified @c index is either equal to or greater than the number of elements or less than @c 0.
336 virtual result GetAt(int index, Type& obj) const
338 TryReturn(index >= 0 && index < __count, E_OUT_OF_RANGE,
339 "[%s] The index(%d) MUST be greater than or equal to 0 and less than the number of elements(%d).",
340 GetErrorMessage(E_OUT_OF_RANGE), index, __count);
342 obj = __pObjArray[index];
347 * Gets the object at the specified index of the list.
351 * @return An error code
352 * @param[in] index The index of the object to read
353 * @param[out] obj An object to get from this list
354 * @exception E_SUCCESS The method is successful.
355 * @exception E_OUT_OF_RANGE The specified index is outside the bounds of the data structure, or
356 * the specified @c index is either equal to or greater than the number of elements or less than @c 0.
359 virtual result GetAt(int index, Type& obj)
361 TryReturn(index >= 0 && index < __count, E_OUT_OF_RANGE,
362 "[%s] The index(%d) MUST be greater than or equal to 0 and less than the number of elements(%d).",
363 GetErrorMessage(E_OUT_OF_RANGE), index, __count);
365 obj = __pObjArray[index];
370 * Gets a list of a specified number of elements starting from a specified index.
374 * @return An instance of the IListT derived class within the specified range of the list, @n
375 * else @c null if an exception occurs
376 * @param[in] startIndex The index to start reading elements from
377 * @param[in] count The number of elements to read
378 * @exception E_SUCCESS The method is successful.
379 * @exception E_INVALID_ARG A specified input parameter is invalid.
380 * @exception E_OUT_OF_RANGE Either of the following conditions has occurred: @n
381 * - The specified index is outside the bounds of the data structure. @n
382 * - The specified @c startIndex is either greater than or equal to the number of elements or less than @c 0. @n
383 * - The specified @c count is either greater than the number of elements starting from @c startIndex or less than @c 0.
385 * @remarks The specific error code can be accessed using the GetLastResult() method.
387 virtual IListT< Type >* GetItemsN(int startIndex, int count) const
389 result r = E_SUCCESS;
391 ArrayListT< Type >* pList = null;
393 TryCatch(startIndex >= 0 && count >= 0, r = E_OUT_OF_RANGE,
394 "[%s] Both of the startIndex(%d) and count(%d) MUST be greater than or equal to 0.", GetErrorMessage(E_OUT_OF_RANGE), startIndex, count);
395 TryCatch(startIndex < __count, r = E_OUT_OF_RANGE,
396 "[%s] The startIndex(%d) MUST be less than the number of elements(%d).", GetErrorMessage(E_OUT_OF_RANGE), startIndex, __count);
397 TryCatch(count <= __count && (startIndex + count <= __count), r = E_OUT_OF_RANGE,
398 "[%s] The startIndex(%d) + count(%d) MUST be less than or equal to the number of elements(%d).",
399 GetErrorMessage(E_OUT_OF_RANGE), startIndex, count, __count);
401 pList = new ArrayListT< Type >();
403 r = pList->Construct(count);
404 TryCatch(r == E_SUCCESS, delete pList, "[%s] Propagating.", GetErrorMessage(r));
406 for (int i = startIndex; i < (startIndex + count); i++)
408 pList->__pObjArray[pList->__count++] = __pObjArray[i];
411 SetLastResult(E_SUCCESS);
420 * Searches for an object in this list. @n
421 * Gets the index of the object if found.
425 * @return An error code
426 * @param[in] obj The object to locate
427 * @param[out] index The index of the object
428 * @exception E_SUCCESS The method is successful.
429 * @exception E_OBJ_NOT_FOUND The specified @c obj is not found.
432 virtual result IndexOf(const Type& obj, int& index) const
434 return IndexOf(obj, 0, __count, index);
438 * Searches for an object starting from the specified index. @n
439 * Gets the index of the object if found.
443 * @return An error code
444 * @param[in] obj The object to locate
445 * @param[in] startIndex The starting index for the search @n
446 * It must be less than the number of elements.
447 * @param[out] index The index of the object
448 * @exception E_SUCCESS The method is successful.
449 * @exception E_OUT_OF_RANGE The specified index is outside the bounds of the data structure, or
450 * the specified @c startIndex is either equal to or greater than the number of elements or less than @c 0.
451 * @exception E_OBJ_NOT_FOUND The specified @c obj is not found.
454 virtual result IndexOf(const Type& obj, int startIndex, int& index) const
456 TryReturn(startIndex >= 0 && startIndex < __count, E_OUT_OF_RANGE,
457 "[%s] The startIndex(%d) MUST be greater than or equal to 0, and less than the number of elements(%d).",
458 GetErrorMessage(E_OUT_OF_RANGE), startIndex, __count);
460 return IndexOf(obj, startIndex, (__count - startIndex), index);
464 * Searches for an object within the specified range. @n
465 * Gets the index of the object if found.
469 * @return An error code
470 * @param[in] obj The object to locate
471 * @param[in] startIndex The starting index of the range
472 * @param[in] count The number of elements to read
473 * @param[out] index The index of the object
474 * @exception E_SUCCESS The method is successful.
475 * @exception E_OUT_OF_RANGE Either of the following conditions has occurred: @n
476 * - The specified index is outside the bounds of the data structure. @n
477 * - The specified @c startIndex is either greater than or equal to the number of elements or less than @c 0. @n
478 * - The specified @c count is either greater than the number of elements starting from @c startIndex or less than @c 0.
479 * @exception E_OBJ_NOT_FOUND The specified @c obj is not found.
482 virtual result IndexOf(const Type& obj, int startIndex, int count, int& index) const
484 TryReturn(startIndex >= 0 && count >= 0, E_OUT_OF_RANGE,
485 "[%s] Both of the startIndex(%d) and count(%d) MUST be greater than or equal to 0.", GetErrorMessage(E_OUT_OF_RANGE), startIndex, count);
486 TryReturn(startIndex < __count, E_OUT_OF_RANGE,
487 "[%s] The startIndex(%d) MUST be less than the number of elements(%d).", GetErrorMessage(E_OUT_OF_RANGE), startIndex, __count);
488 TryReturn(count <= __count && (startIndex + count <= __count), E_OUT_OF_RANGE,
489 "[%s] The startIndex(%d) + count(%d) MUST be less than or equal to the number of elements(%d).",
490 GetErrorMessage(E_OUT_OF_RANGE), startIndex, count, __count);
492 int arrayListCount = startIndex + count;
493 for (int i = startIndex; i < arrayListCount; i++)
495 if (obj == __pObjArray[i])
502 return E_OBJ_NOT_FOUND;
506 * Inserts an object at a specified location.
510 * @return An error code
511 * @param[in] obj The object to insert
512 * @param[in] index The index at which the object must be inserted
513 * @exception E_SUCCESS The method is successful.
514 * @exception E_OUT_OF_RANGE The specified index is outside the bounds of the data structure, or
515 * the @c index is greater than the number of elements or less than @c 0.
516 * @remarks The elements that follow the insertion point move down to accommodate the new element.
517 * If the @c index equals the number of elements in the list, the new element
518 * is added at the end of the list.
522 virtual result InsertAt(const Type& obj, int index)
524 TryReturn(index >= 0 && index <= __count, E_OUT_OF_RANGE,
525 "[%s] The index(%d) MUST be greater than or equal to 0, and less than or equal to the number of elements(%d).",
526 GetErrorMessage(E_OUT_OF_RANGE), index, __count);
528 result r = E_SUCCESS;
530 if (__count >= __capacity)
532 r = SetCapacity(__capacity + DEFAULT_CAPACITY);
533 TryReturn(r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
536 for (int i = __count; i > index; i--)
538 __pObjArray[i] = __pObjArray[i - 1];
543 __pObjArray[index] = obj;
549 * Inserts the elements of a collection at a specified location.
553 * @return An error code
554 * @param[in] collection The collection to insert
555 * @param[in] startIndex The index from which the collection must be inserted
556 * @exception E_SUCCESS The method is successful.
557 * @exception E_OUT_OF_RANGE The specified index is outside the bounds of the data structure, or
558 * the @c startIndex is greater than the number of elements or less than @c 0.
559 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
560 * the @c collection is modified during the operation of this method.
561 * @remarks The elements that follow the insertion point move down to accommodate the new elements.
562 * If the @c startIndex equals the number of elements in the list, the new elements
563 * are added at the end of the list.
567 virtual result InsertItemsFrom(const ICollectionT< Type >& collection, int startIndex)
569 TryReturn(startIndex >= 0 && startIndex <= __count, E_OUT_OF_RANGE,
570 "[%s] The startIndex(%d) MUST be greater than or equal to 0, and less than or equal to the number of elements(%d).",
571 GetErrorMessage(E_OUT_OF_RANGE), startIndex, __count);
573 result r = E_SUCCESS;
575 IEnumeratorT< Type >* pEnum = null;
576 int count = collection.GetCount();
580 if (count > (__capacity - __count))
582 r = SetCapacity(__count + count);
583 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
587 for (int i = (__count - 1); i >= (startIndex + count); i--)
589 __pObjArray[i] = __pObjArray[i - count];
592 ICollectionT< Type >* pCol = const_cast< ICollectionT< Type >* >(&collection);
593 pEnum = pCol->GetEnumeratorN();
594 TryReturn(pEnum != null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
598 while ((r = pEnum->MoveNext()) != E_OUT_OF_RANGE)
602 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
604 r = pEnum->GetCurrent(item);
605 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
607 __pObjArray[startIndex++] = item;
620 * Searches for the last occurrence of an object in this list. @n
621 * Gets the index of the object if found.
625 * @return An error code
626 * @param[in] obj The object to locate
627 * @param[out] index The index of the last occurrence of the specified object
628 * @exception E_SUCCESS The method is successful.
629 * @exception E_OBJ_NOT_FOUND The specified @c obj is not found.
632 virtual result LastIndexOf(const Type& obj, int& index) const
634 for (int i = (__count - 1); i >= 0; i--)
636 if (obj == __pObjArray[i])
643 return E_OBJ_NOT_FOUND;
647 * Removes the first occurrence of a specified object.
651 * @return An error code
652 * @param[in] obj The object to remove
653 * @exception E_SUCCESS The method is successful.
654 * @exception E_OBJ_NOT_FOUND The specified @c obj is not found.
659 virtual result Remove(const Type& obj)
662 result r = IndexOf(obj, index);
665 return E_OBJ_NOT_FOUND;
669 return RemoveAt(index);
674 * Removes all the elements of a specified collection from the list.
678 * @return An error code
679 * @param[in] collection The collection to remove from this list
680 * @exception E_SUCCESS The method is successful.
681 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
682 * the @c collection is modified during the operation of this method.
686 virtual result RemoveItems(const ICollectionT< Type >& collection)
688 result r = E_SUCCESS;
689 int oldCount = __count;
691 ICollectionT< Type >* pCol = const_cast< ICollectionT< Type >* >(&collection);
692 IEnumeratorT< Type >* pEnum = pCol->GetEnumeratorN();
693 TryCatch(pEnum != null, r = GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
695 while ((r = pEnum->MoveNext()) != E_OUT_OF_RANGE)
699 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
701 r = pEnum->GetCurrent(item);
702 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
705 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
708 if (__count < oldCount)
722 * Removes an object from a specified location.
726 * @return An error code
727 * @param[in] index The index of the object that is to remove
728 * @exception E_SUCCESS The method is successful.
729 * @exception E_OUT_OF_RANGE The specified index is outside the bounds of the data structure, or
730 * the specified @c index is greater than or equal to the number of elements or less than @c 0.
731 * @remarks The elements that follow the deleted object move up the list to occupy the empty location.
735 virtual result RemoveAt(int index)
737 TryReturn(index < __count && index >= 0, E_OUT_OF_RANGE,
738 "[%s] The index MUST be greater than or equal to 0, and less than the number of elements(%d).",
739 GetErrorMessage(E_OUT_OF_RANGE), index, __count);
744 for (int i = index; i < __count; i++)
746 __pObjArray[i] = __pObjArray[i + 1];
755 * Removes all the elements within a specified range.
759 * @return An error code
760 * @param[in] startIndex The starting index of the range
761 * @param[in] count The number of elements to remove
762 * @exception E_SUCCESS The method is successful.
763 * @exception E_OUT_OF_RANGE Either of the following conditions has occurred: @n
764 * - The specified index is outside the bounds of the data structure. @n
765 * - The specified @c startIndex is either greater than or equal to the number of elements or less than @c 0. @n
766 * - The specified @c count is either greater than the number of elements starting from @c startIndex or less than @c 0.
767 * @remarks The elements that follow the deleted elements move up the list to occupy the empty locations.
769 * @see InsertItemsFrom()
771 virtual result RemoveItems(int startIndex, int count)
773 TryReturn(startIndex >= 0 && count >= 0, E_OUT_OF_RANGE,
774 "[%s] Both of the startIndex(%d) and count(%d) MUST be greater than or equal to 0.", GetErrorMessage(E_OUT_OF_RANGE), startIndex, count);
775 TryReturn(startIndex < __count, E_OUT_OF_RANGE,
776 "[%s] The startIndex(%d) MUST be less than the number of elements(%d).", GetErrorMessage(E_OUT_OF_RANGE), startIndex, __count);
777 TryReturn(count <= __count && (startIndex + count <= __count), E_OUT_OF_RANGE,
778 "[%s] The startIndex(%d) + count(%d) MUST be less than or equal to the number of elements(%d).",
779 GetErrorMessage(E_OUT_OF_RANGE), startIndex, count, __count);
783 Type* newArray = new Type[__capacity];
784 TryReturn(newArray != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
789 for (int i = 0; i < startIndex; i++)
791 newArray[i] = __pObjArray[i];
793 for (int i = startIndex; i < __count; i++)
795 newArray[i] = __pObjArray[i + count];
798 delete[] __pObjArray;
799 __pObjArray = newArray;
806 * Removes all elements in the list.
810 virtual void RemoveAll(void)
814 delete[] __pObjArray;
826 * Sets the object at a specified index of the current instance of ByteBuffer with the specified object.
830 * @return An error code
831 * @param[in] obj The object to set
832 * @param[in] index The index at which the object must be set
833 * @exception E_SUCCESS The method is successful.
834 * @exception E_OUT_OF_RANGE The specified index is outside the bounds of the data structure, or
835 * the specified @c index is either equal to or greater than the number of elements or less than @c 0.
838 virtual result SetAt(const Type& obj, int index)
840 TryReturn(index >= 0 && index < __count, E_OUT_OF_RANGE,
841 "[%s] The index(%d) MUST be greater than or equal to 0, less than the number of elements(%d).",
842 GetErrorMessage(E_OUT_OF_RANGE), index, __count);
846 __pObjArray[index] = obj;
852 * Sets the capacity of the list to a specified value.
856 * @return An error code
857 * @param[in] newCapacity The new capacity to set for the list
858 * @exception E_SUCCESS The method is successful.
859 * @exception E_INVALID_ARG A specified input parameter is invalid, or
860 * the @c newCapacity is negative.
861 * @remarks If the new capacity is less than the current capacity, the memory
862 * is truncated and the elements within the truncated memory are destroyed.
867 virtual result SetCapacity(int newCapacity)
869 TryReturn(newCapacity >= 0, E_INVALID_ARG, "[%s] The newCapacity(%d) MUST be greater than or equal to 0.",
870 GetErrorMessage(E_INVALID_ARG), newCapacity);
872 result r = E_SUCCESS;
873 if (__capacity != newCapacity)
875 Type* newArray = null;
878 newArray = new Type[newCapacity];
879 TryCatch(newArray != null, r = E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
881 if (__pObjArray != null)
883 int count = __count < newCapacity ? __count : newCapacity;
884 for (int i = 0; i < count; i++)
886 newArray[i] = __pObjArray[i];
890 if (__pObjArray != null)
892 delete[] __pObjArray;
894 if (__count > newCapacity)
897 __count = newCapacity;
899 __pObjArray = newArray;
900 __capacity = newCapacity;
910 * Sorts the elements in the list using a comparer.
914 * @return An error code
915 * @param[in] comparer A pointer to IComparerT
916 * @exception E_SUCCESS The method is successful.
917 * @exception E_INVALID_ARG A specified input parameter is invalid, or
918 * the @c comparer is not valid.
920 virtual result Sort(const IComparerT< Type >& comparer)
925 __pComparer = const_cast< IComparerT< Type >* >(&comparer);
926 result r = QuickSort(0, (__count - 1));
929 AppLogException("[%s] Propagating.", GetErrorMessage(r));
939 * Trims the capacity of a list to the actual number of elements in the list.
943 * @return An error code
944 * @exception E_SUCCESS The method is successful.
945 * @exception E_OUT_OF_MEMORY The memory is insufficient.
946 * @remarks The specific error code can be accessed using the GetLastResult() method.
949 virtual void Trim(void)
956 result r = SetCapacity(__count);
959 AppLogException("[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
964 * Gets the current capacity of the list.
968 * @return The current capacity of the list
971 int GetCapacity(void) const
977 * Gets the number of objects currently stored in the list.
981 * @return The number of objects stored in the list
983 virtual int GetCount(void) const
989 * Checks whether a list contains the specified object.
993 * @return @c true if the object is present in the list, @n
995 * @param[in] obj The object to locate
998 virtual bool Contains(const Type& obj) const
1005 for (int i = 0; i < __count; i++)
1007 if (obj == __pObjArray[i])
1017 * Checks whether the list contains all the elements of the specified collection.
1021 * @return An error code
1022 * @param[in] collection The collection to check for in the list
1023 * @param[out] out @c true if the list contains all the elements of the specified collection, @n
1025 * @exception E_SUCCESS The method is successful.
1026 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
1027 * the @c collection is modified during the operation of this method.
1028 * @remarks If the given @c collection is empty, the @c out parameter will be set to @c true.
1031 virtual result ContainsAll(const ICollectionT< Type >& collection, bool& out) const
1033 result r = E_SUCCESS;
1036 if (collection.GetCount() == 0)
1042 ICollectionT< Type >* pCol = const_cast< ICollectionT< Type >* >(&collection);
1043 IEnumeratorT< Type >* pEnum = pCol->GetEnumeratorN();
1044 TryReturn(pEnum != null, r = GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1046 while ((r = pEnum->MoveNext()) != E_OUT_OF_RANGE)
1050 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
1052 r = pEnum->GetCurrent(item);
1053 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
1055 if (false == Contains(item))
1077 * Compares two instances of the %ArrayListT class.
1081 * @return @c true if the two instances match, @n
1083 * @param[in] obj The object to compare with the current instance
1084 * @remarks This method returns @c true if and only if the two instances contain the same elements in the same order.
1086 virtual bool Equals(const Tizen::Base::Object& obj) const
1088 const ArrayListT< Type >* other = dynamic_cast< const ArrayListT< Type >* >(&obj);
1093 else if (other == this)
1097 else if (__count != other->__count)
1103 for (int i = 0; i < __count; i++)
1105 if (__pObjArray[i] != other->__pObjArray[i])
1116 * Gets the hash value of the current instance.
1120 * @return The hash value of the current instance
1121 * @remarks The two Tizen::Base::Object::Equals() instances must return the same hash value. For better performance, @n
1122 * the used hash function must generate a random distribution for all inputs.
1124 virtual int GetHashCode(void) const
1127 for (int i = 0; i < __count; i++)
1129 if (&(__pObjArray[i]) != null)
1131 hash += reinterpret_cast< int >(&(__pObjArray[i]));
1139 * The implementation of this copy constructor is intentionally blank and declared as private to prohibit copying of objects.
1141 * @param[in] list The instance of the %ArrayListT class to copy from
1143 ArrayListT(const ArrayListT< Type >& list);
1146 * The implementation of this copy assignment operator is intentionally blank and declared as private to prohibit copying of objects.
1148 * @param[in] list An instance of %ArrayListT
1150 ArrayListT< Type >& operator =(const ArrayListT< Type >& list);
1153 * Sorts a section of a list using a comparer.
1155 * @return An error code
1156 * @param[in] startIndex The start index of the section of the list
1157 * @param[in] endIndex The end index of the section of the list
1158 * @exception E_SUCCESS The method is successful.
1159 * @exception E_INVALID_ARG A specified input parameter is invalid, or
1160 * the comparer has failed to compare the elements.
1162 result QuickSort(int startIndex, int endIndex)
1164 result r = E_SUCCESS;
1166 if (startIndex < endIndex)
1169 int i = startIndex - 1;
1170 int j = endIndex + 1;
1173 int compareResult = 1;
1174 while ((compareResult > 0) && (j > static_cast< int >(startIndex)))
1178 r = __pComparer->Compare(__pObjArray[j], __pObjArray[startIndex], compareResult);
1179 TryReturn(r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
1183 while ((compareResult < 0) && (i < static_cast< int >(endIndex)))
1187 r = __pComparer->Compare(__pObjArray[i], __pObjArray[startIndex], compareResult);
1188 TryReturn(r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
1193 Type temp = __pObjArray[j];
1194 __pObjArray[j] = __pObjArray[i];
1195 __pObjArray[i] = temp;
1204 r = QuickSort(startIndex, middleIndex);
1205 TryReturn(r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
1207 r = QuickSort(middleIndex + 1, endIndex);
1208 TryReturn(r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
1218 IComparerT< Type >* __pComparer;
1219 static const int DEFAULT_CAPACITY = 10;
1221 friend class __ArrayListEnumeratorT< Type >;
1226 // @class __ArrayListEnumeratorT
1227 // @brief This class is an implementation of the IEnumeratorT interface for the %ArrayListT class.
1230 template< class Type >
1231 class __ArrayListEnumeratorT
1232 : public IBidirectionalEnumeratorT< Type >
1233 , public Tizen::Base::Object
1236 __ArrayListEnumeratorT(const ArrayListT< Type >& list, int modCount)
1238 , __modCount(modCount)
1243 virtual ~__ArrayListEnumeratorT(void)
1247 virtual result GetCurrent(Type& obj) const
1249 TryReturn((__modCount == __list.__modCount), E_INVALID_OPERATION,
1250 "[%s] The source collection is modified after the creation of this enumerator.", GetErrorMessage(E_INVALID_OPERATION));
1251 TryReturn((__position > -1) && (__position < static_cast <int>(__list.__count)), E_INVALID_OPERATION,
1252 "[%s] Current position(%d) is before the first element or past the last element.", GetErrorMessage(E_INVALID_OPERATION), __position);
1254 obj = __list.__pObjArray[__position];
1258 virtual result MoveNext(void)
1260 TryReturn((__modCount == __list.__modCount), E_INVALID_OPERATION,
1261 "[%s] The source collection is modified after the creation of this enumerator.", GetErrorMessage(E_INVALID_OPERATION));
1263 if ((__position + 1) >= static_cast< int >(__list.__count))
1265 return E_OUT_OF_RANGE;
1275 virtual result Reset(void)
1277 TryReturn((__modCount == __list.__modCount), E_INVALID_OPERATION,
1278 "[%s] The source collection is modified after the creation of this enumerator.", GetErrorMessage(E_INVALID_OPERATION));
1284 virtual result MovePrevious(void)
1286 TryReturn(__modCount == __list.__modCount, E_INVALID_OPERATION,
1287 "[%s] The source collection is modified after the creation of this enumerator.", GetErrorMessage(E_INVALID_OPERATION));
1289 TryReturn(__position > 0, E_OUT_OF_RANGE, "[%s] Reached start of the list, no previous element", GetErrorMessage(E_OUT_OF_RANGE));
1296 virtual result ResetLast(void)
1298 TryReturn(__modCount == __list.__modCount, E_INVALID_OPERATION,
1299 "[%s] The source collection is modified after the creation of this enumerator.", GetErrorMessage(E_INVALID_OPERATION));
1301 __position = __list.__count;
1306 const ArrayListT< Type >& __list;
1310 }; //__ArrayListEnumeratorT
1312 } } } // Tizen::Base::Collection
1314 #endif // _FBASE_COL_ARRAY_LIST_T_H_