source: tags/0.55/stopos_key.h @ 9

Last change on this file since 9 was 9, checked in by willem, 11 years ago

willem

File size: 1.3 KB
Line 
1#ifndef STOPOS_KEY_H
2#define STOPOS_KEY_H
3#include <string>
4#include <iostream>
5
6class stopos_key
7{
8  protected:
9    std::string value;
10    static const char firstchar = 'a';
11    static const char lastchar  = 'z';
12    static const unsigned int base = lastchar - firstchar + 1;
13    template <typename T> void tostring1(std::string &v,const T n)
14    {
15      if (n == 0)
16        return;
17      unsigned int c = n + firstchar - base*(n/base);
18      tostring1(v,n/base);
19      v.push_back(c);
20    }
21    template <typename T> void tostring(std::string &v,const T n)
22    {
23      if (n == 0)
24        v = firstchar;
25      else
26        tostring1(v,n);
27    }
28  public:
29    stopos_key();
30    stopos_key(const std::string &s);
31    void increment(void);
32    std::string str(void) const;
33    void impossible();
34    bool operator==(const stopos_key &k) const;
35    bool operator!=(const stopos_key &k) const;
36
37    // to assign an unsigned integer value to a key
38    template <typename T> void set(const T k)
39      {
40        this->value = "";
41        this->tostring(this->value,k);
42        return;
43      }
44
45    // to get the unsigned integer value from a key
46    template <typename T> T get() const
47    {
48      T r = 0;
49
50      for (unsigned int i=0; i<this->value.size(); i++)
51        r = this->base*r + this->value[i]-this->firstchar;
52
53      return r;
54    }
55};
56#endif
Note: See TracBrowser for help on using the repository browser.