1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     List.h
4 
5   Copyright (C)2009-2012 Nintendo Co., Ltd.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law.  They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Rev: 46365 $
14  *---------------------------------------------------------------------------*/
15 
16 #ifndef NN_COMMON_SCENE_LIST_H_
17 #define NN_COMMON_SCENE_LIST_H_
18 
19 namespace scene
20 {
21 
22 template <class T> class Node
23 {
24 public:
25     // Data
26     T data;
27     // Next node
28     Node<T>* pNextNode;
29 };
30 
31 template <class T> class List
32 {
33 public:
34     // Iterator
35     class iterator
36     {
37     public:
38         // Constructor
iterator(Node<T> * pNode)39         iterator(Node<T>* pNode)
40         {
41             m_pNode = pNode;
42         }
43 
44         // Return the data reference
45         T operator*()
46         {
47             return m_pNode->data;
48         }
49 
50         // Compare
51         bool operator==(List<T>::iterator it)
52         {
53             return (m_pNode == it.m_pNode);
54         }
55 
56         // Compare
57         bool operator!=(List<T>::iterator it)
58         {
59             return (m_pNode != it.m_pNode);
60         }
61 
62         // Later positioned increment
63         void operator++(int)
64         {
65             m_pNode = m_pNode->pNextNode;
66         }
67 
68     protected:
69         Node<T>* m_pNode;
70     };
71 
72 public:
73     // Constructor
List()74     List()
75     {
76         m_pHeadNode = NULL;
77         m_pTailNode = NULL;
78     }
79 
80     // Add to start
push_front(T data)81     void push_front(T data)
82     {
83         Node<T>* pNode = new Node<T>;
84 
85         pNode->data      = data;
86         pNode->pNextNode = NULL;
87 
88         if (m_pHeadNode == NULL)
89         {
90             m_pHeadNode = pNode;
91         }
92         if (m_pTailNode != NULL)
93         {
94             m_pTailNode->pNextNode = pNode;
95         }
96         m_pTailNode = pNode;
97     }
98 
99     // Clear
clear()100     void clear()
101     {
102         Node<T>* pCurNode = m_pHeadNode;
103 
104         while (pCurNode)
105         {
106             Node<T>* pNextNode = pCurNode->pNextNode;
107 
108             delete pCurNode;
109             pCurNode = pNextNode;
110         }
111 
112         m_pHeadNode = NULL;
113         m_pTailNode = NULL;
114     }
115 
116     // Return start iterator
begin()117     List<T>::iterator begin()
118     {
119         return iterator(m_pHeadNode);
120     }
121 
122     // Return terminal iterator
end()123     List<T>::iterator end()
124     {
125         return iterator(NULL);
126     }
127 
128 protected:
129     // Start node
130     Node<T>* m_pHeadNode;
131     // Terminal node
132     Node<T>* m_pTailNode;
133 };
134 
135 } // namespace scene
136 
137 #endif // NN_COMMON_SCENE_LIST_H_
138