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>
31 namespace Tizen { namespace Base { namespace Collection
34 template< class Type > class __ArrayListEnumeratorT;
38 * @brief This class provides a template-based collection of objects that can be individually accessed by an index.
42 * The %ArrayListT class provides a template-based collection of objects that can be individually accessed by an index.
44 * For more information on the class features, see <a href="../org.tizen.native.appprogramming/html/guide/base/arraylist_linkedlist.htm">ArrayList and LinkedList</a>.
46 * The following example demonstrates how to use the %ArrayListT class to create and initialize an %ArrayListT instance and to print out its values.
51 * using namespace Tizen::Base;
52 * using namespace Tizen::Base::Collection;
55 * MyClass::ArrayListTSample(void)
57 * ArrayListT< int > list;
66 * list.Add(int1); // 1
67 * list.Add(int2); // 1,2
68 * list.Add(int3); // 1,2,3
71 * for (int i = 0; i < list.GetCount(); i++)
73 * list.GetAt(i, temp);
76 * list.InsertAt(int4, 1); // 1,4,2,3
78 * ComparerT< int >* pIntComparer = new ComparerT<int>();
79 * list.Sort(*pIntComparer); // 1,2,3,4
81 * delete pIntComparer;
83 * list.Remove(int3); // 1,2,4
85 * list.RemoveAt(0); // 2,4
87 * // Uses an enumerator to access elements in the list
88 * IEnumeratorT< int >* pEnum = list.GetEnumeratorN();
89 * while (pEnum->MoveNext() == E_SUCCESS)
91 * pEnum->GetCurrent(temp);
98 template< class Type >
100 : public IListT< Type >
105 * This is the default constructor for this class.
109 * @remarks After creating an instance of this class, one of the Construct() methods must be called explicitly to initialize this instance.
121 * This destructor overrides Tizen::Base::Object::~Object().
125 virtual ~ArrayListT(void)
127 if (__pObjArray != null)
129 delete[] __pObjArray;
134 * Initializes this instance of %ArrayListT with the specified parameter.
138 * @return An error code
139 * @param[in] capacity The initial capacity of the class @n
140 * The default capacity is @c 10.
141 * @exception E_SUCCESS The method is successful.
142 * @exception E_INVALID_ARG A specified input parameter is invalid, or
143 * the specified @c capacity is negative.
144 * @remarks If the number of elements added to the list reaches its current capacity,
145 * the capacity is automatically increased by memory reallocation.
146 * Thus, if the size of the list can be estimated,
147 * specifying the initial capacity eliminates the need to perform a number of
148 * resizing operations while adding elements to the list.
151 result Construct(int capacity = DEFAULT_CAPACITY)
153 TryReturn(capacity >= 0, E_INVALID_ARG, "[%s] The capacity(%d) MUST be greater than or equal to 0.", GetErrorMessage(E_INVALID_ARG), capacity);
155 result r = SetCapacity(capacity);
156 TryReturn(r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
162 * Initializes this instance of %ArrayListT with the specified parameter. @n
163 * The capacity of the list is the same as the number of elements copied to it.
167 * @return An error code
168 * @param[in] collection A collection of elements to add
169 * @exception E_SUCCESS The method is successful.
170 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
171 * the @c collection is modified during the operation of this method.
174 result Construct(const ICollectionT< Type >& collection)
176 result r = AddItems(collection);
177 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
182 delete[] __pObjArray;
189 * Adds the specified object to the end of the list.
193 * @return An error code
194 * @param[in] obj An object to add to the list
195 * @exception E_SUCCESS The method is successful.
196 * @exception E_OUT_OF_MEMORY The memory is insufficient.
199 virtual result Add(const Type& obj)
201 if (__count >= __capacity)
203 result r = SetCapacity(__capacity + DEFAULT_CAPACITY);
204 TryReturn(r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
207 __pObjArray[__count++] = obj;
215 * Adds the elements of the specified collection to the end of the list.
219 * @return An error code
220 * @param[in] collection A collection of elements to add to the list
221 * @exception E_SUCCESS The method is successful.
222 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
223 * the @c collection is modified during the operation of this method.
226 virtual result AddItems(const ICollectionT< Type >& collection)
228 result r = E_SUCCESS;
230 IEnumeratorT< Type >* pEnum = null;
231 int count = collection.GetCount();
234 if (count > (__capacity - __count))
236 r = SetCapacity(__count + count);
237 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
240 ICollectionT< Type >* pCol = const_cast< ICollectionT< Type >* >(&collection);
241 pEnum = pCol->GetEnumeratorN();
242 TryReturn(pEnum != null, r = GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
246 while ((r = pEnum->MoveNext()) != E_OUT_OF_RANGE)
248 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
251 r = pEnum->GetCurrent(item);
252 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
254 __pObjArray[__count++] = item;
267 * Gets the elements of the list in an instance of the IEnumeratorT derived class.
271 * @return An instance of the IEnumeratorT derived class if successful, @n
272 * else @c null if an exception occurs
273 * @exception E_SUCCESS The method is successful.
274 * @exception E_OUT_OF_MEMORY The memory is insufficient.
275 * @remarks The specific error code can be accessed using the GetLastResult() method.
276 * @see Tizen::Base::Collection::IEnumeratorT
278 virtual IEnumeratorT< Type >* GetEnumeratorN(void) const
280 result r = E_SUCCESS;
282 __ArrayListEnumeratorT< Type >* pEnum = new __ArrayListEnumeratorT< Type >(*this, __modCount);
283 TryCatch(pEnum != null, r = E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
285 SetLastResult(E_SUCCESS);
294 * Gets a bidirectional enumerator (an instance of the IBidirectionalEnumeratorT derived class) of this list.
298 * @return An instance of the IBidirectionalEnumeratorT derived class, @n
299 * else @c null if an exception occurs
300 * @exception E_SUCCESS The method is successful.
301 * @exception E_OUT_OF_MEMORY The memory is insufficient.
302 * @remarks Use this method to obtain a bidirectional enumerator (an instance of the IBidirectionalEnumeratorT derived class)
303 * to iterate over a collection (an instance of the IListT derived class).
304 * @remarks The specific error code can be accessed using the GetLastResult() method.
305 * @see Tizen::Base::Collection::IBidirectionalEnumeratorT
307 virtual IBidirectionalEnumeratorT< Type >* GetBidirectionalEnumeratorN(void) const
309 result r = E_SUCCESS;
311 __ArrayListEnumeratorT< Type >* pEnum = new __ArrayListEnumeratorT< Type >(*this, __modCount);
312 TryCatch(pEnum != null, r = E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
314 SetLastResult(E_SUCCESS);
323 * Gets the object at the specified @c index of the list.
327 * @return An error code
328 * @param[in] index The index of the object to read
329 * @param[out] obj An object to get from this list
330 * @exception E_SUCCESS The method is successful.
331 * @exception E_OUT_OF_RANGE The specified @c index is outside the bounds of the data structure, or
332 * the specified @c index is either equal to or greater than the number of elements or less than @c 0.
335 virtual result GetAt(int index, Type& obj) const
337 TryReturn(index >= 0 && index < __count, E_OUT_OF_RANGE,
338 "[%s] The index(%d) MUST be greater than or equal to 0 and less than the number of elements(%d).",
339 GetErrorMessage(E_OUT_OF_RANGE), index, __count);
341 obj = __pObjArray[index];
346 * Gets the object at the specified @c index of the list.
350 * @return An error code
351 * @param[in] index The index of the object to read
352 * @param[out] obj An object to get from this list
353 * @exception E_SUCCESS The method is successful.
354 * @exception E_OUT_OF_RANGE The specified @c index is outside the bounds of the data structure, or
355 * the specified @c index is either equal to or greater than the number of elements or less than @c 0.
358 virtual result GetAt(int index, Type& obj)
360 TryReturn(index >= 0 && index < __count, E_OUT_OF_RANGE,
361 "[%s] The index(%d) MUST be greater than or equal to 0 and less than the number of elements(%d).",
362 GetErrorMessage(E_OUT_OF_RANGE), index, __count);
364 obj = __pObjArray[index];
369 * Gets a list of a specified number of elements starting from a specified index.
373 * @return An instance of the IListT derived class within the specified range of the list, @n
374 * else @c null if an exception occurs
375 * @param[in] startIndex The index to start reading elements from
376 * @param[in] count The number of elements to read
377 * @exception E_SUCCESS The method is successful.
378 * @exception E_INVALID_ARG A specified input parameter is invalid.
379 * @exception E_OUT_OF_RANGE Either of the following conditions has occurred: @n
380 * - The specified index is outside the bounds of the data structure. @n
381 * - The specified @c startIndex is either greater than or equal to the number of elements or less than @c 0. @n
382 * - The specified @c count is either greater than the number of elements starting from @c startIndex or less than @c 0.
384 * @remarks The specific error code can be accessed using the GetLastResult() method.
386 virtual IListT< Type >* GetItemsN(int startIndex, int count) const
388 result r = E_SUCCESS;
390 ArrayListT< Type >* pList = null;
392 TryCatch(startIndex >= 0 && count >= 0, r = E_OUT_OF_RANGE,
393 "[%s] Both of the startIndex(%d) and count(%d) MUST be greater than or equal to 0.", GetErrorMessage(E_OUT_OF_RANGE), startIndex, count);
394 TryCatch(startIndex < __count, r = E_OUT_OF_RANGE,
395 "[%s] The startIndex(%d) MUST be less than the number of elements(%d).", GetErrorMessage(E_OUT_OF_RANGE), startIndex, __count);
396 TryCatch(count <= __count && (startIndex + count <= __count), r = E_OUT_OF_RANGE,
397 "[%s] The startIndex(%d) + count(%d) MUST be less than or equal to the number of elements(%d).",
398 GetErrorMessage(E_OUT_OF_RANGE), startIndex, count, __count);
400 pList = new ArrayListT< Type >();
402 r = pList->Construct(count);
403 TryCatch(r == E_SUCCESS, delete pList, "[%s] Propagating.", GetErrorMessage(r));
405 for (int i = startIndex; i < (startIndex + count); i++)
407 pList->__pObjArray[pList->__count++] = __pObjArray[i];
410 SetLastResult(E_SUCCESS);
419 * Searches for an object in this list. @n
420 * Gets the index of the object if found.
424 * @return An error code
425 * @param[in] obj The object to locate
426 * @param[out] index The index of the object
427 * @exception E_SUCCESS The method is successful.
428 * @exception E_OBJ_NOT_FOUND The specified @c obj is not found.
431 virtual result IndexOf(const Type& obj, int& index) const
433 return IndexOf(obj, 0, __count, index);
437 * Searches for an object starting from the specified @c index. @n
438 * Gets the index of the object if found.
442 * @return An error code
443 * @param[in] obj The object to locate
444 * @param[in] startIndex The starting index for the search @n
445 * It must be less than the number of elements.
446 * @param[out] index The index of the object
447 * @exception E_SUCCESS The method is successful.
448 * @exception E_OUT_OF_RANGE The specified @c index is outside the bounds of the data structure, or
449 * the specified @c startIndex is either equal to or greater than the number of elements or less than @c 0.
450 * @exception E_OBJ_NOT_FOUND The specified @c obj is not found.
453 virtual result IndexOf(const Type& obj, int startIndex, int& index) const
455 TryReturn(startIndex >= 0 && startIndex < __count, E_OUT_OF_RANGE,
456 "[%s] The startIndex(%d) MUST be greater than or equal to 0, and less than the number of elements(%d).",
457 GetErrorMessage(E_OUT_OF_RANGE), startIndex, __count);
459 return IndexOf(obj, startIndex, (__count - startIndex), index);
463 * Searches for an object within the specified range. @n
464 * Gets the index of the object if found.
468 * @return An error code
469 * @param[in] obj The object to locate
470 * @param[in] startIndex The starting index of the range
471 * @param[in] count The number of elements to read
472 * @param[out] index The index of the object
473 * @exception E_SUCCESS The method is successful.
474 * @exception E_OUT_OF_RANGE Either of the following conditions has occurred: @n
475 * - The specified @c index is outside the bounds of the data structure. @n
476 * - The specified @c startIndex is either greater than or equal to the number of elements or less than @c 0. @n
477 * - The specified @c count is either greater than the number of elements starting from @c startIndex or less than @c 0.
478 * @exception E_OBJ_NOT_FOUND The specified @c obj is not found.
481 virtual result IndexOf(const Type& obj, int startIndex, int count, int& index) const
483 TryReturn(startIndex >= 0 && count >= 0, E_OUT_OF_RANGE,
484 "[%s] Both of the startIndex(%d) and count(%d) MUST be greater than or equal to 0.", GetErrorMessage(E_OUT_OF_RANGE), startIndex, count);
485 TryReturn(startIndex < __count, E_OUT_OF_RANGE,
486 "[%s] The startIndex(%d) MUST be less than the number of elements(%d).", GetErrorMessage(E_OUT_OF_RANGE), startIndex, __count);
487 TryReturn(count <= __count && (startIndex + count <= __count), E_OUT_OF_RANGE,
488 "[%s] The startIndex(%d) + count(%d) MUST be less than or equal to the number of elements(%d).",
489 GetErrorMessage(E_OUT_OF_RANGE), startIndex, count, __count);
491 int arrayListCount = startIndex + count;
492 for (int i = startIndex; i < arrayListCount; i++)
494 if (obj == __pObjArray[i])
501 return E_OBJ_NOT_FOUND;
505 * Inserts an object at a specified location.
509 * @return An error code
510 * @param[in] obj The object to insert
511 * @param[in] index The index at which the object must be inserted
512 * @exception E_SUCCESS The method is successful.
513 * @exception E_OUT_OF_RANGE The specified @c index is outside the bounds of the data structure, or
514 * the @c index is greater than the number of elements or less than @c 0.
515 * @remarks The elements that follow the insertion point move down to accommodate the new element.
516 * If the @c index equals the number of elements in the list, the new element
517 * is added at the end of the list.
521 virtual result InsertAt(const Type& obj, int index)
523 TryReturn(index >= 0 && index <= __count, E_OUT_OF_RANGE,
524 "[%s] The index(%d) MUST be greater than or equal to 0, and less than or equal to the number of elements(%d).",
525 GetErrorMessage(E_OUT_OF_RANGE), index, __count);
527 result r = E_SUCCESS;
529 if (__count >= __capacity)
531 r = SetCapacity(__capacity + DEFAULT_CAPACITY);
532 TryReturn(r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
535 for (int i = __count; i > index; i--)
537 __pObjArray[i] = __pObjArray[i - 1];
542 __pObjArray[index] = obj;
548 * Inserts the elements of a collection at a specified location.
552 * @return An error code
553 * @param[in] collection The collection to insert
554 * @param[in] startIndex The index from which the collection must be inserted
555 * @exception E_SUCCESS The method is successful.
556 * @exception E_OUT_OF_RANGE The specified index is outside the bounds of the data structure, or
557 * the @c startIndex is greater than the number of elements or less than @c 0.
558 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
559 * the @c collection is modified during the operation of this method.
560 * @remarks The elements that follow the insertion point move down to accommodate the new elements.
561 * If the @c startIndex equals the number of elements in the list, the new elements
562 * are added at the end of the list.
566 virtual result InsertItemsFrom(const ICollectionT< Type >& collection, int startIndex)
568 TryReturn(startIndex >= 0 && startIndex <= __count, E_OUT_OF_RANGE,
569 "[%s] The startIndex(%d) MUST be greater than or equal to 0, and less than or equal to the number of elements(%d).",
570 GetErrorMessage(E_OUT_OF_RANGE), startIndex, __count);
572 result r = E_SUCCESS;
574 IEnumeratorT< Type >* pEnum = null;
575 int count = collection.GetCount();
579 if (count > (__capacity - __count))
581 r = SetCapacity(__count + count);
582 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
586 for (int i = (__count - 1); i >= (startIndex + count); i--)
588 __pObjArray[i] = __pObjArray[i - count];
591 ICollectionT< Type >* pCol = const_cast< ICollectionT< Type >* >(&collection);
592 pEnum = pCol->GetEnumeratorN();
593 TryReturn(pEnum != null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
597 while ((r = pEnum->MoveNext()) != E_OUT_OF_RANGE)
601 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
603 r = pEnum->GetCurrent(item);
604 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
606 __pObjArray[startIndex++] = item;
619 * Searches for the last occurrence of an object in this list. @n
620 * Gets the index of the object if found.
624 * @return An error code
625 * @param[in] obj The object to locate
626 * @param[out] index The index of the last occurrence of the specified object
627 * @exception E_SUCCESS The method is successful.
628 * @exception E_OBJ_NOT_FOUND The specified @c obj is not found.
631 virtual result LastIndexOf(const Type& obj, int& index) const
633 for (int i = (__count - 1); i >= 0; i--)
635 if (obj == __pObjArray[i])
642 return E_OBJ_NOT_FOUND;
646 * Removes the first occurrence of a specified object.
650 * @return An error code
651 * @param[in] obj The object to remove
652 * @exception E_SUCCESS The method is successful.
653 * @exception E_OBJ_NOT_FOUND The specified @c obj is not found.
658 virtual result Remove(const Type& obj)
661 result r = IndexOf(obj, index);
664 return E_OBJ_NOT_FOUND;
668 return RemoveAt(index);
673 * Removes all the elements of a specified collection from the list.
677 * @return An error code
678 * @param[in] collection The collection to remove from this list
679 * @exception E_SUCCESS The method is successful.
680 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
681 * the @c collection is modified during the operation of this method.
685 virtual result RemoveItems(const ICollectionT< Type >& collection)
687 result r = E_SUCCESS;
688 int oldCount = __count;
690 ICollectionT< Type >* pCol = const_cast< ICollectionT< Type >* >(&collection);
691 IEnumeratorT< Type >* pEnum = pCol->GetEnumeratorN();
692 TryCatch(pEnum != null, r = GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
694 while ((r = pEnum->MoveNext()) != E_OUT_OF_RANGE)
698 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
700 r = pEnum->GetCurrent(item);
701 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
704 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
707 if (__count < oldCount)
721 * Removes an object from a specified location.
725 * @return An error code
726 * @param[in] index The index of the object that is to remove
727 * @exception E_SUCCESS The method is successful.
728 * @exception E_OUT_OF_RANGE The specified @c index is outside the bounds of the data structure, or
729 * the specified @c index is greater than or equal to the number of elements or less than @c 0.
730 * @remarks The elements that follow the deleted object move up the list to occupy the empty location.
734 virtual result RemoveAt(int index)
736 TryReturn(index < __count && index >= 0, E_OUT_OF_RANGE,
737 "[%s] The index MUST be greater than or equal to 0, and less than the number of elements(%d).",
738 GetErrorMessage(E_OUT_OF_RANGE), index, __count);
743 for (int i = index; i < __count; i++)
745 __pObjArray[i] = __pObjArray[i + 1];
754 * Removes all the elements within a specified range.
758 * @return An error code
759 * @param[in] startIndex The starting index of the range
760 * @param[in] count The number of elements to remove
761 * @exception E_SUCCESS The method is successful.
762 * @exception E_OUT_OF_RANGE Either of the following conditions has occurred: @n
763 * - The specified index is outside the bounds of the data structure. @n
764 * - The specified @c startIndex is either greater than or equal to the number of elements or less than @c 0. @n
765 * - The specified @c count is either greater than the number of elements starting from @c startIndex or less than @c 0.
766 * @remarks The elements that follow the deleted elements move up the list to occupy the empty locations.
768 * @see InsertItemsFrom()
770 virtual result RemoveItems(int startIndex, int count)
772 TryReturn(startIndex >= 0 && count >= 0, E_OUT_OF_RANGE,
773 "[%s] Both of the startIndex(%d) and count(%d) MUST be greater than or equal to 0.", GetErrorMessage(E_OUT_OF_RANGE), startIndex, count);
774 TryReturn(startIndex < __count, E_OUT_OF_RANGE,
775 "[%s] The startIndex(%d) MUST be less than the number of elements(%d).", GetErrorMessage(E_OUT_OF_RANGE), startIndex, __count);
776 TryReturn(count <= __count && (startIndex + count <= __count), E_OUT_OF_RANGE,
777 "[%s] The startIndex(%d) + count(%d) MUST be less than or equal to the number of elements(%d).",
778 GetErrorMessage(E_OUT_OF_RANGE), startIndex, count, __count);
782 Type* newArray = new Type[__capacity];
783 TryReturn(newArray != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
788 for (int i = 0; i < startIndex; i++)
790 newArray[i] = __pObjArray[i];
792 for (int i = startIndex; i < __count; i++)
794 newArray[i] = __pObjArray[i + count];
797 delete[] __pObjArray;
798 __pObjArray = newArray;
805 * Removes all elements in the list.
809 virtual void RemoveAll(void)
813 delete[] __pObjArray;
825 * Sets the object at a specified @c index of the current instance of ByteBuffer with the specified object.
829 * @return An error code
830 * @param[in] obj The object to set
831 * @param[in] index The index at which the object must be set
832 * @exception E_SUCCESS The method is successful.
833 * @exception E_OUT_OF_RANGE The specified @c index is outside the bounds of the data structure, or
834 * the specified @c index is either equal to or greater than the number of elements or less than @c 0.
837 virtual result SetAt(const Type& obj, int index)
839 TryReturn(index >= 0 && index < __count, E_OUT_OF_RANGE,
840 "[%s] The index(%d) MUST be greater than or equal to 0, less than the number of elements(%d).",
841 GetErrorMessage(E_OUT_OF_RANGE), index, __count);
845 __pObjArray[index] = obj;
851 * Sets the capacity of the list to a specified value.
855 * @return An error code
856 * @param[in] newCapacity The new capacity to set for the list
857 * @exception E_SUCCESS The method is successful.
858 * @exception E_INVALID_ARG A specified input parameter is invalid, or
859 * the @c newCapacity is negative.
860 * @remarks If the new capacity is less than the current capacity, the memory
861 * is truncated and the elements within the truncated memory are destroyed.
866 virtual result SetCapacity(int newCapacity)
868 TryReturn(newCapacity >= 0, E_INVALID_ARG, "[%s] The newCapacity(%d) MUST be greater than or equal to 0.",
869 GetErrorMessage(E_INVALID_ARG), newCapacity);
871 result r = E_SUCCESS;
872 if (__capacity != newCapacity)
874 Type* newArray = null;
877 newArray = new Type[newCapacity];
878 TryCatch(newArray != null, r = E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
880 if (__pObjArray != null)
882 int count = __count < newCapacity ? __count : newCapacity;
883 for (int i = 0; i < count; i++)
885 newArray[i] = __pObjArray[i];
889 if (__pObjArray != null)
891 delete[] __pObjArray;
893 if (__count > newCapacity)
896 __count = newCapacity;
898 __pObjArray = newArray;
899 __capacity = newCapacity;
909 * Sorts the elements in the list using a comparer.
913 * @return An error code
914 * @param[in] comparer A pointer to IComparerT
915 * @exception E_SUCCESS The method is successful.
916 * @exception E_INVALID_ARG A specified input parameter is invalid, or
917 * the @c comparer is not valid.
919 virtual result Sort(const IComparerT< Type >& comparer)
924 __pComparer = const_cast< IComparerT< Type >* >(&comparer);
925 result r = QuickSort(0, (__count - 1));
928 AppLogException("[%s] Propagating.", GetErrorMessage(r));
938 * Trims the capacity of a list to the actual number of elements in the list.
942 * @return An error code
943 * @exception E_SUCCESS The method is successful.
944 * @exception E_OUT_OF_MEMORY The memory is insufficient.
945 * @remarks The specific error code can be accessed using the GetLastResult() method.
948 virtual void Trim(void)
955 result r = SetCapacity(__count);
958 AppLogException("[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
963 * Gets the current capacity of the list.
967 * @return The current capacity of the list
970 int GetCapacity(void) const
976 * Gets the number of objects currently stored in the list.
980 * @return The number of objects stored in the list
982 virtual int GetCount(void) const
988 * Checks whether a list contains the specified object.
992 * @return @c true if the object is present in the list, @n
994 * @param[in] obj The object to locate
997 virtual bool Contains(const Type& obj) const
1004 for (int i = 0; i < __count; i++)
1006 if (obj == __pObjArray[i])
1016 * Checks whether the list contains all the elements of the specified @c collection.
1020 * @return An error code
1021 * @param[in] collection The collection to check for in the list
1022 * @param[out] out @c true if the list contains all the elements of the specified @c collection, @n
1024 * @exception E_SUCCESS The method is successful.
1025 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
1026 * the @c collection is modified during the operation of this method.
1027 * @remarks If the given @c collection is empty, the @c out parameter will be set to @c true.
1030 virtual result ContainsAll(const ICollectionT< Type >& collection, bool& out) const
1032 result r = E_SUCCESS;
1035 if (collection.GetCount() == 0)
1041 ICollectionT< Type >* pCol = const_cast< ICollectionT< Type >* >(&collection);
1042 IEnumeratorT< Type >* pEnum = pCol->GetEnumeratorN();
1043 TryReturn(pEnum != null, r = GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1045 while ((r = pEnum->MoveNext()) != E_OUT_OF_RANGE)
1049 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
1051 r = pEnum->GetCurrent(item);
1052 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
1054 if (false == Contains(item))
1076 * Compares two instances of the %ArrayListT class.
1080 * @return @c true if the two instances match, @n
1082 * @param[in] obj The object to compare with the current instance
1083 * @remarks This method returns @c true if and only if the two instances contain the same elements in the same order.
1085 virtual bool Equals(const Tizen::Base::Object& obj) const
1087 const ArrayListT< Type >* other = dynamic_cast< const ArrayListT< Type >* >(&obj);
1092 else if (other == this)
1096 else if (__count != other->__count)
1102 for (int i = 0; i < __count; i++)
1104 if (__pObjArray[i] != other->__pObjArray[i])
1115 * Gets the hash value of the current instance.
1119 * @return The hash value of the current instance
1120 * @remarks The two Tizen::Base::Object::Equals() instances must return the same hash value. For better performance, @n
1121 * the used hash function must generate a random distribution for all inputs.
1123 virtual int GetHashCode(void) const
1126 for (int i = 0; i < __count; i++)
1128 if (&(__pObjArray[i]) != null)
1130 hash += reinterpret_cast< int >(&(__pObjArray[i]));
1138 * The implementation of this copy constructor is intentionally blank and declared as private to prohibit copying of objects.
1140 * @param[in] list The instance of the %ArrayListT class to copy from
1142 ArrayListT(const ArrayListT< Type >& list);
1145 * The implementation of this copy assignment operator is intentionally blank and declared as private to prohibit copying of objects.
1147 * @param[in] list An instance of %ArrayListT
1149 ArrayListT< Type >& operator =(const ArrayListT< Type >& list);
1152 * Sorts a section of a list using a comparer.
1154 * @return An error code
1155 * @param[in] startIndex The start index of the section of the list
1156 * @param[in] endIndex The end index of the section of the list
1157 * @exception E_SUCCESS The method is successful.
1158 * @exception E_INVALID_ARG A specified input parameter is invalid, or
1159 * the comparer has failed to compare the elements.
1161 result QuickSort(int startIndex, int endIndex)
1163 result r = E_SUCCESS;
1165 if (startIndex < endIndex)
1168 int i = startIndex - 1;
1169 int j = endIndex + 1;
1172 int compareResult = 1;
1173 while ((compareResult > 0) && (j > static_cast< int >(startIndex)))
1177 r = __pComparer->Compare(__pObjArray[j], __pObjArray[startIndex], compareResult);
1178 TryReturn(r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
1182 while ((compareResult < 0) && (i < static_cast< int >(endIndex)))
1186 r = __pComparer->Compare(__pObjArray[i], __pObjArray[startIndex], compareResult);
1187 TryReturn(r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
1192 Type temp = __pObjArray[j];
1193 __pObjArray[j] = __pObjArray[i];
1194 __pObjArray[i] = temp;
1203 r = QuickSort(startIndex, middleIndex);
1204 TryReturn(r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
1206 r = QuickSort(middleIndex + 1, endIndex);
1207 TryReturn(r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
1217 IComparerT< Type >* __pComparer;
1218 static const int DEFAULT_CAPACITY = 10;
1220 friend class __ArrayListEnumeratorT< Type >;
1225 // @class __ArrayListEnumeratorT
1226 // @brief This class is an implementation of the IEnumeratorT interface for the %ArrayListT class.
1229 template< class Type >
1230 class __ArrayListEnumeratorT
1231 : public IBidirectionalEnumeratorT< Type >
1232 , public Tizen::Base::Object
1235 __ArrayListEnumeratorT(const ArrayListT< Type >& list, int modCount)
1237 , __modCount(modCount)
1242 virtual ~__ArrayListEnumeratorT(void)
1246 virtual result GetCurrent(Type& obj) const
1248 TryReturn((__modCount == __list.__modCount), E_INVALID_OPERATION,
1249 "[%s] The source collection is modified after the creation of this enumerator.", GetErrorMessage(E_INVALID_OPERATION));
1250 TryReturn((__position > -1) && (__position < static_cast <int>(__list.__count)), E_INVALID_OPERATION,
1251 "[%s] Current position(%d) is before the first element or past the last element.", GetErrorMessage(E_INVALID_OPERATION), __position);
1253 obj = __list.__pObjArray[__position];
1257 virtual result MoveNext(void)
1259 TryReturn((__modCount == __list.__modCount), E_INVALID_OPERATION,
1260 "[%s] The source collection is modified after the creation of this enumerator.", GetErrorMessage(E_INVALID_OPERATION));
1262 if ((__position + 1) >= static_cast< int >(__list.__count))
1264 return E_OUT_OF_RANGE;
1274 virtual result Reset(void)
1276 TryReturn((__modCount == __list.__modCount), E_INVALID_OPERATION,
1277 "[%s] The source collection is modified after the creation of this enumerator.", GetErrorMessage(E_INVALID_OPERATION));
1283 virtual result MovePrevious(void)
1285 TryReturn(__modCount == __list.__modCount, E_INVALID_OPERATION,
1286 "[%s] The source collection is modified after the creation of this enumerator.", GetErrorMessage(E_INVALID_OPERATION));
1288 TryReturn(__position > 0, E_OUT_OF_RANGE, "[%s] Reached start of the list, no previous element", GetErrorMessage(E_OUT_OF_RANGE));
1295 virtual result ResetLast(void)
1297 TryReturn(__modCount == __list.__modCount, E_INVALID_OPERATION,
1298 "[%s] The source collection is modified after the creation of this enumerator.", GetErrorMessage(E_INVALID_OPERATION));
1300 __position = __list.__count;
1305 const ArrayListT< Type >& __list;
1309 }; //__ArrayListEnumeratorT
1311 } } } // Tizen::Base::Collection
1313 #endif // _FBASE_COL_ARRAY_LIST_T_H_