initial
[prop.git] / include / AD / rete / alphamem.h
blob218483f1411fa468623c54b101f283e10acee140
1 //////////////////////////////////////////////////////////////////////////////
2 // NOTICE:
3 //
4 // ADLib, Prop and their related set of tools and documentation are in the
5 // public domain. The author(s) of this software reserve no copyrights on
6 // the source code and any code generated using the tools. You are encouraged
7 // to use ADLib and Prop to develop software, in both academic and commercial
8 // settings, and are welcomed to incorporate any part of ADLib and Prop into
9 // your programs.
11 // Although you are under no obligation to do so, we strongly recommend that
12 // you give away all software developed using our tools.
14 // We also ask that credit be given to us when ADLib and/or Prop are used in
15 // your programs, and that this notice be preserved intact in all the source
16 // code.
18 // This software is still under development and we welcome(read crave for)
19 // any suggestions and help from the users.
21 // Allen Leung
22 // 1994
23 //////////////////////////////////////////////////////////////////////////////
25 #ifndef alpha_memory_h
26 #define alpha_memory_h
28 #include <AD/rete/fact.h> // database fact
29 #include <AD/memory/mem.h> // memory manager
31 //////////////////////////////////////////////////////////////////////////////
33 // Alpha memory is used to cache facts that pass intra-object tests.
35 //////////////////////////////////////////////////////////////////////////////
36 class AlphaMem {
38 AlphaMem(const AlphaMem&); // no copy constructor
39 void operator = (const AlphaMem&); // no assignment
41 protected:
43 int count; // number of facts in the list.
44 int capacity; // number of facts that we can hold.
45 Fact ** facts; // list of facts.
47 public:
49 ///////////////////////////////////////////////////////////////////////////
50 // Constructor and destructor
51 ///////////////////////////////////////////////////////////////////////////
52 AlphaMem() : count(0), capacity(0), facts(0) {}
53 ~AlphaMem() {}
55 ///////////////////////////////////////////////////////////////////////////
56 // Conversion.
57 ///////////////////////////////////////////////////////////////////////////
58 // inline operator Fact ** () const { return facts; }
59 inline Fact ** get_facts () { return facts; }
61 ///////////////////////////////////////////////////////////////////////////
62 // Selectors
63 ///////////////////////////////////////////////////////////////////////////
64 inline int size() const { return count; }
65 inline Fact * operator [] (int i) const { return facts[i]; }
66 inline int find(const Fact * f) const
67 { for (int i = capacity - 1; i >= 0; i--)
68 if (f == facts[i]) return i;
69 return -1;
72 ///////////////////////////////////////////////////////////////////////////
73 // Method to insert a new fact into the alpha memory.
74 ///////////////////////////////////////////////////////////////////////////
75 inline void add_fact(Mem& mem, Fact * f)
76 { if (count < capacity) {
77 facts[count++] = f;
78 } else {
79 int new_capacity = 4 + capacity * 3 / 2;
80 Fact ** new_facts =
81 (Fact**)mem.m_alloc(sizeof(Fact*) * new_capacity);
82 for (int i = capacity - 1; i >= 0; i--) new_facts[i] = facts[i];
83 mem.free(facts);
84 new_facts[count++] = f;
85 capacity = new_capacity;
86 facts = new_facts;
90 ///////////////////////////////////////////////////////////////////////////
91 // Method to remove an old fact from the alpha memory.
92 ///////////////////////////////////////////////////////////////////////////
93 inline void remove_fact(int i) { facts[--count] = facts[i]; }
96 #endif