Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/FSharpx.Collections/Deque.fs
Original file line number Diff line number Diff line change
Expand Up @@ -244,5 +244,34 @@ module Deque =
let inline toSeq(q: Deque<'T>) =
q :> seq<'T>

///O(n). Returns a list of the deque elements in FIFO order.
let toList(q: Deque<'T>) : 'T list =
q.front @ List.rev q.rBack

///O(n). Returns an array of the deque elements in FIFO order.
let toArray(q: Deque<'T>) : 'T[] =
Array.ofSeq q

///O(n). Returns a new deque whose elements are the results of applying the given function to each element.
let map (f: 'T -> 'U) (q: Deque<'T>) : Deque<'U> =
Deque<'U>(List.map f q.front, List.map f q.rBack)

///O(n). Returns a new deque containing only the elements of the original for which the given predicate returns true.
let filter (predicate: 'T -> bool) (q: Deque<'T>) : Deque<'T> =
Deque<'T>(List.filter predicate q.front, List.filter predicate q.rBack)

///O(n). Applies the given function to each element of the deque.
let iter (f: 'T -> unit) (q: Deque<'T>) =
List.iter f q.front
List.iter f (List.rev q.rBack)

///O(n). Returns true if any element of the deque satisfies the given predicate.
let exists (predicate: 'T -> bool) (q: Deque<'T>) : bool =
List.exists predicate q.front || List.exists predicate q.rBack

///O(n). Returns true if all elements of the deque satisfy the given predicate.
let forall (predicate: 'T -> bool) (q: Deque<'T>) : bool =
List.forall predicate q.front && List.forall predicate q.rBack
Comment on lines +270 to +274
Comment on lines +270 to +274

let inline tryUnconj(q: Deque<'T>) =
q.TryUnconj
107 changes: 64 additions & 43 deletions src/FSharpx.Collections/Deque.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -12,136 +12,157 @@ type Deque<'T> =
interface System.Collections.Generic.IReadOnlyCollection<'T>

///O(1). Returns a new deque with the element added to the end.
member Conj : 'T -> Deque<'T>
member Conj: 'T -> Deque<'T>

///O(1). Returns a new deque with the element added to the beginning.
member Cons : 'T -> Deque<'T>
member Cons: 'T -> Deque<'T>

///O(1) amortized, O(n), worst case. Returns the first element.
member Head : 'T
member Head: 'T

///O(1) amortized, O(n), worst case. Returns option first element.
member TryHead : 'T option
member TryHead: 'T option

///O(1) amortized, O(n), worst case. Returns a new deque of the elements before the last element.
member Initial : Deque<'T>
member Initial: Deque<'T>

///O(1) amortized, O(n), worst case. Returns a new deque of the elements before the last element.
member TryInitial : Deque<'T> option
member TryInitial: Deque<'T> option

///O(1). Returns true if the deque has no elements.
member IsEmpty : bool
member IsEmpty: bool

///O(1) amortized, O(n), worst case. Returns the last element.
member Last : 'T
member Last: 'T

///O(1) amortized, O(n), worst case. Returns option last element.
member TryLast : 'T option
member TryLast: 'T option

///O(1). Returns the count of elememts.
member Length : int
member Length: int

///O(1). Returns deque reversed.
member Rev : Deque<'T>
member Rev: Deque<'T>

///O(1) amortized, O(n), worst case. Returns a new deque of the elements trailing the first element.
member Tail : Deque<'T>
member Tail: Deque<'T>

///O(1) amortized, O(n), worst case. Returns option deque of the elements trailing the first element.
member TryTail : Deque<'T> option
member TryTail: Deque<'T> option

///O(1) amortized, O(n), worst case. Returns init and the last element.
member Unconj : Deque<'T> * 'T
member Unconj: Deque<'T> * 'T

///O(1) amortized, O(n), worst case. Returns option init and the last element.
member TryUnconj : (Deque<'T> * 'T) option
member TryUnconj: (Deque<'T> * 'T) option

///O(1) amortized, O(n), worst case. Returns the first element and tail.
member Uncons : 'T * Deque<'T>
member Uncons: 'T * Deque<'T>

///O(1) amortized, O(n), worst case. Returns option first element and tail.
member TryUncons : ('T * Deque<'T>) option
member TryUncons: ('T * Deque<'T>) option

[<RequireQualifiedAccess>]
module Deque =

//pattern discriminators
val (|Cons|Nil|) : Deque<'T> -> Choice<('T * Deque<'T>),unit>
val (|Cons|Nil|): Deque<'T> -> Choice<('T * Deque<'T>), unit>

val (|Conj|Nil|) : Deque<'T> -> Choice<(Deque<'T> * 'T),unit>
val (|Conj|Nil|): Deque<'T> -> Choice<(Deque<'T> * 'T), unit>

///O(1). Returns a new deque with the element added to the end.
val inline conj : 'T -> Deque<'T> -> Deque<'T>
val inline conj: 'T -> Deque<'T> -> Deque<'T>

///O(1). Returns a new deque with the element added to the beginning.
val inline cons : 'T -> Deque<'T> -> Deque<'T>
val inline cons: 'T -> Deque<'T> -> Deque<'T>

///O(1). Returns deque of no elements.
[<GeneralizableValue>]
val empty<'T> : Deque<'T>

///O(n). Applies a function to each element of the deque, threading an accumulator argument through the computation, left to right
val fold : ('State -> 'T -> 'State) -> 'State -> Deque<'T> -> 'State
val fold: ('State -> 'T -> 'State) -> 'State -> Deque<'T> -> 'State

///O(n). Applies a function to each element of the deque, threading an accumulator argument through the computation, right to left
val foldBack : ('T -> 'State -> 'State) -> Deque<'T> -> 'State -> 'State
val foldBack: ('T -> 'State -> 'State) -> Deque<'T> -> 'State -> 'State

Comment on lines +69 to 88
///O(1) amortized, O(n), worst case. Returns the first element.
val inline head : Deque<'T> -> 'T
val inline head: Deque<'T> -> 'T

///O(1) amortized, O(n), worst case. Returns option first element.
val inline tryHead : Deque<'T> -> 'T option
val inline tryHead: Deque<'T> -> 'T option

///O(1) amortized, O(n), worst case. Returns a new deque of the elements before the last element.
val inline initial : Deque<'T> -> Deque<'T>
val inline initial: Deque<'T> -> Deque<'T>

///O(1) amortized, O(n), worst case. Returns option deque of the elements before the last element.
val inline tryInitial : Deque<'T> -> Deque<'T> option
val inline tryInitial: Deque<'T> -> Deque<'T> option

///O(1). Returns true if the deque has no elements.
val inline isEmpty : Deque<'T> -> bool
val inline isEmpty: Deque<'T> -> bool

///O(1) amortized, O(n), worst case. Returns the last element.
val inline last : Deque<'T> -> 'T
val inline last: Deque<'T> -> 'T

///O(1) amortized, O(n), worst case. Returns option last element.
val inline tryLast : Deque<'T> -> 'T option
val inline tryLast: Deque<'T> -> 'T option

///O(1). Returns the count of elememts.
val inline length : Deque<'T> -> int
val inline length: Deque<'T> -> int

///O(n), worst case. Returns a deque of the two lists concatenated.
val ofCatLists : 'T list -> 'T list -> Deque<'T>
val ofCatLists: 'T list -> 'T list -> Deque<'T>

///O(n), worst case. Returns a deque of the list.
val ofList : 'T list -> Deque<'T>
val ofList: 'T list -> Deque<'T>

///O(n), worst case. Returns a deque of the seq.
val ofSeq : seq<'T> -> Deque<'T>
val ofSeq: seq<'T> -> Deque<'T>

///O(1). Returns deque reversed.
val inline rev : Deque<'T> -> Deque<'T>
val inline rev: Deque<'T> -> Deque<'T>

///O(1). Returns a deque of one element.
val singleton : 'T -> Deque<'T>
val singleton: 'T -> Deque<'T>

///O(1) amortized, O(n), worst case. Returns a new deque of the elements trailing the first element.
val inline tail : Deque<'T> -> Deque<'T>
val inline tail: Deque<'T> -> Deque<'T>

///O(1) amortized, O(n), worst case. Returns option deque of the elements trailing the first element.
val inline tryTail : Deque<'T> -> Deque<'T> option
val inline tryTail: Deque<'T> -> Deque<'T> option

///O(1) amortized, O(n), worst case. Returns init and the last element.
val inline unconj : Deque<'T> -> Deque<'T> * 'T
val inline unconj: Deque<'T> -> Deque<'T> * 'T

///O(1) amortized, O(n), worst case. Returns option init and the last element.
val inline tryUnconj : Deque<'T> -> (Deque<'T> * 'T) option
val inline tryUnconj: Deque<'T> -> (Deque<'T> * 'T) option

///O(1) amortized, O(n), worst case. Returns the first element and tail.
val inline uncons : Deque<'T> -> 'T * Deque<'T>
val inline uncons: Deque<'T> -> 'T * Deque<'T>

///O(n). Views the given deque as a sequence.
val inline toSeq : Deque<'T> -> seq<'T>
val inline toSeq: Deque<'T> -> seq<'T>

///O(n). Returns a list of the deque elements in FIFO order.
val toList: Deque<'T> -> 'T list

///O(n). Returns an array of the deque elements in FIFO order.
val toArray: Deque<'T> -> 'T[]

///O(n). Returns a new deque whose elements are the results of applying the given function to each element.
val map: ('T -> 'U) -> Deque<'T> -> Deque<'U>

///O(n). Returns a new deque containing only the elements for which the given predicate returns true.
val filter: ('T -> bool) -> Deque<'T> -> Deque<'T>

///O(n). Applies the given function to each element of the deque.
val iter: ('T -> unit) -> Deque<'T> -> unit

///O(n). Returns true if any element of the deque satisfies the given predicate.
val exists: ('T -> bool) -> Deque<'T> -> bool

///O(n). Returns true if all elements of the deque satisfy the given predicate.
val forall: ('T -> bool) -> Deque<'T> -> bool

///O(1) amortized, O(n), worst case. Returns option first element and tail.
val inline tryUncons : Deque<'T> -> ('T * Deque<'T>) option
val inline tryUncons: Deque<'T> -> ('T * Deque<'T>) option
49 changes: 49 additions & 0 deletions src/FSharpx.Collections/Queue.fs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,55 @@ module Queue =
let inline toSeq(q: Queue<'T>) =
q :> seq<'T>

///O(n). Returns a list of the queue elements in FIFO order.
let toList(q: Queue<'T>) : 'T list =
q.front @ List.rev q.rBack

///O(n). Returns an array of the queue elements in FIFO order.
let toArray(q: Queue<'T>) : 'T[] =
let arr = Array.zeroCreate q.Length
let mutable i = 0

List.iter
(fun x ->
arr.[i] <- x
i <- i + 1)
q.front

List.iter
(fun x ->
arr.[i] <- x
i <- i + 1)
(List.rev q.rBack)

arr

///O(n). Returns a new queue whose elements are the results of applying the given function to each element.
let map (f: 'T -> 'U) (q: Queue<'T>) : Queue<'U> =
Queue<'U>(List.map f q.front, List.map f q.rBack)

///O(n). Returns a new queue containing only the elements of the original for which the given predicate returns true.
let filter (predicate: 'T -> bool) (q: Queue<'T>) : Queue<'T> =
let f = List.filter predicate q.front
let r = List.filter predicate q.rBack

match f with
| [] -> Queue<'T>(List.rev r, [])
| _ -> Queue<'T>(f, r)

///O(n). Applies the given function to each element of the queue.
let iter (f: 'T -> unit) (q: Queue<'T>) =
List.iter f q.front
List.iter f (List.rev q.rBack)

///O(n). Returns true if any element of the queue satisfies the given predicate.
let exists (predicate: 'T -> bool) (q: Queue<'T>) : bool =
List.exists predicate q.front || List.exists predicate q.rBack
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmmmm


///O(n). Returns true if all elements of the queue satisfy the given predicate.
let forall (predicate: 'T -> bool) (q: Queue<'T>) : bool =
List.forall predicate q.front && List.forall predicate q.rBack

let inline uncons(q: Queue<'T>) = q.Uncons

let inline tryUncons(q: Queue<'T>) =
Expand Down
Loading
Loading