2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
19 * @file FBaseColLinkedListT.h
20 * @brief This is the header file for the %LinkedListT class.
22 * This header file contains the declarations of the %LinkedListT class.
24 #ifndef _FCOL_LINKED_LIST_T_H_
25 #define _FCOL_LINKED_LIST_T_H_
27 #include <FBaseResult.h>
28 #include <FBaseColIComparerT.h>
29 #include <FBaseColIListT.h>
32 namespace Tizen { namespace Base { namespace Collection
35 template< class Type > class __LinkedListEnumeratorT;
36 template< class Type > class __LinkedListNodeT;
40 * @brief This class represents a template-based collection of objects that can be individually accessed by index.
44 * The %LinkedListT class represents a template-based collection of objects that can be individually accessed by index.
46 * For more information on the class features, see <a href="../org.tizen.native.appprogramming/html/guide/base/arraylist_linkedlist.htm">ArrayList and LinkedList</a>.
48 * The following example demonstrates how to use the %LinkedListT class.
54 * using namespace Tizen::Base;
55 * using namespace Tizen::Base::Collection;
58 * MyClass::LinkedListTSample(void)
60 * LinkedListT< int > list;
67 * list.Add(int1); // 1
68 * list.Add(int2); // 1,2
69 * list.Add(int3); // 1,2,3
72 * for (int i = 0; i < list.GetCount(); i++)
74 * list.GetAt(i, temp);
77 * list.InsertAt(int4, 1); // 1,4,2,3
79 * ComparerT< int >* pIntComparer = new ComparerT<int>();
80 * list.Sort(*pIntComparer); // 1,2,3,4
82 * delete pIntComparer;
84 * list.Remove(int3); // 1,2,4
85 * list.RemoveAt(0); // 2,4
87 * // Uses an enumerator to access elements in the list
88 * IEnumeratorT< int >* pEnum = list.GetEnumeratorN();;
89 * while (pEnum->MoveNext() == E_SUCCESS)
91 * pEnum->GetCurrent(temp);
98 template< class Type >
100 : public IListT< Type >
105 * This is the default constructor for this class.
118 * This destructor overrides Tizen::Base::Object::~Object().
122 virtual ~LinkedListT(void)
130 * Adds the specified object to the end of this list.
134 * @return An error code
135 * @param[in] obj An object to add
136 * @exception E_SUCCESS The method is successful.
137 * @exception E_OUT_OF_MEMORY The memory is insufficient.
140 virtual result Add(const Type& obj)
142 result r = E_SUCCESS;
144 if (null == __pListHead)
146 r = InsertFirst(obj);
154 AppLogException("[%s] Propagating.", GetErrorMessage(r));
161 * Adds the elements of the specified collection to the end of this list.
165 * @return An error code
166 * @param[in] collection A collection to add
167 * @exception E_SUCCESS The method is successful.
168 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
169 * the specified @c collection is modified during the operation of this method.
172 virtual result AddItems(const ICollectionT< Type >& collection)
174 result r = InsertItemsFrom(collection, __count);
177 AppLogException("[%s] Propagating.", GetErrorMessage(r));
184 * Gets an enumerator to this list.
188 * @return An enumerator (an instance of the IEnumeratorT derived class) of this list, @n
189 * else @c null if an exception occurs
190 * @exception E_SUCCESS The method is successful.
191 * @exception E_OUT_OF_MEMORY The memory is insufficient.
192 * @remarks The specific error code can be accessed using the GetLastResult() method.
193 * @see Tizen::Base::Collection::IEnumeratorT
195 virtual IEnumeratorT< Type >* GetEnumeratorN(void) const
199 result r = E_SUCCESS;
201 __LinkedListEnumeratorT< Type >* pEnum = new __LinkedListEnumeratorT< Type >(*this, __modCount);
202 TryCatch(pEnum != null, r = E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
212 * Gets a bidirectional enumerator (an instance of the IBidirectionalEnumeratorT derived class) of this list.
216 * @return An instance of the IBidirectionalEnumeratorT derived class if successful, @n
217 * else @c null if an exception occurs
218 * @exception E_SUCCESS The method is successful.
219 * @exception E_OUT_OF_MEMORY The memory is insufficient.
220 * @remarks Use this method to obtain a bidirectional enumerator (an instance of the IBidirectionalEnumeratorT derived class)
221 * to iterate over a collection (an instance of the IListT derived class).
222 * The specific error code can be accessed using the GetLastResult() method.
223 * @see Tizen::Base::Collection::IBidirectionalEnumeratorT
225 virtual IBidirectionalEnumeratorT< Type >* GetBidirectionalEnumeratorN(void) const
229 result r = E_SUCCESS;
231 __LinkedListEnumeratorT< Type >* pEnum = new __LinkedListEnumeratorT< Type >(*this, __modCount);
232 TryCatch(pEnum != null, r = E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
242 * Gets the object at the specified index of this list.
246 * @return An error code
247 * @param[in] index The index of the object to read
248 * @param[out] obj An object to get from this list
249 * @exception E_SUCCESS The method is successful.
250 * @exception E_OUT_OF_RANGE The specified index is outside the bounds of the data structure, or
251 * the specified @c index is equal to or greater than the number of elements or less than @c 0.
254 virtual result GetAt(int index, Type& obj) const
256 TryReturn(index >= 0 && index < __count, E_OUT_OF_RANGE,
257 "[%s] The index(%d) MUST be greater than or equal to 0, and less than the number of elements(%d).",
258 GetErrorMessage(E_OUT_OF_RANGE), index, __count);
260 __LinkedListNodeT< Type >* pNode = GetNode(index);
269 * Gets the object at the specified index of this list.
273 * @return An error code
274 * @param[in] index The index of the object to read
275 * @param[out] obj An object to get from this list
276 * @exception E_SUCCESS The method is successful.
277 * @exception E_OUT_OF_RANGE The specified index is outside the bounds of the data structure, or
278 * the specified @c index is equal to or greater than the number of elements or less than @c 0.
281 virtual result GetAt(int index, Type& obj)
283 TryReturn(index >= 0 && index < __count, E_OUT_OF_RANGE,
284 "[%s] The index(%d) MUST be greater than or equal to 0, and less than the number of elements(%d).",
285 GetErrorMessage(E_OUT_OF_RANGE), index, __count);
287 __LinkedListNodeT< Type >* pNode = GetNode(index);
296 * Gets an IListT-derived instance with the specified range from the calling list object.
300 * @return A pointer to the IListT derived instance, @n
301 * else @c null if an exception occurs.
302 * @param[in] startIndex The starting index of the range
303 * @param[in] count The number of elements to read
304 * @exception E_SUCCESS The method is successful.
305 * @exception E_OUT_OF_RANGE Either of the following conditions has occurred: @n
306 * - The specified index is outside the bounds of the data structure. @n
307 * - The specified @c startIndex is either equal to or greater than the number of elements or less than @c 0. @n
308 * - The specified @c count is either greater than the number of elements starting from @c startIndex or less than @c 0.
310 * @remarks The specific error code can be accessed using the GetLastResult() method.
312 virtual IListT< Type >* GetItemsN(int startIndex, int count) const
316 result r = E_SUCCESS;
317 LinkedListT< Type >* pList = null;
318 __LinkedListNodeT< Type >* pNode = null;
319 __LinkedListNodeT< Type >* pPrevNode = null;
321 TryCatch(startIndex >= 0 && count >= 0, r = E_OUT_OF_RANGE,
322 "[%s] Both of the startIndex(%d) and count(%d) MUST be greater than or equal to 0.",
323 GetErrorMessage(E_OUT_OF_RANGE), startIndex, count);
324 TryCatch(startIndex < __count, r = E_OUT_OF_RANGE,
325 "[%s] The startIndex(%d) MUST be less than the number of elements(%d).", GetErrorMessage(E_OUT_OF_RANGE), startIndex, __count);
326 TryCatch(count <= __count && (startIndex + count <= __count), r = E_OUT_OF_RANGE,
327 "[%s] The startIndex(%d) + count(%d) MUST be less than or equal to the number of elements(%d).",
328 GetErrorMessage(E_OUT_OF_RANGE), startIndex, count, __count);
330 pList = new LinkedListT< Type >();
332 pNode = GetNode(startIndex);
333 if ((pList != null) && (pNode != null))
335 r = pList->InsertFirst(pNode->pObj);
336 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
338 pNode = pNode->pNext;
339 pPrevNode = pList->__pListTail;
341 for (int i = 0; i < (count - 1); i++)
345 pPrevNode = pList->InsertNext(pPrevNode, pNode->pObj);
346 TryCatch(pPrevNode != null, r = GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
347 pNode = pNode->pNext;
362 * Searches for an object in this list. @n
363 * Gets the index of the object if found.
367 * @return An error code
368 * @param[in] obj The object to locate
369 * @param[out] index The index of the object
370 * @exception E_SUCCESS The method is successful.
371 * @exception E_OBJ_NOT_FOUND The specified @c obj is not found.
373 virtual result IndexOf(const Type& obj, int& index) const
375 result r = E_SUCCESS;
382 r = IndexOf(obj, 0, __count, index);
389 * Searches for an object starting from the specified index. @n
390 * Gets the index of the object if found.
394 * @return An error code
395 * @param[in] obj The object to locate
396 * @param[in] startIndex The starting index for the search @n
397 * It must be less than the number of elements.
398 * @param[out] index The index of the object
399 * @exception E_SUCCESS The method is successful.
400 * @exception E_OUT_OF_RANGE The specified index is outside the bounds of the data structure, or
401 * the specified @c startIndex is either equal to or greater than the number of elements in the list or less than @c 0.
402 * @exception E_OBJ_NOT_FOUND The specified @c obj is not found.
405 virtual result IndexOf(const Type& obj, int startIndex, int& index) const
407 TryReturn((startIndex >= 0 && startIndex < __count), E_OUT_OF_RANGE,
408 "[%s] The startIndex(%d) MUST be greater than or equal to 0, and less than the number of elements(%d).",
409 GetErrorMessage(E_OUT_OF_RANGE), startIndex, __count);
411 return IndexOf(obj, startIndex, (__count - startIndex), index);
415 * Searches for an object within the specified range. @n
416 * Gets the index of the object if found.
420 * @return An error code
421 * @param[in] obj The object to locate
422 * @param[in] startIndex The starting index of the range
423 * @param[in] count The number of elements to read
424 * @param[out] index The index of the object
425 * @exception E_SUCCESS The method is successful.
426 * @exception E_OUT_OF_RANGE Either of the following conditions has occurred: @n
427 * - The specified index is outside the bounds of the data structure. @n
428 * - The specified @c startIndex is either equal to or greater than the number of elements in the list or less than @c 0. @n
429 * - The specified @c count is either greater than the number of elements starting from @c startIndex or less than @c 0.
430 * @exception E_OBJ_NOT_FOUND The specified @c obj is not found.
433 virtual result IndexOf(const Type& obj, int startIndex, int count, int& index) const
435 TryReturn(startIndex >= 0 && count >= 0, E_OUT_OF_RANGE,
436 "[%s] Both of the startIndex(%d) and count(%d) MUST be greater than or equal to 0.",
437 GetErrorMessage(E_OUT_OF_RANGE), startIndex, count);
438 TryReturn(startIndex < __count, E_OUT_OF_RANGE,
439 "[%s] The startIndex(%d) MUST be less than the number of elements(%d).", GetErrorMessage(E_OUT_OF_RANGE), startIndex, __count);
440 TryReturn(count <= __count && (startIndex + count <= __count), E_OUT_OF_RANGE,
441 "[%s] The startIndex(%d) + count(%d) MUST be less than or equal to the number of elements(%d).",
442 GetErrorMessage(E_OUT_OF_RANGE), startIndex, count, __count);
444 result r = E_OBJ_NOT_FOUND;
445 __LinkedListNodeT< Type >* pNode = GetNode(startIndex);
446 for (int i = 0; i < count; i++)
450 if (obj == pNode->pObj)
452 index = (startIndex + i);
456 pNode = pNode->pNext;
464 * Inserts the object at the specified location.
468 * @return An error code
469 * @param[in] obj An object to insert
470 * @param[in] index The index at which the object must be inserted
471 * @exception E_SUCCESS The method is successful.
472 * @exception E_OUT_OF_RANGE The specified index is outside the bounds of the data structure, or
473 * the specified @c index is greater than the number of elements or less than @c 0.
474 * @remarks If the @c index equals to the number of elements then the new element
475 * is added at the end of this list.
479 virtual result InsertAt(const Type& obj, int index)
481 TryReturn(index >= 0 && index <= __count, E_OUT_OF_RANGE,
482 "[%s] The index(%d) MUST be greater than or equal to 0, and less than or equal to the number of elements(%d).",
483 GetErrorMessage(E_OUT_OF_RANGE), index, __count);
485 result r = E_SUCCESS;
487 __LinkedListNodeT< Type >* pPrevNode = null;
490 r = InsertFirst(obj);
492 else if (index == __count)
498 pPrevNode = GetNode(index - 1);
499 if (pPrevNode != null)
501 InsertNext(pPrevNode, obj);
507 AppLogException("[%s] Propagating.", GetErrorMessage(r));
515 * Inserts the elements of the @c collection at the location specified.
519 * @return An error code
520 * @param[in] collection The collection to insert
521 * @param[in] startIndex The starting index at which the collection must be inserted
522 * @exception E_SUCCESS The method is successful.
523 * @exception E_OUT_OF_RANGE The specified index is outside the bounds of the data structure, or
524 * the specified @c startIndex is either greater than the number of elements or less than @c 0.
525 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
526 * the specified @c collection is modified during the operation of this method.
527 * @remarks If the @c startIndex equals to the number of elements then the new elements
528 * are added at the end of this list.
532 virtual result InsertItemsFrom(const ICollectionT< Type >& collection, int startIndex)
534 TryReturn(startIndex >= 0 && startIndex <= __count, E_OUT_OF_RANGE,
535 "[%s] The startIndex(%d) MUST be greater than or equal to 0, and less than or equal to the number of elements(%d).",
536 GetErrorMessage(E_OUT_OF_RANGE), startIndex, __count);
538 result r = E_SUCCESS;
540 IEnumeratorT< Type >* pEnum = null;
541 int count = collection.GetCount();
544 __LinkedListNodeT< Type >* pPrevNode = null;
546 if (startIndex == __count)
548 pPrevNode = __pListTail;
550 else if (startIndex > 0)
552 pPrevNode = GetNode(startIndex - 1);
555 ICollectionT< Type >* pCol = const_cast< ICollectionT< Type >* >(&collection);
556 pEnum = pCol->GetEnumeratorN();
557 TryCatch(pEnum != null, r = GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
562 r = pEnum->MoveNext();
564 if (E_OUT_OF_RANGE == r)
569 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
571 r = pEnum->GetCurrent(obj);
572 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
574 if (null == pPrevNode)
576 r = InsertFirst(obj);
577 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
578 pPrevNode = __pListHead;
582 pPrevNode = InsertNext(pPrevNode, obj);
583 TryCatch(pPrevNode != null, r = GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
604 * Searches for the last occurrence of an object in this list. @n
605 * Gets the index of the object if found.
609 * @return An error code
610 * @param[in] obj The object to locate
611 * @param[out] index The index of the last occurrence of the specified object
612 * @exception E_SUCCESS The method is successful.
613 * @exception E_OBJ_NOT_FOUND The specified @c obj is not found.
616 virtual result LastIndexOf(const Type& obj, int& index) const
618 result r = E_OBJ_NOT_FOUND;
619 __LinkedListNodeT< Type >* pNode = __pListTail;
620 for (int i = (__count - 1); i >= 0; i--)
622 if (obj == pNode->pObj)
628 pNode = pNode->pPrev;
635 * Removes the first occurrence of the specified object from the list.
639 * @return An error code
640 * @param[in] obj An object to remove
641 * @exception E_SUCCESS The method is successful.
642 * @exception E_OBJ_NOT_FOUND The specified @c obj is not found.
647 virtual result Remove(const Type& obj)
649 result r = E_OBJ_NOT_FOUND;
650 __LinkedListNodeT< Type >* pNode = __pListHead;
651 while (null != pNode)
653 if (pNode->pObj == obj)
659 pNode = pNode->pNext;
666 * Removes all the elements in the specified @c collection.
670 * @return An error code
671 * @param[in] collection The collection to remove from this list
672 * @exception E_SUCCESS The method is successful.
673 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
674 * the specified @c collection is modified during the operation of this method.
678 virtual result RemoveItems(const ICollectionT< Type >& collection)
680 result r = E_SUCCESS;
682 ICollectionT< Type >* pCol = const_cast< ICollectionT< Type >* >(&collection);
683 IEnumeratorT< Type >* pEnum = pCol->GetEnumeratorN();
684 TryCatch(pEnum != null, r = GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
689 r = pEnum->MoveNext();
691 if (E_OUT_OF_RANGE == r)
696 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
698 r = pEnum->GetCurrent(temp);
699 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
702 TryLog(r == E_SUCCESS, "[%s] Remove() failed.", GetErrorMessage(r));
720 * Removes the object at the specified location.
724 * @return An error code
725 * @param[in] index The index at which the object must be removed
726 * @exception E_SUCCESS The method is successful.
727 * @exception E_OUT_OF_RANGE The specified index is outside the bounds of the data structure, or
728 * the specified @c index is equal to or greater than the number of elements or less than @c 0.
731 virtual result RemoveAt(int index)
733 TryReturn(index < __count && index >= 0, E_OUT_OF_RANGE,
734 "[%s] The index(%d) MUST be greater than or equal to 0, and less than the number of elements(%d).",
735 GetErrorMessage(E_OUT_OF_RANGE), index, __count);
737 RemoveNode(GetNode(index));
742 * Removes all elements within the specified range.
746 * @return An error code
747 * @param[in] startIndex The starting index of the range
748 * @param[in] count The number of elements to read
749 * @exception E_SUCCESS The method is successful.
750 * @exception E_OUT_OF_RANGE Either of the following conditions has occurred: @n
751 * - The specified index is outside the bounds of the data structure. @n
752 * - The specified @c startIndex is either equal to or greater than the number of elements or less than @c 0. @n
753 * - The specified @c count is either greater than the number of elements starting from @c startIndex or less than @c 0.
755 * @see InsertItemsFrom()
757 virtual result RemoveItems(int startIndex, int count)
759 TryReturn(startIndex >= 0 && count >= 0, E_OUT_OF_RANGE,
760 "[%s] Both of the startIndex(%d) and count(%d) MUST be greater than or equal to 0.",
761 GetErrorMessage(E_OUT_OF_RANGE), startIndex, count);
762 TryReturn(startIndex < __count, E_OUT_OF_RANGE,
763 "[%s] The startIndex(%d) MUST be less than the number of elements(%d).", GetErrorMessage(E_OUT_OF_RANGE), startIndex, __count);
764 TryReturn(count <= __count && (startIndex + count <= __count), E_OUT_OF_RANGE,
765 "[%s] The startIndex(%d) + count(%d) MUST be less than or equal to the number of elements(%d).",
766 GetErrorMessage(E_OUT_OF_RANGE), startIndex, count, __count);
770 __LinkedListNodeT< Type >* pNode = GetNode(startIndex);
771 for (int i = 0; i < count; i++)
775 __LinkedListNodeT< Type >* pNextNode = pNode->pNext;
785 * Removes all the elements from this list.
789 virtual void RemoveAll(void)
794 __LinkedListNodeT< Type >* pNode = __pListHead;
795 __LinkedListNodeT< Type >* pTemp;
796 while (null != pNode)
798 pTemp = pNode->pNext;
809 * Sets the object at the specified index with the specified object.
813 * @return An error code
814 * @param[in] obj An object to set
815 * @param[in] index The index at which the object must be set
816 * @exception E_SUCCESS The method is successful.
817 * @exception E_OUT_OF_RANGE The specified index is outside the bounds of the data structure, or
818 * the specified @c index is equal to or greater than the number of elements or less than @c 0.
821 virtual result SetAt(const Type& obj, int index)
823 TryReturn(index >= 0 && index < __count, E_OUT_OF_RANGE,
824 "[%s] The index(%d) MUST be greater than or equal to 0, less than the number of elements(%d).",
825 GetErrorMessage(E_OUT_OF_RANGE), index, __count);
828 __LinkedListNodeT< Type >* pNode = GetNode(index);
838 * Sorts the elements of this list using the comparer provided.
842 * @return An error code
843 * @param[in] comparer The IComparerT implementation to use when comparing the elements
844 * @exception E_SUCCESS The method is successful.
845 * @exception E_INVALID_ARG A specified input parameter is invalid, or
846 * the @c comparer is not valid.
848 virtual result Sort(const IComparerT< Type >& comparer)
855 result r = QuickSort(0, (__count - 1), comparer);
858 AppLogException("[%s] Propagating.", GetErrorMessage(r));
865 * Gets the number of objects currently stored in this list.
869 * @return The number of objects currently stored in this list
871 virtual int GetCount(void) const
877 * Checks whether the list contains the specified object.
881 * @return @c true if the list contains the specified object, @n
883 * @param[in] obj The object to locate
886 virtual bool Contains(const Type& obj) const
893 __LinkedListNodeT< Type >* pNode = GetNode(0);
894 for (int i = 0; i < __count; i++)
898 if (obj == pNode->pObj)
902 pNode = pNode->pNext;
910 * Checks whether the list contains all the elements of the specified collection.
914 * @return An error code
915 * @param[in] collection The collection to check for containment in this list
916 * @param[out] out Set to @c true if this list contains all of the elements in the specified collection, @n
918 * @exception E_SUCCESS The method is successful.
919 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
920 * the specified @c collection is modified during the operation of this method.
921 * @remarks If the given @c collection is empty, the @c out parameter is set to @c true.
924 virtual result ContainsAll(const ICollectionT< Type >& collection, bool& out) const
926 result r = E_SUCCESS;
929 if (collection.GetCount() == 0)
935 ICollectionT< Type >* pCol = const_cast< ICollectionT< Type >* >(&collection);
936 IEnumeratorT< Type >* pEnum = pCol->GetEnumeratorN();
937 TryCatch(pEnum != null, r = GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
943 r = pEnum->MoveNext();
945 if (E_OUT_OF_RANGE == r)
951 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
953 r = pEnum->GetCurrent(temp);
954 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
956 if (false == Contains(temp))
978 * Compares the given object with the calling LinkedList object.
982 * @return @c true if the specified instance equals to the current instance, @n
984 * @param[in] obj The object to compare with the calling object
985 * @remarks This method returns @c true only if the specified object is also an instance of LinkedList class,
986 * both lists have the same size, and all corresponding pairs of elements in the two lists are equal.
987 * In other words, two lists are equal if they contain the same elements in the same order.
989 virtual bool Equals(const Object& obj) const
993 const LinkedListT< Type >* other = dynamic_cast< const LinkedListT< Type >* >(&obj);
998 else if (other == this)
1002 else if (__count != other->__count)
1008 __LinkedListNodeT< Type >* pNode = __pListHead;
1009 __LinkedListNodeT< Type >* pOtherNode = other->__pListHead;
1010 for (int i = 0; i < __count; i++)
1012 if (!(pNode->pObj == pOtherNode->pObj))
1017 pNode = pNode->pNext;
1018 pOtherNode = pOtherNode->pNext;
1026 * Gets the hash value of the current instance.
1030 * @return The hash value of the current instance
1031 * @remarks The two Tizen::Base::Object::Equals() instances must return the same hash value. For better performance, @n
1032 * the used hash function must generate a random distribution for all inputs.
1034 virtual int GetHashCode(void) const
1039 __LinkedListNodeT< Type >* pNode = __pListHead;
1040 while (pNode != null)
1042 hash += reinterpret_cast< int >(pNode);
1043 pNode = pNode->pNext;
1052 * The implementation of this copy constructor is intentionally blank and declared as private to prohibit copying of objects.
1054 * @param[in] list The %LinkedListT object to initialize the new object
1056 LinkedListT(const LinkedListT< Type >& list);
1059 * The implementation of this copy assignment operator is intentionally blank and declared as private to prohibit copying of objects.
1061 * @param[in] list An instance of %LinkedListT
1063 LinkedListT< Type >& operator =(const LinkedListT< Type >& list);
1066 * Gets the node at the specified index.
1068 * @return A node at the specified index
1069 * @param[in] index The index of the node to read
1071 __LinkedListNodeT< Type >* GetNode(int index) const
1073 if (index >= __count)
1078 __LinkedListNodeT< Type >* pNode = __pListHead;
1079 for (int i = 0; i < index; i++)
1081 pNode = pNode->pNext;
1088 * Inserts an object to the beginning of the %LinkedList.
1090 * @return An error code
1091 * @param[in] obj The object to insert
1092 * @exception E_OUT_OF_MEMORY The memory is insufficient.
1093 * @exception E_SUCCESS The method is successful.
1095 result InsertFirst(const Type& obj)
1097 result r = E_SUCCESS;
1098 __LinkedListNodeT< Type >* pNode = new __LinkedListNodeT< Type >(obj);
1099 TryCatch(pNode != null, r = E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1103 if (null == __pListHead)
1105 __pListHead = pNode;
1106 __pListTail = pNode;
1110 pNode->pNext = __pListHead;
1111 __pListHead->pPrev = pNode;
1112 __pListHead = pNode;
1122 * Inserts an object to the end of the LinkedList.
1124 * @return An error code
1125 * @param[in] obj The object to insert
1126 * @exception E_OUT_OF_MEMORY The memory is insufficient.
1127 * @exception E_SUCCESS The method is successful.
1129 result InsertLast(const Type& obj)
1131 __LinkedListNodeT< Type >* pNode = InsertNext(__pListTail, obj);
1134 AppLogException("[%s] Propagating.", GetErrorMessage(GetLastResult()));
1137 return GetLastResult();
1141 * Inserts an object after the specified node.
1143 * @return An error code
1144 * @param[in] obj The object to insert
1145 * @param[in] pPrevNode The node after which the object must be inserted
1146 * @exception E_OUT_OF_MEMORY The memory is insufficient.
1147 * @exception E_SUCCESS The method is successful.
1148 * @remarks The specific error code can be accessed using the GetLastResult() method.
1150 __LinkedListNodeT< Type >* InsertNext(__LinkedListNodeT< Type >* pPrevNode, const Type& obj)
1153 result r = E_SUCCESS;
1154 __LinkedListNodeT< Type >* pNode = new __LinkedListNodeT< Type >(obj);
1155 TryCatch(pNode != null, r = E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1159 if (null == __pListHead)
1161 __pListHead = pNode;
1162 __pListTail = pNode;
1164 else if (pPrevNode == __pListTail)
1166 pNode->pPrev = __pListTail;
1167 __pListTail->pNext = pNode;
1168 __pListTail = pNode;
1172 TryCatch(pPrevNode != null, r = E_INVALID_ARG, "[%s] The pPrevNode is null.", GetErrorMessage(E_INVALID_ARG));
1173 pNode->pPrev = pPrevNode;
1174 pNode->pNext = pPrevNode->pNext;
1175 pPrevNode->pNext->pPrev = pNode;
1176 pPrevNode->pNext = pNode;
1193 * Removes the specified node.
1195 * @param[in] pNode The pointer of the node to remove
1197 void RemoveNode(__LinkedListNodeT< Type >* pNode)
1199 AppAssertf((null != pNode), "pNode is null.\n");
1200 AppAssertf((__count > 0), "__count is zero.\n");
1209 else if (pNode == __pListHead)
1211 __pListHead = pNode->pNext;
1212 __pListHead->pPrev = null;
1214 else if (pNode == __pListTail)
1216 __pListTail = pNode->pPrev;
1217 __pListTail->pNext = null;
1221 pNode->pNext->pPrev = pNode->pPrev;
1222 pNode->pPrev->pNext = pNode->pNext;
1228 * Sorts the specified sub-list within the calling instance.
1230 * @param[in] startIndex The starting index of the sub-list to sort
1231 * @param[in] endIndex The ending index of the sub-list to sort
1232 * @param[in] pComparer The comparer function to sort the list
1233 * @exception E_SUCCESS The method is successful.
1234 * @exception E_INVALID_ARG A specified input parameter is invalid, or @n
1235 * the comparer has failed to compare the elements.
1237 result QuickSort(int startIndex, int endIndex, const IComparerT< Type >& comparer)
1239 result r = E_SUCCESS;
1241 if (startIndex < endIndex)
1244 int i = startIndex - 1;
1245 int j = endIndex + 1;
1246 Type startObj = Type();
1247 __LinkedListNodeT< Type >* pNodeI = null;
1248 __LinkedListNodeT< Type >* pNodeJ = null;
1249 __LinkedListNodeT< Type >* pNode = GetNode(startIndex);
1253 startObj = pNode->pObj;
1258 int compareResult = 1;
1259 while ((compareResult > 0) && (j > static_cast< int >(startIndex)))
1262 pNodeJ = (null == pNodeJ) ? GetNode(j) : pNodeJ->pPrev;
1265 r = comparer.Compare(pNodeJ->pObj, startObj, compareResult);
1266 TryReturn(r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
1271 while ((compareResult < 0) && (i < static_cast< int >(endIndex)))
1274 pNodeI = (null == pNodeI) ? GetNode(i) : pNodeI->pNext;
1277 r = comparer.Compare(pNodeI->pObj, startObj, compareResult);
1278 TryReturn(r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
1284 if ((pNodeJ != null) && (pNodeI != null))
1286 Type temp = pNodeJ->pObj;
1287 pNodeJ->pObj = pNodeI->pObj;
1288 pNodeI->pObj = temp;
1298 r = QuickSort(startIndex, middleIndex, comparer);
1299 TryReturn(r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
1301 r = QuickSort(middleIndex + 1, endIndex, comparer);
1302 TryReturn(r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
1308 __LinkedListNodeT< Type >* __pListHead;
1309 __LinkedListNodeT< Type >* __pListTail;
1312 friend class __LinkedListEnumeratorT< Type >;
1317 // @class __LinkedListNodeT
1318 // @brief This is a node for LinkedListT.
1321 template< class Type >
1322 class __LinkedListNodeT
1327 * This is the constructor for this class.
1331 * @param[in] obj An object to include in this node
1333 __LinkedListNodeT(const Type& obj)
1341 * Internal variable.
1345 __LinkedListNodeT< Type >* pPrev;
1348 * Internal variable.
1352 __LinkedListNodeT< Type >* pNext;
1355 * Internal variable.
1361 }; // __LinkedListNodeT
1364 // @class __LinkedListEnumeratorT
1365 // @brief This class is an implementation of IEnumeratorT for %LinkedListT.
1368 template< class Type >
1369 class __LinkedListEnumeratorT
1370 : public IBidirectionalEnumeratorT< Type >
1375 * This is the constructor for this class.
1379 * @param[in] list A list to enumerate
1380 * @param[in] modCount The modification count to detect the change in the list
1382 __LinkedListEnumeratorT(const LinkedListT< Type >& list, int modCount)
1385 , __modCount(modCount)
1391 * This is the destructor for this class.
1395 virtual ~__LinkedListEnumeratorT(void)
1401 * Gets the current object in the list.
1405 * @return An error code
1406 * @param[out] obj The current object
1407 * @exception E_INVALID_OPERATION Either of the following conditions has occurred: @n
1408 * - The current state of the instance prohibits the execution of the specified operation. @n
1409 * - This enumerator is currently positioned before the first element or
1410 * past the last element. @n
1411 - The list is modified after this enumerator is created.
1412 * @exception E_SUCCESS The method is successful.
1414 result GetCurrent(Type& obj) const
1416 TryReturn(__modCount == __list.__modCount, E_INVALID_OPERATION,
1417 "[%s] The source collection is modified after the creation of this enumerator.", GetErrorMessage(E_INVALID_OPERATION));
1418 TryReturn(__pNode != null, E_INVALID_OPERATION,
1419 "[%s] Current position is before the first element or past the last element.", GetErrorMessage(E_INVALID_OPERATION));
1421 obj = __pNode->pObj;
1427 * Moves this enumerator to the next element of the list. @n
1428 * When this enumerator is first created or after call to Reset(),
1429 * the first call to MoveNext() positions this enumerator to the first element in the list.
1433 * @return An error code
1434 * @exception E_SUCCESS The method is successful.
1435 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
1436 * the list is modified after this enumerator is created.
1437 * @exception E_OUT_OF_RANGE The enumerator has passed the end of the list.
1440 result MoveNext(void)
1442 TryReturn(__modCount == __list.__modCount, E_INVALID_OPERATION,
1443 "[%s] The source collection is modified after the creation of this enumerator.", GetErrorMessage(E_INVALID_OPERATION));
1445 if (null == __pNode)
1447 __pNode = __list.__pListHead;
1448 if (null == __pNode)
1450 return E_OUT_OF_RANGE;
1455 if (null != __pNode->pNext)
1457 __pNode = __pNode->pNext;
1461 return E_OUT_OF_RANGE;
1469 * Positions this enumerator before the first elements in the list.
1473 * @return An error code
1474 * @exception E_SUCCESS The method is successful.
1475 * @exception E_INVALID_OPERATION The current state of the instance prohibits the execution of the specified operation, or
1476 * the list is modified after this enumerator is created.
1480 TryReturn(__modCount == __list.__modCount, E_INVALID_OPERATION,
1481 "[%s] The source collection is modified after the creation of this enumerator.", GetErrorMessage(E_INVALID_OPERATION));
1487 result MovePrevious(void)
1489 TryReturn(__modCount == __list.__modCount, E_INVALID_OPERATION,
1490 "[%s] The source collection is modified after the creation of this enumerator.", GetErrorMessage(E_INVALID_OPERATION));
1492 if (__pNode == null)
1494 __pNode = __list.__pListTail;
1495 if (__pNode == null)
1497 return E_OUT_OF_RANGE;
1502 if (__pNode->pPrev != null)
1504 __pNode = __pNode->pPrev;
1508 return E_OUT_OF_RANGE;
1515 result ResetLast(void)
1521 const LinkedListT< Type >& __list;
1522 __LinkedListNodeT< Type >* __pNode;
1525 }; // __LinkedListEnumeratorT
1527 }}} // Tizen::Base::Collection
1529 #endif // _FCOL_LINKED_LIST_T_H_