1 #include <SWI-Prolog.h>
7 lookup_ht(HT,Key,Values) :-
9 HT = ht(Capacity,_,Table),
10 Index is (Hash mod Capacity) + 1,
11 arg(Index,Table,Bucket),
17 lookup(Bucket,Key,Values)
20 lookup([K - V | KVs],Key,Value) :-
28 pl_lookup_ht1(term_t ht
, term_t pl_hash
, term_t key
, term_t values
)
34 term_t pl_capacity
= PL_new_term_ref();
35 term_t table
= PL_new_term_ref();
36 term_t bucket
= PL_new_term_ref();
38 /* HT = ht(Capacity,_,Table) */
39 PL_get_arg(1, ht
, pl_capacity
);
40 PL_get_integer(pl_capacity
, &capacity
);
41 PL_get_arg(3, ht
, table
);
43 /* Index is (Hash mod Capacity) + 1 */
44 PL_get_integer(pl_hash
, &hash
);
45 index
= (hash
% capacity
) + 1;
47 /* arg(Index,Table,Bucket) */
48 PL_get_arg(index
, table
, bucket
);
51 if (PL_is_variable(bucket
)) PL_fail
;
53 if (PL_is_list(bucket
)) {
54 term_t pair
= PL_new_term_ref();
55 term_t k
= PL_new_term_ref();
56 term_t vs
= PL_new_term_ref();
57 while (PL_get_list(bucket
, pair
,bucket
)) {
58 PL_get_arg(1, pair
, k
);
59 if ( PL_compare(k
,key
) == 0 ) {
61 PL_get_arg(2, pair
, vs
);
62 return PL_unify(values
,vs
);
67 term_t k
= PL_new_term_ref();
68 term_t vs
= PL_new_term_ref();
69 PL_get_arg(1, bucket
, k
);
71 if ( PL_compare(k
,key
) == 0 ) {
73 PL_get_arg(2, bucket
, vs
);
74 return PL_unify(values
,vs
);
82 pl_memberchk_eq(term_t element
, term_t maybe_list
)
85 term_t head
= PL_new_term_ref(); /* variable for the elements */
86 term_t list
= PL_copy_term_ref(maybe_list
); /* copy as we need to write */
88 while( PL_get_list(list
, head
, list
) )
89 { if ( PL_compare(element
,head
) == 0 )
100 install_chr_support()
102 PL_register_foreign("memberchk_eq",2, pl_memberchk_eq
, 0);
103 PL_register_foreign("lookup_ht1",4, pl_lookup_ht1
, 0);