From 47414c7f40ad76be26d79beb9b5290218dbf3c89 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Sat, 5 Sep 2015 01:52:55 +0200 Subject: [PATCH] add mem to `CCHashTrie` --- src/data/CCHashTrie.ml | 14 ++++++++++---- src/data/CCHashTrie.mli | 2 ++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/data/CCHashTrie.ml b/src/data/CCHashTrie.ml index a568f4b9..78febfeb 100644 --- a/src/data/CCHashTrie.ml +++ b/src/data/CCHashTrie.ml @@ -38,6 +38,8 @@ module type S = sig val add : key -> 'a -> 'a t -> 'a t + val mem : key -> _ t -> bool + val get : key -> 'a t -> 'a option val get_exn : key -> 'a t -> 'a @@ -115,14 +117,14 @@ end (* from https://en.wikipedia.org/wiki/Hamming_weight - //This uses fewer arithmetic operations than any other known + //This uses fewer arithmetic operations than any other known //implementation on machines with fast multiplication. //It uses 12 arithmetic operations, one of which is a multiply. int popcount_3(uint64_t x) { x -= (x >> 1) & m1; //put count of each 2 bits into those 2 bits - x = (x & m2) + ((x >> 2) & m2); //put count of each 4 bits into those 4 bits - x = (x + (x >> 4)) & m4; //put count of each 8 bits into those 8 bits - return (x * h01)>>56; //returns left 8 bits of x + (x<<8) + (x<<16) + (x<<24) + ... + x = (x & m2) + ((x >> 2) & m2); //put count of each 4 bits into those 4 bits + x = (x + (x >> 4)) & m4; //put count of each 8 bits into those 8 bits + return (x * h01)>>56; //returns left 8 bits of x + (x<<8) + (x<<16) + (x<<24) + ... } *) @@ -317,6 +319,10 @@ module Make(Key : KEY) try Some (get_exn_ k ~h:(hash_ k) m) with Not_found -> None + let mem k m = + try ignore (get_exn_ k ~h:(hash_ k) m); true + with Not_found -> false + (* TODO: use Hash.combine if array only has one non-empty LEAF element? *) (* [left] list nodes already visited *) diff --git a/src/data/CCHashTrie.mli b/src/data/CCHashTrie.mli index 865e393a..b63f1ba5 100644 --- a/src/data/CCHashTrie.mli +++ b/src/data/CCHashTrie.mli @@ -47,6 +47,8 @@ module type S = sig val add : key -> 'a -> 'a t -> 'a t + val mem : key -> _ t -> bool + val get : key -> 'a t -> 'a option val get_exn : key -> 'a t -> 'a