4 people like it.

Hashtable

Readonly generic Hashtable class implementation example.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
type Hashtable<'TKey,'TValue> when 'TKey : equality 
        (capacity:int, items:('TKey * 'TValue) seq)  =
    let table = Array.create capacity (ResizeArray<_>())
    let indexOf key = (hash key)%capacity
    do  for key, value in items do table.[indexOf key].Add(key,value)
    new (items) = Hashtable(Seq.length items, items)
    member this.TryGetValue(key:'TKey) =       
        table.[indexOf key] 
        |> Seq.tryFind (fst >> (=) key) 
        |> Option.map snd
Multiple items
type Hashtable<'TKey,'TValue (requires equality)> =
  new : items:seq<'TKey * 'TValue> -> Hashtable<'TKey,'TValue>
  new : capacity:int * items:seq<'TKey * 'TValue> -> Hashtable<'TKey,'TValue>
  member TryGetValue : key:'TKey -> 'TValue option

Full name: Script.Hashtable<_,_>

--------------------
new : items:seq<'TKey * 'TValue> -> Hashtable<'TKey,'TValue>
new : capacity:int * items:seq<'TKey * 'TValue> -> Hashtable<'TKey,'TValue>
val capacity : int
Multiple items
val int : value:'T -> int (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.int

--------------------
type int = int32

Full name: Microsoft.FSharp.Core.int

--------------------
type int<'Measure> = int

Full name: Microsoft.FSharp.Core.int<_>
val items : seq<'TKey * 'TValue> (requires equality)
Multiple items
val seq : sequence:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Core.Operators.seq

--------------------
type seq<'T> = System.Collections.Generic.IEnumerable<'T>

Full name: Microsoft.FSharp.Collections.seq<_>
val table : System.Collections.Generic.List<'TKey * 'TValue> [] (requires equality)
module Array

from Microsoft.FSharp.Collections
val create : count:int -> value:'T -> 'T []

Full name: Microsoft.FSharp.Collections.Array.create
type ResizeArray<'T> = System.Collections.Generic.List<'T>

Full name: Microsoft.FSharp.Collections.ResizeArray<_>
val indexOf : ('c -> int) (requires equality)
val key : 'c (requires equality)
val hash : obj:'T -> int (requires equality)

Full name: Microsoft.FSharp.Core.Operators.hash
val key : 'TKey (requires equality)
val value : 'TValue
module Seq

from Microsoft.FSharp.Collections
val length : source:seq<'T> -> int

Full name: Microsoft.FSharp.Collections.Seq.length
val this : Hashtable<'TKey,'TValue> (requires equality)
member Hashtable.TryGetValue : key:'TKey -> 'TValue option

Full name: Script.Hashtable`2.TryGetValue
val tryFind : predicate:('T -> bool) -> source:seq<'T> -> 'T option

Full name: Microsoft.FSharp.Collections.Seq.tryFind
val fst : tuple:('T1 * 'T2) -> 'T1

Full name: Microsoft.FSharp.Core.Operators.fst
module Option

from Microsoft.FSharp.Core
val map : mapping:('T -> 'U) -> option:'T option -> 'U option

Full name: Microsoft.FSharp.Core.Option.map
val snd : tuple:('T1 * 'T2) -> 'T2

Full name: Microsoft.FSharp.Core.Operators.snd
Raw view New version

More information

Link:http://fssnip.net/e0
Posted:5 years ago
Author:Phillip Trelford
Tags: data structures