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 A function pointer to the type of the element deleter
118 * - To create an owning collection, set the element deleter value as @c SingleObjectDeleter. @n
119 * This gives the collection the ownership of the elements and the collection will destroy the elements. @n
120 * On the other hand, to create a non-owning collection, you do not need to set the element deleter value,
121 * as @c NoOpDeleter is the default element deleter. @n
122 * It means that you do not transfer the ownership of the elements to the collection.
123 * - After creating an instance of the %ArrayList class, one of the Construct() methods must be called explicitly to initialize this instance.
125 * @see SingleObjectDeleter()
126 * @see ArrayDeleter()
128 explicit ArrayList(DeleterFunctionType deleter = NoOpDeleter);
131 * This destructor overrides Tizen::Base::Object::~Object().
135 virtual ~ArrayList(void);
138 * Initializes this instance of %ArrayList with the specified parameter and sets its initial capacity to the
143 * @return An error code
144 * @param[in] capacity The number of elements @n
145 * The default capacity is @c 10.
146 * @exception E_SUCCESS The method is successful.
147 * @exception E_INVALID_ARG Either of the following conditions has occurred:
148 * - A specified input parameter is invalid.
149 * - The specified @c capacity is negative.
150 * @remarks If the number of elements added to the list reaches the current capacity,
151 * the capacity is automatically increased by memory reallocation. @n
152 * Therefore, if the size of the list can be estimated,
153 * specifying the initial capacity eliminates the need to perform a number of
154 * resizing operations while adding elements to the list.
157 result Construct(int capacity = DEFAULT_CAPACITY);
161 * Initializes this instance of %ArrayList by copying the elements of the specified
163 * The capacity of the list is the same as the number of elements copied.
167 * @return An error code
168 * @param[in] collection The collection 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.
173 * @remarks This method performs a shallow copy. It copies just the pointer and not the element itself.
176 result Construct(const ICollection& collection);
179 * Adds the specified object to the end of this list.
183 * @return An error code
184 * @param[in] pObj A pointer to the object to add
185 * @exception E_SUCCESS The method is successful.
186 * @exception E_INVALID_ARG The specified input parameter is invalid.
187 * @remarks This method performs a shallow copy. It copies just the pointer and not the element itself.
190 virtual result Add(Object* pObj);
194 * Adds the elements of the specified @c collection to the end of this list.
198 * @return An error code
199 * @param[in] collection The collection to add
200 * @exception E_SUCCESS The method is successful.
201 * @exception E_INVALID_OPERATION Either of the following conditions has occurred:
202 * - The current state of the instance prohibits the execution of the specified operation.
203 * - The specified @c collection is modified during the operation of this method.
204 * @remarks This method performs a shallow copy. It copies just the pointer and not the element itself.
207 virtual result AddItems(const ICollection& collection);
211 * Gets the enumerator (an instance of the IEnumerator derived class) of this list.
215 * @return An instance of the IEnumerator derived class, @n
216 * else @c null if an exception occurs
217 * @remarks The specific error code can be accessed using the GetLastResult() method.
219 virtual IEnumerator* GetEnumeratorN(void) const;
222 * Gets the bidirectional enumerator (an instance of the IBidirectionalEnumerator derived class) of this list.
226 * @return An instance of the IBidirectionalEnumerator derived class, @n
227 * else @c null if an exception occurs
229 * - Use this method to obtain a bidirectional enumerator (an instance of the IBidirectionalEnumerator derived class)
230 * to iterate over a collection (an instance of the IList derived class).
231 * - The specific error code can be accessed using GetLastResult() method.
233 virtual IBidirectionalEnumerator* GetBidirectionalEnumeratorN(void) const;
236 * Gets the object at the specified @c index of this list.
240 * @return The object at the specified @c index of this list, @n
241 * else @c null if the @c index is not valid
242 * @param[in] index The index of the object to read in the calling list
243 * @exception E_SUCCESS The method is successful.
244 * @exception E_OUT_OF_RANGE Either of the following conditions has occurred:
245 * - The specified @c index is outside the bounds of the data structure.
246 * - The specified @c index is either equal to or greater than the number of elements.
247 * - The specified @c index is less than @c 0.
248 * @remarks The specific error code can be accessed using the GetLastResult() method.
251 virtual const Object* GetAt(int index) const;
255 * Gets the object at the specified @c index of this list.
259 * @return The object at the specified @c index of this list, @n
260 * else @c null if the @c index is not valid
261 * @param[in] index The index of the object to read
262 * @exception E_SUCCESS The method is successful.
263 * @exception E_OUT_OF_RANGE Either of the following conditions has occurred:
264 * - The specified @c index is outside the bounds of the data structure.
265 * - The specified @c index is either equal to or greater than the number of elements.
266 * - The specified @c index is less than @c 0.
267 * @remarks The specific error code can be accessed using the GetLastResult() method.
270 virtual Object* GetAt(int index);
273 * Gets an IList instance within the specified range of this list.
277 * @return A pointer to IList, @n
278 * else @c null if an exception occurs
279 * @param[in] startIndex The starting index of the range
280 * @param[in] count The number of elements to read
281 * @exception E_SUCCESS The method is successful.
282 * @exception E_OUT_OF_RANGE Either of the following conditions has occurred:
283 * - The specified index is outside the bounds of the data structure.
284 * - The specified @c startIndex is either equal to or greater than the number of elements.
285 * - The specified @c startIndex is less than @c 0.
286 * - The specified @c count is greater than the number of elements starting from @c startIndex.
287 * - The specified @c count is less than @c 0.
289 * - The IList stores only the pointers to the elements in the list, not the elements themselves.
290 * - The specific error code can be accessed using the GetLastResult() method.
292 virtual IList* GetItemsN(int startIndex, int count) const;
295 * Searches for an object in this list. @n
296 * Gets the @c index of the object if found.
300 * @return An error code
301 * @param[in] obj The object to locate
302 * @param[out] index The index of the object
303 * @exception E_SUCCESS The method is successful.
304 * @exception E_OBJ_NOT_FOUND The specified @c obj has not been found.
307 virtual result IndexOf(const Object& obj, int& index) const;
310 * Searches for an object starting from the specified @c startIndex. @n
311 * Gets the @c index of the object if found.
315 * @return An error code
316 * @param[in] obj The object to locate
317 * @param[in] startIndex The starting index for the search @n
318 * It must be less than the number of elements.
319 * @param[out] index The index of the object
320 * @exception E_SUCCESS The method is successful.
321 * @exception E_OUT_OF_RANGE Either of the following conditions has occurred:
322 * - The specified @c index is outside the bounds of the data structure.
323 * - The specified @c startIndex is either equal to or greater than the number of elements.
324 * - The specified @c startIndex is less than @c 0.
325 * @exception E_OBJ_NOT_FOUND The specified @c obj has not been 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 @c 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:
343 * - The specified @c index is outside the bounds of the data structure.
344 * - The specified @c startIndex is either equal to or greater than the number of elements.
345 * - The specified @c startIndex is less than @c 0.
346 * - The specified @c count is greater than the number of elements starting from @c startIndex.
347 * - The specified @c count is less than @c 0.
348 * @exception E_OBJ_NOT_FOUND The specified @c obj has not been found.
351 virtual result IndexOf(const Object& obj, int startIndex, int count, int& index) const;
354 * Searches for the last occurrence of an object in this list. @n
355 * Gets the @c index of the object if found.
359 * @return An error code
360 * @param[in] obj The object to locate
361 * @param[out] index The index of the last occurrence of the specified object
362 * @exception E_SUCCESS The method is successful.
363 * @exception E_OBJ_NOT_FOUND The specified @c obj has not been found.
366 virtual result LastIndexOf(const Object& obj, int& index) const;
369 * Inserts the object at the specified location.
373 * @return An error code
374 * @param[in] pObj A pointer to the object to insert
375 * @param[in] index The index at which the object is inserted
376 * @exception E_SUCCESS The method is successful.
377 * @exception E_INVALID_ARG The specified input parameter is invalid.
378 * @exception E_OUT_OF_RANGE Either of the following conditions has occurred:
379 * - The specified @c index is outside the bounds of the data structure.
380 * - The specified @c index is greater than the number of elements.
381 * - The specified @c index is less than @c 0.
383 * - The elements that follow the insertion point move down to accommodate the new element.
384 * - If the @c index is equal to the number of elements, then the new element
385 * is added at the end of this list.
386 * - This method performs a shallow copy. It inserts just the pointer and not the element itself.
390 virtual result InsertAt(Object* pObj, int index);
393 * Inserts the elements of the collection at the specified location.
397 * @return An error code
398 * @param[in] collection The collection to insert
399 * @param[in] startIndex The starting index at which the collection is inserted
400 * @exception E_SUCCESS The method is successful.
401 * @exception E_OUT_OF_RANGE Either of the following conditions has occurred:
402 * - The specified index is outside the bounds of the data structure.
403 * - The specified @c startIndex is greater than the number of elements.
404 * - The specified @c startIndex is less than @c 0.
405 * @exception E_INVALID_OPERATION Either of the following conditions has occurred:
406 * - The current state of the instance prohibits the execution of the specified operation.
407 * - The specified @c collection is modified during the operation of this method.
409 * - The elements that follow the insertion point move down to accommodate the new element.
410 * - If the @c startIndex is equal to the number of elements then the new elements
411 * are added at the end of this list.
412 * - This method performs a shallow copy. It inserts just the pointer and not the element itself.
416 virtual result InsertItemsFrom(const ICollection& collection, int startIndex);
419 * Removes the first occurrence of the specified object.
423 * @return An error code
424 * @param[in] obj The object to remove
425 * @exception E_SUCCESS The method is successful.
426 * @exception E_OBJ_NOT_FOUND The specified @c obj has not been found.
431 virtual result Remove(const Object& obj);
434 * Removes the object at the specified location.
438 * @return An error code
439 * @param[in] index The index at which the object is removed
440 * @exception E_SUCCESS The method is successful.
441 * @exception E_OUT_OF_RANGE Either of the following conditions has occurred:
442 * - The specified @c index is outside the bounds of the data structure.
443 * - The specified @c index is either equal to or greater than the number of elements.
444 * - The specified @c index is less than @c 0.
445 * @remarks The elements that follow the deletion point move up to occupy the vacated spot.
449 virtual result RemoveAt(int index);
452 * Removes all the elements within the specified range.
456 * @return An error code
457 * @param[in] startIndex The starting index of the range
458 * @param[in] count The number of elements to read
459 * @exception E_SUCCESS The method is successful.
460 * @exception E_OUT_OF_RANGE Either of the following conditions has occurred:
461 * - The specified index is outside the bounds of the data structure.
462 * - The specified @c startIndex is either equal to or greater than the number of elements.
463 * - The specified @c startIndex is less than @c 0.
464 * - The specified @c count is greater than the number of elements starting from @c startIndex.
465 * - The specified @c count is less than @c 0.
466 * @remarks The elements that follow the deletion point move up to occupy the vacated spot.
469 virtual result RemoveItems(int startIndex, int count);
472 * Removes all of the elements that are in the intersection of the specified @c collection
477 * @return An error code
478 * @param[in] collection The collection to remove from this list
479 * @exception E_SUCCESS The method is successful.
480 * @exception E_INVALID_OPERATION Either of the following conditions has occurred:
481 * - The current state of the instance prohibits the execution of the specified operation.
482 * - The specified @c collection is modified during the operation of this method.
486 virtual result RemoveItems(const ICollection& collection);
489 * Removes all the object pointers in the collection and also removes all the objects depending on the specified element deleter.
490 * The %RemoveAll() can be called before the collection is deleted.
494 virtual void RemoveAll(void);
497 * Replaces the object at the specified @c index with the specified object.
501 * @return An error code
502 * @param[in] pObj A pointer to the object to set
503 * @param[in] index The index at which the object is set
504 * @exception E_SUCCESS The method is successful.
505 * @exception E_INVALID_ARG A specified input parameter is invalid.
506 * @exception E_OUT_OF_RANGE Either of the following conditions has occurred:
507 * - The specified @c index is outside the bounds of the data structure.
508 * - The specified @c index is either equal to or greater than the number of elements.
509 * - The specified @c index is less than @c 0.
512 virtual result SetAt(Object* pObj, int index);
515 * Sets the capacity of this list at the specified value.
519 * @return An error code
520 * @param[in] newCapacity The new capacity of this list
521 * @exception E_SUCCESS The method is successful.
522 * @exception E_INVALID_ARG Either of the following conditions has occurred:
523 * - The specified input parameter is invalid.
524 * - The specified @c newCapacity is negative.
525 * @remarks When the new capacity is less than the current capacity, the elements
526 * within the truncated memory are not destroyed.
531 virtual result SetCapacity(int newCapacity);
534 * Sorts the elements of this list using the @c comparer provided.
538 * @return An error code
539 * @param[in] comparer The IComparer implementation to use when comparing elements
540 * @exception E_SUCCESS The method is successful.
541 * @exception E_INVALID_ARG The specified input parameter is invalid.
543 virtual result Sort(const IComparer& comparer);
546 * Sets the capacity to the actual number of elements in this list.
550 virtual void Trim(void);
553 * Gets the current capacity of this list.
557 * @return The current capacity of this list
560 virtual int GetCapacity(void) const;
563 * Gets the number of objects currently stored in this list.
567 * @return The number of objects currently stored in this list
569 virtual int GetCount(void) const;
572 * Checks whether the list contains the specified object.
576 * @return @c true if the object is present in the list, @n
578 * @param[in] obj The object to locate
581 virtual bool Contains(const Object& obj) const;
584 * Checks whether the list contains all the elements of the specified @c collection.
588 * @return @c true if the list contains all the elements of the specified @c collection, @n
590 * @param[in] collection The collection to check for in the list
591 * @exception E_SUCCESS The method is successful.
592 * @exception E_INVALID_OPERATION Either of the following conditions has occurred:
593 * - The current state of the instance prohibits the execution of the specified operation.
594 * - The specified @c collection is modified during the operation of this method.
596 * - The specific error code can be accessed using the GetLastResult() method.
597 * - If the given @c collection is empty, this method will return @c true.
600 virtual bool ContainsAll(const ICollection& collection) const;
603 * Compares the specified Tizen::Base::Object instance with the calling %ArrayList instance.
607 * @return @c true if the given Tizen::Base::Object matches the calling list, @n
609 * @param[in] obj The object to compare with the calling list
610 * @remarks This method returns @c true only if the specified @c obj is also an instance of %ArrayList,
611 * both lists have the same size, and all the corresponding pairs of the elements in the two lists are equal. @n
612 * In other words, the two lists are equal if they contain the same elements in the same order.
614 virtual bool Equals(const Object& obj) const;
617 * Gets the hash value of the current instance.
621 * @return The hash value of the current instance
622 * @remarks The two Tizen::Base::Object::Equals() instances must return the same hash value. @n
623 * For better performance, the used hash function must generate a random distribution for all the inputs.
625 virtual int GetHashCode(void) const;
628 * Checks whether the instance is an %ArrayList or a LinkedList.
631 * @return @c true if it is an %ArrayList, @n
632 * else @c false if it is a LinkedList
634 virtual bool IsRandomAccessible(void) const
640 * Gets the element deleter of the collection.
644 * @return A function pointer to the existing element deleter
646 virtual DeleterFunctionType GetDeleter(void) const;
650 * The implementation of this copy constructor is intentionally blank and declared as private to prohibit copying of objects.
652 * @param[in] list The instance of the %ArrayList class to copy from
654 ArrayList(const ArrayList& list);
657 * The implementation of this copy assignment operator is intentionally blank and declared as private to prohibit copying of objects.
659 * @param[in] list An instance of %ArrayList
661 ArrayList& operator =(const ArrayList& list);
664 * Sorts the specified sub-list.
666 * @return An error code
667 * @param[in] startIndex The starting point of the sub-list to sort
668 * @param[in] endIndex The end point of the sub-list to sort
669 * @exception E_SUCCESS The method is successful.
670 * @exception E_INVALID_ARG A specified input parameter is invalid, or
671 * the comparer has failed to compare the elements.
673 result QuickSort(int startIndex, int endIndex);
676 * Sets the element deleter of the collection.
680 * @param[in] deleter A function pointer to the element deleter to set
682 virtual void SetDeleter(DeleterFunctionType deleter);
686 Object** __pObjArray;
688 IComparer* __pComparer;
689 static const int DEFAULT_CAPACITY = 10;
690 DeleterFunctionType __deleter;
692 friend class _ArrayListEnumerator;
693 friend class _ArrayListImpl;
694 class _ArrayListImpl* __pArrayListImpl;
698 }}} // Tizen::Base::Collection
700 #endif // _FBASE_COL_ARRAY_LIST_H_