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>
31 namespace Tizen { namespace Base { namespace Collection
34 template< class Type > class __LinkedListEnumeratorT;
35 template< class Type > class __LinkedListNodeT;
39 * @brief This class represents a template-based collection of objects that can be individually accessed by index.
43 * The %LinkedListT class represents a template-based collection of objects that can be individually accessed by index.
45 * For more information on the class features, see <a href="../org.tizen.native.appprogramming/html/guide/base/arraylist_linkedlist.htm">ArrayList and LinkedList</a>.
47 * The following example demonstrates how to use the %LinkedListT class.
53 * using namespace Tizen::Base;
54 * using namespace Tizen::Base::Collection;
57 * MyClass::LinkedListTSample(void)
59 * LinkedListT< int > list;
66 * list.Add(int1); // 1
67 * list.Add(int2); // 1,2
68 * list.Add(int3); // 1,2,3
71 * for (int i = 0; i < list.GetCount(); i++)
73 * list.GetAt(i, temp);
76 * list.InsertAt(int4, 1); // 1,4,2,3
78 * ComparerT< int >* pIntComparer = new ComparerT<int>();
79 * list.Sort(*pIntComparer); // 1,2,3,4
81 * delete pIntComparer;
83 * list.Remove(int3); // 1,2,4
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.
117 * This destructor overrides Tizen::Base::Object::~Object().
121 virtual ~LinkedListT(void)
129 * Adds the specified object to the end of this list.
133 * @return An error code
134 * @param[in] obj An object to add
135 * @exception E_SUCCESS The method is successful.
136 * @exception E_OUT_OF_MEMORY The memory is insufficient.
139 virtual result Add(const Type& obj)
141 result r = E_SUCCESS;
143 if (null == __pListHead)
145 r = InsertFirst(obj);
153 AppLogException("[%s] Propagating.", GetErrorMessage(r));
160 * Adds the elements of the specified collection to the end of this list.
164 * @return An error code
165 * @param[in] collection A collection to add
166 * @exception E_SUCCESS The method is successful.
167 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
168 * the specified @c collection is modified during the operation of this method.
171 virtual result AddItems(const ICollectionT< Type >& collection)
173 result r = InsertItemsFrom(collection, __count);
176 AppLogException("[%s] Propagating.", GetErrorMessage(r));
183 * Gets an enumerator to this list.
187 * @return An enumerator (an instance of the IEnumeratorT derived class) of this list, @n
188 * else @c null if an exception occurs
189 * @exception E_SUCCESS The method is successful.
190 * @exception E_OUT_OF_MEMORY The memory is insufficient.
191 * @remarks The specific error code can be accessed using the GetLastResult() method.
192 * @see Tizen::Base::Collection::IEnumeratorT
194 virtual IEnumeratorT< Type >* GetEnumeratorN(void) const
198 result r = E_SUCCESS;
200 __LinkedListEnumeratorT< Type >* pEnum = new __LinkedListEnumeratorT< Type >(*this, __modCount);
201 TryCatch(pEnum != null, r = E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
211 * Gets a bidirectional enumerator (an instance of the IBidirectionalEnumeratorT derived class) of this list.
215 * @return An instance of the IBidirectionalEnumeratorT derived class if successful, @n
216 * else @c null if an exception occurs
217 * @exception E_SUCCESS The method is successful.
218 * @exception E_OUT_OF_MEMORY The memory is insufficient.
219 * @remarks Use this method to obtain a bidirectional enumerator (an instance of the IBidirectionalEnumeratorT derived class)
220 * to iterate over a collection (an instance of the IListT derived class).
221 * The specific error code can be accessed using the GetLastResult() method.
222 * @see Tizen::Base::Collection::IBidirectionalEnumeratorT
224 virtual IBidirectionalEnumeratorT< Type >* GetBidirectionalEnumeratorN(void) const
228 result r = E_SUCCESS;
230 __LinkedListEnumeratorT< Type >* pEnum = new __LinkedListEnumeratorT< Type >(*this, __modCount);
231 TryCatch(pEnum != null, r = E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
241 * Gets the object at the specified index of this list.
245 * @return An error code
246 * @param[in] index The index of the object to read
247 * @param[out] obj An object to get from this list
248 * @exception E_SUCCESS The method is successful.
249 * @exception E_OUT_OF_RANGE The specified index is outside the bounds of the data structure, or
250 * the specified @c index is equal to or greater than the number of elements or less than @c 0.
253 virtual result GetAt(int index, Type& obj) const
255 TryReturn(index >= 0 && index < __count, E_OUT_OF_RANGE,
256 "[%s] The index(%d) MUST be greater than or equal to 0, and less than the number of elements(%d).",
257 GetErrorMessage(E_OUT_OF_RANGE), index, __count);
259 __LinkedListNodeT< Type >* pNode = GetNode(index);
268 * Gets the object at the specified index of this list.
272 * @return An error code
273 * @param[in] index The index of the object to read
274 * @param[out] obj An object to get from this list
275 * @exception E_SUCCESS The method is successful.
276 * @exception E_OUT_OF_RANGE The specified index is outside the bounds of the data structure, or
277 * the specified @c index is equal to or greater than the number of elements or less than @c 0.
280 virtual result GetAt(int index, Type& obj)
282 TryReturn(index >= 0 && index < __count, E_OUT_OF_RANGE,
283 "[%s] The index(%d) MUST be greater than or equal to 0, and less than the number of elements(%d).",
284 GetErrorMessage(E_OUT_OF_RANGE), index, __count);
286 __LinkedListNodeT< Type >* pNode = GetNode(index);
295 * Gets an IListT-derived instance with the specified range from the calling list object.
299 * @return A pointer to the IListT derived instance, @n
300 * else @c null if an exception occurs.
301 * @param[in] startIndex The starting index of the range
302 * @param[in] count The number of elements to read
303 * @exception E_SUCCESS The method is successful.
304 * @exception E_OUT_OF_RANGE Either of the following conditions has occurred: @n
305 * - The specified index is outside the bounds of the data structure. @n
306 * - The specified @c startIndex is either equal to or greater than the number of elements or less than @c 0. @n
307 * - The specified @c count is either greater than the number of elements starting from @c startIndex or less than @c 0.
309 * @remarks The specific error code can be accessed using the GetLastResult() method.
311 virtual IListT< Type >* GetItemsN(int startIndex, int count) const
315 result r = E_SUCCESS;
316 LinkedListT< Type >* pList = null;
317 __LinkedListNodeT< Type >* pNode = null;
318 __LinkedListNodeT< Type >* pPrevNode = null;
320 TryCatch(startIndex >= 0 && count >= 0, r = E_OUT_OF_RANGE,
321 "[%s] Both of the startIndex(%d) and count(%d) MUST be greater than or equal to 0.",
322 GetErrorMessage(E_OUT_OF_RANGE), startIndex, count);
323 TryCatch(startIndex < __count, r = E_OUT_OF_RANGE,
324 "[%s] The startIndex(%d) MUST be less than the number of elements(%d).", GetErrorMessage(E_OUT_OF_RANGE), startIndex, __count);
325 TryCatch(count <= __count && (startIndex + count <= __count), r = E_OUT_OF_RANGE,
326 "[%s] The startIndex(%d) + count(%d) MUST be less than or equal to the number of elements(%d).",
327 GetErrorMessage(E_OUT_OF_RANGE), startIndex, count, __count);
329 pList = new LinkedListT< Type >();
331 pNode = GetNode(startIndex);
332 if ((pList != null) && (pNode != null))
334 r = pList->InsertFirst(pNode->pObj);
335 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
337 pNode = pNode->pNext;
338 pPrevNode = pList->__pListTail;
340 for (int i = 0; i < (count - 1); i++)
344 pPrevNode = pList->InsertNext(pPrevNode, pNode->pObj);
345 TryCatch(pPrevNode != null, r = GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
346 pNode = pNode->pNext;
361 * Searches for an object in this list. @n
362 * Gets the index of the object if found.
366 * @return An error code
367 * @param[in] obj The object to locate
368 * @param[out] index The index of the object
369 * @exception E_SUCCESS The method is successful.
370 * @exception E_OBJ_NOT_FOUND The specified @c obj is not found.
372 virtual result IndexOf(const Type& obj, int& index) const
374 result r = E_SUCCESS;
381 r = IndexOf(obj, 0, __count, index);
388 * Searches for an object starting from the specified index. @n
389 * Gets the index of the object if found.
393 * @return An error code
394 * @param[in] obj The object to locate
395 * @param[in] startIndex The starting index for the search @n
396 * It must be less than the number of elements.
397 * @param[out] index The index of the object
398 * @exception E_SUCCESS The method is successful.
399 * @exception E_OUT_OF_RANGE The specified index is outside the bounds of the data structure, or
400 * the specified @c startIndex is either equal to or greater than the number of elements in the list or less than @c 0.
401 * @exception E_OBJ_NOT_FOUND The specified @c obj is not found.
404 virtual result IndexOf(const Type& obj, int startIndex, int& index) const
406 TryReturn((startIndex >= 0 && startIndex < __count), E_OUT_OF_RANGE,
407 "[%s] The startIndex(%d) MUST be greater than or equal to 0, and less than the number of elements(%d).",
408 GetErrorMessage(E_OUT_OF_RANGE), startIndex, __count);
410 return IndexOf(obj, startIndex, (__count - startIndex), index);
414 * Searches for an object within the specified range. @n
415 * Gets the index of the object if found.
419 * @return An error code
420 * @param[in] obj The object to locate
421 * @param[in] startIndex The starting index of the range
422 * @param[in] count The number of elements to read
423 * @param[out] index The index of the object
424 * @exception E_SUCCESS The method is successful.
425 * @exception E_OUT_OF_RANGE Either of the following conditions has occurred: @n
426 * - The specified index is outside the bounds of the data structure. @n
427 * - The specified @c startIndex is either equal to or greater than the number of elements in the list or less than @c 0. @n
428 * - The specified @c count is either greater than the number of elements starting from @c startIndex or less than @c 0.
429 * @exception E_OBJ_NOT_FOUND The specified @c obj is not found.
432 virtual result IndexOf(const Type& obj, int startIndex, int count, int& index) const
434 TryReturn(startIndex >= 0 && count >= 0, E_OUT_OF_RANGE,
435 "[%s] Both of the startIndex(%d) and count(%d) MUST be greater than or equal to 0.",
436 GetErrorMessage(E_OUT_OF_RANGE), startIndex, count);
437 TryReturn(startIndex < __count, E_OUT_OF_RANGE,
438 "[%s] The startIndex(%d) MUST be less than the number of elements(%d).", GetErrorMessage(E_OUT_OF_RANGE), startIndex, __count);
439 TryReturn(count <= __count && (startIndex + count <= __count), E_OUT_OF_RANGE,
440 "[%s] The startIndex(%d) + count(%d) MUST be less than or equal to the number of elements(%d).",
441 GetErrorMessage(E_OUT_OF_RANGE), startIndex, count, __count);
443 result r = E_OBJ_NOT_FOUND;
444 __LinkedListNodeT< Type >* pNode = GetNode(startIndex);
445 for (int i = 0; i < count; i++)
449 if (obj == pNode->pObj)
451 index = (startIndex + i);
455 pNode = pNode->pNext;
463 * Inserts the object at the specified location.
467 * @return An error code
468 * @param[in] obj An object to insert
469 * @param[in] index The index at which the object must be inserted
470 * @exception E_SUCCESS The method is successful.
471 * @exception E_OUT_OF_RANGE The specified index is outside the bounds of the data structure, or
472 * the specified @c index is greater than the number of elements or less than @c 0.
473 * @remarks If the @c index equals to the number of elements then the new element
474 * is added at the end of this list.
478 virtual result InsertAt(const Type& obj, int index)
480 TryReturn(index >= 0 && index <= __count, E_OUT_OF_RANGE,
481 "[%s] The index(%d) MUST be greater than or equal to 0, and less than or equal to the number of elements(%d).",
482 GetErrorMessage(E_OUT_OF_RANGE), index, __count);
484 result r = E_SUCCESS;
486 __LinkedListNodeT< Type >* pPrevNode = null;
489 r = InsertFirst(obj);
491 else if (index == __count)
497 pPrevNode = GetNode(index - 1);
498 if (pPrevNode != null)
500 InsertNext(pPrevNode, obj);
506 AppLogException("[%s] Propagating.", GetErrorMessage(r));
514 * Inserts the elements of the @c collection at the location specified.
518 * @return An error code
519 * @param[in] collection The collection to insert
520 * @param[in] startIndex The starting index at which the collection must be inserted
521 * @exception E_SUCCESS The method is successful.
522 * @exception E_OUT_OF_RANGE The specified index is outside the bounds of the data structure, or
523 * the specified @c startIndex is either greater than the number of elements or less than @c 0.
524 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
525 * the specified @c collection is modified during the operation of this method.
526 * @remarks If the @c startIndex equals to the number of elements then the new elements
527 * are added at the end of this list.
531 virtual result InsertItemsFrom(const ICollectionT< Type >& collection, int startIndex)
533 TryReturn(startIndex >= 0 && startIndex <= __count, E_OUT_OF_RANGE,
534 "[%s] The startIndex(%d) MUST be greater than or equal to 0, and less than or equal to the number of elements(%d).",
535 GetErrorMessage(E_OUT_OF_RANGE), startIndex, __count);
537 result r = E_SUCCESS;
539 IEnumeratorT< Type >* pEnum = null;
540 int count = collection.GetCount();
543 __LinkedListNodeT< Type >* pPrevNode = null;
545 if (startIndex == __count)
547 pPrevNode = __pListTail;
549 else if (startIndex > 0)
551 pPrevNode = GetNode(startIndex - 1);
554 ICollectionT< Type >* pCol = const_cast< ICollectionT< Type >* >(&collection);
555 pEnum = pCol->GetEnumeratorN();
556 TryCatch(pEnum != null, r = GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
561 r = pEnum->MoveNext();
563 if (E_OUT_OF_RANGE == r)
568 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
570 r = pEnum->GetCurrent(obj);
571 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
573 if (null == pPrevNode)
575 r = InsertFirst(obj);
576 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
577 pPrevNode = __pListHead;
581 pPrevNode = InsertNext(pPrevNode, obj);
582 TryCatch(pPrevNode != null, r = GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
603 * Searches for the last occurrence of an object in this list. @n
604 * Gets the index of the object if found.
608 * @return An error code
609 * @param[in] obj The object to locate
610 * @param[out] index The index of the last occurrence of the specified object
611 * @exception E_SUCCESS The method is successful.
612 * @exception E_OBJ_NOT_FOUND The specified @c obj is not found.
615 virtual result LastIndexOf(const Type& obj, int& index) const
617 result r = E_OBJ_NOT_FOUND;
618 __LinkedListNodeT< Type >* pNode = __pListTail;
619 for (int i = (__count - 1); i >= 0; i--)
621 if (obj == pNode->pObj)
627 pNode = pNode->pPrev;
634 * Removes the first occurrence of the specified object from the list.
638 * @return An error code
639 * @param[in] obj An object to remove
640 * @exception E_SUCCESS The method is successful.
641 * @exception E_OBJ_NOT_FOUND The specified @c obj is not found.
646 virtual result Remove(const Type& obj)
648 result r = E_OBJ_NOT_FOUND;
649 __LinkedListNodeT< Type >* pNode = __pListHead;
650 while (null != pNode)
652 if (pNode->pObj == obj)
658 pNode = pNode->pNext;
665 * Removes all the elements in the specified @c collection.
669 * @return An error code
670 * @param[in] collection The collection to remove from this list
671 * @exception E_SUCCESS The method is successful.
672 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
673 * the specified @c collection is modified during the operation of this method.
677 virtual result RemoveItems(const ICollectionT< Type >& collection)
679 result r = E_SUCCESS;
681 ICollectionT< Type >* pCol = const_cast< ICollectionT< Type >* >(&collection);
682 IEnumeratorT< Type >* pEnum = pCol->GetEnumeratorN();
683 TryCatch(pEnum != null, r = GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
688 r = pEnum->MoveNext();
690 if (E_OUT_OF_RANGE == r)
695 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
697 r = pEnum->GetCurrent(temp);
698 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
701 TryLog(r == E_SUCCESS, "[%s] Remove() failed.", GetErrorMessage(r));
719 * Removes the object at the specified location.
723 * @return An error code
724 * @param[in] index The index at which the object must be removed
725 * @exception E_SUCCESS The method is successful.
726 * @exception E_OUT_OF_RANGE The specified index is outside the bounds of the data structure, or
727 * the specified @c index is equal to or greater than the number of elements or less than @c 0.
730 virtual result RemoveAt(int index)
732 TryReturn(index < __count && index >= 0, E_OUT_OF_RANGE,
733 "[%s] The index(%d) MUST be greater than or equal to 0, and less than the number of elements(%d).",
734 GetErrorMessage(E_OUT_OF_RANGE), index, __count);
736 RemoveNode(GetNode(index));
741 * Removes all elements within the specified range.
745 * @return An error code
746 * @param[in] startIndex The starting index of the range
747 * @param[in] count The number of elements to read
748 * @exception E_SUCCESS The method is successful.
749 * @exception E_OUT_OF_RANGE Either of the following conditions has occurred: @n
750 * - The specified index is outside the bounds of the data structure. @n
751 * - The specified @c startIndex is either equal to or greater than the number of elements or less than @c 0. @n
752 * - The specified @c count is either greater than the number of elements starting from @c startIndex or less than @c 0.
754 * @see InsertItemsFrom()
756 virtual result RemoveItems(int startIndex, int count)
758 TryReturn(startIndex >= 0 && count >= 0, E_OUT_OF_RANGE,
759 "[%s] Both of the startIndex(%d) and count(%d) MUST be greater than or equal to 0.",
760 GetErrorMessage(E_OUT_OF_RANGE), startIndex, count);
761 TryReturn(startIndex < __count, E_OUT_OF_RANGE,
762 "[%s] The startIndex(%d) MUST be less than the number of elements(%d).", GetErrorMessage(E_OUT_OF_RANGE), startIndex, __count);
763 TryReturn(count <= __count && (startIndex + count <= __count), E_OUT_OF_RANGE,
764 "[%s] The startIndex(%d) + count(%d) MUST be less than or equal to the number of elements(%d).",
765 GetErrorMessage(E_OUT_OF_RANGE), startIndex, count, __count);
769 __LinkedListNodeT< Type >* pNode = GetNode(startIndex);
770 for (int i = 0; i < count; i++)
774 __LinkedListNodeT< Type >* pNextNode = pNode->pNext;
784 * Removes all the elements from this list.
788 virtual void RemoveAll(void)
793 __LinkedListNodeT< Type >* pNode = __pListHead;
794 __LinkedListNodeT< Type >* pTemp;
795 while (null != pNode)
797 pTemp = pNode->pNext;
808 * Sets the object at the specified index with the specified object.
812 * @return An error code
813 * @param[in] obj An object to set
814 * @param[in] index The index at which the object must be set
815 * @exception E_SUCCESS The method is successful.
816 * @exception E_OUT_OF_RANGE The specified index is outside the bounds of the data structure, or
817 * the specified @c index is equal to or greater than the number of elements or less than @c 0.
820 virtual result SetAt(const Type& obj, int index)
822 TryReturn(index >= 0 && index < __count, E_OUT_OF_RANGE,
823 "[%s] The index(%d) MUST be greater than or equal to 0, less than the number of elements(%d).",
824 GetErrorMessage(E_OUT_OF_RANGE), index, __count);
827 __LinkedListNodeT< Type >* pNode = GetNode(index);
837 * Sorts the elements of this list using the comparer provided.
841 * @return An error code
842 * @param[in] comparer The IComparerT implementation to use when comparing the elements
843 * @exception E_SUCCESS The method is successful.
844 * @exception E_INVALID_ARG A specified input parameter is invalid, or
845 * the @c comparer is not valid.
847 virtual result Sort(const IComparerT< Type >& comparer)
854 result r = QuickSort(0, (__count - 1), comparer);
857 AppLogException("[%s] Propagating.", GetErrorMessage(r));
864 * Gets the number of objects currently stored in this list.
868 * @return The number of objects currently stored in this list
870 virtual int GetCount(void) const
876 * Checks whether the list contains the specified object.
880 * @return @c true if the list contains the specified object, @n
882 * @param[in] obj The object to locate
885 virtual bool Contains(const Type& obj) const
892 __LinkedListNodeT< Type >* pNode = GetNode(0);
893 for (int i = 0; i < __count; i++)
897 if (obj == pNode->pObj)
901 pNode = pNode->pNext;
909 * Checks whether the list contains all the elements of the specified collection.
913 * @return An error code
914 * @param[in] collection The collection to check for containment in this list
915 * @param[out] out Set to @c true if this list contains all of the elements in the specified collection, @n
917 * @exception E_SUCCESS The method is successful.
918 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
919 * the specified @c collection is modified during the operation of this method.
920 * @remarks If the given @c collection is empty, the @c out parameter is set to @c true.
923 virtual result ContainsAll(const ICollectionT< Type >& collection, bool& out) const
925 result r = E_SUCCESS;
928 if (collection.GetCount() == 0)
934 ICollectionT< Type >* pCol = const_cast< ICollectionT< Type >* >(&collection);
935 IEnumeratorT< Type >* pEnum = pCol->GetEnumeratorN();
936 TryCatch(pEnum != null, r = GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
942 r = pEnum->MoveNext();
944 if (E_OUT_OF_RANGE == r)
950 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
952 r = pEnum->GetCurrent(temp);
953 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
955 if (false == Contains(temp))
977 * Compares the given object with the calling LinkedList object.
981 * @return @c true if the specified instance equals to the current instance, @n
983 * @param[in] obj The object to compare with the calling object
984 * @remarks This method returns @c true only if the specified object is also an instance of LinkedList class,
985 * both lists have the same size, and all corresponding pairs of elements in the two lists are equal.
986 * In other words, two lists are equal if they contain the same elements in the same order.
988 virtual bool Equals(const Object& obj) const
992 const LinkedListT< Type >* other = dynamic_cast< const LinkedListT< Type >* >(&obj);
997 else if (other == this)
1001 else if (__count != other->__count)
1007 __LinkedListNodeT< Type >* pNode = __pListHead;
1008 __LinkedListNodeT< Type >* pOtherNode = other->__pListHead;
1009 for (int i = 0; i < __count; i++)
1011 if (!(pNode->pObj == pOtherNode->pObj))
1016 pNode = pNode->pNext;
1017 pOtherNode = pOtherNode->pNext;
1025 * Gets the hash value of the current instance.
1029 * @return The hash value of the current instance
1030 * @remarks The two Tizen::Base::Object::Equals() instances must return the same hash value. For better performance, @n
1031 * the used hash function must generate a random distribution for all inputs.
1033 virtual int GetHashCode(void) const
1038 __LinkedListNodeT< Type >* pNode = __pListHead;
1039 while (pNode != null)
1041 hash += reinterpret_cast< int >(pNode);
1042 pNode = pNode->pNext;
1051 * The implementation of this copy constructor is intentionally blank and declared as private to prohibit copying of objects.
1053 * @param[in] list The %LinkedListT object to initialize the new object
1055 LinkedListT(const LinkedListT< Type >& list);
1058 * The implementation of this copy assignment operator is intentionally blank and declared as private to prohibit copying of objects.
1060 * @param[in] list An instance of %LinkedListT
1062 LinkedListT< Type >& operator =(const LinkedListT< Type >& list);
1065 * Gets the node at the specified index.
1067 * @return A node at the specified index
1068 * @param[in] index The index of the node to read
1070 __LinkedListNodeT< Type >* GetNode(int index) const
1072 if (index >= __count)
1077 __LinkedListNodeT< Type >* pNode = __pListHead;
1078 for (int i = 0; i < index; i++)
1080 pNode = pNode->pNext;
1087 * Inserts an object to the beginning of the %LinkedList.
1089 * @return An error code
1090 * @param[in] obj The object to insert
1091 * @exception E_OUT_OF_MEMORY The memory is insufficient.
1092 * @exception E_SUCCESS The method is successful.
1094 result InsertFirst(const Type& obj)
1096 result r = E_SUCCESS;
1097 __LinkedListNodeT< Type >* pNode = new __LinkedListNodeT< Type >(obj);
1098 TryCatch(pNode != null, r = E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1102 if (null == __pListHead)
1104 __pListHead = pNode;
1105 __pListTail = pNode;
1109 pNode->pNext = __pListHead;
1110 __pListHead->pPrev = pNode;
1111 __pListHead = pNode;
1121 * Inserts an object to the end of the LinkedList.
1123 * @return An error code
1124 * @param[in] obj The object to insert
1125 * @exception E_OUT_OF_MEMORY The memory is insufficient.
1126 * @exception E_SUCCESS The method is successful.
1128 result InsertLast(const Type& obj)
1130 __LinkedListNodeT< Type >* pNode = InsertNext(__pListTail, obj);
1133 AppLogException("[%s] Propagating.", GetErrorMessage(GetLastResult()));
1136 return GetLastResult();
1140 * Inserts an object after the specified node.
1142 * @return An error code
1143 * @param[in] obj The object to insert
1144 * @param[in] pPrevNode The node after which the object must be inserted
1145 * @exception E_OUT_OF_MEMORY The memory is insufficient.
1146 * @exception E_SUCCESS The method is successful.
1147 * @remarks The specific error code can be accessed using the GetLastResult() method.
1149 __LinkedListNodeT< Type >* InsertNext(__LinkedListNodeT< Type >* pPrevNode, const Type& obj)
1152 result r = E_SUCCESS;
1153 __LinkedListNodeT< Type >* pNode = new __LinkedListNodeT< Type >(obj);
1154 TryCatch(pNode != null, r = E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1158 if (null == __pListHead)
1160 __pListHead = pNode;
1161 __pListTail = pNode;
1163 else if (pPrevNode == __pListTail)
1165 pNode->pPrev = __pListTail;
1166 __pListTail->pNext = pNode;
1167 __pListTail = pNode;
1171 TryCatch(pPrevNode != null, r = E_INVALID_ARG, "[%s] The pPrevNode is null.", GetErrorMessage(E_INVALID_ARG));
1172 pNode->pPrev = pPrevNode;
1173 pNode->pNext = pPrevNode->pNext;
1174 pPrevNode->pNext->pPrev = pNode;
1175 pPrevNode->pNext = pNode;
1192 * Removes the specified node.
1194 * @param[in] pNode The pointer of the node to remove
1196 void RemoveNode(__LinkedListNodeT< Type >* pNode)
1198 AppAssertf((null != pNode), "pNode is null.\n");
1199 AppAssertf((__count > 0), "__count is zero.\n");
1208 else if (pNode == __pListHead)
1210 __pListHead = pNode->pNext;
1211 __pListHead->pPrev = null;
1213 else if (pNode == __pListTail)
1215 __pListTail = pNode->pPrev;
1216 __pListTail->pNext = null;
1220 pNode->pNext->pPrev = pNode->pPrev;
1221 pNode->pPrev->pNext = pNode->pNext;
1227 * Sorts the specified sub-list within the calling instance.
1229 * @param[in] startIndex The starting index of the sub-list to sort
1230 * @param[in] endIndex The ending index of the sub-list to sort
1231 * @param[in] pComparer The comparer function to sort the list
1232 * @exception E_SUCCESS The method is successful.
1233 * @exception E_INVALID_ARG A specified input parameter is invalid, or @n
1234 * the comparer has failed to compare the elements.
1236 result QuickSort(int startIndex, int endIndex, const IComparerT< Type >& comparer)
1238 result r = E_SUCCESS;
1240 if (startIndex < endIndex)
1243 int i = startIndex - 1;
1244 int j = endIndex + 1;
1245 Type startObj = Type();
1246 __LinkedListNodeT< Type >* pNodeI = null;
1247 __LinkedListNodeT< Type >* pNodeJ = null;
1248 __LinkedListNodeT< Type >* pNode = GetNode(startIndex);
1252 startObj = pNode->pObj;
1257 int compareResult = 1;
1258 while ((compareResult > 0) && (j > static_cast< int >(startIndex)))
1261 pNodeJ = (null == pNodeJ) ? GetNode(j) : pNodeJ->pPrev;
1264 r = comparer.Compare(pNodeJ->pObj, startObj, compareResult);
1265 TryReturn(r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
1270 while ((compareResult < 0) && (i < static_cast< int >(endIndex)))
1273 pNodeI = (null == pNodeI) ? GetNode(i) : pNodeI->pNext;
1276 r = comparer.Compare(pNodeI->pObj, startObj, compareResult);
1277 TryReturn(r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
1283 if ((pNodeJ != null) && (pNodeI != null))
1285 Type temp = pNodeJ->pObj;
1286 pNodeJ->pObj = pNodeI->pObj;
1287 pNodeI->pObj = temp;
1297 r = QuickSort(startIndex, middleIndex, comparer);
1298 TryReturn(r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
1300 r = QuickSort(middleIndex + 1, endIndex, comparer);
1301 TryReturn(r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
1307 __LinkedListNodeT< Type >* __pListHead;
1308 __LinkedListNodeT< Type >* __pListTail;
1311 friend class __LinkedListEnumeratorT< Type >;
1316 // @class __LinkedListNodeT
1317 // @brief This is a node for LinkedListT.
1320 template< class Type >
1321 class __LinkedListNodeT
1326 * This is the constructor for this class.
1330 * @param[in] obj An object to include in this node
1332 __LinkedListNodeT(const Type& obj)
1340 * Internal variable.
1344 __LinkedListNodeT< Type >* pPrev;
1347 * Internal variable.
1351 __LinkedListNodeT< Type >* pNext;
1354 * Internal variable.
1360 }; // __LinkedListNodeT
1363 // @class __LinkedListEnumeratorT
1364 // @brief This class is an implementation of IEnumeratorT for %LinkedListT.
1367 template< class Type >
1368 class __LinkedListEnumeratorT
1369 : public IBidirectionalEnumeratorT< Type >
1374 * This is the constructor for this class.
1378 * @param[in] list A list to enumerate
1379 * @param[in] modCount The modification count to detect the change in the list
1381 __LinkedListEnumeratorT(const LinkedListT< Type >& list, int modCount)
1384 , __modCount(modCount)
1390 * This is the destructor for this class.
1394 virtual ~__LinkedListEnumeratorT(void)
1400 * Gets the current object in the list.
1404 * @return An error code
1405 * @param[out] obj The current object
1406 * @exception E_INVALID_OPERATION Either of the following conditions has occurred: @n
1407 * - The current state of the instance prohibits the execution of the specified operation. @n
1408 * - This enumerator is currently positioned before the first element or
1409 * past the last element. @n
1410 - The list is modified after this enumerator is created.
1411 * @exception E_SUCCESS The method is successful.
1413 result GetCurrent(Type& obj) const
1415 TryReturn(__modCount == __list.__modCount, E_INVALID_OPERATION,
1416 "[%s] The source collection is modified after the creation of this enumerator.", GetErrorMessage(E_INVALID_OPERATION));
1417 TryReturn(__pNode != null, E_INVALID_OPERATION,
1418 "[%s] Current position is before the first element or past the last element.", GetErrorMessage(E_INVALID_OPERATION));
1420 obj = __pNode->pObj;
1426 * Moves this enumerator to the next element of the list. @n
1427 * When this enumerator is first created or after call to Reset(),
1428 * the first call to MoveNext() positions this enumerator to the first element in the list.
1432 * @return An error code
1433 * @exception E_SUCCESS The method is successful.
1434 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
1435 * the list is modified after this enumerator is created.
1436 * @exception E_OUT_OF_RANGE The enumerator has passed the end of the list.
1439 result MoveNext(void)
1441 TryReturn(__modCount == __list.__modCount, E_INVALID_OPERATION,
1442 "[%s] The source collection is modified after the creation of this enumerator.", GetErrorMessage(E_INVALID_OPERATION));
1444 if (null == __pNode)
1446 __pNode = __list.__pListHead;
1447 if (null == __pNode)
1449 return E_OUT_OF_RANGE;
1454 if (null != __pNode->pNext)
1456 __pNode = __pNode->pNext;
1460 return E_OUT_OF_RANGE;
1468 * Positions this enumerator before the first elements in the list.
1472 * @return An error code
1473 * @exception E_SUCCESS The method is successful.
1474 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
1475 * the list is modified after this enumerator is created.
1479 TryReturn(__modCount == __list.__modCount, E_INVALID_OPERATION,
1480 "[%s] The source collection is modified after the creation of this enumerator.", GetErrorMessage(E_INVALID_OPERATION));
1486 result MovePrevious(void)
1488 TryReturn(__modCount == __list.__modCount, E_INVALID_OPERATION,
1489 "[%s] The source collection is modified after the creation of this enumerator.", GetErrorMessage(E_INVALID_OPERATION));
1491 if (__pNode == null)
1493 __pNode = __list.__pListTail;
1494 if (__pNode == null)
1496 return E_OUT_OF_RANGE;
1501 if (__pNode->pPrev != null)
1503 __pNode = __pNode->pPrev;
1507 return E_OUT_OF_RANGE;
1514 result ResetLast(void)
1520 const LinkedListT< Type >& __list;
1521 __LinkedListNodeT< Type >* __pNode;
1524 }; // __LinkedListEnumeratorT
1526 }}} // Tizen::Base::Collection
1528 #endif // _FCOL_LINKED_LIST_T_H_