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 FBaseColArrayList.h
19 * @brief This is the header file for the %ArrayList class.
21 * This header file contains the declarations of the %ArrayList class.
23 #ifndef _FBASE_COL_ARRAY_LIST_H_
24 #define _FBASE_COL_ARRAY_LIST_H_
26 #include <FBaseObject.h>
27 #include <FBaseColIComparer.h>
28 #include <FBaseColIList.h>
31 namespace Tizen { namespace Base { namespace Collection
36 * @brief This class represents a collection of objects that can be individually accessed by an index.
41 * The %ArrayList class represents a 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 %ArrayList class.
51 * using namespace Tizen::Base;
52 * using namespace Tizen::Base::Collection;
56 * MyClass::ArrayListSample(void)
58 * ArrayList list(SingleObjectDeleter);
62 * list.Add(new Integer(1)); // 1
63 * list.Add(new Integer(2)); // 1,2
64 * list.Add(new Integer(3)); // 1,2,3
66 * Integer* pInt = static_cast< Integer* > (list.GetAt(0));
68 * if (pInt->Equals(Integer(1)))
73 * list.InsertAt(new Integer(4), 1); // 1,4,2,3
75 * list.Remove(Integer(3)); // 1,4,2
77 * list.RemoveAt(0); // 4,2
79 * list.Sort(IntegerComparer()); // 2,4
81 * // Uses an enumerator to access elements in the list
82 * IEnumerator* pEnum = list.GetEnumeratorN();
83 * Object* pObj = null;
84 * while (pEnum->MoveNext() == E_SUCCESS)
86 * pObj = pEnum->GetCurrent();
92 * // Deallocates all objects
93 * // Because the destructor calls RemoveAll() internally, you do not need to call RemoveAll() to destroy all elements at the end.
94 * // list.RemoveAll();
98 class _OSP_EXPORT_ ArrayList
104 using IList::InsertAt;
106 using IList::RemoveAt;
107 using IList::RemoveItems;
108 using IList::RemoveAll;
110 using IList::ContainsAll;
112 * This is the default constructor for this class.
116 * @param[in] deleter The function pointer to type of the element deleter
117 * @remarks To create an owing collection, set the element deleter value as @c SingleObjectDeleter. This gives the collection the ownership of elements and the collection will destroy elements. @n
118 * On the other hand, to create a non-owning collection, you do not need to set the element deleter value, as @c NoOpDeleter is the default element deleter.
119 * It means that you do not transfer the ownership of elements to the collection.
120 * @remarks After creating an instance of the %ArrayList class, one of the Construct() methods must be called explicitly to initialize this instance.
122 * @see SingleObjectDeleter()
123 * @see ArrayDeleter()
125 explicit ArrayList(DeleterFunctionType deleter = NoOpDeleter);
128 * This destructor overrides Tizen::Base::Object::~Object().
132 virtual ~ArrayList(void);
135 * Initializes this instance of %ArrayList with the specified parameter and sets its initial capacity to the
140 * @return An error code
141 * @param[in] capacity The number of elements @n
142 * The default capacity is @c 10.
143 * @exception E_SUCCESS The method is successful.
144 * @exception E_INVALID_ARG A specified input parameter is invalid, or
145 * the specified @c capacity is negative.
146 * @remarks If the number of elements added to the list reaches the current capacity,
147 * the capacity is automatically increased by memory reallocation.
148 * Therefore, if the size of the list can be estimated,
149 * specifying the initial capacity eliminates the need to perform a number of
150 * resizing operations while adding elements to the list.
153 result Construct(int capacity = DEFAULT_CAPACITY);
157 * Initializes this instance of %ArrayList by copying the elements of the specified
159 * The capacity of the list is the same as the number of elements copied.
163 * @return An error code
164 * @param[in] collection A collection to add
165 * @exception E_SUCCESS The method is successful.
166 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
167 * the @c collection is modified during the operation of this method.
168 * @remarks This method performs a shallow copy. It copies just the pointer; not the element itself.
171 result Construct(const ICollection& collection);
174 * Adds the specified object to the end of this list.
178 * @return An error code
179 * @param[in] pObj An pointer to object to add
180 * @exception E_SUCCESS The method is successful.
181 * @exception E_INVALID_ARG The specified input parameter is invalid.
182 * @remarks This method performs a shallow copy. It adds just the pointer; not the element itself.
185 virtual result Add(Object* pObj);
189 * Adds the elements of the specified @c collection to the end of this list.
193 * @return An error code
194 * @param[in] collection A collection to add
195 * @exception E_SUCCESS The method is successful.
196 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
197 * the @c collection is modified during the operation of this method.
198 * @remarks This method performs a shallow copy. It adds just the pointer; not the element itself.
201 virtual result AddItems(const ICollection& collection);
205 * Gets an enumerator (an instance of the IEnumerator-derived class) of this list.
209 * @return An instance of the IEnumerator-derived class, @n
210 * else @c null if some exception occurs
211 * @remarks The specific error code can be accessed using the GetLastResult() method.
213 virtual IEnumerator* GetEnumeratorN(void) const;
216 * Gets a bidirectional enumerator (an instance of the IBidirectionalEnumerator derived class) of this list.
220 * @return An instance of the IBidirectionalEnumerator derived class, @n
221 * else @c null if some exception occurs
222 * @remarks Use this method to obtain a bidirectional enumerator (an instance of the IBidirectionalEnumerator derived class)
223 * to iterate over a collection (an instance of the IList derived class).
224 * The specific error code can be accessed using GetLastResult() method.
225 * @see Tizen::Base::Collection::IBidirectionalEnumerator
227 virtual IBidirectionalEnumerator* GetBidirectionalEnumeratorN(void) const;
230 * Gets the object at the specified @c index of this list.
234 * @return The object at the specified @c index of this list, @n
235 * else @c null if the index is not valid
236 * @param[in] index The index of the object in the calling list to read
237 * @exception E_SUCCESS The method is successful.
238 * @exception E_OUT_OF_RANGE The specified @c index is outside the bounds of the data structure, or
239 * the specified @c index is either equal to or greater than the number of elements or less than @c 0.
240 * @remarks The specific error code can be accessed using the GetLastResult() method.
243 virtual const Object* GetAt(int index) const;
247 * Gets the object at the specified @c index of this list.
251 * @return The object at the specified @c index of this list, @n
252 * else @c null if the index is not valid
253 * @param[in] index The index of the object to read
254 * @exception E_SUCCESS The method is successful.
255 * @exception E_OUT_OF_RANGE The specified @c index is outside the bounds of the data structure, or
256 * the specified @c index is either equal to or greater than the number of elements or less than @c 0.
257 * @remarks The specific error code can be accessed using the GetLastResult() method.
260 virtual Object* GetAt(int index);
263 * Gets the reference of the object at the specified @c index of this list.
267 * @return The reference of the object at the specified @c index of this list
268 * @param[in] index The index of the object to read
269 * @exception E_SUCCESS The method is successful.
270 * @exception E_OUT_OF_RANGE The specified @c index is outside the bounds of the data structure, or
271 * the specified @c index is either equal to or greater than the number of elements or less than @c 0.
272 * @remarks The specific error code can be accessed using the GetLastResult() method.
274 virtual Object*& GetAtRef(int index);
277 * Gets the IList within the specified range of this list.
281 * @return A pointer to IList, @n
282 * else @c null if some exception occurs
283 * @param[in] startIndex The starting index of the range
284 * @param[in] count The number of elements to read
285 * @exception E_SUCCESS The method is successful.
286 * @exception E_OUT_OF_RANGE Either of the following conditions has occurred: @n
287 * - The specified index is outside the bounds of the data structure. @n
288 * - The specified @c startIndex is either equal to or greater than the number of elements or less than @c 0. @n
289 * - The @c count is greater than the number of elements starting from @c startIndex
291 * @remarks The IList stores just the pointers to the elements in the list, not the elements themselves.
292 * The specific error code can be accessed using the GetLastResult() method.
294 virtual IList* GetItemsN(int startIndex, int count) const;
297 * Searches for an object in this list. @n
298 * Gets the index of the object if found.
302 * @return An error code
303 * @param[in] obj The object to locate
304 * @param[out] index The index of the object
305 * @exception E_SUCCESS The method is successful.
306 * @exception E_OBJ_NOT_FOUND The specified @c obj is not found.
309 virtual result IndexOf(const Object& obj, int& index) const;
312 * Searches for an object starting from the specified index. @n
313 * Gets the index of the object if found.
317 * @return An error code
318 * @param[in] obj The object to locate
319 * @param[in] startIndex The starting index for the search @n
320 * It must be less than the number of elements.
321 * @param[out] index The index of the object
322 * @exception E_SUCCESS The method is successful.
323 * @exception E_OUT_OF_RANGE The specified index is outside the bounds of the data structure, or
324 * the specified @c startIndex is either equal to or greater than the number of elements or less than @c 0.
325 * @exception E_OBJ_NOT_FOUND The specified @c obj is not found.
328 virtual result IndexOf(const Object& obj, int startIndex, int& index) const;
331 * Searches for an object within the specified range. @n
332 * Gets the index of the object if found.
336 * @return An error code
337 * @param[in] obj The object to locate
338 * @param[in] startIndex The starting index of the range
339 * @param[in] count The number of elements to read
340 * @param[out] index The index of the object
341 * @exception E_SUCCESS The method is successful.
342 * @exception E_OUT_OF_RANGE Either of the following conditions has occurred: @n
343 * - The specified index is outside the bounds of the data structure. @n
344 * - The specified @c startIndex is either equal to or greater than the number of elements or less than @c 0. @n
345 * - The @c count is greater than the number of elements starting from @c startIndex
347 * @exception E_OBJ_NOT_FOUND The specified @c obj is not found.
350 virtual result IndexOf(const Object& obj, int startIndex, int count, int& index) const;
353 * Searches for the last occurrence of an object in this list. @n
354 * Gets the index of the object if found.
358 * @return An error code
359 * @param[in] obj The object to locate
360 * @param[out] index The index of the last occurrence of the specified object
361 * @exception E_SUCCESS The method is successful.
362 * @exception E_OBJ_NOT_FOUND The specified @c obj is not found.
365 virtual result LastIndexOf(const Object& obj, int& index) const;
368 * Inserts the object at the specified location.
372 * @return An error code
373 * @param[in] pObj The pointer to object to insert
374 * @param[in] index The index at which the object must be inserted
375 * @exception E_SUCCESS The method is successful.
376 * @exception E_INVALID_ARG The specified input parameter is invalid.
377 * @exception E_OUT_OF_RANGE The specified index is outside the bounds of the data structure, or
378 * the @c index is greater than the number of elements or less than @c 0.
379 * @remarks The elements that follow the insertion point move down to accommodate the new element.
380 * If the @c index equals to the number of elements, then the new element
381 * is added at the end of this list.
382 * This method performs a shallow copy. It inserts just the pointer; not the element itself.
386 virtual result InsertAt(Object* pObj, int index);
389 * Inserts the elements of the collection at the specified location.
393 * @return An error code
394 * @param[in] collection The collection to insert
395 * @param[in] startIndex The starting index at which the collection must be inserted
396 * @exception E_SUCCESS The method is successful.
397 * @exception E_OUT_OF_RANGE The specified index is outside the bounds of the data structure, or
398 * the @c startIndex is greater than the number of elements or less than @c 0.
399 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
400 * the @c collection is modified during the operation of this method.
401 * @remarks The elements that follow the insertion point move down to accommodate the new element.
402 * If the @c startIndex equals to the number of elements then the new elements
403 * are added at the end of this list.
404 * This method performs a shallow copy. It inserts just the pointer; not the element itself.
408 virtual result InsertItemsFrom(const ICollection& collection, int startIndex);
411 * Removes the first occurrence of the specified object.
415 * @return An error code
416 * @param[in] obj An object to remove
417 * @exception E_SUCCESS The method is successful.
418 * @exception E_OBJ_NOT_FOUND The specified @c obj is not found.
423 virtual result Remove(const Object& obj);
426 * Removes the object at the specified location.
430 * @return An error code
431 * @param[in] index The index at which the object must be removed
432 * @exception E_SUCCESS The method is successful.
433 * @exception E_OUT_OF_RANGE The specified index is outside the bounds of the data structure, or
434 * the specified @c index is either equal to or greater than the number of elements or less than @c 0.
435 * @remarks The elements that follow the deletion point move up to occupy the vacated spot.
439 virtual result RemoveAt(int index);
442 * Removes all the elements within the specified range.
446 * @return An error code
447 * @param[in] startIndex The starting index of the range
448 * @param[in] count The number of elements to read
449 * @exception E_SUCCESS The method is successful.
450 * @exception E_OUT_OF_RANGE Either of the following conditions has occurred: @n
451 * - The specified index is outside the bounds of the data structure. @n
452 * - The specified @c startIndex is either equal to or greater than the number of elements or less than @c 0. @n
453 * - The @c count is greater than the number of elements starting from @c startIndex
455 * @remarks The elements that follow the deletion point move up to occupy the vacated spot.
458 virtual result RemoveItems(int startIndex, int count);
461 * Removes all of the elements that are in the intersection of the specified @c collection
466 * @return An error code
467 * @param[in] collection The collection to remove from this list
468 * @exception E_SUCCESS The method is successful.
469 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
470 * the @c collection is modified during the operation of this method.
474 virtual result RemoveItems(const ICollection& collection);
477 * Removes all of the object pointers in the collection and also removes all of the objects depending on the specified element deleter.
478 * The %RemoveAll() can be called before the collection is deleted.
482 virtual void RemoveAll(void);
485 * Replaces the object at the specified @c index with the specified object.
489 * @return An error code
490 * @param[in] pObj An pointer to object to set
491 * @param[in] index The index at which the object must be set
492 * @exception E_SUCCESS The method is successful.
493 * @exception E_INVALID_ARG The specified input parameter is invalid.
494 * @exception E_OUT_OF_RANGE The specified index is outside the bounds of the data structure, or
495 * the specified @c index is either equal to or greater than the number of elements or less than @c 0.
498 virtual result SetAt(Object* pObj, int index);
501 * Sets the capacity of this list to the specified value.
505 * @return An error code
506 * @param[in] newCapacity The new capacity of this list
507 * @exception E_SUCCESS The method is successful.
508 * @exception E_INVALID_ARG The specified input parameter is invalid, or
509 * the @c newCapacity is negative.
510 * @remarks When the new capacity is less than the current capacity, the elements
511 * within the truncated memory are not destroyed.
516 virtual result SetCapacity(int newCapacity);
519 * Sorts the elements of this list using the comparer provided.
523 * @return An error code
524 * @param[in] comparer The IComparer implementation to use when comparing elements
525 * @exception E_SUCCESS The method is successful.
526 * @exception E_INVALID_ARG The specified input parameter is invalid.
528 virtual result Sort(const IComparer& comparer);
531 * Sets the capacity to the actual number of elements in this list.
535 virtual void Trim(void);
538 * Gets the current capacity of this list.
542 * @return The current capacity of this list
545 virtual int GetCapacity(void) const;
548 * Gets the number of objects currently stored in this list.
552 * @return The number of objects currently stored in this list
554 virtual int GetCount(void) const;
557 * Checks whether a list contains the specified object.
561 * @return @c true if the object is present in the list, @n
563 * @param[in] obj The object to locate
566 virtual bool Contains(const Object& obj) const;
569 * Checks whether the list contains all the elements of the specified @c collection.
573 * @return @c true if the list contains all the elements of the specified @c collection, @n
575 * @param[in] collection The collection to check for in the list
576 * @exception E_SUCCESS The method is successful.
577 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
578 * the @c collection is modified during the operation of this method.
579 * @remarks The specific error code can be accessed using the GetLastResult() method.
580 * @remarks If the given @c collection is empty, this method will return @c true.
583 virtual bool ContainsAll(const ICollection& collection) const;
586 * Compares the specified Object instance with the calling %ArrayList instance.
590 * @return @c true if the given object matches the calling List, @n
592 * @param[in] obj The object to compare with the calling list
593 * @remarks This method returns @c true only if the specified object @c obj is also an instance of %ArrayList class,
594 * both lists have the same size, and all the corresponding pairs of the elements in the two lists are equal.
595 * In other words, the two lists are equal if they contain the same elements in the same order.
597 virtual bool Equals(const Object& obj) const;
600 * Gets the hash value of the current instance.
604 * @return The hash value of the current instance
605 * @remarks The two Tizen::Base::Object::Equals() instances must return the same hash value. For better performance, @n
606 * the used hash function must generate a random distribution for all inputs.
608 virtual int GetHashCode(void) const;
611 * Distinguish between %ArrayList and LinkedList.
614 * @return @c true if it is an %ArrayList, @n
615 * else @c false if it is a LinkedList
617 virtual bool IsRandomAccessible(void) const
623 * Gets the element deleter of the collection.
627 * @return A function pointer to the existing element deleter
629 virtual DeleterFunctionType GetDeleter(void) const;
633 * The implementation of this copy constructor is intentionally blank and declared as private to prohibit copying of objects.
635 * @param[in] list The instance of the %ArrayList class to copy from
637 ArrayList(const ArrayList& list);
640 * The implementation of this copy assignment operator is intentionally blank and declared as private to prohibit copying of objects.
642 * @param[in] list An instance of %ArrayList
644 ArrayList& operator =(const ArrayList& list);
647 * Sorts the specified sub-list.
649 * @return An error code
650 * @param[in] startIndex The starting point of the sub-list to sort
651 * @param[in] endIndex The end point of the sub-list to sort
652 * @exception E_SUCCESS The method is successful.
653 * @exception E_INVALID_ARG A specified input parameter is invalid, or
654 * the comparer has failed to compare the elements.
656 result QuickSort(int startIndex, int endIndex);
659 * Sets the element deleter of the collection.
663 * @param[in] deleter A function pointer to the element deleter to set
665 virtual void SetDeleter(DeleterFunctionType deleter);
669 Object** __pObjArray;
671 IComparer* __pComparer;
672 static const int DEFAULT_CAPACITY = 10;
673 DeleterFunctionType __deleter;
675 friend class _ArrayListEnumerator;
676 friend class _ArrayListImpl;
677 class _ArrayListImpl* __pArrayListImpl;
681 }}} // Tizen::Base::Collection
683 #endif // _FBASE_COL_ARRAY_LIST_H_