View source with raw comments or as raw
    1/*  Part of SWI-Prolog
    2
    3    Author:        Jan Wielemaker
    4    E-mail:        J.Wielemaker@vu.nl
    5    WWW:           http://www.swi-prolog.org
    6    Copyright (c)  2005-2015, VU University Amsterdam
    7    All rights reserved.
    8
    9    Redistribution and use in source and binary forms, with or without
   10    modification, are permitted provided that the following conditions
   11    are met:
   12
   13    1. Redistributions of source code must retain the above copyright
   14       notice, this list of conditions and the following disclaimer.
   15
   16    2. Redistributions in binary form must reproduce the above copyright
   17       notice, this list of conditions and the following disclaimer in
   18       the documentation and/or other materials provided with the
   19       distribution.
   20
   21    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   22    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   23    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   24    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   25    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   26    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   27    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   28    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   29    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   30    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   31    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   32    POSSIBILITY OF SUCH DAMAGE.
   33*/
   34
   35:- module(nb_set,
   36          [ empty_nb_set/1,             % -EmptySet
   37            add_nb_set/2,               % +Key, !Set
   38            add_nb_set/3,               % +Key, !Set, ?New
   39            gen_nb_set/2,               % +Set, -Key
   40            size_nb_set/2,              % +Set, -Size
   41            nb_set_to_list/2            % +Set, -List
   42          ]).   43:- use_module(library(lists)).   44:- use_module(library(terms)).   45:- use_module(library(apply_macros), []).

Non-backtrackable sets

This library provides a non-backtrackabe set of terms that are variants of each other. It is primarily intended to implement distinct/1 from library(solution_sequences). The set is implemented as a hash table that is built using non-backtrackable primitives, notably nb_setarg/3.

The original version of this library used binary trees which provides immediate ordering. As the trees were not balanced, performance could get really poor. The complexity of balancing trees using non-backtrackable primitives is too high.

author
- Jan Wielemaker */
   62initial_size(32).                       % initial hash-table size
 empty_nb_set(-Set)
Create an empty non-backtrackable set.
   68empty_nb_set(nb_set(Buckets, 0)) :-
   69    initial_size(Size),
   70    '$filled_array'(Buckets, buckets, Size, []).
 add_nb_set(+Key, !Set) is det
 add_nb_set(+Key, !Set, ?New) is semidet
add_nb_set(+Key, !Set, ?New) is semidet
Insert Key into the set. If a variant (see =@=/2) of Key is already in the set, the set is unchanged and New is unified with false. Otherwise, New is unified with true and a copy of Key is added to the set.
To be done
- Computing the hash for cyclic terms is performed with the help of term_factorized/3, which performs rather poorly.
   85add_nb_set(Key, Set) :-
   86    add_nb_set(Key, Set, _).
   87add_nb_set(Key, Set, New) :-
   88    arg(1, Set, Buckets),
   89    compound_name_arity(Buckets, _, BCount),
   90    hash_key(Key, BCount, Hash),
   91    arg(Hash, Buckets, Bucket),
   92    (   member(X, Bucket),
   93        Key =@= X
   94    ->  New = false
   95    ;   New = true,
   96        duplicate_term(Key, Copy),
   97        nb_linkarg(Hash, Buckets, [Copy|Bucket]),
   98        arg(2, Set, Size0),
   99        Size is Size0+1,
  100        nb_setarg(2, Set, Size),
  101        (   Size > BCount
  102        ->  rehash(Set)
  103        ;   true
  104        )
  105    ).
 hash_key(+Term, +BucketCount, -Key) is det
Compute a hash for Term. Note that variant_hash/2 currently does not handle cyclic terms, so use term_factorized/3 to get rid of the cycles. This means that this library is rather slow when cyclic terms are involved.
  114:- if(catch((A = f(A), variant_hash(A,_)), error(type_error(_,_),_), fail)).  115hash_key(Term, BCount, Key) :-
  116    variant_hash(Term, IntHash),
  117    Key is (IntHash mod BCount)+1.
  118:- else.  119hash_key(Term, BCount, Key) :-
  120    acyclic_term(Key),
  121    !,
  122    variant_hash(Term, IntHash),
  123    Key is (IntHash mod BCount)+1.
  124hash_key(Term, BCount, Key) :-
  125    term_factorized(Term, Skeleton, Substiution),
  126    variant_hash(Skeleton+Substiution, IntHash),
  127    Key is (IntHash mod BCount)+1.
  128:- endif.  129
  130rehash(Set) :-
  131    arg(1, Set, Buckets0),
  132    compound_name_arity(Buckets0, Name, Arity0),
  133    Arity is Arity0*2,
  134    '$filled_array'(Buckets, Name, Arity, []),
  135    nb_setarg(1, Set, Buckets),
  136    nb_setarg(2, Set, 0),
  137    forall(( arg(_, Buckets0, Chain),
  138             member(Key, Chain)
  139           ),
  140           add_nb_set(Key, Set, _)).
 nb_set_to_list(+Set, -List)
Get the elements of a an nb_set. List is sorted to the standard order of terms.
  147nb_set_to_list(nb_set(Buckets, _Size), OrdSet) :-
  148    compound_name_arguments(Buckets, _, Args),
  149    append(Args, List),
  150    sort(List, OrdSet).
 gen_nb_set(+Set, -Key)
Enumerate the members of a set in the standard order of terms.
  156gen_nb_set(Set, Key) :-
  157    nb_set_to_list(Set, OrdSet),
  158    member(Key, OrdSet).
 size_nb_set(+Set, -Size)
Unify Size with the number of elements in the set
  164size_nb_set(nb_set(_, Size), Size)