Skip to content

Commit

Permalink
added some IndexList combinators (newIndexAfter/tryGetNext/etc.) (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
krauthaufen committed Apr 16, 2020
1 parent a831528 commit cf64135
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions src/FSharp.Data.Adaptive/Datastructures/IndexList.fs
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,51 @@ type IndexList< [<EqualityConditionalOn>] 'T> internal(l : Index, h : Index, con
member x.Neighbours(index : Index) =
MapExt.neighbours index content

/// Gets an unused index directly after the given one.
member x.NewIndexAfter (index : Index) =
let (l, s, r) = MapExt.neighbours index content
let l =
match s with
| Some (si, _) -> ValueSome si
| None -> match l with | Some (li, _) -> ValueSome li | _ -> ValueNone

match l with
| ValueSome li ->
match r with
| Some (ri, _) -> Index.between li ri
| None -> Index.after li
| ValueNone ->
match r with
| Some (ri, _) -> Index.before ri
| None -> Index.after Index.zero

/// Gets an unused index directly before the given one.
member x.NewIndexBefore (index : Index) =
let (l, s, r) = MapExt.neighbours index content
let r =
match s with
| Some (si, _) -> ValueSome si
| None -> match r with | Some (ri, _) -> ValueSome ri | _ -> ValueNone

match l with
| Some (li, _) ->
match r with
| ValueSome ri -> Index.between li ri
| ValueNone -> Index.after li
| None ->
match r with
| ValueSome ri -> Index.before ri
| ValueNone -> Index.after Index.zero

/// Gets the element directly after index in the list (if any).
member x.TryGetNext (index : Index) =
let (_,_,n) = MapExt.neighbours index content
n

/// Gets the element directly before index in the list (if any).
member x.TryGetPrev (index : Index) =
let (p,_,_) = MapExt.neighbours index content
p

override x.ToString() =
let suffix =
Expand Down Expand Up @@ -405,6 +450,22 @@ module IndexList =
/// Finds the optional neighbour elements in the list for the given index.
let inline neighbours (index : Index) (list : IndexList<'T>) =
list.Neighbours(index)

/// Gets an unused index directly after the given one.
let inline newIndexAfter (index : Index) (list : IndexList<'T>) =
list.NewIndexAfter(index)

/// Gets an unused index directly after the given one.
let inline newIndexBefore (index : Index) (list : IndexList<'T>) =
list.NewIndexBefore(index)

/// Gets the element directly after index in the list (if any).
let inline tryGetNext (index : Index) (list : IndexList<'T>) =
list.TryGetNext(index)

/// Gets the element directly before index in the list (if any).
let inline tryGetPrev (index : Index) (list : IndexList<'T>) =
list.TryGetPrev(index)

/// inserts an element directly after the given index.
let inline insertAfter (index : Index) (value : 'T) (list : IndexList<'T>) =
Expand Down

0 comments on commit cf64135

Please sign in to comment.