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 an index.
42 * The %LinkedListT class represents a template-based collection of objects that can be individually accessed by an 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 The 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 The collection to add
165 * @exception E_SUCCESS The method is successful.
166 * @exception E_INVALID_OPERATION Either of the following conditions has occurred:
167 * - The current state of the instance prohibits the execution of the specified operation.
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 the enumerator of this list.
187 * @return The 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.
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 the bidirectional enumerator (an instance of the IBidirectionalEnumeratorT derived class) of this list.
214 * @return An instance of the IBidirectionalEnumeratorT derived class, @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.
219 * - 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.
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 The object to get from this list
247 * @exception E_SUCCESS The method is successful.
248 * @exception E_OUT_OF_RANGE Either of the following conditions has occurred:
249 * - The specified @c index is outside the bounds of the data structure.
250 * - The specified @c index is either greater than or equal to the number of elements.
251 * - The specified @c index is 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 The object to get from this list
276 * @exception E_SUCCESS The method is successful.
277 * @exception E_OUT_OF_RANGE Either of the following conditions has occurred:
278 * - The specified @c index is outside the bounds of the data structure.
279 * - The specified @c index is either greater than or equal to the number of elements.
280 * - The specified @c index is less than @c 0.
283 virtual result GetAt(int index, Type& obj)
285 TryReturn(index >= 0 && index < __count, E_OUT_OF_RANGE,
286 "[%s] The index(%d) MUST be greater than or equal to 0, and less than the number of elements(%d).",
287 GetErrorMessage(E_OUT_OF_RANGE), index, __count);
289 __LinkedListNodeT< Type >* pNode = GetNode(index);
298 * Gets an IListT-derived instance with the specified range from the calling list object.
302 * @return A pointer to the IListT derived instance, @n
303 * else @c null if an exception occurs
304 * @param[in] startIndex The starting index of the range
305 * @param[in] count The number of elements to read
306 * @exception E_SUCCESS The method is successful.
307 * @exception E_OUT_OF_RANGE Either of the following conditions has occurred:
308 * - The specified @c index is outside the bounds of the data structure.
309 * - The specified @c startIndex is either greater than or equal to the number of elements.
310 * - The specified @c startIndex is less than @c 0. @n
311 * - The specified @c count is greater than the number of elements starting from @c startIndex.
312 * - The specified @c count is less than @c 0.
314 * @remarks The specific error code can be accessed using the GetLastResult() method.
316 virtual IListT< Type >* GetItemsN(int startIndex, int count) const
320 result r = E_SUCCESS;
321 LinkedListT< Type >* pList = null;
322 __LinkedListNodeT< Type >* pNode = null;
323 __LinkedListNodeT< Type >* pPrevNode = null;
325 TryCatch(startIndex >= 0 && count >= 0, r = E_OUT_OF_RANGE,
326 "[%s] Both of the startIndex(%d) and count(%d) MUST be greater than or equal to 0.",
327 GetErrorMessage(E_OUT_OF_RANGE), startIndex, count);
328 TryCatch(startIndex < __count, r = E_OUT_OF_RANGE,
329 "[%s] The startIndex(%d) MUST be less than the number of elements(%d).", GetErrorMessage(E_OUT_OF_RANGE), startIndex, __count);
330 TryCatch(count <= __count && (startIndex + count <= __count), r = E_OUT_OF_RANGE,
331 "[%s] The startIndex(%d) + count(%d) MUST be less than or equal to the number of elements(%d).",
332 GetErrorMessage(E_OUT_OF_RANGE), startIndex, count, __count);
334 pList = new LinkedListT< Type >();
336 pNode = GetNode(startIndex);
337 if ((pList != null) && (pNode != null))
339 r = pList->InsertFirst(pNode->pObj);
340 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
342 pNode = pNode->pNext;
343 pPrevNode = pList->__pListTail;
345 for (int i = 0; i < (count - 1); i++)
349 pPrevNode = pList->InsertNext(pPrevNode, pNode->pObj);
350 TryCatch(pPrevNode != null, r = GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
351 pNode = pNode->pNext;
366 * Searches for an object in this list. @n
367 * Gets the index of the object if found.
371 * @return An error code
372 * @param[in] obj The object to locate
373 * @param[out] index The index of the object
374 * @exception E_SUCCESS The method is successful.
375 * @exception E_OBJ_NOT_FOUND The specified @c obj has not been found.
377 virtual result IndexOf(const Type& obj, int& index) const
379 result r = E_SUCCESS;
386 r = IndexOf(obj, 0, __count, index);
393 * Searches for an object starting from the specified index. @n
394 * Gets the index of the object if found.
398 * @return An error code
399 * @param[in] obj The object to locate
400 * @param[in] startIndex The starting index for the search @n
401 * It must be less than the number of elements.
402 * @param[out] index The index of the object
403 * @exception E_SUCCESS The method is successful.
404 * @exception E_OUT_OF_RANGE Either of the following conditions has occurred:
405 * - The specified @c index is outside the bounds of the data structure.
406 * - The specified @c startIndex is either greater than or equal to the number of elements in the list.
407 * - The specified @c startIndex is less than @c 0.
408 * @exception E_OBJ_NOT_FOUND The specified @c obj has not been found.
411 virtual result IndexOf(const Type& obj, int startIndex, int& index) const
413 TryReturn((startIndex >= 0 && startIndex < __count), E_OUT_OF_RANGE,
414 "[%s] The startIndex(%d) MUST be greater than or equal to 0, and less than the number of elements(%d).",
415 GetErrorMessage(E_OUT_OF_RANGE), startIndex, __count);
417 return IndexOf(obj, startIndex, (__count - startIndex), index);
421 * Searches for an object within the specified range. @n
422 * Gets the index of the object if found.
426 * @return An error code
427 * @param[in] obj The object to locate
428 * @param[in] startIndex The starting index of the range
429 * @param[in] count The number of elements to read
430 * @param[out] index The index of the object
431 * @exception E_SUCCESS The method is successful.
432 * @exception E_OUT_OF_RANGE Either of the following conditions has occurred:
433 * - The specified @c index is outside the bounds of the data structure.
434 * - The specified @c startIndex is either greater than or equal to the number of elements in the list.
435 * - The specified @c startIndex is less than @c 0.
436 * - The specified @c count is greater than the number of elements starting from @c startIndex.
437 * - The specified @c count is less than @c 0.
438 * @exception E_OBJ_NOT_FOUND The specified @c obj has not been found.
441 virtual result IndexOf(const Type& obj, int startIndex, int count, int& index) const
443 TryReturn(startIndex >= 0 && count >= 0, E_OUT_OF_RANGE,
444 "[%s] Both of the startIndex(%d) and count(%d) MUST be greater than or equal to 0.",
445 GetErrorMessage(E_OUT_OF_RANGE), startIndex, count);
446 TryReturn(startIndex < __count, E_OUT_OF_RANGE,
447 "[%s] The startIndex(%d) MUST be less than the number of elements(%d).", GetErrorMessage(E_OUT_OF_RANGE), startIndex, __count);
448 TryReturn(count <= __count && (startIndex + count <= __count), E_OUT_OF_RANGE,
449 "[%s] The startIndex(%d) + count(%d) MUST be less than or equal to the number of elements(%d).",
450 GetErrorMessage(E_OUT_OF_RANGE), startIndex, count, __count);
452 result r = E_OBJ_NOT_FOUND;
453 __LinkedListNodeT< Type >* pNode = GetNode(startIndex);
454 for (int i = 0; i < count; i++)
458 if (obj == pNode->pObj)
460 index = (startIndex + i);
464 pNode = pNode->pNext;
472 * Inserts the object at the specified location.
476 * @return An error code
477 * @param[in] obj The object to insert
478 * @param[in] index The index at which the object is inserted
479 * @exception E_SUCCESS The method is successful.
480 * @exception E_OUT_OF_RANGE Either of the following conditions has occurred:
481 * - The specified @c index is outside the bounds of the data structure.
482 * - The specified @c index is greater than the number of elements.
483 * - The specified @c index is less than @c 0.
484 * @remarks If the @c index is equal to the number of elements then the new elements
485 * is added at the end of this list.
489 virtual result InsertAt(const Type& obj, int index)
491 TryReturn(index >= 0 && index <= __count, E_OUT_OF_RANGE,
492 "[%s] The index(%d) MUST be greater than or equal to 0, and less than or equal to the number of elements(%d).",
493 GetErrorMessage(E_OUT_OF_RANGE), index, __count);
495 result r = E_SUCCESS;
497 __LinkedListNodeT< Type >* pPrevNode = null;
500 r = InsertFirst(obj);
502 else if (index == __count)
508 pPrevNode = GetNode(index - 1);
509 if (pPrevNode != null)
511 InsertNext(pPrevNode, obj);
517 AppLogException("[%s] Propagating.", GetErrorMessage(r));
525 * Inserts the elements of the collection at the specified location.
529 * @return An error code
530 * @param[in] collection The collection to insert
531 * @param[in] startIndex The starting index at which the collection is inserted
532 * @exception E_SUCCESS The method is successful.
533 * @exception E_OUT_OF_RANGE Either of the following conditions has occurred:
534 * - The specified @c index is outside the bounds of the data structure.
535 * - The specified @c startIndex is greater than the number of elements.
536 * - The specified @c startIndex is less than @c 0.
537 * @exception E_INVALID_OPERATION Either of the following conditions has occurred:
538 * - The current state of the instance prohibits the execution of the specified operation.
539 * - The specified @c collection is modified during the operation of this method.
540 * @remarks If the @c startIndex is equal to the number of elements then the new elements
541 * are added at the end of this list.
545 virtual result InsertItemsFrom(const ICollectionT< Type >& collection, int startIndex)
547 TryReturn(startIndex >= 0 && startIndex <= __count, E_OUT_OF_RANGE,
548 "[%s] The startIndex(%d) MUST be greater than or equal to 0, and less than or equal to the number of elements(%d).",
549 GetErrorMessage(E_OUT_OF_RANGE), startIndex, __count);
551 result r = E_SUCCESS;
553 IEnumeratorT< Type >* pEnum = null;
554 int count = collection.GetCount();
557 __LinkedListNodeT< Type >* pPrevNode = null;
559 if (startIndex == __count)
561 pPrevNode = __pListTail;
563 else if (startIndex > 0)
565 pPrevNode = GetNode(startIndex - 1);
568 ICollectionT< Type >* pCol = const_cast< ICollectionT< Type >* >(&collection);
569 pEnum = pCol->GetEnumeratorN();
570 TryCatch(pEnum != null, r = GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
575 r = pEnum->MoveNext();
577 if (E_OUT_OF_RANGE == r)
582 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
584 r = pEnum->GetCurrent(obj);
585 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
587 if (null == pPrevNode)
589 r = InsertFirst(obj);
590 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
591 pPrevNode = __pListHead;
595 pPrevNode = InsertNext(pPrevNode, obj);
596 TryCatch(pPrevNode != null, r = GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
617 * Searches for the last occurrence of an object in this list. @n
618 * Gets the index of the object if found.
622 * @return An error code
623 * @param[in] obj The object to locate
624 * @param[out] index The index of the last occurrence of the specified object
625 * @exception E_SUCCESS The method is successful.
626 * @exception E_OBJ_NOT_FOUND The specified @c obj has not been found.
629 virtual result LastIndexOf(const Type& obj, int& index) const
631 result r = E_OBJ_NOT_FOUND;
632 __LinkedListNodeT< Type >* pNode = __pListTail;
633 for (int i = (__count - 1); i >= 0; i--)
635 if (obj == pNode->pObj)
641 pNode = pNode->pPrev;
648 * Removes the first occurrence of the specified object from the list.
652 * @return An error code
653 * @param[in] obj The object to remove
654 * @exception E_SUCCESS The method is successful.
655 * @exception E_OBJ_NOT_FOUND The specified @c obj has not been found.
660 virtual result Remove(const Type& obj)
662 result r = E_OBJ_NOT_FOUND;
663 __LinkedListNodeT< Type >* pNode = __pListHead;
664 while (null != pNode)
666 if (pNode->pObj == obj)
672 pNode = pNode->pNext;
679 * Removes all the elements from the specified collection.
683 * @return An error code
684 * @param[in] collection The collection to remove from this list
685 * @exception E_SUCCESS The method is successful.
686 * @exception E_INVALID_OPERATION Either of the following conditions has occurred:
687 * - The current state of the instance prohibits the execution of the specified operation.
688 * - The specified @c collection is modified during the operation of this method.
692 virtual result RemoveItems(const ICollectionT< Type >& collection)
694 result r = E_SUCCESS;
696 ICollectionT< Type >* pCol = const_cast< ICollectionT< Type >* >(&collection);
697 IEnumeratorT< Type >* pEnum = pCol->GetEnumeratorN();
698 TryCatch(pEnum != null, r = GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
703 r = pEnum->MoveNext();
705 if (E_OUT_OF_RANGE == r)
710 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
712 r = pEnum->GetCurrent(temp);
713 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
716 TryLog(r == E_SUCCESS, "[%s] Remove() failed.", GetErrorMessage(r));
734 * Removes the object at the specified location.
738 * @return An error code
739 * @param[in] index The index at which the object is removed
740 * @exception E_SUCCESS The method is successful.
741 * @exception E_OUT_OF_RANGE Either of the following conditions has occurred:
742 * - The specified @c index is outside the bounds of the data structure.
743 * - The specified @c index is greater than the number of elements.
744 * - The specified @c index is less than @c 0.
747 virtual result RemoveAt(int index)
749 TryReturn(index < __count && index >= 0, E_OUT_OF_RANGE,
750 "[%s] The index(%d) MUST be greater than or equal to 0, and less than the number of elements(%d).",
751 GetErrorMessage(E_OUT_OF_RANGE), index, __count);
753 RemoveNode(GetNode(index));
758 * Removes all the elements within the specified range.
762 * @return An error code
763 * @param[in] startIndex The starting index of the range
764 * @param[in] count The number of elements to read
765 * @exception E_SUCCESS The method is successful.
766 * @exception E_OUT_OF_RANGE Either of the following conditions has occurred:
767 * - The specified @c index is outside the bounds of the data structure.
768 * - The specified @c startIndex is either greater than or equal to the number of elements.
769 * - The specified @c startIndex is less than @c 0.
770 * - The specified @c count is greater than the number of elements starting from @c startIndex.
771 * - The specified @c count is less than @c 0.
773 * @see InsertItemsFrom()
775 virtual result RemoveItems(int startIndex, int count)
777 TryReturn(startIndex >= 0 && count >= 0, E_OUT_OF_RANGE,
778 "[%s] Both of the startIndex(%d) and count(%d) MUST be greater than or equal to 0.",
779 GetErrorMessage(E_OUT_OF_RANGE), startIndex, count);
780 TryReturn(startIndex < __count, E_OUT_OF_RANGE,
781 "[%s] The startIndex(%d) MUST be less than the number of elements(%d).", GetErrorMessage(E_OUT_OF_RANGE), startIndex, __count);
782 TryReturn(count <= __count && (startIndex + count <= __count), E_OUT_OF_RANGE,
783 "[%s] The startIndex(%d) + count(%d) MUST be less than or equal to the number of elements(%d).",
784 GetErrorMessage(E_OUT_OF_RANGE), startIndex, count, __count);
788 __LinkedListNodeT< Type >* pNode = GetNode(startIndex);
789 for (int i = 0; i < count; i++)
793 __LinkedListNodeT< Type >* pNextNode = pNode->pNext;
803 * Removes all the elements from this list.
807 virtual void RemoveAll(void)
812 __LinkedListNodeT< Type >* pNode = __pListHead;
813 __LinkedListNodeT< Type >* pTemp;
814 while (null != pNode)
816 pTemp = pNode->pNext;
827 * Sets the object at the specified index with the specified object.
831 * @return An error code
832 * @param[in] obj The object to set
833 * @param[in] index The index at which the object is set
834 * @exception E_SUCCESS The method is successful.
835 * @exception E_OUT_OF_RANGE Either of the following conditions has occurred:
836 * - The specified @c index is outside the bounds of the data structure.
837 * - The specified @c index is either greater than or equal to the number of elements.
838 * - The specified @c index is less than @c 0.
841 virtual result SetAt(const Type& obj, int index)
843 TryReturn(index >= 0 && index < __count, E_OUT_OF_RANGE,
844 "[%s] The index(%d) MUST be greater than or equal to 0, less than the number of elements(%d).",
845 GetErrorMessage(E_OUT_OF_RANGE), index, __count);
848 __LinkedListNodeT< Type >* pNode = GetNode(index);
858 * Sorts the elements of this list using the comparer provided.
862 * @return An error code
863 * @param[in] comparer The IComparerT implementation to use when comparing the elements
864 * @exception E_SUCCESS The method is successful.
865 * @exception E_INVALID_ARG Either of the following conditions has occurred:
866 * - The specified input parameter is invalid.
867 * - The specified @c comparer is invalid.
869 virtual result Sort(const IComparerT< Type >& comparer)
876 result r = QuickSort(0, (__count - 1), comparer);
879 AppLogException("[%s] Propagating.", GetErrorMessage(r));
886 * Gets the number of objects currently stored in this list.
890 * @return The number of objects currently stored in this list
892 virtual int GetCount(void) const
898 * Checks whether the list contains the specified object.
902 * @return @c true if the list contains the specified object, @n
904 * @param[in] obj The object to locate
907 virtual bool Contains(const Type& obj) const
914 __LinkedListNodeT< Type >* pNode = GetNode(0);
915 for (int i = 0; i < __count; i++)
919 if (obj == pNode->pObj)
923 pNode = pNode->pNext;
931 * Checks whether the list contains all the elements of the specified collection.
935 * @return An error code
936 * @param[in] collection The collection to check for containment in this list
937 * @param[out] out Set to @c true if this list contains all of the elements in the specified collection, @n
939 * @exception E_SUCCESS The method is successful.
940 * @exception E_INVALID_OPERATION Either of the following conditions has occurred:
941 * - The current state of the instance prohibits the execution of the specified operation.
942 * - The specified @c collection is modified during the operation of this method.
943 * @remarks If the given @c collection is empty, then @c out is set to @c true.
946 virtual result ContainsAll(const ICollectionT< Type >& collection, bool& out) const
948 result r = E_SUCCESS;
951 if (collection.GetCount() == 0)
957 ICollectionT< Type >* pCol = const_cast< ICollectionT< Type >* >(&collection);
958 IEnumeratorT< Type >* pEnum = pCol->GetEnumeratorN();
959 TryCatch(pEnum != null, r = GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
965 r = pEnum->MoveNext();
967 if (E_OUT_OF_RANGE == r)
973 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
975 r = pEnum->GetCurrent(temp);
976 TryCatch(r == E_SUCCESS, , "[%s] Propagating.", GetErrorMessage(r));
978 if (false == Contains(temp))
1000 * Compares the given object with the calling LinkedList object.
1004 * @return @c true if the specified instance equals the current instance, @n
1006 * @param[in] obj The object to compare with the calling object
1007 * @remarks This method returns @c true only if the specified object is also an instance of the LinkedList class,
1008 * both lists have the same size, and all the corresponding pairs of elements in the two lists are equal. @n
1009 * In other words, the two lists are equal if they contain the same elements in the same order.
1011 virtual bool Equals(const Object& obj) const
1015 const LinkedListT< Type >* other = dynamic_cast< const LinkedListT< Type >* >(&obj);
1020 else if (other == this)
1024 else if (__count != other->__count)
1030 __LinkedListNodeT< Type >* pNode = __pListHead;
1031 __LinkedListNodeT< Type >* pOtherNode = other->__pListHead;
1032 for (int i = 0; i < __count; i++)
1034 if (!(pNode->pObj == pOtherNode->pObj))
1039 pNode = pNode->pNext;
1040 pOtherNode = pOtherNode->pNext;
1048 * Gets the hash value of the current instance.
1052 * @return The hash value of the current instance
1053 * @remarks The two Tizen::Base::Object::Equals() instances must return the same hash value. @n
1054 * For better performance, the used hash function must generate a random distribution for all the inputs.
1056 virtual int GetHashCode(void) const
1061 __LinkedListNodeT< Type >* pNode = __pListHead;
1062 while (pNode != null)
1064 hash += reinterpret_cast< int >(pNode);
1065 pNode = pNode->pNext;
1074 * The implementation of this copy constructor is intentionally blank and declared as private to prohibit copying of objects.
1076 * @param[in] list The %LinkedListT object to initialize the new object
1078 LinkedListT(const LinkedListT< Type >& list);
1081 * The implementation of this copy assignment operator is intentionally blank and declared as private to prohibit copying of objects.
1083 * @param[in] list An instance of %LinkedListT
1085 LinkedListT< Type >& operator =(const LinkedListT< Type >& list);
1088 * Gets the node at the specified index.
1090 * @return A node at the specified index
1091 * @param[in] index The index of the node to read
1093 __LinkedListNodeT< Type >* GetNode(int index) const
1095 if (index >= __count)
1100 __LinkedListNodeT< Type >* pNode = __pListHead;
1101 for (int i = 0; i < index; i++)
1103 pNode = pNode->pNext;
1110 * Inserts an object to the beginning of the %LinkedList.
1112 * @return An error code
1113 * @param[in] obj The object to insert
1114 * @exception E_OUT_OF_MEMORY The memory is insufficient.
1115 * @exception E_SUCCESS The method is successful.
1117 result InsertFirst(const Type& obj)
1119 result r = E_SUCCESS;
1120 __LinkedListNodeT< Type >* pNode = new __LinkedListNodeT< Type >(obj);
1121 TryCatch(pNode != null, r = E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1125 if (null == __pListHead)
1127 __pListHead = pNode;
1128 __pListTail = pNode;
1132 pNode->pNext = __pListHead;
1133 __pListHead->pPrev = pNode;
1134 __pListHead = pNode;
1144 * Inserts an object to the end of the LinkedList.
1146 * @return An error code
1147 * @param[in] obj The object to insert
1148 * @exception E_OUT_OF_MEMORY The memory is insufficient.
1149 * @exception E_SUCCESS The method is successful.
1151 result InsertLast(const Type& obj)
1153 __LinkedListNodeT< Type >* pNode = InsertNext(__pListTail, obj);
1156 AppLogException("[%s] Propagating.", GetErrorMessage(GetLastResult()));
1159 return GetLastResult();
1163 * Inserts an object after the specified node.
1165 * @return An error code
1166 * @param[in] obj The object to insert
1167 * @param[in] pPrevNode The node after which the object must be inserted
1168 * @exception E_OUT_OF_MEMORY The memory is insufficient.
1169 * @exception E_SUCCESS The method is successful.
1170 * @remarks The specific error code can be accessed using the GetLastResult() method.
1172 __LinkedListNodeT< Type >* InsertNext(__LinkedListNodeT< Type >* pPrevNode, const Type& obj)
1175 result r = E_SUCCESS;
1176 __LinkedListNodeT< Type >* pNode = new __LinkedListNodeT< Type >(obj);
1177 TryCatch(pNode != null, r = E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
1181 if (null == __pListHead)
1183 __pListHead = pNode;
1184 __pListTail = pNode;
1186 else if (pPrevNode == __pListTail)
1188 pNode->pPrev = __pListTail;
1189 __pListTail->pNext = pNode;
1190 __pListTail = pNode;
1194 TryCatch(pPrevNode != null, r = E_INVALID_ARG, "[%s] The pPrevNode is null.", GetErrorMessage(E_INVALID_ARG));
1195 pNode->pPrev = pPrevNode;
1196 pNode->pNext = pPrevNode->pNext;
1197 pPrevNode->pNext->pPrev = pNode;
1198 pPrevNode->pNext = pNode;
1215 * Removes the specified node.
1217 * @param[in] pNode The pointer of the node to remove
1219 void RemoveNode(__LinkedListNodeT< Type >* pNode)
1221 AppAssertf((null != pNode), "pNode is null.\n");
1222 AppAssertf((__count > 0), "__count is zero.\n");
1231 else if (pNode == __pListHead)
1233 __pListHead = pNode->pNext;
1234 __pListHead->pPrev = null;
1236 else if (pNode == __pListTail)
1238 __pListTail = pNode->pPrev;
1239 __pListTail->pNext = null;
1243 pNode->pNext->pPrev = pNode->pPrev;
1244 pNode->pPrev->pNext = pNode->pNext;
1250 * Sorts the specified sub-list within the calling instance.
1252 * @param[in] startIndex The starting index of the sub-list to sort
1253 * @param[in] endIndex The ending index of the sub-list to sort
1254 * @param[in] pComparer The comparer function to sort the list
1255 * @exception E_SUCCESS The method is successful.
1256 * @exception E_INVALID_ARG A specified input parameter is invalid, or @n
1257 * the comparer has failed to compare the elements.
1259 result QuickSort(int startIndex, int endIndex, const IComparerT< Type >& comparer)
1261 result r = E_SUCCESS;
1263 if (startIndex < endIndex)
1266 int i = startIndex - 1;
1267 int j = endIndex + 1;
1268 Type startObj = Type();
1269 __LinkedListNodeT< Type >* pNodeI = null;
1270 __LinkedListNodeT< Type >* pNodeJ = null;
1271 __LinkedListNodeT< Type >* pNode = GetNode(startIndex);
1275 startObj = pNode->pObj;
1280 int compareResult = 1;
1281 while ((compareResult > 0) && (j > static_cast< int >(startIndex)))
1284 pNodeJ = (null == pNodeJ) ? GetNode(j) : pNodeJ->pPrev;
1287 r = comparer.Compare(pNodeJ->pObj, startObj, compareResult);
1288 TryReturn(r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
1293 while ((compareResult < 0) && (i < static_cast< int >(endIndex)))
1296 pNodeI = (null == pNodeI) ? GetNode(i) : pNodeI->pNext;
1299 r = comparer.Compare(pNodeI->pObj, startObj, compareResult);
1300 TryReturn(r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
1306 if ((pNodeJ != null) && (pNodeI != null))
1308 Type temp = pNodeJ->pObj;
1309 pNodeJ->pObj = pNodeI->pObj;
1310 pNodeI->pObj = temp;
1320 r = QuickSort(startIndex, middleIndex, comparer);
1321 TryReturn(r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
1323 r = QuickSort(middleIndex + 1, endIndex, comparer);
1324 TryReturn(r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
1330 __LinkedListNodeT< Type >* __pListHead;
1331 __LinkedListNodeT< Type >* __pListTail;
1334 friend class __LinkedListEnumeratorT< Type >;
1339 // @class __LinkedListNodeT
1340 // @brief This is a node for LinkedListT.
1343 template< class Type >
1344 class __LinkedListNodeT
1349 * This is the constructor for this class.
1353 * @param[in] obj The object to include in this node
1355 __LinkedListNodeT(const Type& obj)
1363 * Internal variable.
1367 __LinkedListNodeT< Type >* pPrev;
1370 * Internal variable.
1374 __LinkedListNodeT< Type >* pNext;
1377 * Internal variable.
1383 }; // __LinkedListNodeT
1386 // @class __LinkedListEnumeratorT
1387 // @brief This class is an implementation of IEnumeratorT for %LinkedListT.
1390 template< class Type >
1391 class __LinkedListEnumeratorT
1392 : public IBidirectionalEnumeratorT< Type >
1397 * This is the constructor for this class.
1401 * @param[in] list The list to enumerate
1402 * @param[in] modCount The modification count to detect the change in the list
1404 __LinkedListEnumeratorT(const LinkedListT< Type >& list, int modCount)
1407 , __modCount(modCount)
1413 * This is the destructor for this class.
1417 virtual ~__LinkedListEnumeratorT(void)
1423 * Gets the current object in the list.
1427 * @return An error code
1428 * @param[out] obj The current object
1429 * @exception E_INVALID_OPERATION Either of the following conditions has occurred:
1430 * - The current state of the instance prohibits the execution of the specified operation.
1431 * - This enumerator is currently positioned before the first element or
1432 * past the last element.
1433 * - The list is modified after this enumerator is created.
1434 * @exception E_SUCCESS The method is successful.
1436 result GetCurrent(Type& obj) const
1438 TryReturn(__modCount == __list.__modCount, E_INVALID_OPERATION,
1439 "[%s] The source collection is modified after the creation of this enumerator.", GetErrorMessage(E_INVALID_OPERATION));
1440 TryReturn(__pNode != null, E_INVALID_OPERATION,
1441 "[%s] Current position is before the first element or past the last element.", GetErrorMessage(E_INVALID_OPERATION));
1443 obj = __pNode->pObj;
1449 * Moves this enumerator to the next element of the list. @n
1450 * When this enumerator is first created or after a call to Reset(),
1451 * the first call to MoveNext() positions this enumerator to the first element in the list.
1455 * @return An error code
1456 * @exception E_SUCCESS The method is successful.
1457 * @exception E_INVALID_OPERATION Either of the following conditions has occurred:
1458 * - The current state of the instance prohibits the execution of the specified operation.
1459 * - The list is modified after this enumerator is created.
1460 * @exception E_OUT_OF_RANGE The enumerator has passed the end of the list.
1463 result MoveNext(void)
1465 TryReturn(__modCount == __list.__modCount, E_INVALID_OPERATION,
1466 "[%s] The source collection is modified after the creation of this enumerator.", GetErrorMessage(E_INVALID_OPERATION));
1468 if (null == __pNode)
1470 __pNode = __list.__pListHead;
1471 if (null == __pNode)
1473 return E_OUT_OF_RANGE;
1478 if (null != __pNode->pNext)
1480 __pNode = __pNode->pNext;
1484 return E_OUT_OF_RANGE;
1492 * Positions this enumerator before the first element in the list.
1496 * @return An error code
1497 * @exception E_SUCCESS The method is successful.
1498 * @exception E_INVALID_OPERATION Either of the following conditions has occurred:
1499 * - The current state of the instance prohibits the execution of the specified operation.
1500 * - The list is modified after this enumerator is created.
1504 TryReturn(__modCount == __list.__modCount, E_INVALID_OPERATION,
1505 "[%s] The source collection is modified after the creation of this enumerator.", GetErrorMessage(E_INVALID_OPERATION));
1511 result MovePrevious(void)
1513 TryReturn(__modCount == __list.__modCount, E_INVALID_OPERATION,
1514 "[%s] The source collection is modified after the creation of this enumerator.", GetErrorMessage(E_INVALID_OPERATION));
1516 if (__pNode == null)
1518 __pNode = __list.__pListTail;
1519 if (__pNode == null)
1521 return E_OUT_OF_RANGE;
1526 if (__pNode->pPrev != null)
1528 __pNode = __pNode->pPrev;
1532 return E_OUT_OF_RANGE;
1539 result ResetLast(void)
1545 const LinkedListT< Type >& __list;
1546 __LinkedListNodeT< Type >* __pNode;
1549 }; // __LinkedListEnumeratorT
1551 }}} // Tizen::Base::Collection
1553 #endif // _FCOL_LINKED_LIST_T_H_