OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
OPAL
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 
19 // include files
20 #include <map>
21 
22 
23 // default cycle size, if not specified by the user
24 #define DEF_CYCLE_SIZE 1000
25 
26 
27 class TagMaker
28 {
29 
30 public:
31  // constructor/destructor
32  TagMaker(void) { }
33  virtual ~TagMaker(void) { }
34 
35  // generate a new tag given a base tag. If the base tag has not been
36  // previously established by create_base_tag, it will be done so by
37  // this routine with the default cycle size. A new tag can be established
38  // at the same time by also giving a cycle size as the second argument.
39  int next_tag(int t, int s = DEF_CYCLE_SIZE)
40  {
41  TagInfo& found = create_base_tag(t, s);
42  found.current = (found.current + 1) % found.cycleSize;
43  return (found.base + found.current);
44  }
45 
46  // just return the `current' tag that is to be generated from the
47  // given base tag, without incrementing the cycle counter.
48  int current_tag(int t, int s = DEF_CYCLE_SIZE)
49  {
50  TagInfo& found = create_base_tag(t, s);
51  return (found.base + found.current);
52  }
53 
54  // reset the cycle counter for the given tag to be 0. If the tag is
55  // not in the list, it is added. Returns the reset tag.
56  int reset_tag(int t, int s = DEF_CYCLE_SIZE)
57  {
58  TagInfo& found = create_base_tag(t, s);
59  found.current = 0;
60  return found.base;
61  }
62 
63 private:
64  // Simple struct holding info about the cycle size and current tag
65  // for a base tag
66  class TagInfo
67  {
68  public:
69  int base; // base tag value, the key for the map
70  int cycleSize; // range through which to cycle tag
71  int current; // current value of tag
72  TagInfo(int b, int s) : base(b), cycleSize(s), current(0) { }
73  TagInfo() : base(-1), cycleSize(-1), current(0) { }
74  };
75 
76  // class used for comparisons
77  class TagCompare
78  {
79  public:
80  bool operator()(const int& x, const int& y) const
81  {
82  return x < y;
83  }
84  };
85 
86  // the list of base tags which have been established
87  std::map<int, TagInfo, TagCompare> TagList;
88 
89  // Establish a new base tag and cycle size. Returns a reference to
90  // the new TagInfo structure.
91  // Arguments are: base tag, cycle size.
93  {
94  TagInfo& found = TagList[t];
95  if ( found.base < 0 )
96  {
97  found.base = t;
98  found.cycleSize = s;
99  }
100  return TagList[t];
101  }
102 
103 };
104 
105 #endif // TAG_MAKER_H
#define DEF_CYCLE_SIZE
Definition: TagMaker.h:24
std::map< int, TagInfo, TagCompare > TagList
Definition: TagMaker.h:87
int reset_tag(int t, int s=1000)
Definition: TagMaker.h:56
TagInfo & create_base_tag(int t, int s=1000)
Definition: TagMaker.h:92
TagMaker(void)
Definition: TagMaker.h:32
int current_tag(int t, int s=1000)
Definition: TagMaker.h:48
int next_tag(int t, int s=1000)
Definition: TagMaker.h:39
virtual ~TagMaker(void)
Definition: TagMaker.h:33
TagInfo(int b, int s)
Definition: TagMaker.h:72
bool operator()(const int &x, const int &y) const
Definition: TagMaker.h:80