source: src/Helpers/enumeration.hpp@ 70672e3

Last change on this file since 70672e3 was 70672e3, checked in by Frederik Heber <heber@…>, 15 years ago

Added config.h to all header files.

  • Property mode set to 100644
File size: 1.4 KB
Line 
1/*
2 * enumerator.hpp
3 *
4 * Created on: Dec 21, 2010
5 * Author: heber
6 */
7
8#ifndef ENUMERATOR_HPP_
9#define ENUMERATOR_HPP_
10
11// include config.h
12#ifdef HAVE_CONFIG_H
13#include <config.h>
14#endif
15
16/************ struct to contain simple enumerations ***************/
17template <class C>
18struct enumeration{
19 enumeration() : max(0) {}
20 enumeration(unsigned int i) : max(i) {}
21 enumeration(const enumeration &src) :
22 there(src.there),
23 back(src.back),
24 max(src.max)
25 {}
26 enumeration &operator=(const enumeration &src){
27 /* no self-assignment check needed */
28 there = src.there;
29 back = src.back;
30 max = src.max;
31 return *this;
32 }
33 void add(const C &value){
34 if(!there.count(value)){
35 there[value]=max;
36 back[max++]=value;
37 }
38 }
39 unsigned int getMax() const{
40 return max;
41 }
42
43 map<C,unsigned int> there;
44 map<unsigned int,C> back;
45private:
46 unsigned int max;
47};
48
49/***** A counter to generate sequential numbers *******************/
50struct counter{
51 inline counter() : count(0){};
52 inline counter(int i) : count(i){};
53 inline unsigned int operator()(){
54 return count++;
55 }
56private:
57 unsigned int count;
58};
59
60template <class C,class ForwardIterator>
61enumeration<C> enumerate(ForwardIterator first,ForwardIterator last){
62 enumeration<C> res;
63 for_each(first,last,bind1st(mem_fun(&enumeration<C>::add),&res));
64 return res;
65}
66
67#endif /* ENUMERATOR_HPP_ */
Note: See TracBrowser for help on using the repository browser.