summaryrefslogtreecommitdiff
path: root/common/list.h
blob: 513f79cb6e01afe88ee9cff7c81939f4a8517cbc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#ifndef LIST_H
#define LIST_H

#include <vector>
#include <algorithm>
#include <boost/serialization/base_object.hpp>

//! List class with extended functionality over std::vector.
template<class T, class B = std::vector<T> >
class List : public B {
	public:
		//! Default constructor.
		List() : B() {}
		
		//! Iterator constructor.
		template <class InputIterator>
		List(InputIterator first, InputIterator last) : B(first, last) {}
		
		//! Sort the list.
		void sort() {
			std::sort(B::begin(), B::end());
		}
		
		//! Check if specified item is present in list.
		bool contains(const T& item) {
			return std::find(B::begin(), B::end(), item) != B::end();
		}
		
		//! Count number of instances of specified item in list.
		std::size_t count(const T& item) {
			return std::count(B::begin(), B::end(), item);
		}
		
		//! Delete item by index.
		void del(std::size_t index) {
			erase(B::begin() + index);
		}
		
		//! Check whether list is empty or not.
		operator bool() const {
			return !B::empty();
		}
		
		//! Allow serialization through Boost.Serialize.
		template<class Archive>
		void serialize(Archive & ar, const unsigned int version) {
			ar & boost::serialization::base_object<B>(*this);
		}
};

#endif