diff options
| author | Sven Eisenhauer <sven@sven-eisenhauer.net> | 2023-11-10 15:11:48 +0100 |
|---|---|---|
| committer | Sven Eisenhauer <sven@sven-eisenhauer.net> | 2023-11-10 15:11:48 +0100 |
| commit | 33613a85afc4b1481367fbe92a17ee59c240250b (patch) | |
| tree | 670b842326116b376b505ec2263878912fca97e2 /Bachelor/Prog2/SortedList2/SortedList.h | |
| download | Studium-master.tar.gz Studium-master.tar.bz2 | |
Diffstat (limited to 'Bachelor/Prog2/SortedList2/SortedList.h')
| -rw-r--r-- | Bachelor/Prog2/SortedList2/SortedList.h | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/Bachelor/Prog2/SortedList2/SortedList.h b/Bachelor/Prog2/SortedList2/SortedList.h new file mode 100644 index 0000000..300bd74 --- /dev/null +++ b/Bachelor/Prog2/SortedList2/SortedList.h @@ -0,0 +1,63 @@ +// Template for sorted list
+// Author: U.Breymann / H.P.Weber
+// Date: 05.05.05
+
+#ifndef SORTEDLIST_H
+#define SORTEDLIST_H
+
+#include <list>
+#include <algorithm> // find
+
+#include "compare.h"
+
+template< class T >
+class SortedList
+{
+public:
+ typedef std::list< T >::iterator Iterator;
+ typedef std::list< T >::const_iterator ConstIterator;
+ SortedList( const Compare< T >& cmp = Compare< T >() ) : comp( cmp ) { }
+ virtual ~SortedList() { }
+
+ bool empty() const { return myList.empty(); }
+ int size() const { return myList.size(); }
+
+ // take first or last element
+ void pop_front() { myList.pop_front(); }
+ void pop_back() { myList.pop_back(); }
+
+ // read first or last element
+ T& front() { return myList.front(); }
+ const T& front() const { return myList.front(); }
+ T& back() { return myList.back(); }
+ const T& back() const { return myList.back(); }
+
+ Iterator begin() { return myList.begin(); }
+ ConstIterator begin() const { return myList.begin(); }
+ Iterator end() { return myList.end(); }
+ ConstIterator end() const { return myList.end(); }
+
+ void erase( Iterator& pos ) { myList.erase( pos ); }
+
+ // find element
+ Iterator find( const T& value )
+ {
+ return std::find( myList.begin(), myList.end(), value );
+ }
+
+ // sorted insert
+ Iterator insert( const T& value )
+ {
+ // find place to insert
+ Iterator temp( begin() );
+ while( temp != end() && comp( *temp, value ) ) ++temp;
+ return myList.insert( temp, value );
+ }
+
+private:
+ std::list< T > myList;
+ Compare< T > comp;
+};
+
+#endif
+
|
