IPPL (Independent Parallel Particle Layer)
IPPL
Loading...
Searching...
No Matches
TagMaker.h
Go to the documentation of this file.
1/***************************************************************************
2 *
3 * The IPPL Framework
4 *
5 ***************************************************************************/
6
7#ifndef TAG_MAKER_H
8#define TAG_MAKER_H
9
10/*
11 * TagMaker.h - creates tags from a given base tag and a cycle size. New
12 * tags are generated each time one is requested, by adding an
13 * integer which varies from 0 ... (cycle size - 1) to the provided
14 * base tag. Routines exist to establish a base tag and cycle size,
15 * and to get a new tag for a given base tag.
16 */
17
18// include files
19#include <map>
20
21// default cycle size, if not specified by the user
22#define DEF_CYCLE_SIZE 1000
23
24class TagMaker {
25public:
26 // constructor/destructor
27 TagMaker(void) {}
28 virtual ~TagMaker(void) {}
29
30 // generate a new tag given a base tag. If the base tag has not been
31 // previously established by create_base_tag, it will be done so by
32 // this routine with the default cycle size. A new tag can be established
33 // at the same time by also giving a cycle size as the second argument.
34 int next_tag(int t, int s = DEF_CYCLE_SIZE) {
35 TagInfo& found = create_base_tag(t, s);
36 found.current = (found.current + 1) % found.cycleSize;
37 return (found.base + found.current);
38 }
39
40 // determine the tag immediately preceding the current one
41 // for a given base tag. If the base tag doesn't exist, it will be
42 // created and the largest possible tag within the cycle will
43 // be returned.
44 int preceding_tag(int t, int s = DEF_CYCLE_SIZE) {
45 const TagInfo& found = create_base_tag(t, s);
46 if (found.current == 0) {
47 return (found.base + found.cycleSize - 1);
48 }
49 return (found.base + found.current - 1);
50 }
51
52 // determine the tag immediately following the current one
53 // for a given base tag. If the base tag doesn't exist, it will be
54 // created and the second smallest possible tag within the cycle will
55 // be returned
56 int following_tag(int t, int s = DEF_CYCLE_SIZE) {
57 const TagInfo& found = create_base_tag(t, s);
58 const int following = (found.current + 1) % found.cycleSize;
59 return (found.base + following);
60 }
61
62 // just return the `current' tag that is to be generated from the
63 // given base tag, without incrementing the cycle counter.
64 int current_tag(int t, int s = DEF_CYCLE_SIZE) {
65 TagInfo& found = create_base_tag(t, s);
66 return (found.base + found.current);
67 }
68
69 // reset the cycle counter for the given tag to be 0. If the tag is
70 // not in the list, it is added. Returns the reset tag.
71 int reset_tag(int t, int s = DEF_CYCLE_SIZE) {
72 TagInfo& found = create_base_tag(t, s);
73 found.current = 0;
74 return found.base;
75 }
76
77private:
78 // Simple struct holding info about the cycle size and current tag
79 // for a base tag
80 class TagInfo {
81 public:
82 int base; // base tag value, the key for the map
83 int cycleSize; // range through which to cycle tag
84 int current; // current value of tag
85 TagInfo(int b, int s)
86 : base(b)
87 , cycleSize(s)
88 , current(0) {}
90 : base(-1)
91 , cycleSize(-1)
92 , current(0) {}
93 };
94
95 // class used for comparisons
96 class TagCompare {
97 public:
98 bool operator()(const int& x, const int& y) const { return x < y; }
99 };
100
101 // the list of base tags which have been established
102 std::map<int, TagInfo, TagCompare> TagList;
103
104 // Establish a new base tag and cycle size. Returns a reference to
105 // the new TagInfo structure.
106 // Arguments are: base tag, cycle size.
108 TagInfo& found = TagList[t];
109 if (found.base < 0) {
110 found.base = t;
111 found.cycleSize = s;
112 }
113 return TagList[t];
114 }
115};
116
117#endif // TAG_MAKER_H
118
119// vi: set et ts=4 sw=4 sts=4:
120// Local Variables:
121// mode:c
122// c-basic-offset: 4
123// indent-tabs-mode: nil
124// require-final-newline: nil
125// End:
#define DEF_CYCLE_SIZE
Definition TagMaker.h:22
std::map< int, TagInfo, TagCompare > TagList
Definition TagMaker.h:102
int following_tag(int t, int s=DEF_CYCLE_SIZE)
Definition TagMaker.h:56
int current_tag(int t, int s=DEF_CYCLE_SIZE)
Definition TagMaker.h:64
TagInfo & create_base_tag(int t, int s=DEF_CYCLE_SIZE)
Definition TagMaker.h:107
TagMaker(void)
Definition TagMaker.h:27
int preceding_tag(int t, int s=DEF_CYCLE_SIZE)
Definition TagMaker.h:44
int next_tag(int t, int s=DEF_CYCLE_SIZE)
Definition TagMaker.h:34
int reset_tag(int t, int s=DEF_CYCLE_SIZE)
Definition TagMaker.h:71
virtual ~TagMaker(void)
Definition TagMaker.h:28
TagInfo(int b, int s)
Definition TagMaker.h:85
bool operator()(const int &x, const int &y) const
Definition TagMaker.h:98