1 // -*- Mode: C++; tab-width: 2; -*-
2 // vi: set ts=2:
3 //
4 
5 #ifndef BALL_DATATYPE_LIST_H
6 #define BALL_DATATYPE_LIST_H
7 
8 #ifndef BALL_COMMON_H
9 #	include <BALL/common.h>
10 #endif
11 
12 #ifndef BALL_CONCEPT_VISITOR_H
13 #	include <BALL/CONCEPT/visitor.h>
14 #endif
15 
16 #ifndef BALL_CONCEPT_PROCESSOR_H
17 #	include <BALL/CONCEPT/processor.h>
18 #endif
19 
20 #include <list>
21 
22 #ifdef BALL_COMPILER_GXX
23 #warning "This header file is deprecated and should not be used in new code! As a replacement for BALL::List the use of std::list is strongly suggested."
24 #endif
25 
26 namespace BALL
27 {
28 	/** Extended list object.
29 		This object is an improved version of the STL list class
30 
31     \ingroup  DatatypeMiscellaneous
32 	*/
33  	template <typename Value>
34 	class List
35 		:	public std::list<Value>
36 	{
37 		public:
38 
39 		/**	@name	Type Definitions
40 		*/
41 		//@{
42 
43 		/**	Iterator type.
44 		*/
45 		typedef typename std::list<Value>::iterator Iterator;
46 		// for STL compatibility
47 		typedef typename std::list<Value>::iterator iterator;
48 
49 		/**	Constant iterator type.
50 		*/
51 		typedef typename std::list<Value>::const_iterator ConstIterator;
52 		// for STL compatibility
53 		typedef typename std::list<Value>::const_iterator const_iterator;
54 
55 		//@}
56 		/**	@name	Constructors and Destructors */
57 		//@{
58 
59 		BALL_CREATE_DEEP(List)
60 
61 		/**	Default constructor.
62 				Create an empty list.
63 		*/
List()64 		List()
65 			: std::list<Value>()
66 		{
67 		}
68 
69 		/** Copy constructor.
70 				Create a copy of an existing list.
71 				@param	map the list to be copied
72 				@param	deep ignored
73 		*/
List(const List & new_list,bool)74 		List(const List& new_list, bool /* deep = true */)
75 			: std::list<Value>(new_list)
76 		{
77 		}
78 
79 		/** Clear the list
80 		*/
destroy()81  		void destroy()
82 		{
83 			std::list<Value>::clear();
84 		}
85 
86 		/** Destructor
87 		*/
~List()88 		virtual ~List()
89 		{
90 			std::list<Value>::clear();
91 		}
92 
93 		//@}
94 		/**	@name	Assignment */
95 		//@{
96 
97 		/** Assign a list from another.
98 				@param	list	the map to be copied
99 				@param	deep ignored
100 		*/
101     void set(const List& list, bool /* deep */ = true)
102 		{
103 			std::list<Value>::clear();
104 
105 			ConstIterator it = list.begin();
106 			for ( ; it != list.end(); ++it)
107 			{
108 				std::list<Value>::push_back(const_cast<Value&>(*it));
109 			}
110 		}
111 
112 		/** Assign a list from another.
113 		*/
114 		const List& operator = (const List& list)
115 		{
116 			set(list);
117 			return *this;
118 		}
119 
120 		/// Assign the content of a list to another
121     void get(List& list, bool deep = true) const
122 		{
123 			list.set(*this, deep);
124 		}
125 
126 		/// Swaps the contents of two lists
swap(List & list)127     void swap(List& list)
128 		{
129 			List<Value> temp;
130 			temp.set(*this);
131 			(*this).set(list);
132 			list.set(temp);
133 		}
134 
135 		//@}
136 		/**	@name Accessors */
137 		//@{
138 
139 		/** Return the size of the list.
140 		*/
getSize()141 		Size getSize() const
142 		{
143 			return (Size)std::list<Value>::size();
144 		}
145 
146 		/** Remove an item from the list. The first item that matches <tt>item</tt> will be removed.
147 				@param 	item the item to be removed
148 				@return bool <b>true</b> if the item was removed
149 		 */
remove(const Value & item)150 		bool remove(const Value& item)
151 		{
152 			Iterator it = std::list<Value>::begin();
153 			for (; it != std::list<Value>::end(); ++it)
154 			{
155 				if (*it == item)
156 				{
157 					std::list<Value>::erase(it);
158 					return true;
159 				}
160 			}
161 			return false;
162 		}
163 
164 		//@}
165 		/**	@name	Predicates */
166 		//@{
167 
168 		/** Return true if the list is empty.
169 				This method return <b>true</b> if the list does not contain any entries.
170 		*/
isEmpty()171 		bool isEmpty() const
172 		{
173 			return (std::list<Value>::size() == 0);
174 		}
175 
176 		//@}
177 		/**	@name	DatatypeMiscellaneous */
178 		//@{
179 
180 		/**	Visitor host method.
181 				Lists may be visited.
182 				@param	visitor	the visitor
183 		*/
184 		virtual void host(Visitor<List<Value> >& visitor);
185 
186 		//@}
187 		/**	@name	Internal Iterators */
188 		//@{
189 
190 		/** Processor application method.
191 				Applies the processor to each entry of the list.
192 				@param processor the processor to be applied
193 		*/
apply(UnaryProcessor<Value> & processor)194 		bool apply(UnaryProcessor<Value>& processor)
195 		{
196 			if (!processor.start()) return false;
197 
198 			for (Iterator it = std::list<Value>::begin(); it != std::list<Value>::end(); ++it)
199 			{
200 				Processor::Result result = processor(*it);
201 				if (result <= Processor::BREAK)
202 				{
203 					return (result == Processor::BREAK);
204 				}
205 			}
206 
207 			return processor.finish();
208 		}
209 
210 		//@}
211 
212 		/** Equality operator.
213 				Test if two instances have the same size and same items at the same positions.
214 		*/
215 		bool operator == (const List<Value>& list) const
216 		{
217 			if (std::list<Value>::size() != list.size())
218 			{
219 				return false;
220 			}
221 
222 			typename List<Value>::ConstIterator this_it = std::list<Value>::begin();
223 			typename List<Value>::ConstIterator list_it = list.begin();
224 
225 			for (; this_it != std::list<Value>::end(); ++this_it)
226 			{
227 				if (!(*this_it == *list_it))
228 				{
229 					return false;
230 				}
231 				++list_it;
232 			}
233 			return true;
234 		}
235 
236 		/** Inequality operator.
237 				Test if two instances differ in at least one element.
238 		*/
239 		bool operator != (const List<Value>& list) const
240 		{
241 			return !(*this == list);
242 		}
243 
244 	};
245 
246 	template <typename Value>
host(Visitor<List<Value>> & visitor)247 	void List<Value>::host(Visitor<List<Value> >& visitor)
248 	{
249 		visitor.visit(*this);
250 	}
251 } // namespace BALL
252 
253 #endif // BALL_DATATYPE_LIST_H
254