summaryrefslogtreecommitdiff
path: root/common/tools.hh
blob: af8e5e910d9d5dd132571d95c13f2d39a79806e6 (plain)
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
33
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
#ifndef __COMMON_TOOLS_HH__
#define __COMMON_TOOLS_HH__

#include "lout/object.hh"
#include "lout/container.hh"

namespace rtfl {

namespace tools {

const char *numSuffix (int n);
void numToRoman (int num, char *buf, int buflen);
void syserr (const char *fmt, ...);

class EquivalenceRelation: public lout::object::Object {
private:
   class RefTarget: public lout::object::Object {
   private:
      bool ownerOfObject;
      int refCount;
      lout::object::Object *object;
      lout::container::untyped::HashSet *allKeys;

   public:
      RefTarget (lout::object::Object *object, bool ownerOfObject);
      ~RefTarget ();

      inline lout::object::Object *getObject () { return object; }
      inline void ref () { refCount++; }
      inline void unref () { if (--refCount == 0) delete this; }

      inline lout::container::untyped::HashSet *getAllKeys ()
      { return allKeys; }
      inline void putKey (Object *key) { allKeys->put (key); }
      inline void removeKey (Object *key) { allKeys->remove (key); }
   };


   class RefSource: public lout::object::Object {
      RefTarget *target;
      lout::object::Object *key;

      void refTarget ();
      void unrefTarget ();
      
   public:
      RefSource (lout::object::Object *key, RefTarget *target);
      ~RefSource ();

      inline RefTarget *getTarget () { return target; }
      void setTarget (RefTarget *target);
   };
   
   bool ownerOfKeys, ownerOfValues;
   lout::container::untyped::HashTable *sources;

   lout::container::untyped::HashSet *initSet (lout::object::Object *o);

 public:
   EquivalenceRelation (bool ownerOfKeys, bool ownerOfValues);
   ~EquivalenceRelation ();
  
   void put (lout::object::Object *key, lout::object::Object *value);
   lout::object::Object *get (lout::object::Object *key) const;
   bool contains (lout::object::Object *key) const;
   lout::container::untyped::Iterator iterator ();
   lout::container::untyped::Iterator relatedIterator (Object *key);

   void relate (lout::object::Object *key1, lout::object::Object *key2);
   void putRelated (lout::object::Object *oldKey, lout::object::Object *newKey);
   
   void removeSimple (lout::object::Object *key);
   void remove (lout::object::Object *key);
};

} // namespace tools

} // namespace rtfl

#endif // __COMMON_TOOLS_HH__