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 Either of the following conditions has occurred:
142 * - The specified input parameter is invalid.
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. @n
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 The collection of elements to add
169 * @exception E_SUCCESS The method is successful.
170 * @exception E_INVALID_OPERATION Either of the following conditions has occurred:
171 * - The current state of the instance prohibits the execution of the specified operation.
172 * - The specified @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 @c obj to the end of the list.
194 * @return An error code
195 * @param[in] obj The 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 @c collection to the end of the list.
220 * @return An error code
221 * @param[in] collection The collection of elements to add to the list
222 * @exception E_SUCCESS The method is successful.
223 * @exception E_INVALID_OPERATION Either of the following conditions has occurred:
224 * - The current state of the instance prohibits the execution of the specified operation.
225 * - The specified @c collection is modified during the operation of this method.
228 virtual result AddItems(const ICollectionT< Type >& collection)
230 result r = E_SUCCESS;
232 IEnumeratorT< Type >* pEnum = null;
233 int count = collection.GetCount();
236 if (count > (__capacity - __count))
238 r = SetCapacity(__count + count);
239 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
242 ICollectionT< Type >* pCol = const_cast< ICollectionT< Type >* >(&collection);
243 pEnum = pCol->GetEnumeratorN();
244 TryReturn(pEnum != null, r = GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
248 while ((r = pEnum->MoveNext()) != E_OUT_OF_RANGE)
250 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
253 r = pEnum->GetCurrent(item);
254 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
256 __pObjArray[__count++] = item;
269 * Gets the elements of the list through an instance of the IEnumeratorT derived class.
273 * @return An instance of the IEnumeratorT derived class, @n
274 * else @c null if an exception occurs
275 * @exception E_SUCCESS The method is successful.
276 * @exception E_OUT_OF_MEMORY The memory is insufficient.
277 * @remarks The specific error code can be accessed using the GetLastResult() method.
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 the 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.
304 * - Use this method to obtain a bidirectional enumerator (an instance of the IBidirectionalEnumeratorT derived class)
305 * to iterate over a collection (an instance of the IListT derived class).
306 * - The specific error code can be accessed using the GetLastResult() method.
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 @c index of the list.
328 * @return An error code
329 * @param[in] index The index of the object to read
330 * @param[out] obj The object to get from this list
331 * @exception E_SUCCESS The method is successful.
332 * @exception E_OUT_OF_RANGE Either of the following conditions has occurred:
333 * - The specified @c index is outside the bounds of the data structure.
334 * - The specified @c index is either greater than or equal to the number of elements.
335 * - The specified @c index is less than @c 0.
338 virtual result GetAt(int index, Type& obj) const
340 TryReturn(index >= 0 && index < __count, E_OUT_OF_RANGE,
341 "[%s] The index(%d) MUST be greater than or equal to 0 and less than the number of elements(%d).",
342 GetErrorMessage(E_OUT_OF_RANGE), index, __count);
344 obj = __pObjArray[index];
349 * Gets the object at the specified @c index of the list.
353 * @return An error code
354 * @param[in] index The index of the object to read
355 * @param[out] obj The object to get from this list
356 * @exception E_SUCCESS The method is successful.
357 * @exception E_OUT_OF_RANGE Either of the following conditions has occurred:
358 * - The specified @c index is outside the bounds of the data structure.
359 * - The specified @c index is either greater than or equal to the number of elements.
360 * - The specified @c index is less than @c 0.
363 virtual result GetAt(int index, Type& obj)
365 TryReturn(index >= 0 && index < __count, E_OUT_OF_RANGE,
366 "[%s] The index(%d) MUST be greater than or equal to 0 and less than the number of elements(%d).",
367 GetErrorMessage(E_OUT_OF_RANGE), index, __count);
369 obj = __pObjArray[index];
374 * Gets a list of the specified number of elements starting from the specified index.
378 * @return An instance of the IListT derived class within the specified range of the list, @n
379 * else @c null if an exception occurs
380 * @param[in] startIndex The index to start reading elements from
381 * @param[in] count The number of elements to read
382 * @exception E_SUCCESS The method is successful.
383 * @exception E_INVALID_ARG A specified input parameter is invalid.
384 * @exception E_OUT_OF_RANGE Either of the following conditions has occurred:
385 * - The specified index is outside the bounds of the data structure.
386 * - The specified @c startIndex is either greater than or equal to the number of elements.
387 * - The specified @c startIndex is less than @c 0.
388 * - The specified @c count is greater than the number of elements starting from @c startIndex.
389 * - The specified @c count is less than @c 0.
391 * @remarks The specific error code can be accessed using the GetLastResult() method.
393 virtual IListT< Type >* GetItemsN(int startIndex, int count) const
395 result r = E_SUCCESS;
397 ArrayListT< Type >* pList = null;
399 TryCatch(startIndex >= 0 && count >= 0, r = E_OUT_OF_RANGE,
400 "[%s] Both of the startIndex(%d) and count(%d) MUST be greater than or equal to 0.", GetErrorMessage(E_OUT_OF_RANGE), startIndex, count);
401 TryCatch(startIndex < __count, r = E_OUT_OF_RANGE,
402 "[%s] The startIndex(%d) MUST be less than the number of elements(%d).", GetErrorMessage(E_OUT_OF_RANGE), startIndex, __count);
403 TryCatch(count <= __count && (startIndex + count <= __count), r = E_OUT_OF_RANGE,
404 "[%s] The startIndex(%d) + count(%d) MUST be less than or equal to the number of elements(%d).",
405 GetErrorMessage(E_OUT_OF_RANGE), startIndex, count, __count);
407 pList = new ArrayListT< Type >();
409 r = pList->Construct(count);
410 TryCatch(r == E_SUCCESS, delete pList, "[%s] Propagating.", GetErrorMessage(r));
412 for (int i = startIndex; i < (startIndex + count); i++)
414 pList->__pObjArray[pList->__count++] = __pObjArray[i];
417 SetLastResult(E_SUCCESS);
426 * Searches for an object in this list. @n
427 * Gets the @c index of the object if found.
431 * @return An error code
432 * @param[in] obj The object to locate
433 * @param[out] index The index of the object
434 * @exception E_SUCCESS The method is successful.
435 * @exception E_OBJ_NOT_FOUND The specified @c obj has not been found.
438 virtual result IndexOf(const Type& obj, int& index) const
440 return IndexOf(obj, 0, __count, index);
444 * Searches for an object starting from the specified @c index. @n
445 * Gets the @c index of the object if found.
449 * @return An error code
450 * @param[in] obj The object to locate
451 * @param[in] startIndex The starting index for the search @n
452 * It must be less than the number of elements.
453 * @param[out] index The index of the object
454 * @exception E_SUCCESS The method is successful.
455 * @exception E_OUT_OF_RANGE Either of the following conditions has occurred:
456 * - The specified @c index is outside the bounds of the data structure.
457 * - The specified @c startIndex is either greater than or equal to the number of elements.
458 * - The specified @c startIndex is less than @c 0.
459 * @exception E_OBJ_NOT_FOUND The specified @c obj has not been found.
462 virtual result IndexOf(const Type& obj, int startIndex, int& index) const
464 TryReturn(startIndex >= 0 && startIndex < __count, E_OUT_OF_RANGE,
465 "[%s] The startIndex(%d) MUST be greater than or equal to 0, and less than the number of elements(%d).",
466 GetErrorMessage(E_OUT_OF_RANGE), startIndex, __count);
468 return IndexOf(obj, startIndex, (__count - startIndex), index);
472 * Searches for an object within the specified range. @n
473 * Gets the @c index of the object if found.
477 * @return An error code
478 * @param[in] obj The object to locate
479 * @param[in] startIndex The starting index of the range
480 * @param[in] count The number of elements to read
481 * @param[out] index The index of the object
482 * @exception E_SUCCESS The method is successful.
483 * @exception E_OUT_OF_RANGE Either of the following conditions has occurred:
484 * - The specified @c index is outside the bounds of the data structure.
485 * - The specified @c startIndex is either greater than or equal to the number of elements.
486 * - The specified @c startIndex is less than @c 0.
487 * - The specified @c count is greater than the number of elements starting from @c startIndex.
488 * - The specified @c count is less than @c 0.
489 * @exception E_OBJ_NOT_FOUND The specified @c obj has not been found.
492 virtual result IndexOf(const Type& obj, int startIndex, int count, int& index) const
494 TryReturn(startIndex >= 0 && count >= 0, E_OUT_OF_RANGE,
495 "[%s] Both of the startIndex(%d) and count(%d) MUST be greater than or equal to 0.", GetErrorMessage(E_OUT_OF_RANGE), startIndex, count);
496 TryReturn(startIndex < __count, E_OUT_OF_RANGE,
497 "[%s] The startIndex(%d) MUST be less than the number of elements(%d).", GetErrorMessage(E_OUT_OF_RANGE), startIndex, __count);
498 TryReturn(count <= __count && (startIndex + count <= __count), E_OUT_OF_RANGE,
499 "[%s] The startIndex(%d) + count(%d) MUST be less than or equal to the number of elements(%d).",
500 GetErrorMessage(E_OUT_OF_RANGE), startIndex, count, __count);
502 int arrayListCount = startIndex + count;
503 for (int i = startIndex; i < arrayListCount; i++)
505 if (obj == __pObjArray[i])
512 return E_OBJ_NOT_FOUND;
516 * Inserts an object at the specified location.
520 * @return An error code
521 * @param[in] obj The object to insert
522 * @param[in] index The index at which the object is inserted
523 * @exception E_SUCCESS The method is successful.
524 * @exception E_OUT_OF_RANGE Either of the following conditions has occurred:
525 * - The specified @c index is outside the bounds of the data structure.
526 * - The specified @c index is greater than the number of elements.
527 * - The specified @c index is less than @c 0.
529 * - The elements that follow the insertion point move down to accommodate the new element.
530 * - If the @c index is equal to the number of elements in the list, the new element
531 * is added at the end of the list.
535 virtual result InsertAt(const Type& obj, int index)
537 TryReturn(index >= 0 && index <= __count, E_OUT_OF_RANGE,
538 "[%s] The index(%d) MUST be greater than or equal to 0, and less than or equal to the number of elements(%d).",
539 GetErrorMessage(E_OUT_OF_RANGE), index, __count);
541 result r = E_SUCCESS;
543 if (__count >= __capacity)
545 r = SetCapacity(__capacity + DEFAULT_CAPACITY);
546 TryReturn(r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
549 for (int i = __count; i > index; i--)
551 __pObjArray[i] = __pObjArray[i - 1];
556 __pObjArray[index] = obj;
562 * Inserts the elements of the collection from a specified location.
566 * @return An error code
567 * @param[in] collection The collection to insert
568 * @param[in] startIndex The index from which the collection is inserted
569 * @exception E_SUCCESS The method is successful.
570 * @exception E_OUT_OF_RANGE Either of the following conditions has occurred:
571 * - The specified @c index is outside the bounds of the data structure.
572 * - The specified @c startIndex is greater than the number of elements.
573 * - The specified @c startIndex is less than @c 0.
574 * @exception E_INVALID_OPERATION Either of the following conditions has occurred:
575 * - The current state of the instance prohibits the execution of the specified operation.
576 * - The specified @c collection is modified during the operation of this method.
578 * - The elements that follow the insertion point move down to accommodate the new elements.
579 * - If the @c startIndex is equal to the number of elements in the list, the new elements
580 * are added at the end of the list.
584 virtual result InsertItemsFrom(const ICollectionT< Type >& collection, int startIndex)
586 TryReturn(startIndex >= 0 && startIndex <= __count, E_OUT_OF_RANGE,
587 "[%s] The startIndex(%d) MUST be greater than or equal to 0, and less than or equal to the number of elements(%d).",
588 GetErrorMessage(E_OUT_OF_RANGE), startIndex, __count);
590 result r = E_SUCCESS;
592 IEnumeratorT< Type >* pEnum = null;
593 int count = collection.GetCount();
597 if (count > (__capacity - __count))
599 r = SetCapacity(__count + count);
600 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
604 for (int i = (__count - 1); i >= (startIndex + count); i--)
606 __pObjArray[i] = __pObjArray[i - count];
609 ICollectionT< Type >* pCol = const_cast< ICollectionT< Type >* >(&collection);
610 pEnum = pCol->GetEnumeratorN();
611 TryReturn(pEnum != null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
615 while ((r = pEnum->MoveNext()) != E_OUT_OF_RANGE)
619 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
621 r = pEnum->GetCurrent(item);
622 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
624 __pObjArray[startIndex++] = item;
637 * Searches for the last occurrence of an object in this list. @n
638 * Gets the @c index of the object if found.
642 * @return An error code
643 * @param[in] obj The object to locate
644 * @param[out] index The index of the last occurrence of the specified object
645 * @exception E_SUCCESS The method is successful.
646 * @exception E_OBJ_NOT_FOUND The specified @c obj has not been found.
649 virtual result LastIndexOf(const Type& obj, int& index) const
651 for (int i = (__count - 1); i >= 0; i--)
653 if (obj == __pObjArray[i])
660 return E_OBJ_NOT_FOUND;
664 * Removes the first occurrence of the specified object.
668 * @return An error code
669 * @param[in] obj The object to remove
670 * @exception E_SUCCESS The method is successful.
671 * @exception E_OBJ_NOT_FOUND The specified @c obj has not been found.
676 virtual result Remove(const Type& obj)
679 result r = IndexOf(obj, index);
682 return E_OBJ_NOT_FOUND;
686 return RemoveAt(index);
691 * Removes all the elements of the specified collection from the list.
695 * @return An error code
696 * @param[in] collection The collection to remove from this list
697 * @exception E_SUCCESS The method is successful.
698 * @exception E_INVALID_OPERATION Either of the following conditions has occurred:
699 * - The current state of the instance prohibits the execution of the specified operation.
700 * - The specified @c collection is modified during the operation of this method.
704 virtual result RemoveItems(const ICollectionT< Type >& collection)
706 result r = E_SUCCESS;
707 int oldCount = __count;
709 ICollectionT< Type >* pCol = const_cast< ICollectionT< Type >* >(&collection);
710 IEnumeratorT< Type >* pEnum = pCol->GetEnumeratorN();
711 TryCatch(pEnum != null, r = GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
713 while ((r = pEnum->MoveNext()) != E_OUT_OF_RANGE)
717 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
719 r = pEnum->GetCurrent(item);
720 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
723 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
726 if (__count < oldCount)
740 * Removes an object from the specified location.
744 * @return An error code
745 * @param[in] index The index of the object to remove
746 * @exception E_SUCCESS The method is successful.
747 * @exception E_OUT_OF_RANGE Either of the following conditions has occurred:
748 * - The specified @c index is outside the bounds of the data structure.
749 * - The specified @c index is greater than or equal to the number of elements.
750 * - The specified @c index is less than @c 0.
751 * @remarks The elements that follow the deleted object move up the list to occupy the empty location.
755 virtual result RemoveAt(int index)
757 TryReturn(index < __count && index >= 0, E_OUT_OF_RANGE,
758 "[%s] The index MUST be greater than or equal to 0, and less than the number of elements(%d).",
759 GetErrorMessage(E_OUT_OF_RANGE), index, __count);
764 for (int i = index; i < __count; i++)
766 __pObjArray[i] = __pObjArray[i + 1];
775 * Removes all the elements within the specified range.
779 * @return An error code
780 * @param[in] startIndex The starting index of the range
781 * @param[in] count The number of elements to remove
782 * @exception E_SUCCESS The method is successful.
783 * @exception E_OUT_OF_RANGE Either of the following conditions has occurred:
784 * - The specified index is outside the bounds of the data structure.
785 * - The specified @c startIndex is either greater than or equal to the number of elements.
786 * - The specified @c startIndex is less than @c 0.
787 * - The specified @c count is greater than the number of elements starting from @c startIndex.
788 * - The specified @c count is less than @c 0.
789 * @remarks The elements that follow the deleted elements move up the list to occupy the empty locations.
791 * @see InsertItemsFrom()
793 virtual result RemoveItems(int startIndex, int count)
795 TryReturn(startIndex >= 0 && count >= 0, E_OUT_OF_RANGE,
796 "[%s] Both of the startIndex(%d) and count(%d) MUST be greater than or equal to 0.", GetErrorMessage(E_OUT_OF_RANGE), startIndex, count);
797 TryReturn(startIndex < __count, E_OUT_OF_RANGE,
798 "[%s] The startIndex(%d) MUST be less than the number of elements(%d).", GetErrorMessage(E_OUT_OF_RANGE), startIndex, __count);
799 TryReturn(count <= __count && (startIndex + count <= __count), E_OUT_OF_RANGE,
800 "[%s] The startIndex(%d) + count(%d) MUST be less than or equal to the number of elements(%d).",
801 GetErrorMessage(E_OUT_OF_RANGE), startIndex, count, __count);
805 Type* newArray = new Type[__capacity];
806 TryReturn(newArray != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
811 for (int i = 0; i < startIndex; i++)
813 newArray[i] = __pObjArray[i];
815 for (int i = startIndex; i < __count; i++)
817 newArray[i] = __pObjArray[i + count];
820 delete[] __pObjArray;
821 __pObjArray = newArray;
828 * Removes all the elements in the list.
832 virtual void RemoveAll(void)
836 delete[] __pObjArray;
848 * Sets the object at the specified @c index of the current instance of ByteBuffer.
852 * @return An error code
853 * @param[in] obj The object to set
854 * @param[in] index The index at which the object must be set
855 * @exception E_SUCCESS The method is successful.
856 * @exception E_OUT_OF_RANGE Either of the following conditions has occurred:
857 * - The specified @c index is outside the bounds of the data structure.
858 * - The specified @c index is either greater than or equal to the number of elements.
859 * - The specified @c index is less than @c 0.
862 virtual result SetAt(const Type& obj, int index)
864 TryReturn(index >= 0 && index < __count, E_OUT_OF_RANGE,
865 "[%s] The index(%d) MUST be greater than or equal to 0, less than the number of elements(%d).",
866 GetErrorMessage(E_OUT_OF_RANGE), index, __count);
870 __pObjArray[index] = obj;
876 * Sets the capacity of the list at the specified value.
880 * @return An error code
881 * @param[in] newCapacity The new capacity to set for the list
882 * @exception E_SUCCESS The method is successful.
883 * @exception E_INVALID_ARG Either of the following conditions has occurred:
884 * - The specified input parameter is invalid.
885 * - The specified @c newCapacity is negative.
886 * @remarks If the new capacity is less than the current capacity, the memory
887 * is truncated and the elements within the truncated memory are destroyed.
892 virtual result SetCapacity(int newCapacity)
894 TryReturn(newCapacity >= 0, E_INVALID_ARG, "[%s] The newCapacity(%d) MUST be greater than or equal to 0.",
895 GetErrorMessage(E_INVALID_ARG), newCapacity);
897 result r = E_SUCCESS;
898 if (__capacity != newCapacity)
900 Type* newArray = null;
903 newArray = new Type[newCapacity];
904 TryCatch(newArray != null, r = E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
906 if (__pObjArray != null)
908 int count = __count < newCapacity ? __count : newCapacity;
909 for (int i = 0; i < count; i++)
911 newArray[i] = __pObjArray[i];
915 if (__pObjArray != null)
917 delete[] __pObjArray;
919 if (__count > newCapacity)
922 __count = newCapacity;
924 __pObjArray = newArray;
925 __capacity = newCapacity;
935 * Sorts the elements in the list using a comparer.
939 * @return An error code
940 * @param[in] comparer A pointer to IComparerT
941 * @exception E_SUCCESS The method is successful.
942 * @exception E_INVALID_ARG Either of the following conditions has occurred:
943 * - The specified input parameter is invalid.
944 * - The specified @c comparer is invalid.
946 virtual result Sort(const IComparerT< Type >& comparer)
951 __pComparer = const_cast< IComparerT< Type >* >(&comparer);
952 result r = QuickSort(0, (__count - 1));
955 AppLogException("[%s] Propagating.", GetErrorMessage(r));
965 * Trims the capacity of the list to the actual number of elements in the list.
969 * @return An error code
970 * @exception E_SUCCESS The method is successful.
971 * @exception E_OUT_OF_MEMORY The memory is insufficient.
972 * @remarks The specific error code can be accessed using the GetLastResult() method.
975 virtual void Trim(void)
982 result r = SetCapacity(__count);
985 AppLogException("[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
990 * Gets the current capacity of the list.
994 * @return The current capacity of the list
997 int GetCapacity(void) const
1003 * Gets the number of objects currently stored in the list.
1007 * @return The number of objects stored in the list
1009 virtual int GetCount(void) const
1015 * Checks whether the list contains the specified object.
1019 * @return @c true if the object is present in the list, @n
1021 * @param[in] obj The object to locate
1022 * @see ContainsAll()
1024 virtual bool Contains(const Type& obj) const
1031 for (int i = 0; i < __count; i++)
1033 if (obj == __pObjArray[i])
1043 * Checks whether the list contains all the elements of the specified @c collection.
1047 * @return An error code
1048 * @param[in] collection The collection to check in the list
1049 * @param[out] out @c true if the list contains all the elements of the specified @c collection, @n
1051 * @exception E_SUCCESS The method is successful.
1052 * @exception E_INVALID_OPERATION Either of the following conditions has occurred:
1053 * - The current state of the instance prohibits the execution of the specified operation.
1054 * - The specified @c collection is modified during the operation of this method.
1055 * @remarks If the given @c collection is empty, then @c out is set to @c true.
1058 virtual result ContainsAll(const ICollectionT< Type >& collection, bool& out) const
1060 result r = E_SUCCESS;
1063 if (collection.GetCount() == 0)
1069 ICollectionT< Type >* pCol = const_cast< ICollectionT< Type >* >(&collection);
1070 IEnumeratorT< Type >* pEnum = pCol->GetEnumeratorN();
1071 TryReturn(pEnum != null, r = GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
1073 while ((r = pEnum->MoveNext()) != E_OUT_OF_RANGE)
1077 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
1079 r = pEnum->GetCurrent(item);
1080 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
1082 if (false == Contains(item))
1104 * Compares two instances of %ArrayListT.
1108 * @return @c true if the two instances match, @n
1110 * @param[in] obj The object to compare with the current instance
1111 * @remarks This method returns @c true if and only if the two instances contain the same elements in the same order.
1113 virtual bool Equals(const Tizen::Base::Object& obj) const
1115 const ArrayListT< Type >* other = dynamic_cast< const ArrayListT< Type >* >(&obj);
1120 else if (other == this)
1124 else if (__count != other->__count)
1130 for (int i = 0; i < __count; i++)
1132 if (__pObjArray[i] != other->__pObjArray[i])
1143 * Gets the hash value of the current instance.
1147 * @return The hash value of the current instance
1148 * @remarks The two Tizen::Base::Object::Equals() instances must return the same hash value. @n
1149 * For better performance, the used hash function must generate a random distribution for all the inputs.
1151 virtual int GetHashCode(void) const
1154 for (int i = 0; i < __count; i++)
1156 if (&(__pObjArray[i]) != null)
1158 hash += reinterpret_cast< int >(&(__pObjArray[i]));
1166 * The implementation of this copy constructor is intentionally blank and declared as private to prohibit copying of objects.
1168 * @param[in] list The instance of the %ArrayListT class to copy from
1170 ArrayListT(const ArrayListT< Type >& list);
1173 * The implementation of this copy assignment operator is intentionally blank and declared as private to prohibit copying of objects.
1175 * @param[in] list An instance of %ArrayListT
1177 ArrayListT< Type >& operator =(const ArrayListT< Type >& list);
1180 * Sorts a section of a list using a comparer.
1182 * @return An error code
1183 * @param[in] startIndex The start index of the section of the list
1184 * @param[in] endIndex The end index of the section of the list
1185 * @exception E_SUCCESS The method is successful.
1186 * @exception E_INVALID_ARG A specified input parameter is invalid, or
1187 * the comparer has failed to compare the elements.
1189 result QuickSort(int startIndex, int endIndex)
1191 result r = E_SUCCESS;
1193 if (startIndex < endIndex)
1196 int i = startIndex - 1;
1197 int j = endIndex + 1;
1200 int compareResult = 1;
1201 while ((compareResult > 0) && (j > static_cast< int >(startIndex)))
1205 r = __pComparer->Compare(__pObjArray[j], __pObjArray[startIndex], compareResult);
1206 TryReturn(r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
1210 while ((compareResult < 0) && (i < static_cast< int >(endIndex)))
1214 r = __pComparer->Compare(__pObjArray[i], __pObjArray[startIndex], compareResult);
1215 TryReturn(r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
1220 Type temp = __pObjArray[j];
1221 __pObjArray[j] = __pObjArray[i];
1222 __pObjArray[i] = temp;
1231 r = QuickSort(startIndex, middleIndex);
1232 TryReturn(r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
1234 r = QuickSort(middleIndex + 1, endIndex);
1235 TryReturn(r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
1245 IComparerT< Type >* __pComparer;
1246 static const int DEFAULT_CAPACITY = 10;
1248 friend class __ArrayListEnumeratorT< Type >;
1253 // @class __ArrayListEnumeratorT
1254 // @brief This class is an implementation of the IEnumeratorT interface for the %ArrayListT class.
1257 template< class Type >
1258 class __ArrayListEnumeratorT
1259 : public IBidirectionalEnumeratorT< Type >
1260 , public Tizen::Base::Object
1263 __ArrayListEnumeratorT(const ArrayListT< Type >& list, int modCount)
1265 , __modCount(modCount)
1270 virtual ~__ArrayListEnumeratorT(void)
1274 virtual result GetCurrent(Type& obj) const
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));
1278 TryReturn((__position > -1) && (__position < static_cast< int >(__list.__count)), E_INVALID_OPERATION,
1279 "[%s] Current position(%d) is before the first element or past the last element.", GetErrorMessage(E_INVALID_OPERATION), __position);
1281 obj = __list.__pObjArray[__position];
1285 virtual result MoveNext(void)
1287 TryReturn((__modCount == __list.__modCount), E_INVALID_OPERATION,
1288 "[%s] The source collection is modified after the creation of this enumerator.", GetErrorMessage(E_INVALID_OPERATION));
1290 if ((__position + 1) >= static_cast< int >(__list.__count))
1292 return E_OUT_OF_RANGE;
1302 virtual result Reset(void)
1304 TryReturn((__modCount == __list.__modCount), E_INVALID_OPERATION,
1305 "[%s] The source collection is modified after the creation of this enumerator.", GetErrorMessage(E_INVALID_OPERATION));
1311 virtual result MovePrevious(void)
1313 TryReturn(__modCount == __list.__modCount, E_INVALID_OPERATION,
1314 "[%s] The source collection is modified after the creation of this enumerator.", GetErrorMessage(E_INVALID_OPERATION));
1316 TryReturn(__position > 0, E_OUT_OF_RANGE, "[%s] Reached start of the list, no previous element", GetErrorMessage(E_OUT_OF_RANGE));
1323 virtual result ResetLast(void)
1325 TryReturn(__modCount == __list.__modCount, E_INVALID_OPERATION,
1326 "[%s] The source collection is modified after the creation of this enumerator.", GetErrorMessage(E_INVALID_OPERATION));
1328 __position = __list.__count;
1333 const ArrayListT< Type >& __list;
1337 }; //__ArrayListEnumeratorT
1339 } } } // Tizen::Base::Collection
1341 #endif // _FBASE_COL_ARRAY_LIST_T_H_