模組 MoreLabels.Set

module Set: sig .. end

針對已排序型別的集合。

此模組實作了集合資料結構,並給定集合元素的總排序函式。所有集合操作皆為純粹應用式(無副作用)。實作使用平衡二元樹,因此相當有效率:例如,插入和成員資格檢查的時間複雜度與集合大小呈對數關係。

MoreLabels.Set.Make函子會針對任何型別建構實作,並給定一個 compare 函式。例如

       module IntPairs =
         struct
           type t = int * int
           let compare (x0,y0) (x1,y1) =
             match Stdlib.compare x0 x1 with
                 0 -> Stdlib.compare y0 y1
               | c -> c
         end

       module PairsSet = Set.Make(IntPairs)

       let m = PairsSet.(empty |> add (2,3) |> add (5,7) |> add (11,13))
     

這會建立一個新的模組 PairsSet,其具有新的型別 PairsSet.t,表示 int * int 的集合。

module type OrderedType = sig .. end

函子 MoreLabels.Set.Make 的輸入簽名。

module type S = sig .. end

函子 MoreLabels.Set.Make 的輸出簽名。

module Make: 
functor (Ord : OrderedType-> S with type elt = Ord.t and type t = Set.Make(Ord).t

此函子會根據一個完全排序的型別,建構集合結構的實作。