TagMaker.h 3.12 KB
Newer Older
gsell's avatar
gsell committed
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
/***************************************************************************
 *
 * The IPPL Framework
 *
 ***************************************************************************/

#ifndef TAG_MAKER_H
#define TAG_MAKER_H

/*
 * TagMaker.h - creates tags from a given base tag and a cycle size.  New
 *	tags are generated each time one is requested, by adding an
 *	integer which varies from 0 ... (cycle size - 1) to the provided
 *	base tag.  Routines exist to establish a base tag and cycle size,
 *	and to get a new tag for a given base tag.
 */


// include files
#include <map>


// default cycle size, if not specified by the user
#define DEF_CYCLE_SIZE 1000


class TagMaker
{

public:
    // constructor/destructor
    TagMaker(void) { }
33
    virtual ~TagMaker(void) { }
gsell's avatar
gsell committed
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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105

    // generate a new tag given a base tag.  If the base tag has not been
    // previously established by create_base_tag, it will be done so by
    // this routine with the default cycle size.  A new tag can be established
    // at the same time by also giving a cycle size as the second argument.
    int next_tag(int t, int s = DEF_CYCLE_SIZE)
    {
        TagInfo& found = create_base_tag(t, s);
        found.current = (found.current + 1) % found.cycleSize;
        return (found.base + found.current);
    }

    // just return the `current' tag that is to be generated from the
    // given base tag, without incrementing the cycle counter.
    int current_tag(int t, int s = DEF_CYCLE_SIZE)
    {
        TagInfo& found = create_base_tag(t, s);
        return (found.base + found.current);
    }

    // reset the cycle counter for the given tag to be 0.  If the tag is
    // not in the list, it is added.  Returns the reset tag.
    int reset_tag(int t, int s = DEF_CYCLE_SIZE)
    {
        TagInfo& found = create_base_tag(t, s);
        found.current = 0;
        return found.base;
    }

private:
    // Simple struct holding info about the cycle size and current tag
    // for a base tag
    class TagInfo
    {
    public:
        int base;			// base tag value, the key for the map
        int cycleSize;		// range through which to cycle tag
        int current;		// current value of tag
        TagInfo(int b, int s) : base(b), cycleSize(s), current(0) { }
        TagInfo() : base(-1), cycleSize(-1), current(0) { }
    };

    // class used for comparisons
    class TagCompare
    {
    public:
        bool operator()(const int& x, const int& y) const
        {
            return x < y;
        }
    };

    // the list of base tags which have been established
    std::map<int, TagInfo, TagCompare> TagList;

    // Establish a new base tag and cycle size.  Returns a reference to
    // the new TagInfo structure.
    // Arguments are: base tag, cycle size.
    TagInfo& create_base_tag(int t, int s = DEF_CYCLE_SIZE)
    {
        TagInfo& found = TagList[t];
        if ( found.base < 0 )
        {
            found.base = t;
            found.cycleSize = s;
        }
        return TagList[t];
    }

};

#endif // TAG_MAKER_H
106 107 108 109 110 111 112 113

// vi: set et ts=4 sw=4 sts=4:
// Local Variables:
// mode:c
// c-basic-offset: 4
// indent-tabs-mode: nil
// require-final-newline: nil
// End: