LCOV - code coverage report
Current view: top level - src/Communicate - TagMaker.h (source / functions) Coverage Total Hit
Test: final_report.info Lines: 65.4 % 26 17
Test Date: 2025-07-09 17:06:48 Functions: 66.7 % 9 6

            Line data    Source code
       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              : 
      24              : class TagMaker {
      25              : public:
      26              :     // constructor/destructor
      27         1770 :     TagMaker(void) {}
      28         2766 :     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          336 :     int next_tag(int t, int s = DEF_CYCLE_SIZE) {
      35          336 :         TagInfo& found = create_base_tag(t, s);
      36          336 :         found.current  = (found.current + 1) % found.cycleSize;
      37          336 :         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            0 :     int preceding_tag(int t, int s = DEF_CYCLE_SIZE) {
      45            0 :         const TagInfo& found = create_base_tag(t, s);
      46            0 :         if (found.current == 0) {
      47            0 :             return (found.base + found.cycleSize - 1);
      48              :         }
      49            0 :         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            0 :     int following_tag(int t, int s = DEF_CYCLE_SIZE) {
      57            0 :         const TagInfo& found = create_base_tag(t, s);
      58            0 :         const int following  = (found.current + 1) % found.cycleSize;
      59            0 :         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              : 
      77              : private:
      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) {}
      89           36 :         TagInfo()
      90           36 :             : base(-1)
      91           36 :             , cycleSize(-1)
      92           36 :             , current(0) {}
      93              :     };
      94              : 
      95              :     // class used for comparisons
      96              :     class TagCompare {
      97              :     public:
      98         1272 :         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.
     107          336 :     TagInfo& create_base_tag(int t, int s = DEF_CYCLE_SIZE) {
     108          336 :         TagInfo& found = TagList[t];
     109          336 :         if (found.base < 0) {
     110           36 :             found.base      = t;
     111           36 :             found.cycleSize = s;
     112              :         }
     113          336 :         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:
        

Generated by: LCOV version 2.0-1