Skip to content

Commit

Permalink
Move MaxBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
forki committed Jun 25, 2019
1 parent 68bc5fb commit d2bc93e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 36 deletions.
33 changes: 33 additions & 0 deletions src/fsharp/ErrorLogger.fs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,39 @@ let rec findOriginalException err =
| WrappedError(err, _) -> findOriginalException err
| _ -> err

type MaxBuffer<'K,'V when 'K : comparison and 'V : equality>(maxElements:int) =
let data = Array.zeroCreate<System.Collections.Generic.KeyValuePair<'K,'V>>(maxElements)
let mutable elements = 0
let rec backup i j =
if i < j then
data.[i] <- data.[i + 1]
backup (i + 1) j

let isSmaller i k =
if i >= elements then false else
let kvpr : byref<_> = &data.[i]
kvpr.Key < k

member __.Insert (k,v) =
let mutable pos = 0
while pos < maxElements && isSmaller pos k do
pos <- pos + 1

if pos < maxElements then
for i = maxElements-1 downto (pos+1) do
data.[i] <- data.[i-1]
data.[pos] <- System.Collections.Generic.KeyValuePair(k,v)

elements <- elements + 1

member __.IsEmpty = elements = 0
member __.Values() : 'V [] =
[| let hashSet = System.Collections.Generic.HashSet<'V>()
let bound = min (maxElements-1) (elements-1)
for i in 0..bound do
let x = data.[i].Value
if hashSet.Add x then yield x |]

type Suggestions = unit -> seq<string>

let NoSuggestions : Suggestions = fun () -> Seq.empty
Expand Down
39 changes: 3 additions & 36 deletions src/fsharp/ErrorResolutionHints.fs
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,6 @@ let minThresholdForSuggestions = 0.7
let highConfidenceThreshold = 0.85
let minStringLengthForThreshold = 3

type MaxBuffer<'K,'V when 'K : comparison and 'V : equality>(maxElements:int) =
let data = Array.zeroCreate<System.Collections.Generic.KeyValuePair<'K,'V>>(maxElements)
let mutable elements = 0
let rec backup i j =
if i < j then
data.[i] <- data.[i + 1]
backup (i + 1) j

let isSmaller i k =
if i >= elements then false else
let kvpr : byref<_> = &data.[i]
kvpr.Key < k

member __.Insert (k,v) =
let mutable pos = 0
while pos < maxElements && isSmaller pos k do
pos <- pos + 1

if pos < maxElements then
for i = maxElements-1 downto (pos+1) do
data.[i] <- data.[i-1]
data.[pos] <- System.Collections.Generic.KeyValuePair(k,v)

elements <- elements + 1

member __.IsEmpty = elements = 0
member __.Values() : 'V [] =
[| let hashSet = System.Collections.Generic.HashSet<'V>()
let bound = min (maxElements-1) (elements-1)
for i in 0..bound do
let x = data.[i].Value
if hashSet.Add x then yield x |]

/// We report a candidate if its edit distance is <= the threshold.
/// The threshold is set to about a quarter of the number of characters.
let IsInEditDistanceProximity idText suggestion =
Expand Down Expand Up @@ -77,7 +44,7 @@ let FilterPredictions (suggestionF:ErrorLogger.Suggestions) (idText:string) =

let dotIdText = "." + idText
let mutable parsingError = false
let buffer = MaxBuffer(maxSuggestions)
let buffer = ErrorLogger.MaxBuffer(maxSuggestions)

use e = allSuggestions.GetEnumerator()
while not parsingError && e.MoveNext() do
Expand All @@ -102,10 +69,10 @@ let FilterPredictions (suggestionF:ErrorLogger.Suggestions) (idText:string) =
elif IsInEditDistanceProximity uppercaseText suggestedText then
buffer.Insert(similarity, suggestion) |> ignore

if parsingError then MaxBuffer(maxSuggestions) else buffer
if parsingError then ErrorLogger.MaxBuffer(maxSuggestions) else buffer

/// Formats the given predictions according to the error style.
let FormatPredictions normalizeF (predictions: MaxBuffer<_,string>) =
let FormatPredictions normalizeF (predictions: ErrorLogger.MaxBuffer<_,string>) =
if predictions.IsEmpty then
System.String.Empty
else
Expand Down

0 comments on commit d2bc93e

Please sign in to comment.