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 FBaseColLinkedListT.h
19 * @brief This is the header file for the %LinkedListT class.
21 * This header file contains the declarations of the %LinkedListT class.
23 #ifndef _FCOL_LINKED_LIST_T_H_
24 #define _FCOL_LINKED_LIST_T_H_
26 #include <FBaseResult.h>
27 #include <FBaseColIComparerT.h>
28 #include <FBaseColIListT.h>
30 namespace Tizen { namespace Base { namespace Collection
33 template< class Type > class __LinkedListEnumeratorT;
34 template< class Type > class __LinkedListNodeT;
38 * @brief This class represents a template-based collection of objects that can be individually accessed by index.
42 * The %LinkedListT class represents a template-based collection of objects that can be individually accessed by 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 %LinkedListT class.
52 * using namespace Tizen::Base;
53 * using namespace Tizen::Base::Collection;
56 * MyClass::LinkedListTSample(void)
58 * LinkedListT< 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
83 * list.RemoveAt(0); // 2,4
85 * // Uses an enumerator to access elements in the list
86 * IEnumeratorT< int >* pEnum = list.GetEnumeratorN();;
87 * while (pEnum->MoveNext() == E_SUCCESS)
89 * pEnum->GetCurrent(temp);
96 template< class Type >
98 : public IListT< Type >
103 * This is the default constructor for this class.
116 * This destructor overrides Tizen::Base::Object::~Object().
120 virtual ~LinkedListT(void)
128 * Adds the specified object to the end of this list.
132 * @return An error code
133 * @param[in] obj An object to add
134 * @exception E_SUCCESS The method is successful.
135 * @exception E_OUT_OF_MEMORY The memory is insufficient.
138 virtual result Add(const Type& obj)
140 result r = E_SUCCESS;
142 if (null == __pListHead)
144 r = InsertFirst(obj);
152 AppLogException("[%s] Propagating.", GetErrorMessage(r));
159 * Adds the elements of the specified collection to the end of this list.
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 specified @c collection is modified during the operation of this method.
170 virtual result AddItems(const ICollectionT< Type >& collection)
172 result r = InsertItemsFrom(collection, __count);
175 AppLogException("[%s] Propagating.", GetErrorMessage(r));
182 * Gets an enumerator to this list.
186 * @return An enumerator (an instance of the IEnumeratorT derived class) of this list, @n
187 * else @c null if an exception occurs
188 * @exception E_SUCCESS The method is successful.
189 * @exception E_OUT_OF_MEMORY The memory is insufficient.
190 * @remarks The specific error code can be accessed using the GetLastResult() method.
191 * @see Tizen::Base::Collection::IEnumeratorT
193 virtual IEnumeratorT< Type >* GetEnumeratorN(void) const
197 result r = E_SUCCESS;
199 __LinkedListEnumeratorT< Type >* pEnum = new __LinkedListEnumeratorT< Type >(*this, __modCount);
200 TryCatch(pEnum != null, r = E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
210 * Gets a bidirectional enumerator (an instance of the IBidirectionalEnumeratorT derived class) of this list.
214 * @return An instance of the IBidirectionalEnumeratorT derived class if successful, @n
215 * else @c null if an exception occurs
216 * @exception E_SUCCESS The method is successful.
217 * @exception E_OUT_OF_MEMORY The memory is insufficient.
218 * @remarks Use this method to obtain a bidirectional enumerator (an instance of the IBidirectionalEnumeratorT derived class)
219 * to iterate over a collection (an instance of the IListT derived class).
220 * The specific error code can be accessed using the GetLastResult() method.
221 * @see Tizen::Base::Collection::IBidirectionalEnumeratorT
223 virtual IBidirectionalEnumeratorT< Type >* GetBidirectionalEnumeratorN(void) const
227 result r = E_SUCCESS;
229 __LinkedListEnumeratorT< Type >* pEnum = new __LinkedListEnumeratorT< Type >(*this, __modCount);
230 TryCatch(pEnum != null, r = E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
240 * Gets the object at the specified index of this list.
244 * @return An error code
245 * @param[in] index The index of the object to read
246 * @param[out] obj An object to get from this list
247 * @exception E_SUCCESS The method is successful.
248 * @exception E_OUT_OF_RANGE The specified index is outside the bounds of the data structure, or
249 * the specified @c index is equal to or greater than the number of elements or less than @c 0.
252 virtual result GetAt(int index, Type& obj) const
254 TryReturn(index >= 0 && index < __count, E_OUT_OF_RANGE,
255 "[%s] The index(%d) MUST be greater than or equal to 0, and less than the number of elements(%d).",
256 GetErrorMessage(E_OUT_OF_RANGE), index, __count);
258 __LinkedListNodeT< Type >* pNode = GetNode(index);
267 * Gets the object at the specified index of this list.
271 * @return An error code
272 * @param[in] index The index of the object to read
273 * @param[out] obj An object to get from this list
274 * @exception E_SUCCESS The method is successful.
275 * @exception E_OUT_OF_RANGE The specified index is outside the bounds of the data structure, or
276 * the specified @c index is equal to or greater than the number of elements or less than @c 0.
279 virtual result GetAt(int index, Type& obj)
281 TryReturn(index >= 0 && index < __count, E_OUT_OF_RANGE,
282 "[%s] The index(%d) MUST be greater than or equal to 0, and less than the number of elements(%d).",
283 GetErrorMessage(E_OUT_OF_RANGE), index, __count);
285 __LinkedListNodeT< Type >* pNode = GetNode(index);
294 * Gets an IListT-derived instance with the specified range from the calling list object.
298 * @return A pointer to the IListT derived instance, @n
299 * else @c null if an exception occurs.
300 * @param[in] startIndex The starting index of the range
301 * @param[in] count The number of elements to read
302 * @exception E_SUCCESS The method is successful.
303 * @exception E_OUT_OF_RANGE Either of the following conditions has occurred: @n
304 * - The specified index is outside the bounds of the data structure. @n
305 * - The specified @c startIndex is either equal to or greater than the number of elements or less than @c 0. @n
306 * - The specified @c count is either greater than the number of elements starting from @c startIndex or less than @c 0.
308 * @remarks The specific error code can be accessed using the GetLastResult() method.
310 virtual IListT< Type >* GetItemsN(int startIndex, int count) const
314 result r = E_SUCCESS;
315 LinkedListT< Type >* pList = null;
316 __LinkedListNodeT< Type >* pNode = null;
317 __LinkedListNodeT< Type >* pPrevNode = null;
319 TryCatch(startIndex >= 0 && count >= 0, r = E_OUT_OF_RANGE,
320 "[%s] Both of the startIndex(%d) and count(%d) MUST be greater than or equal to 0.",
321 GetErrorMessage(E_OUT_OF_RANGE), startIndex, count);
322 TryCatch(startIndex < __count, r = E_OUT_OF_RANGE,
323 "[%s] The startIndex(%d) MUST be less than the number of elements(%d).", GetErrorMessage(E_OUT_OF_RANGE), startIndex, __count);
324 TryCatch(count <= __count && (startIndex + count <= __count), r = E_OUT_OF_RANGE,
325 "[%s] The startIndex(%d) + count(%d) MUST be less than or equal to the number of elements(%d).",
326 GetErrorMessage(E_OUT_OF_RANGE), startIndex, count, __count);
328 pList = new LinkedListT< Type >();
330 pNode = GetNode(startIndex);
331 if ((pList != null) && (pNode != null))
333 r = pList->InsertFirst(pNode->pObj);
334 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
336 pNode = pNode->pNext;
337 pPrevNode = pList->__pListTail;
339 for (int i = 0; i < (count - 1); i++)
343 pPrevNode = pList->InsertNext(pPrevNode, pNode->pObj);
344 TryCatch(pPrevNode != null, r = GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
345 pNode = pNode->pNext;
360 * Searches for an object in this list. @n
361 * Gets the index of the object if found.
365 * @return An error code
366 * @param[in] obj The object to locate
367 * @param[out] index The index of the object
368 * @exception E_SUCCESS The method is successful.
369 * @exception E_OBJ_NOT_FOUND The specified @c obj is not found.
371 virtual result IndexOf(const Type& obj, int& index) const
373 result r = E_SUCCESS;
380 r = IndexOf(obj, 0, __count, index);
387 * Searches for an object starting from the specified index. @n
388 * Gets the index of the object if found.
392 * @return An error code
393 * @param[in] obj The object to locate
394 * @param[in] startIndex The starting index for the search @n
395 * It must be less than the number of elements.
396 * @param[out] index The index of the object
397 * @exception E_SUCCESS The method is successful.
398 * @exception E_OUT_OF_RANGE The specified index is outside the bounds of the data structure, or
399 * the specified @c startIndex is either equal to or greater than the number of elements in the list or less than @c 0.
400 * @exception E_OBJ_NOT_FOUND The specified @c obj is not found.
403 virtual result IndexOf(const Type& obj, int startIndex, int& index) const
405 TryReturn((startIndex >= 0 && startIndex < __count), E_OUT_OF_RANGE,
406 "[%s] The startIndex(%d) MUST be greater than or equal to 0, and less than the number of elements(%d).",
407 GetErrorMessage(E_OUT_OF_RANGE), startIndex, __count);
409 return IndexOf(obj, startIndex, (__count - startIndex), index);
413 * Searches for an object within the specified range. @n
414 * Gets the index of the object if found.
418 * @return An error code
419 * @param[in] obj The object to locate
420 * @param[in] startIndex The starting index of the range
421 * @param[in] count The number of elements to read
422 * @param[out] index The index of the object
423 * @exception E_SUCCESS The method is successful.
424 * @exception E_OUT_OF_RANGE Either of the following conditions has occurred: @n
425 * - The specified index is outside the bounds of the data structure. @n
426 * - The specified @c startIndex is either equal to or greater than the number of elements in the list or less than @c 0. @n
427 * - The specified @c count is either greater than the number of elements starting from @c startIndex or less than @c 0.
428 * @exception E_OBJ_NOT_FOUND The specified @c obj is not found.
431 virtual result IndexOf(const Type& obj, int startIndex, int count, int& index) const
433 TryReturn(startIndex >= 0 && count >= 0, E_OUT_OF_RANGE,
434 "[%s] Both of the startIndex(%d) and count(%d) MUST be greater than or equal to 0.",
435 GetErrorMessage(E_OUT_OF_RANGE), startIndex, count);
436 TryReturn(startIndex < __count, E_OUT_OF_RANGE,
437 "[%s] The startIndex(%d) MUST be less than the number of elements(%d).", GetErrorMessage(E_OUT_OF_RANGE), startIndex, __count);
438 TryReturn(count <= __count && (startIndex + count <= __count), E_OUT_OF_RANGE,
439 "[%s] The startIndex(%d) + count(%d) MUST be less than or equal to the number of elements(%d).",
440 GetErrorMessage(E_OUT_OF_RANGE), startIndex, count, __count);
442 result r = E_OBJ_NOT_FOUND;
443 __LinkedListNodeT< Type >* pNode = GetNode(startIndex);
444 for (int i = 0; i < count; i++)
448 if (obj == pNode->pObj)
450 index = (startIndex + i);
454 pNode = pNode->pNext;
462 * Inserts the object at the specified location.
466 * @return An error code
467 * @param[in] obj An object to insert
468 * @param[in] index The index at which the object must be inserted
469 * @exception E_SUCCESS The method is successful.
470 * @exception E_OUT_OF_RANGE The specified index is outside the bounds of the data structure, or
471 * the specified @c index is greater than the number of elements or less than @c 0.
472 * @remarks If the @c index equals to the number of elements then the new element
473 * is added at the end of this list.
477 virtual result InsertAt(const Type& obj, int index)
479 TryReturn(index >= 0 && index <= __count, E_OUT_OF_RANGE,
480 "[%s] The index(%d) MUST be greater than or equal to 0, and less than or equal to the number of elements(%d).",
481 GetErrorMessage(E_OUT_OF_RANGE), index, __count);
483 result r = E_SUCCESS;
485 __LinkedListNodeT< Type >* pPrevNode = null;
488 r = InsertFirst(obj);
490 else if (index == __count)
496 pPrevNode = GetNode(index - 1);
497 if (pPrevNode != null)
499 InsertNext(pPrevNode, obj);
505 AppLogException("[%s] Propagating.", GetErrorMessage(r));
513 * Inserts the elements of the @c collection at the location specified.
517 * @return An error code
518 * @param[in] collection The collection to insert
519 * @param[in] startIndex The starting index at which the collection must be inserted
520 * @exception E_SUCCESS The method is successful.
521 * @exception E_OUT_OF_RANGE The specified index is outside the bounds of the data structure, or
522 * the specified @c startIndex is either greater than the number of elements or less than @c 0.
523 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
524 * the specified @c collection is modified during the operation of this method.
525 * @remarks If the @c startIndex equals to the number of elements then the new elements
526 * are added at the end of this list.
530 virtual result InsertItemsFrom(const ICollectionT< Type >& collection, int startIndex)
532 TryReturn(startIndex >= 0 && startIndex <= __count, E_OUT_OF_RANGE,
533 "[%s] The startIndex(%d) MUST be greater than or equal to 0, and less than or equal to the number of elements(%d).",
534 GetErrorMessage(E_OUT_OF_RANGE), startIndex, __count);
536 result r = E_SUCCESS;
538 IEnumeratorT< Type >* pEnum = null;
539 int count = collection.GetCount();
542 __LinkedListNodeT< Type >* pPrevNode = null;
544 if (startIndex == __count)
546 pPrevNode = __pListTail;
548 else if (startIndex > 0)
550 pPrevNode = GetNode(startIndex - 1);
553 ICollectionT< Type >* pCol = const_cast< ICollectionT< Type >* >(&collection);
554 pEnum = pCol->GetEnumeratorN();
555 TryCatch(pEnum != null, r = GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
560 r = pEnum->MoveNext();
562 if (E_OUT_OF_RANGE == r)
567 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
569 r = pEnum->GetCurrent(obj);
570 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
572 if (null == pPrevNode)
574 r = InsertFirst(obj);
575 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
576 pPrevNode = __pListHead;
580 pPrevNode = InsertNext(pPrevNode, obj);
581 TryCatch(pPrevNode != null, r = GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
602 * Searches for the last occurrence of an object in this list. @n
603 * Gets the index of the object if found.
607 * @return An error code
608 * @param[in] obj The object to locate
609 * @param[out] index The index of the last occurrence of the specified object
610 * @exception E_SUCCESS The method is successful.
611 * @exception E_OBJ_NOT_FOUND The specified @c obj is not found.
614 virtual result LastIndexOf(const Type& obj, int& index) const
616 result r = E_OBJ_NOT_FOUND;
617 __LinkedListNodeT< Type >* pNode = __pListTail;
618 for (int i = (__count - 1); i >= 0; i--)
620 if (obj == pNode->pObj)
626 pNode = pNode->pPrev;
633 * Removes the first occurrence of the specified object from the list.
637 * @return An error code
638 * @param[in] obj An object to remove
639 * @exception E_SUCCESS The method is successful.
640 * @exception E_OBJ_NOT_FOUND The specified @c obj is not found.
645 virtual result Remove(const Type& obj)
647 result r = E_OBJ_NOT_FOUND;
648 __LinkedListNodeT< Type >* pNode = __pListHead;
649 while (null != pNode)
651 if (pNode->pObj == obj)
657 pNode = pNode->pNext;
664 * Removes all the elements in the specified @c collection.
668 * @return An error code
669 * @param[in] collection The collection to remove from this list
670 * @exception E_SUCCESS The method is successful.
671 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
672 * the specified @c collection is modified during the operation of this method.
676 virtual result RemoveItems(const ICollectionT< Type >& collection)
678 result r = E_SUCCESS;
680 ICollectionT< Type >* pCol = const_cast< ICollectionT< Type >* >(&collection);
681 IEnumeratorT< Type >* pEnum = pCol->GetEnumeratorN();
682 TryCatch(pEnum != null, r = GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
687 r = pEnum->MoveNext();
689 if (E_OUT_OF_RANGE == r)
694 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
696 r = pEnum->GetCurrent(temp);
697 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
700 TryLog(r == E_SUCCESS, "[%s] Remove() failed.", GetErrorMessage(r));
718 * Removes the object at the specified location.
722 * @return An error code
723 * @param[in] index The index at which the object must be removed
724 * @exception E_SUCCESS The method is successful.
725 * @exception E_OUT_OF_RANGE The specified index is outside the bounds of the data structure, or
726 * the specified @c index is equal to or greater than the number of elements or less than @c 0.
729 virtual result RemoveAt(int index)
731 TryReturn(index < __count && index >= 0, E_OUT_OF_RANGE,
732 "[%s] The index(%d) MUST be greater than or equal to 0, and less than the number of elements(%d).",
733 GetErrorMessage(E_OUT_OF_RANGE), index, __count);
735 RemoveNode(GetNode(index));
740 * Removes all elements within the specified range.
744 * @return An error code
745 * @param[in] startIndex The starting index of the range
746 * @param[in] count The number of elements to read
747 * @exception E_SUCCESS The method is successful.
748 * @exception E_OUT_OF_RANGE Either of the following conditions has occurred: @n
749 * - The specified index is outside the bounds of the data structure. @n
750 * - The specified @c startIndex is either equal to or greater than the number of elements or less than @c 0. @n
751 * - The specified @c count is either greater than the number of elements starting from @c startIndex or less than @c 0.
753 * @see InsertItemsFrom()
755 virtual result RemoveItems(int startIndex, int count)
757 TryReturn(startIndex >= 0 && count >= 0, E_OUT_OF_RANGE,
758 "[%s] Both of the startIndex(%d) and count(%d) MUST be greater than or equal to 0.",
759 GetErrorMessage(E_OUT_OF_RANGE), startIndex, count);
760 TryReturn(startIndex < __count, E_OUT_OF_RANGE,
761 "[%s] The startIndex(%d) MUST be less than the number of elements(%d).", GetErrorMessage(E_OUT_OF_RANGE), startIndex, __count);
762 TryReturn(count <= __count && (startIndex + count <= __count), E_OUT_OF_RANGE,
763 "[%s] The startIndex(%d) + count(%d) MUST be less than or equal to the number of elements(%d).",
764 GetErrorMessage(E_OUT_OF_RANGE), startIndex, count, __count);
768 __LinkedListNodeT< Type >* pNode = GetNode(startIndex);
769 for (int i = 0; i < count; i++)
773 __LinkedListNodeT< Type >* pNextNode = pNode->pNext;
783 * Removes all the elements from this list.
787 virtual void RemoveAll(void)
792 __LinkedListNodeT< Type >* pNode = __pListHead;
793 __LinkedListNodeT< Type >* pTemp;
794 while (null != pNode)
796 pTemp = pNode->pNext;
807 * Sets the object at the specified index with the specified object.
811 * @return An error code
812 * @param[in] obj An object to set
813 * @param[in] index The index at which the object must be set
814 * @exception E_SUCCESS The method is successful.
815 * @exception E_OUT_OF_RANGE The specified index is outside the bounds of the data structure, or
816 * the specified @c index is equal to or greater than the number of elements or less than @c 0.
819 virtual result SetAt(const Type& obj, int index)
821 TryReturn(index >= 0 && index < __count, E_OUT_OF_RANGE,
822 "[%s] The index(%d) MUST be greater than or equal to 0, less than the number of elements(%d).",
823 GetErrorMessage(E_OUT_OF_RANGE), index, __count);
826 __LinkedListNodeT< Type >* pNode = GetNode(index);
836 * Sorts the elements of this list using the comparer provided.
840 * @return An error code
841 * @param[in] comparer The IComparerT implementation to use when comparing the elements
842 * @exception E_SUCCESS The method is successful.
843 * @exception E_INVALID_ARG A specified input parameter is invalid, or
844 * the @c comparer is not valid.
846 virtual result Sort(const IComparerT< Type >& comparer)
853 result r = QuickSort(0, (__count - 1), comparer);
856 AppLogException("[%s] Propagating.", GetErrorMessage(r));
863 * Gets the number of objects currently stored in this list.
867 * @return The number of objects currently stored in this list
869 virtual int GetCount(void) const
875 * Checks whether the list contains the specified object.
879 * @return @c true if the list contains the specified object, @n
881 * @param[in] obj The object to locate
884 virtual bool Contains(const Type& obj) const
891 __LinkedListNodeT< Type >* pNode = GetNode(0);
892 for (int i = 0; i < __count; i++)
896 if (obj == pNode->pObj)
900 pNode = pNode->pNext;
908 * Checks whether the list contains all the elements of the specified collection.
912 * @return An error code
913 * @param[in] collection The collection to check for containment in this list
914 * @param[out] out Set to @c true if this list contains all of the elements in the specified collection, @n
916 * @exception E_SUCCESS The method is successful.
917 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
918 * the specified @c collection is modified during the operation of this method.
919 * @remarks If the given @c collection is empty, the @c out parameter is set to @c true.
922 virtual result ContainsAll(const ICollectionT< Type >& collection, bool& out) const
924 result r = E_SUCCESS;
927 if (collection.GetCount() == 0)
933 ICollectionT< Type >* pCol = const_cast< ICollectionT< Type >* >(&collection);
934 IEnumeratorT< Type >* pEnum = pCol->GetEnumeratorN();
935 TryCatch(pEnum != null, r = GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
941 r = pEnum->MoveNext();
943 if (E_OUT_OF_RANGE == r)
949 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
951 r = pEnum->GetCurrent(temp);
952 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
954 if (false == Contains(temp))
976 * Compares the given object with the calling LinkedList object.
980 * @return @c true if the specified instance equals to the current instance, @n
982 * @param[in] obj The object to compare with the calling object
983 * @remarks This method returns @c true only if the specified object is also an instance of LinkedList class,
984 * both lists have the same size, and all corresponding pairs of elements in the two lists are equal.
985 * In other words, two lists are equal if they contain the same elements in the same order.
987 virtual bool Equals(const Object& obj) const
991 const LinkedListT< Type >* other = dynamic_cast< const LinkedListT< Type >* >(&obj);
996 else if (other == this)
1000 else if (__count != other->__count)
1006 __LinkedListNodeT< Type >* pNode = __pListHead;
1007 __LinkedListNodeT< Type >* pOtherNode = other->__pListHead;
1008 for (int i = 0; i < __count; i++)
1010 if (!(pNode->pObj == pOtherNode->pObj))
1015 pNode = pNode->pNext;
1016 pOtherNode = pOtherNode->pNext;
1024 * Gets the hash value of the current instance.
1028 * @return The hash value of the current instance
1029 * @remarks The two Tizen::Base::Object::Equals() instances must return the same hash value. For better performance, @n
1030 * the used hash function must generate a random distribution for all inputs.
1032 virtual int GetHashCode(void) const
1037 __LinkedListNodeT< Type >* pNode = __pListHead;
1038 while (pNode != null)
1040 hash += reinterpret_cast< int >(pNode);
1041 pNode = pNode->pNext;
1050 * The implementation of this copy constructor is intentionally blank and declared as private to prohibit copying of objects.
1052 * @param[in] list The %LinkedListT object to initialize the new object
1054 LinkedListT(const LinkedListT< Type >& list);
1057 * The implementation of this copy assignment operator is intentionally blank and declared as private to prohibit copying of objects.
1059 * @param[in] list An instance of %LinkedListT
1061 LinkedListT< Type >& operator =(const LinkedListT< Type >& list);
1064 * Gets the node at the specified index.
1066 * @return A node at the specified index
1067 * @param[in] index The index of the node to read
1069 __LinkedListNodeT< Type >* GetNode(int index) const
1071 if (index >= __count)
1076 __LinkedListNodeT< Type >* pNode = __pListHead;
1077 for (int i = 0; i < index; i++)
1079 pNode = pNode->pNext;
1086 * Inserts an object to the beginning of the %LinkedList.
1088 * @return An error code
1089 * @param[in] obj The object to insert
1090 * @exception E_OUT_OF_MEMORY The memory is insufficient.
1091 * @exception E_SUCCESS The method is successful.
1093 result InsertFirst(const Type& obj)
1095 result r = E_SUCCESS;
1096 __LinkedListNodeT< Type >* pNode = new __LinkedListNodeT< Type >(obj);
1097 TryCatch(pNode != null, r = E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1101 if (null == __pListHead)
1103 __pListHead = pNode;
1104 __pListTail = pNode;
1108 pNode->pNext = __pListHead;
1109 __pListHead->pPrev = pNode;
1110 __pListHead = pNode;
1120 * Inserts an object to the end of the LinkedList.
1122 * @return An error code
1123 * @param[in] obj The object to insert
1124 * @exception E_OUT_OF_MEMORY The memory is insufficient.
1125 * @exception E_SUCCESS The method is successful.
1127 result InsertLast(const Type& obj)
1129 __LinkedListNodeT< Type >* pNode = InsertNext(__pListTail, obj);
1132 AppLogException("[%s] Propagating.", GetErrorMessage(GetLastResult()));
1135 return GetLastResult();
1139 * Inserts an object after the specified node.
1141 * @return An error code
1142 * @param[in] obj The object to insert
1143 * @param[in] pPrevNode The node after which the object must be inserted
1144 * @exception E_OUT_OF_MEMORY The memory is insufficient.
1145 * @exception E_SUCCESS The method is successful.
1146 * @remarks The specific error code can be accessed using the GetLastResult() method.
1148 __LinkedListNodeT< Type >* InsertNext(__LinkedListNodeT< Type >* pPrevNode, const Type& obj)
1151 result r = E_SUCCESS;
1152 __LinkedListNodeT< Type >* pNode = new __LinkedListNodeT< Type >(obj);
1153 TryCatch(pNode != null, r = E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1157 if (null == __pListHead)
1159 __pListHead = pNode;
1160 __pListTail = pNode;
1162 else if (pPrevNode == __pListTail)
1164 pNode->pPrev = __pListTail;
1165 __pListTail->pNext = pNode;
1166 __pListTail = pNode;
1170 TryCatch(pPrevNode != null, r = E_INVALID_ARG, "[%s] The pPrevNode is null.", GetErrorMessage(E_INVALID_ARG));
1171 pNode->pPrev = pPrevNode;
1172 pNode->pNext = pPrevNode->pNext;
1173 pPrevNode->pNext->pPrev = pNode;
1174 pPrevNode->pNext = pNode;
1191 * Removes the specified node.
1193 * @param[in] pNode The pointer of the node to remove
1195 void RemoveNode(__LinkedListNodeT< Type >* pNode)
1197 AppAssertf((null != pNode), "pNode is null.\n");
1198 AppAssertf((__count > 0), "__count is zero.\n");
1207 else if (pNode == __pListHead)
1209 __pListHead = pNode->pNext;
1210 __pListHead->pPrev = null;
1212 else if (pNode == __pListTail)
1214 __pListTail = pNode->pPrev;
1215 __pListTail->pNext = null;
1219 pNode->pNext->pPrev = pNode->pPrev;
1220 pNode->pPrev->pNext = pNode->pNext;
1226 * Sorts the specified sub-list within the calling instance.
1228 * @param[in] startIndex The starting index of the sub-list to sort
1229 * @param[in] endIndex The ending index of the sub-list to sort
1230 * @param[in] pComparer The comparer function to sort the list
1231 * @exception E_SUCCESS The method is successful.
1232 * @exception E_INVALID_ARG A specified input parameter is invalid, or @n
1233 * the comparer has failed to compare the elements.
1235 result QuickSort(int startIndex, int endIndex, const IComparerT< Type >& comparer)
1237 result r = E_SUCCESS;
1239 if (startIndex < endIndex)
1242 int i = startIndex - 1;
1243 int j = endIndex + 1;
1244 Type startObj = Type();
1245 __LinkedListNodeT< Type >* pNodeI = null;
1246 __LinkedListNodeT< Type >* pNodeJ = null;
1247 __LinkedListNodeT< Type >* pNode = GetNode(startIndex);
1251 startObj = pNode->pObj;
1256 int compareResult = 1;
1257 while ((compareResult > 0) && (j > static_cast< int >(startIndex)))
1260 pNodeJ = (null == pNodeJ) ? GetNode(j) : pNodeJ->pPrev;
1263 r = comparer.Compare(pNodeJ->pObj, startObj, compareResult);
1264 TryReturn(r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
1269 while ((compareResult < 0) && (i < static_cast< int >(endIndex)))
1272 pNodeI = (null == pNodeI) ? GetNode(i) : pNodeI->pNext;
1275 r = comparer.Compare(pNodeI->pObj, startObj, compareResult);
1276 TryReturn(r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
1282 if ((pNodeJ != null) && (pNodeI != null))
1284 Type temp = pNodeJ->pObj;
1285 pNodeJ->pObj = pNodeI->pObj;
1286 pNodeI->pObj = temp;
1296 r = QuickSort(startIndex, middleIndex, comparer);
1297 TryReturn(r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
1299 r = QuickSort(middleIndex + 1, endIndex, comparer);
1300 TryReturn(r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
1306 __LinkedListNodeT< Type >* __pListHead;
1307 __LinkedListNodeT< Type >* __pListTail;
1310 friend class __LinkedListEnumeratorT< Type >;
1315 // @class __LinkedListNodeT
1316 // @brief This is a node for LinkedListT.
1319 template< class Type >
1320 class __LinkedListNodeT
1325 * This is the constructor for this class.
1329 * @param[in] obj An object to include in this node
1331 __LinkedListNodeT(const Type& obj)
1339 * Internal variable.
1343 __LinkedListNodeT< Type >* pPrev;
1346 * Internal variable.
1350 __LinkedListNodeT< Type >* pNext;
1353 * Internal variable.
1359 }; // __LinkedListNodeT
1362 // @class __LinkedListEnumeratorT
1363 // @brief This class is an implementation of IEnumeratorT for %LinkedListT.
1366 template< class Type >
1367 class __LinkedListEnumeratorT
1368 : public IBidirectionalEnumeratorT< Type >
1373 * This is the constructor for this class.
1377 * @param[in] list A list to enumerate
1378 * @param[in] modCount The modification count to detect the change in the list
1380 __LinkedListEnumeratorT(const LinkedListT< Type >& list, int modCount)
1383 , __modCount(modCount)
1389 * This is the destructor for this class.
1393 virtual ~__LinkedListEnumeratorT(void)
1399 * Gets the current object in the list.
1403 * @return An error code
1404 * @param[out] obj The current object
1405 * @exception E_INVALID_OPERATION Either of the following conditions has occurred: @n
1406 * - The current state of the instance prohibits the execution of the specified operation. @n
1407 * - This enumerator is currently positioned before the first element or
1408 * past the last element. @n
1409 - The list is modified after this enumerator is created.
1410 * @exception E_SUCCESS The method is successful.
1412 result GetCurrent(Type& obj) const
1414 TryReturn(__modCount == __list.__modCount, E_INVALID_OPERATION,
1415 "[%s] The source collection is modified after the creation of this enumerator.", GetErrorMessage(E_INVALID_OPERATION));
1416 TryReturn(__pNode != null, E_INVALID_OPERATION,
1417 "[%s] Current position is before the first element or past the last element.", GetErrorMessage(E_INVALID_OPERATION));
1419 obj = __pNode->pObj;
1425 * Moves this enumerator to the next element of the list. @n
1426 * When this enumerator is first created or after call to Reset(),
1427 * the first call to MoveNext() positions this enumerator to the first element in the list.
1431 * @return An error code
1432 * @exception E_SUCCESS The method is successful.
1433 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
1434 * the list is modified after this enumerator is created.
1435 * @exception E_OUT_OF_RANGE The enumerator has passed the end of the list.
1438 result MoveNext(void)
1440 TryReturn(__modCount == __list.__modCount, E_INVALID_OPERATION,
1441 "[%s] The source collection is modified after the creation of this enumerator.", GetErrorMessage(E_INVALID_OPERATION));
1443 if (null == __pNode)
1445 __pNode = __list.__pListHead;
1446 if (null == __pNode)
1448 return E_OUT_OF_RANGE;
1453 if (null != __pNode->pNext)
1455 __pNode = __pNode->pNext;
1459 return E_OUT_OF_RANGE;
1467 * Positions this enumerator before the first elements in the list.
1471 * @return An error code
1472 * @exception E_SUCCESS The method is successful.
1473 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
1474 * the list is modified after this enumerator is created.
1478 TryReturn(__modCount == __list.__modCount, E_INVALID_OPERATION,
1479 "[%s] The source collection is modified after the creation of this enumerator.", GetErrorMessage(E_INVALID_OPERATION));
1485 result MovePrevious(void)
1487 TryReturn(__modCount == __list.__modCount, E_INVALID_OPERATION,
1488 "[%s] The source collection is modified after the creation of this enumerator.", GetErrorMessage(E_INVALID_OPERATION));
1490 if (__pNode == null)
1492 __pNode = __list.__pListTail;
1493 if (__pNode == null)
1495 return E_OUT_OF_RANGE;
1500 if (__pNode->pPrev != null)
1502 __pNode = __pNode->pPrev;
1506 return E_OUT_OF_RANGE;
1513 result ResetLast(void)
1519 const LinkedListT< Type >& __list;
1520 __LinkedListNodeT< Type >* __pNode;
1523 }; // __LinkedListEnumeratorT
1525 }}} // Tizen::Base::Collection
1527 #endif // _FCOL_LINKED_LIST_T_H_