Skip to content

Commit

Permalink
Refactor property completion rotation to use infinite sequence.
Browse files Browse the repository at this point in the history
  • Loading branch information
krymtkts committed Jan 25, 2025
1 parent e4db86d commit 0db79d4
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 32 deletions.
10 changes: 1 addition & 9 deletions src/pocof.Benchmark/Benchmarks.fs
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,7 @@ type HandleBenchmarks() =
{ state with
InternalState.QueryState.Query = ":Name"
InternalState.QueryState.Cursor = 5
PropertySearch =
PropertySearch.Rotate(
"Na",
0,
seq {
"Name"
"Names"
}
) }
PropertySearch = PropertySearch.Rotate("Na", 0, Seq.cycle [ "Name"; "Names" ]) }

let context, _ = state |> Query.prepare

Expand Down
42 changes: 25 additions & 17 deletions src/pocof.Test/Handle.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1867,7 +1867,11 @@ module invokeAction =
| PropertySearch.Rotate(a, b, c) ->
a |> shouldEqual ""
b |> shouldEqual 0
c |> List.ofSeq |> shouldEqual [ "first"; "second"; "third" ]

c
|> Seq.take 4
|> List.ofSeq
|> shouldEqual [ "first"; "second"; "third"; "first" ]
| _ -> failwith "PropertySearch should be Rotate"

a2.Queries |> testQueryEnd
Expand All @@ -1891,7 +1895,7 @@ module invokeAction =
| PropertySearch.Rotate(a, b, c) ->
a |> shouldEqual "p"
b |> shouldEqual 0
c |> List.ofSeq |> shouldEqual [ "Path" ]
c |> Seq.take 2 |> List.ofSeq |> shouldEqual [ "Path"; "Path" ]
| _ -> failwith "PropertySearch should be Rotate"

a2.Queries |> testQueryEnd
Expand All @@ -1917,7 +1921,7 @@ module invokeAction =
| PropertySearch.Rotate(a, b, c) ->
a |> shouldEqual "n"
b |> shouldEqual 0
c |> List.ofSeq |> shouldEqual [ "name"; "number" ]
c |> Seq.take 3 |> List.ofSeq |> shouldEqual [ "name"; "number"; "name" ]
| _ -> failwith "PropertySearch should be Rotate"

a2.Queries |> testQueryEnd
Expand All @@ -1941,7 +1945,7 @@ module invokeAction =
| PropertySearch.Rotate(a, b, c) ->
a |> shouldEqual "n"
b |> shouldEqual 0
c |> List.ofSeq |> shouldEqual [ "name" ]
c |> Seq.take 2 |> List.ofSeq |> shouldEqual [ "name"; "name" ]
| _ -> failwith "PropertySearch should be Rotate"

a2.Queries |> testQueryPartProperty "name" "foo"
Expand All @@ -1965,7 +1969,7 @@ module invokeAction =
| PropertySearch.Rotate(a, b, c) ->
a |> shouldEqual "name"
b |> shouldEqual 0
c |> List.ofSeq |> shouldEqual [ "name" ]
c |> Seq.take 2 |> List.ofSeq |> shouldEqual [ "name"; "name" ]
| _ -> failwith "PropertySearch should be Rotate"

a2.Queries |> testQueryEnd
Expand All @@ -1989,7 +1993,7 @@ module invokeAction =
| PropertySearch.Rotate(a, b, c) ->
a |> shouldEqual "name"
b |> shouldEqual 0
c |> List.ofSeq |> shouldEqual [ "name" ]
c |> Seq.take 2 |> List.ofSeq |> shouldEqual [ "name"; "name" ]
| _ -> failwith "PropertySearch should be Rotate"

a2.Queries |> testQueryPartProperty "name" "a"
Expand All @@ -2013,7 +2017,7 @@ module invokeAction =
| PropertySearch.Rotate(a, b, c) ->
a |> shouldEqual "nam"
b |> shouldEqual 0
c |> List.ofSeq |> shouldEqual [ "name" ]
c |> Seq.take 2 |> List.ofSeq |> shouldEqual [ "name"; "name" ]
| _ -> failwith "PropertySearch should be Rotate"

a2.Queries |> testQueryPartProperty "name" "a"
Expand All @@ -2024,7 +2028,7 @@ module invokeAction =
{ state with
InternalState.QueryState.Query = ":name"
InternalState.QueryState.Cursor = 5
PropertySearch = PropertySearch.Rotate("n", 0, [ "name"; "number" ]) }
PropertySearch = PropertySearch.Rotate("n", 0, Seq.cycle [ "name"; "number" ]) }

let context, _ = Query.prepare state

Expand All @@ -2038,8 +2042,8 @@ module invokeAction =
|> function
| PropertySearch.Rotate(a, b, c) ->
a |> shouldEqual "n"
b |> shouldEqual 1
c |> List.ofSeq |> shouldEqual [ "name"; "number" ]
b |> shouldEqual 0
c |> Seq.take 3 |> List.ofSeq |> shouldEqual [ "number"; "name"; "number" ]
| _ -> failwith "PropertySearch should be Rotate"

a2.Queries |> testQueryEnd
Expand All @@ -2050,18 +2054,22 @@ module invokeAction =
{ state with
InternalState.QueryState.Query = ":number"
InternalState.QueryState.Cursor = 7
PropertySearch = PropertySearch.Rotate("n", 1, [ "name"; "number" ]) }
PropertySearch = PropertySearch.Rotate("n", 0, Seq.cycle [ "number"; "name" ]) }

let context, _ = Query.prepare state

let a1, a2 =
invokeAction [ "name"; "path"; "number" ] state context Action.CompleteProperty

a1
|> shouldEqual
{ state with
InternalState.QueryState.Query = ":name"
InternalState.QueryState.Cursor = 5
PropertySearch = PropertySearch.Rotate("n", 0, [ "name"; "number" ]) }
a1.QueryState.Query |> shouldEqual ":name"
a1.QueryState.Cursor |> shouldEqual 5

a1.PropertySearch
|> function
| PropertySearch.Rotate(a, b, c) ->
a |> shouldEqual "n"
b |> shouldEqual 0
c |> Seq.take 3 |> List.ofSeq |> shouldEqual [ "name"; "number"; "name" ]
| _ -> failwith "PropertySearch should be Rotate"

a2.Queries |> testQueryEnd
7 changes: 7 additions & 0 deletions src/pocof/Data.fs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ module LanguageExtension =
let fromIndex (index: int) (s: string) = s.Substring(index)
let upToIndex (index: int) (s: string) = s.Substring(0, index)

module Seq =
let rec cycle source =
seq {
yield! source
yield! cycle source
}

let
#if !DEBUG
inline
Expand Down
12 changes: 6 additions & 6 deletions src/pocof/Handle.fs
Original file line number Diff line number Diff line change
Expand Up @@ -337,16 +337,16 @@ module Handle =
#if DEBUG
Logger.LogFile [ $"Search keyword '{keyword}' head '{head}' candidate '{candidate}' tail '{tail}'" ]
#endif
buildValues head candidate tail keyword 0 candidates basePosition
| PropertySearch.Rotate(keyword, i, candidates) ->
let cur = candidates |> Seq.item i
let i = (i + 1) % Seq.length candidates
let next = candidates |> Seq.item i
buildValues head candidate tail keyword 0 (candidates |> Seq.cycle) basePosition
| PropertySearch.Rotate(keyword, _, candidates) ->
let cur = candidates |> Seq.head
let candidates = candidates |> Seq.tail
let next = candidates |> Seq.head
let basePosition, head, tail = splitQuery cur next
#if DEBUG
Logger.LogFile [ $"Rotate keyword '{keyword}' head '{head}' cur '{cur}' next '{next}' tail '{tail}'" ]
#endif
buildValues head next tail keyword i candidates basePosition
buildValues head next tail keyword 0 candidates basePosition

let invokeAction
(wordDelimiters: string)
Expand Down

0 comments on commit 0db79d4

Please sign in to comment.