-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcounter.h
69 lines (57 loc) · 1.38 KB
/
counter.h
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#ifndef COUNTER_H
#define COUNTER_H
#include <atomic>
#include <array>
#include <iostream>
namespace utl
{
template<int dim>
class Counter
{
typedef std::array<int, dim - 1> Borders;
public:
typedef std::array<int, dim> array;
Counter(const Borders& b) : counter(0), borders(b) {}
Counter() : counter(0) { borders[0] = INT_MAX; }
void changeBorder(const Borders& b) { borders = b;}
array operator ++(int);
operator array() const;
private:
inline array longToArray(long);
std::atomic_int_fast64_t counter;
Borders borders;
};
template<int dim>
typename Counter<dim>::array Counter<dim>::operator ++(int){
return longToArray(counter++);
}
template<int dim>
Counter<dim>::operator array() const{
return longToArray(counter);
}
template<int dim>
typename Counter<dim>::array Counter<dim>::longToArray(long c)
{
std::array<int, dim> cs;
for (int i = 0 ; i < dim - 1; i++){
cs[i] = c % borders[i];
c /= borders[i];
}
cs[dim - 1] = c;
//std::cout<<c << " - " << cs[0] << "-" << cs[1] << " - " << borders[0] <<std::endl;
return cs;
}
/*
template<>
Counter<2>::operator array() const{
long c = counter;
return {c % borders[0], c / borders[0]};
}
template<>
typename Counter<2>::array Counter<2>::operator++(int){
long c = counter++;
return {c % borders[0], c / borders[0]};
}
*/
}
#endif // COUNTER_H