?he 'FDATABASE''Page %'
?fo 'Aaron Sloman'- % -'March 3rd 1977'
.mh
A FASTER VERSION OF THE DATABASE
.br
================================
.hm
A faster version of the database package is available. Instead
of storing all patterns in a single list, it stores them in
separate lists, called sub-contexts. All patterns starting with
the same element are stored in the same sub-context. Moreover,
since all the items in the same context have the same first
element, the first element is stored only once, thereby
saving space.
.sp2
So a sub-context is a list whose HD is the first element
common to all the patterns represented in that sub-context, and
whose TL is a list containing the TL's of all the patterns.
Since the complete patterns are not stored, this saves space. It
also means that during a pattern-directed search of
the database only the TL of the search pattern is matched, after
the appropriate sub-context has been found. So less time is spent
on matching. The HD of the search pattern is used to find the
sub-context.
.sp2
The whole database is  represented as a single list, held in the
variable
CONTEXT.
Each element of this list is a sub-context, of the sort described
above. So searching for an item in the database matching a given
pattern is done in two stages. First, search down
CONTEXT
to find the appropriate sub-context (i.e. the one whose HD is the
same as the HD of the given pattern). Then search the TL of
the sub context (if one is found), to find an item matching the
TL of the given pattern.
.sp2
Normally the list of sub-contexts, and the relevant subcontext will
have lengths which add up to much less than what the length of
CONTEXT
would have been if all patterns were simply stored in a single list.
So searching should on average take less time, quite apart from the
saving made by not having to match initial elements of patterns.
.sp2
The disadvantage is that you can no longer use patterns starting with
"?" or "??" as arguments for 
PRESENT,
or with
FOREACH.
However, this should not be a serious restriction.
.sp2
Note that the first element of a pattern may be a list
(since the comparison of HD's uses
EQUAL),
but that list should not contain "?" or "??" variables.
.bp
.ce2
Using the fast database
.br
-----------------------
.sp
To use the database first type:
 	: LIB "FDATABASE";
.br
After that proceed exactly as in the
DATABASE
demo. The functions
SHOWDATABASE
and
STOREDATABASE
have been altered to take account of the new structure of
CONTEXT.
So what is printed out by
 	: SHOWDATABASE();
.br
and by
 	: CONTEXT =>
.br
will look very different.
.sp2
To get a feel for how it works, start with an empty database, then
ADD
several items with a common first element, e.g.
 	[BLOCK B1] [BLOCK B2] [BLOCK B3] 
.br
or
 	[COLOUR B1 RED] [COLOUR B2 GREEN] [COLOUR B3 BLUE] 
.br
then do
 	: CONTEXT =>
.br
to see how the items are stored.
.sp2
If you are building up a large database and want to achieve maximum
efficiency, you can use the function
REAL
in place of
PRESENT.
.sp2
This is because
PRESENT
has been designed to be more general. In particular, it works with
"ifneeded" methods which enable you to simulate database
entries, whereas
REAL
looks only for actual items. If you are not using "ifneeded" methods,
you should not notice any difference, except that
REAL
is faster.
