This is documentation for version v0.10.0 of patricia-tree,
but the latest version is
v0.11.0.
Click here
to redirect to the latest version.
Patricia Tree API - HashconsedNode
Gives a unique number to each node like NodeWithId, but also performs hash-consing. So two maps with the same bindings will always be physically equal. See Hash-consed maps and sets for more details on this.
This is a generative functor, as calling it creates a new hash-table to store the created nodes, and a reference to store the next unallocated identifier. Maps/sets from different hash-consing functors (even if these functors have the same arguments) will have different (incompatible) numbering systems and be stored in different hash-tables (thus they will never be physically equal).
Using a single HashconsedNode in multiple MakeCustomMap functors will result in all those maps being hash-consed together (stored in the same hash-table, same numbering system).
Parameters
module Key : HETEROGENEOUS_KEYmodule Value : HETEROGENEOUS_HASHED_VALUESignature
include NODE
with type 'a key = 'a Key.t
with type ('key, 'map) value = ('key, 'map) Value.t
Types
type 'a key = 'a Key.tThe type of keys.
type ('key, 'map) value = ('key, 'map) Value.tThe type of value, which depends on the type of the key and the type of the map.
Constructors: build values
val empty : 'map tThe empty map
A singleton leaf, similar to BASE_MAP.singleton
A branch node. This shouldn't be called externally unless you know what you're doing! Doing so could easily break the data structure's invariants.
When called, it assumes that:
- Neither
tree0nortree1should be empty. branching_bitshould have a single bit setprefixshould be normalized (bits belowbranching_bitset to zero)- All elements of
tree0should have theirto_intstart byprefixfollowed by 0 at positionbranching_bit). - All elements of
tree1should have theirto_intstart byprefixfollowed by 0 at positionbranching_bit).
Destructors: access the value
type 'map view = private | Empty : 'map view(*Can happen only at the toplevel: there is no empty interior node.
*)| Branch : {} -> 'map view(*Same constraints as
branch:branching_bitcontains only one bit set; the corresponding mask is (branching_bit - 1).prefixis normalized: the bits below thebranching_bitare set to zero (i.e.prefix & (branching_bit - 1) = 0).- All elements of
tree0should have theirto_intstart byprefixfollowed by 0 at positionbranching_bit). - All elements of
tree1should have theirto_intstart byprefixfollowed by 0 at positionbranching_bit).
| Leaf : {} -> 'map view(*A key -> value mapping.
*)
This makes the map nodes accessible to the pattern matching algorithm; this corresponds 1:1 to the SimpleNode implementation. This just needs to be copy-and-pasted for every node type.
val is_empty : 'map t -> boolCheck if the map is empty. Should be constant time.
val to_int : 'a t -> intReturns a unique number for each map, the hash-consed identifier of the map. Unlike NODE_WITH_ID.to_int, hash-consing ensures that maps which contain the same keys (compared by KEY.to_int) and values (compared by HASHED_VALUE.polyeq) will always be physically equal and have the same identifier.
Maps with the same identifier are also physically equal: to_int m1 = to_int m2 implies m1 == m2.
Note that when using physical equality as HASHED_VALUE.polyeq, some maps of different types a t and b t may be given the same identifier. See the end of the documentation of HASHED_VALUE.polyeq for details.
Constant time equality using the hash-consed nodes identifiers. This is equivalent to physical equality. Two nodes are equal if their trees contain the same bindings, where keys are compared by KEY.to_int and values are compared by HASHED_VALUE.polyeq.
Constant time comparison using the hash-consed node identifiers. This order is fully arbitrary, but it is total and can be used to sort nodes. It is based on node ids which depend on the order in which the nodes where created (older nodes having smaller ids).
One useful property of this order is that child nodes will always have a smaller identifier than their parents.