Skip to content

Commit cf64135

Browse files
committed
added some IndexList combinators (newIndexAfter/tryGetNext/etc.) (#63)
1 parent a831528 commit cf64135

1 file changed

Lines changed: 61 additions & 0 deletions

File tree

src/FSharp.Data.Adaptive/Datastructures/IndexList.fs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,51 @@ type IndexList< [<EqualityConditionalOn>] 'T> internal(l : Index, h : Index, con
289289
member x.Neighbours(index : Index) =
290290
MapExt.neighbours index content
291291

292+
/// Gets an unused index directly after the given one.
293+
member x.NewIndexAfter (index : Index) =
294+
let (l, s, r) = MapExt.neighbours index content
295+
let l =
296+
match s with
297+
| Some (si, _) -> ValueSome si
298+
| None -> match l with | Some (li, _) -> ValueSome li | _ -> ValueNone
299+
300+
match l with
301+
| ValueSome li ->
302+
match r with
303+
| Some (ri, _) -> Index.between li ri
304+
| None -> Index.after li
305+
| ValueNone ->
306+
match r with
307+
| Some (ri, _) -> Index.before ri
308+
| None -> Index.after Index.zero
309+
310+
/// Gets an unused index directly before the given one.
311+
member x.NewIndexBefore (index : Index) =
312+
let (l, s, r) = MapExt.neighbours index content
313+
let r =
314+
match s with
315+
| Some (si, _) -> ValueSome si
316+
| None -> match r with | Some (ri, _) -> ValueSome ri | _ -> ValueNone
317+
318+
match l with
319+
| Some (li, _) ->
320+
match r with
321+
| ValueSome ri -> Index.between li ri
322+
| ValueNone -> Index.after li
323+
| None ->
324+
match r with
325+
| ValueSome ri -> Index.before ri
326+
| ValueNone -> Index.after Index.zero
327+
328+
/// Gets the element directly after index in the list (if any).
329+
member x.TryGetNext (index : Index) =
330+
let (_,_,n) = MapExt.neighbours index content
331+
n
332+
333+
/// Gets the element directly before index in the list (if any).
334+
member x.TryGetPrev (index : Index) =
335+
let (p,_,_) = MapExt.neighbours index content
336+
p
292337

293338
override x.ToString() =
294339
let suffix =
@@ -405,6 +450,22 @@ module IndexList =
405450
/// Finds the optional neighbour elements in the list for the given index.
406451
let inline neighbours (index : Index) (list : IndexList<'T>) =
407452
list.Neighbours(index)
453+
454+
/// Gets an unused index directly after the given one.
455+
let inline newIndexAfter (index : Index) (list : IndexList<'T>) =
456+
list.NewIndexAfter(index)
457+
458+
/// Gets an unused index directly after the given one.
459+
let inline newIndexBefore (index : Index) (list : IndexList<'T>) =
460+
list.NewIndexBefore(index)
461+
462+
/// Gets the element directly after index in the list (if any).
463+
let inline tryGetNext (index : Index) (list : IndexList<'T>) =
464+
list.TryGetNext(index)
465+
466+
/// Gets the element directly before index in the list (if any).
467+
let inline tryGetPrev (index : Index) (list : IndexList<'T>) =
468+
list.TryGetPrev(index)
408469

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

0 commit comments

Comments
 (0)