From jan@swi.psy.uva.nl  Sun Nov 28 14:54:44 1999
Received: from gollem.swi.psy.uva.nl (root@gollem [145.18.114.15])
	by swi.psy.uva.nl (8.9.3/8.9.3) with ESMTP id OAA23856;
	Sun, 28 Nov 1999 14:54:44 +0100 (MET)
Received: (from jan@localhost)
	by gollem.swi.psy.uva.nl (8.9.3/8.9.3) id OAA05891;
	Sun, 28 Nov 1999 14:54:43 +0100
Date: Sun, 28 Nov 1999 14:54:43 +0100
Message-Id: <199911281354.OAA05891@gollem.swi.psy.uva.nl>
From: Jan Wielemaker <jan@swi.psy.uva.nl>
Subject: Re: indexing capabilty of swi-pl
To: Pascal Vaillant <Pascal.Vaillant@rz.hu-berlin.de>, prolog@swi.psy.uva.nl
In-Reply-To: Pascal Vaillant's message of Sat, 27 Nov 1999 23:10:44 +0000 (GMT)
Phone: +31 - 20 - 525 6121

> 
> On Sat, 30 Oct 1999, Jan Wielemaker wrote:
> 
> > > * Wich of this methods do you think is faster to access a dictionary,
> > >    entry(KeyValue,....KeyInfos....)   (about 5.000 entries):
> > >           - put entries as they are in the internal DB of Prolog (with
> > >             KeyValue as first argument, relaying on SWI-PL first arg
> > >             indexing;
> > >           - put entries in a B-tree managed by Prolog.
> > > 
> > This depends on the number of clauses, but using large sets the choice
> > is always a hash-table.  Try to avoid as much as possible duplicates
> > and don't use a `catch-all' clause (using a variable) at the end.
> > 5,000 entries is just peanuts.  We've played with tables more than
> > 10 times as big without any trouble.
> > 
> 
> Can someone mail a small example program showing how to use a hash
> table in SWI Prolog? From the manual, you use hash_term(Term,Key)
> to attribute keys to entries; but I do not understand whether you
> have to do something special to retrieve them afterwards.

Hash-tables are used internally by Prolog to do first-argument indexing
on predicates having lots of clauses.  hash_term/2 is designed to make
it possible to hash on more then just the atomic object or functor of
the first argument.  The idea is that if we want to define a/3, where
argument 1 and 2 should be indexed is replaced by a/4 in this way:

assert_a3(A,B,C) :-
	hash_term(A-B, Hash),
	assert(a(Hash, A, B, C).

a3(A,B,C) :-
	hash_term(A-B, Hash),
	a(Hash, A, B, C).

See also SWI-Prologs :- index(Head). extension.

	Regards --- Jan

