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

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 */
   63initial_size(32).                       % initial hash-table size
 empty_nb_set(-Set)
Create an empty non-backtrackable set.
   69empty_nb_set(nb_set(Buckets, 0)) :-
   70    initial_size(Size),
   71    '$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.
   86add_nb_set(Key, Set) :-
   87    add_nb_set(Key, Set, _).
   88add_nb_set(Key, Set, New) :-
   89    arg(1, Set, Buckets),
   90    compound_name_arity(Buckets, _, BCount),
   91    hash_key(Key, BCount, Hash),
   92    arg(Hash, Buckets, Bucket),
   93    (   member(X, Bucket),
   94        Key =@= X
   95    ->  New = false
   96    ;   New = true,
   97        duplicate_term(Key, Copy),
   98        nb_linkarg(Hash, Buckets, [Copy|Bucket]),
   99        arg(2, Set, Size0),
  100        Size is Size0+1,
  101        nb_setarg(2, Set, Size),
  102        (   Size > BCount
  103        ->  rehash(Set)
  104        ;   true
  105        )
  106    ).
 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.
  115:- if(catch((A = f(A), variant_hash(A,_)), _, fail)).  116hash_key(Term, BCount, Key) :-
  117    variant_hash(Term, IntHash),
  118    Key is (IntHash mod BCount)+1.
  119:- else.  120hash_key(Term, BCount, Key) :-
  121    acyclic_term(Key),
  122    !,
  123    variant_hash(Term, IntHash),
  124    Key is (IntHash mod BCount)+1.
  125hash_key(Term, BCount, Key) :-
  126    term_factorized(Term, Skeleton, Substiution),
  127    variant_hash(Skeleton+Substiution, IntHash),
  128    Key is (IntHash mod BCount)+1.
  129:- endif.  130
  131rehash(Set) :-
  132    arg(1, Set, Buckets0),
  133    compound_name_arity(Buckets0, Name, Arity0),
  134    Arity is Arity0*2,
  135    '$filled_array'(Buckets, Name, Arity, []),
  136    nb_setarg(1, Set, Buckets),
  137    nb_setarg(2, Set, 0),
  138    (  arg(_, Buckets0, Chain),
  139       member(Key, Chain),
  140       add_nb_set(Key, Set, _),
  141       fail
  142    ;  true
  143    ).
 nb_set_to_list(+Set, -List)
Get the elements of a an nb_set. List is sorted to the standard order of terms.
  150nb_set_to_list(nb_set(Buckets, _Size), OrdSet) :-
  151    compound_name_arguments(Buckets, _, Args),
  152    append(Args, List),
  153    sort(List, OrdSet).
 gen_nb_set(+Set, -Key)
Enumerate the members of a set in the standard order of terms.
  159gen_nb_set(Set, Key) :-
  160    nb_set_to_list(Set, OrdSet),
  161    member(Key, OrdSet).
 size_nb_set(+Set, -Size)
Unify Size with the number of elements in the set
  167size_nb_set(nb_set(_, Size), Size)