Skip to content

Commit

Permalink
[FSharp] Rework LookupTable utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
hyazinthh committed Aug 19, 2024
1 parent 0ac497a commit a6cb156
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 21 deletions.
54 changes: 37 additions & 17 deletions src/Aardvark.Base.FSharp/Utilities/Interop/FSLibExtensions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -367,35 +367,55 @@ module NiceUtilities =
module LookupTable =
open System.Collections.Generic

let lookupTable (l : list<'a * 'b>) =
let private build (entries: #seq<'Key * 'Value>) =
let d = Dictionary()
for (k,v) in l do

for k, v in entries do
match d.TryGetValue k with
| (true, vo) -> failwithf "duplicated lookup-entry: %A (%A vs %A)" k vo v
| _ -> ()
| (true, vo) -> raise <| ArgumentException($"Duplicate lookup table entry {k}: {v} and {vo}.")
| _ -> ()

d.[k] <- v

fun (key : 'a) ->
d

/// Builds a lookup table from the given entries and returns a function that retrieves the value associated with a key.
/// Fails if a key occurs more than once in the input collection.
let lookup (entries: #seq<'Key * 'Value>) =
let d = build entries

fun (key : 'Key) ->
match d.TryGetValue key with
| (true, v) -> v
| _ -> failwithf "unsupported %A: %A" typeof<'a> key
| (true, v) -> v
| _ -> raise <| KeyNotFoundException($"Key {key} of type {typeof<'Key>} not found in lookup table.")

/// Builds a lookup table from the given entries and returns a function that retrieves the value associated with a key if it exists.
/// Fails if a key occurs more than once in the input collection.
let tryLookup (entries: #seq<'Key * 'Value>) =
let d = build entries

let lookupTable' (l : list<'a * 'b>) =
let d = Dictionary()
for (k,v) in l do
match d.TryGetValue k with
| (true, vo) -> failwithf "duplicated lookup-entry: %A (%A vs %A)" k vo v
| _ -> ()
fun (key : 'Key) ->
match d.TryGetValue key with
| (true, v) -> Some v
| _ -> None

d.[k] <- v
/// Builds a lookup table from the given entries and returns a function that retrieves the value associated with a key if it exists.
/// Fails if a key occurs more than once in the input collection.
let tryLookupV (entries: #seq<'Key * 'Value>) =
let d = build entries

fun (key : 'a) ->
fun (key : 'Key) ->
match d.TryGetValue key with
| (true, v) -> Some v
| _ -> None
| (true, v) -> ValueSome v
| _ -> ValueNone

[<Obsolete("Use LookupTable.lookup instead.")>]
let lookupTable (l : list<'a * 'b>) =
lookup l

[<Obsolete("Use LookupTable.tryLookup instead.")>]
let lookupTable' (l : list<'a * 'b>) =
tryLookup l

[<AutoOpen>]
module EnumExtensions =
Expand Down
4 changes: 2 additions & 2 deletions src/Aardvark.Base.Tensors/PixImage/ImageTrafo.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module ImageTrafo =
let private composeTable =
LookupTable.lookupTable [
LookupTable.lookup [
struct (ImageTrafo.Identity, ImageTrafo.Identity), ImageTrafo.Identity
struct (ImageTrafo.Identity, ImageTrafo.Rot90), ImageTrafo.Rot90
struct (ImageTrafo.Identity, ImageTrafo.Rot180), ImageTrafo.Rot180
Expand Down Expand Up @@ -74,7 +74,7 @@ module ImageTrafo =
composeTable (struct (l, r))

let inverse =
LookupTable.lookupTable [
LookupTable.lookup [
ImageTrafo.MirrorX, ImageTrafo.MirrorX
ImageTrafo.MirrorY, ImageTrafo.MirrorY
ImageTrafo.Identity, ImageTrafo.Identity
Expand Down
2 changes: 1 addition & 1 deletion src/Aardvark.Base.Tensors/PixImage/PixImageErrorMetric.fs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module FSharpPixImageErrorMetricExtensions =
module ErrorMetricHelpers =

let maxValues =
LookupTable.lookupTable [
LookupTable.lookup [
typeof<uint8>, 255.0
typeof<int8>, 255.0
typeof<uint16>, 65535.0
Expand Down
2 changes: 1 addition & 1 deletion src/Tests/Aardvark.Base.FSharp.Tests/Tensors/PixTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ module PixTests =

let computePSNR =
let table : Type -> (PixImage -> PixImage -> float) =
LookupTable.lookupTable [
LookupTable.lookup [
typeof<uint8>, fun src dst -> PixImage.peakSignalToNoiseRatio (src.AsPixImage<uint8>()) (dst.AsPixImage<uint8>())
typeof<uint16>, fun src dst -> PixImage.peakSignalToNoiseRatio (src.AsPixImage<uint16>()) (dst.AsPixImage<uint16>())
typeof<uint32>, fun src dst -> PixImage.peakSignalToNoiseRatio (src.AsPixImage<uint32>()) (dst.AsPixImage<uint32>())
Expand Down

0 comments on commit a6cb156

Please sign in to comment.