Skip to content

Commit

Permalink
Implementing Peek for the Vectors and fixing Pop
Browse files Browse the repository at this point in the history
  • Loading branch information
forki committed May 29, 2012
1 parent 71946b9 commit 4591a69
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/FSharpx.Core/DataStructures/Interfaces.fs
Expand Up @@ -4,5 +4,6 @@ module FSharpx.DataStructures.Interfaces
type IVector<'a> =
abstract member Conj : 'a -> IVector<'a>
abstract member Count : unit -> int
abstract member Peek : unit -> 'a
abstract member Pop : unit -> IVector<'a>
abstract member AssocN : int*'a -> IVector<'a>
2 changes: 2 additions & 0 deletions src/FSharpx.Core/DataStructures/PersistentVector.fs
Expand Up @@ -12,6 +12,8 @@ let inline nth<'a> i (vector:PersistentVector<'a>) : 'a = vector.nth i

let inline cons<'a> (x:'a) (vector:PersistentVector<'a>) = vector.cons x

let inline peek<'a> (vector:PersistentVector<'a>) = (vector :> IVector<'a>).Peek()

let inline pop<'a> (vector:PersistentVector<'a>) = vector.pop()

let inline assocN<'a> i (x:'a) (vector:PersistentVector<'a>) : PersistentVector<'a> = vector.assocN(i,x)
Expand Down
2 changes: 2 additions & 0 deletions src/FSharpx.Core/DataStructures/TransientVector.fs
Expand Up @@ -12,6 +12,8 @@ let inline nth<'a> i (vector:TransientVector<'a>) : 'a = vector.nth i

let inline conj<'a> (x:'a) (vector:TransientVector<'a>) = vector.conj x

let inline peek<'a> (vector:TransientVector<'a>) = (vector :> IVector<'a>).Peek()

let inline pop<'a> (vector:TransientVector<'a>) = vector.pop()

let inline assocN<'a> i (x:'a) (vector:TransientVector<'a>) : TransientVector<'a> = vector.assocN(i,x)
5 changes: 4 additions & 1 deletion src/FSharpx.Core/DataStructures/Vector.fs
Expand Up @@ -228,6 +228,7 @@ type TransientVector<'a> (count,shift:int,root:Node,tail:obj[]) =
interface IVector<'a> with
member this.Conj x = this.conj x :> IVector<'a>
member this.Pop() = this.pop() :> IVector<'a>
member this.Peek() = if count > 0 then this.nth(count - 1) else failwith "Can't peek empty vector"
member this.Count() = this.EnsureEditable(); count
member this.AssocN(i,x) = this.assocN(i,x) :> IVector<'a>

Expand Down Expand Up @@ -348,7 +349,7 @@ and PersistentVector<'a> (count,shift:int,root:Node,tail:obj[]) =
if count = 1 then PersistentVector<'a>() else

//if(tail.length > 1)
if count - tailOff > 1 then PersistentVector(count - 1, shift, root, tail.[1..]) else
if count - tailOff > 1 then PersistentVector(count - 1, shift, root, tail.[0..(tail.Length-1)]) else

let newtail = this.ArrayFor(count - 2)

Expand Down Expand Up @@ -391,4 +392,6 @@ and PersistentVector<'a> (count,shift:int,root:Node,tail:obj[]) =
member this.Conj x = this.cons x :> IVector<'a>
member this.Pop() = this.pop() :> IVector<'a>
member this.Count() = count
member this.Peek() = if count > 0 then this.nth(count - 1) else failwith "Can't peek empty vector"

member this.AssocN(i,x) = this.assocN(i,x) :> IVector<'a>
22 changes: 21 additions & 1 deletion tests/FSharpx.DataStructures.Tests/PersistentVectorTest.fs
Expand Up @@ -67,9 +67,29 @@ let ``vector with 30000 elements should allow assocN``() =

!vector |> Seq.toList |> should equal [for i in 1..30000 -> i*2]

[<Test>]
let ``can peek elements from a vector``() =
let vector = empty |> cons 1 |> cons 4 |> cons 25
vector |> peek |> should equal 25

[<Test>]
let ``can pop elements from a vector``() =
let vector = empty |> cons 1 |> cons 4 |> cons 25
vector |> peek |> should equal 25
vector |> pop |> peek |> should equal 4
vector |> pop |> pop |> peek |> should equal 1

vector |> count |> should equal 3
vector |> pop |> count |> should equal 2
vector |> pop |> pop |> count |> should equal 1
vector |> pop |> pop |> count |> should equal 1

[<Test>]
let ``vector with 30000 elements should allow pop``() =
let vector = ref empty
for i in 1..30000 do
vector := cons i (!vector)

for i in 1..30000 do
vector := pop (!vector)

!vector |> Seq.toList |> should equal []
21 changes: 20 additions & 1 deletion tests/FSharpx.DataStructures.Tests/TransientVectorTest.fs
Expand Up @@ -62,8 +62,27 @@ let ``vector with 30000 elements should allow assocN``() =

!vector |> Seq.toList |> should equal [for i in 1..30000 -> i*2]

[<Test>]
let ``can peek elements from a vector``() =
let vector = empty |> conj 1 |> conj 4 |> conj 25
vector |> peek |> should equal 25

[<Test>]
let ``can pop elements from a vector``() =
let vector = empty |> conj 1 |> conj 4 |> conj 25
vector |> count |> should equal 3
vector |> pop |> pop |> count |> should equal 1
vector |> pop |> count |> should equal 2
vector |> peek |> should equal 4
vector |> pop |> count |> should equal 1
vector |> peek |> should equal 1

[<Test>]
let ``vector with 30000 elements should allow pop``() =
let vector = ref empty
for i in 1..30000 do
vector := conj i (!vector)

for i in 1..30000 do
vector := pop (!vector)

!vector |> Seq.toList |> should equal []

0 comments on commit 4591a69

Please sign in to comment.