OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
TagMaker.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 /***************************************************************************
3  *
4  * The IPPL Framework
5  *
6  *
7  * Visit http://people.web.psi.ch/adelmann/ for more details
8  *
9  ***************************************************************************/
10 
11 #ifndef TAG_MAKER_H
12 #define TAG_MAKER_H
13 
14 /*
15  * TagMaker.h - creates tags from a given base tag and a cycle size. New
16  * tags are generated each time one is requested, by adding an
17  * integer which varies from 0 ... (cycle size - 1) to the provided
18  * base tag. Routines exist to establish a base tag and cycle size,
19  * and to get a new tag for a given base tag.
20  */
21 
22 
23 // include files
24 #include <map>
25 
26 
27 // default cycle size, if not specified by the user
28 #define DEF_CYCLE_SIZE 1000
29 
30 
31 class TagMaker
32 {
33 
34 public:
35  // constructor/destructor
36  TagMaker(void) { }
37  virtual ~TagMaker(void) { }
38 
39  // generate a new tag given a base tag. If the base tag has not been
40  // previously established by create_base_tag, it will be done so by
41  // this routine with the default cycle size. A new tag can be established
42  // at the same time by also giving a cycle size as the second argument.
43  int next_tag(int t, int s = DEF_CYCLE_SIZE)
44  {
45  TagInfo& found = create_base_tag(t, s);
46  found.current = (found.current + 1) % found.cycleSize;
47  return (found.base + found.current);
48  }
49 
50  // just return the `current' tag that is to be generated from the
51  // given base tag, without incrementing the cycle counter.
52  int current_tag(int t, int s = DEF_CYCLE_SIZE)
53  {
54  TagInfo& found = create_base_tag(t, s);
55  return (found.base + found.current);
56  }
57 
58  // reset the cycle counter for the given tag to be 0. If the tag is
59  // not in the list, it is added. Returns the reset tag.
60  int reset_tag(int t, int s = DEF_CYCLE_SIZE)
61  {
62  TagInfo& found = create_base_tag(t, s);
63  found.current = 0;
64  return found.base;
65  }
66 
67 private:
68  // Simple struct holding info about the cycle size and current tag
69  // for a base tag
70  class TagInfo
71  {
72  public:
73  int base; // base tag value, the key for the map
74  int cycleSize; // range through which to cycle tag
75  int current; // current value of tag
76  TagInfo(int b, int s) : base(b), cycleSize(s), current(0) { }
77  TagInfo() : base(-1), cycleSize(-1), current(0) { }
78  };
79 
80  // class used for comparisons
81  class TagCompare
82  {
83  public:
84  bool operator()(const int& x, const int& y) const
85  {
86  return x < y;
87  }
88  };
89 
90  // the list of base tags which have been established
91  std::map<int, TagInfo, TagCompare> TagList;
92 
93  // Establish a new base tag and cycle size. Returns a reference to
94  // the new TagInfo structure.
95  // Arguments are: base tag, cycle size.
97  {
98  TagInfo& found = TagList[t];
99  if ( found.base < 0 )
100  {
101  found.base = t;
102  found.cycleSize = s;
103  }
104  return TagList[t];
105  }
106 
107 };
108 
109 #endif // TAG_MAKER_H
std::map< int, TagInfo, TagCompare > TagList
Definition: TagMaker.h:91
TagInfo(int b, int s)
Definition: TagMaker.h:76
bool operator()(const int &x, const int &y) const
Definition: TagMaker.h:84
TagMaker(void)
Definition: TagMaker.h:36
int next_tag(int t, int s=1000)
Definition: TagMaker.h:43
#define DEF_CYCLE_SIZE
Definition: TagMaker.h:28
int current_tag(int t, int s=1000)
Definition: TagMaker.h:52
TagInfo & create_base_tag(int t, int s=1000)
Definition: TagMaker.h:96
virtual ~TagMaker(void)
Definition: TagMaker.h:37
int reset_tag(int t, int s=1000)
Definition: TagMaker.h:60