source: tags/0.5/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    bool operator==(const stopos_key &k) const;
34    bool operator!=(const stopos_key &k) const;
35
36    // to assign an unsigned integer value to a key
37    template <typename T> void set(const T k)
38      {
39        this->value = "";
40        this->tostring(this->value,k);
41        return;
42      }
43
44    // to get the unsigned integer value from a key
45    template <typename T> T get() const
46    {
47      T r = 0;
48
49      for (unsigned int i=0; i<this->value.size(); i++)
50        r = this->base*r + this->value[i]-this->firstchar;
51
52      return r;
53    }
54};
55#endif
Note: See TracBrowser for help on using the repository browser.