1 //////////////////////////////////////////////////////////////////////////////
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
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
18 // This software is still under development and we welcome(read crave for)
19 // any suggestions and help from the users.
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 //////////////////////////////////////////////////////////////////////////////
38 AlphaMem(const AlphaMem
&); // no copy constructor
39 void operator = (const AlphaMem
&); // no assignment
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.
49 ///////////////////////////////////////////////////////////////////////////
50 // Constructor and destructor
51 ///////////////////////////////////////////////////////////////////////////
52 AlphaMem() : count(0), capacity(0), facts(0) {}
55 ///////////////////////////////////////////////////////////////////////////
57 ///////////////////////////////////////////////////////////////////////////
58 // inline operator Fact ** () const { return facts; }
59 inline Fact
** get_facts () { return facts
; }
61 ///////////////////////////////////////////////////////////////////////////
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
;
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
) {
79 int new_capacity
= 4 + capacity
* 3 / 2;
81 (Fact
**)mem
.m_alloc(sizeof(Fact
*) * new_capacity
);
82 for (int i
= capacity
- 1; i
>= 0; i
--) new_facts
[i
] = facts
[i
];
84 new_facts
[count
++] = f
;
85 capacity
= new_capacity
;
90 ///////////////////////////////////////////////////////////////////////////
91 // Method to remove an old fact from the alpha memory.
92 ///////////////////////////////////////////////////////////////////////////
93 inline void remove_fact(int i
) { facts
[--count
] = facts
[i
]; }