From 030535348da0774ee8139560a417916d9f99afda Mon Sep 17 00:00:00 2001 From: Narysev Date: Fri, 12 Jun 2026 22:04:49 +0300 Subject: [PATCH 1/2] feat(quadtree): add filter, exists, forall with tests and benchmarks Implement: - Vector.filter / Matrix.filter - Vector.exists / Vector.forall (short-circuit || / &&) - Matrix.exists / Matrix.forall (short-circuit || / &&) Tests (64 total): - Vector.filter: 9 - Vector.exists: 12 - Vector.forall: 12 - Matrix.filter: 9 - Matrix.exists: 11 - Matrix.forall: 11 Benchmarks: - Filters.fs: Vector/Matrix filter (dense + sparse), N=64,256,4096 - Exists.fs: Vector/Matrix exists (dense + sparse), N=64,256,4096 - Forall.fs: Vector/Matrix forall (dense + sparse), N=64,256,4096 - Register in BenchmarkSwitcher and .fsproj --- QuadTree.Benchmark/Exists.fs | 52 +++ QuadTree.Benchmark/Filters.fs | 52 +++ QuadTree.Benchmark/Forall.fs | 52 +++ QuadTree.Benchmark/Main.fs | 5 +- QuadTree.Benchmark/QuadTree.Benchmark.fsproj | 3 + QuadTree.Tests/Tests.Matrix.fs | 354 +++++++++++++++++ QuadTree.Tests/Tests.Vector.fs | 383 +++++++++++++++++++ QuadTree/Matrix.fs | 50 +++ QuadTree/Vector.fs | 40 +- 9 files changed, 989 insertions(+), 2 deletions(-) create mode 100644 QuadTree.Benchmark/Exists.fs create mode 100644 QuadTree.Benchmark/Filters.fs create mode 100644 QuadTree.Benchmark/Forall.fs diff --git a/QuadTree.Benchmark/Exists.fs b/QuadTree.Benchmark/Exists.fs new file mode 100644 index 0000000..2fc08a8 --- /dev/null +++ b/QuadTree.Benchmark/Exists.fs @@ -0,0 +1,52 @@ +namespace QuadTree.Benchmarks.Exists + + +open BenchmarkDotNet.Attributes +open QuadTree.Benchmarks.Utils + +[)>] +type Benchmark() = + + let mutable denseVec = Unchecked.defaultof> + let mutable sparseVec = Unchecked.defaultof> + let mutable denseMat = Unchecked.defaultof> + let mutable sparseMat = Unchecked.defaultof> + + [] + member val N = 0 with get, set + + [] + member this.Setup() = + let denseData = [ for i in 0UL .. uint64 this.N - 1UL -> (i * 1UL, int i % 100) ] + denseVec <- Vector.fromCoordinateList (Vector.CoordinateList(uint64 this.N * 1UL, denseData)) + + let sparseData = [ for i in 0UL .. 10UL .. uint64 this.N - 1UL -> (i * 1UL, int i % 100) ] + sparseVec <- Vector.fromCoordinateList (Vector.CoordinateList(uint64 this.N * 1UL, sparseData)) + + let denseMatData = [ + for r in 0UL .. uint64 this.N - 1UL do + for c in 0UL .. uint64 this.N - 1UL do + (r * 1UL, c * 1UL, int (r + c) % 100) ] + denseMat <- Matrix.fromCoordinateList (Matrix.CoordinateList(uint64 this.N * 1UL, uint64 this.N * 1UL, denseMatData)) + + let sparseMatData = [ + for r in 0UL .. 10UL .. uint64 this.N - 1UL do + for c in 0UL .. 10UL .. uint64 this.N - 1UL do + (r * 1UL, c * 1UL, int (r + c) % 100) ] + sparseMat <- Matrix.fromCoordinateList (Matrix.CoordinateList(uint64 this.N * 1UL, uint64 this.N * 1UL, sparseMatData)) + + [] + member this.VectorExistsDense() = + Vector.exists denseVec (fun x -> x < 0) + + [] + member this.VectorExistsSparse() = + Vector.exists sparseVec (fun x -> x < 0) + + [] + member this.MatrixExistsDense() = + Matrix.exists denseMat (fun x -> x < 0) + + [] + member this.MatrixExistsSparse() = + Matrix.exists sparseMat (fun x -> x < 0) diff --git a/QuadTree.Benchmark/Filters.fs b/QuadTree.Benchmark/Filters.fs new file mode 100644 index 0000000..075e339 --- /dev/null +++ b/QuadTree.Benchmark/Filters.fs @@ -0,0 +1,52 @@ +namespace QuadTree.Benchmarks.Filters + +open BenchmarkDotNet.Attributes +open QuadTree.Benchmarks.Utils + +[)>] +type Benchmark() = + + let mutable denseVec = Unchecked.defaultof> + let mutable sparseVec = Unchecked.defaultof> + let mutable denseMat = Unchecked.defaultof> + let mutable sparseMat = Unchecked.defaultof> + + // [] + [] + member val N = 0 with get, set + + [] + member this.Setup() = + let denseData = [ for i in 0UL .. uint64 this.N - 1UL -> (i * 1UL, int i % 100) ] + denseVec <- Vector.fromCoordinateList (Vector.CoordinateList(uint64 this.N * 1UL, denseData)) + + let sparseData = [ for i in 0UL .. 10UL .. uint64 this.N - 1UL -> (i * 1UL, int i % 100) ] + sparseVec <- Vector.fromCoordinateList (Vector.CoordinateList(uint64 this.N * 1UL, sparseData)) + + let denseMatData = [ + for r in 0UL .. uint64 this.N - 1UL do + for c in 0UL .. uint64 this.N - 1UL do + (r * 1UL, c * 1UL, int (r + c) % 100) ] + denseMat <- Matrix.fromCoordinateList (Matrix.CoordinateList(uint64 this.N * 1UL, uint64 this.N * 1UL, denseMatData)) + + let sparseMatData = [ + for r in 0UL .. 10UL .. uint64 this.N - 1UL do + for c in 0UL .. 10UL .. uint64 this.N - 1UL do + (r * 1UL, c * 1UL, int (r + c) % 100) ] + sparseMat <- Matrix.fromCoordinateList (Matrix.CoordinateList(uint64 this.N * 1UL, uint64 this.N * 1UL, sparseMatData)) + + [] + member this.VectorFilterDense() = + Vector.filter denseVec (fun x -> x % 2 = 0) + + [] + member this.VectorFilterSparse() = + Vector.filter sparseVec (fun x -> x % 2 = 0) + + [] + member this.MatrixFilterDense() = + Matrix.filter denseMat (fun x -> x % 2 = 0) + + [] + member this.MatrixFilterSparse() = + Matrix.filter sparseMat (fun x -> x % 2 = 0) diff --git a/QuadTree.Benchmark/Forall.fs b/QuadTree.Benchmark/Forall.fs new file mode 100644 index 0000000..8d8ad46 --- /dev/null +++ b/QuadTree.Benchmark/Forall.fs @@ -0,0 +1,52 @@ +namespace QuadTree.Benchmarks.Forall + + +open BenchmarkDotNet.Attributes +open QuadTree.Benchmarks.Utils + +[)>] +type Benchmark() = + + let mutable denseVec = Unchecked.defaultof> + let mutable sparseVec = Unchecked.defaultof> + let mutable denseMat = Unchecked.defaultof> + let mutable sparseMat = Unchecked.defaultof> + + [] + member val N = 0 with get, set + + [] + member this.Setup() = + let denseData = [ for i in 0UL .. uint64 this.N - 1UL -> (i * 1UL, int i % 100) ] + denseVec <- Vector.fromCoordinateList (Vector.CoordinateList(uint64 this.N * 1UL, denseData)) + + let sparseData = [ for i in 0UL .. 10UL .. uint64 this.N - 1UL -> (i * 1UL, int i % 100) ] + sparseVec <- Vector.fromCoordinateList (Vector.CoordinateList(uint64 this.N * 1UL, sparseData)) + + let denseMatData = [ + for r in 0UL .. uint64 this.N - 1UL do + for c in 0UL .. uint64 this.N - 1UL do + (r * 1UL, c * 1UL, int (r + c) % 100) ] + denseMat <- Matrix.fromCoordinateList (Matrix.CoordinateList(uint64 this.N * 1UL, uint64 this.N * 1UL, denseMatData)) + + let sparseMatData = [ + for r in 0UL .. 10UL .. uint64 this.N - 1UL do + for c in 0UL .. 10UL .. uint64 this.N - 1UL do + (r * 1UL, c * 1UL, int (r + c) % 100) ] + sparseMat <- Matrix.fromCoordinateList (Matrix.CoordinateList(uint64 this.N * 1UL, uint64 this.N * 1UL, sparseMatData)) + + [] + member this.VectorForallDense() = + Vector.forall denseVec (fun x -> x > 0) + + [] + member this.VectorForallSparse() = + Vector.forall sparseVec (fun x -> x > 0) + + [] + member this.MatrixForallDense() = + Matrix.forall denseMat (fun x -> x > 0) + + [] + member this.MatrixForallSparse() = + Matrix.forall sparseMat (fun x -> x > 0) diff --git a/QuadTree.Benchmark/Main.fs b/QuadTree.Benchmark/Main.fs index 61394af..3fb2bf4 100644 --- a/QuadTree.Benchmark/Main.fs +++ b/QuadTree.Benchmark/Main.fs @@ -4,7 +4,10 @@ open BenchmarkDotNet.Running let main argv = let benchmarks = BenchmarkSwitcher - [| typeof + [| typeof + typeof + typeof + typeof typeof typeof |] diff --git a/QuadTree.Benchmark/QuadTree.Benchmark.fsproj b/QuadTree.Benchmark/QuadTree.Benchmark.fsproj index 4edb362..ba98b9d 100644 --- a/QuadTree.Benchmark/QuadTree.Benchmark.fsproj +++ b/QuadTree.Benchmark/QuadTree.Benchmark.fsproj @@ -8,6 +8,9 @@ + + + diff --git a/QuadTree.Tests/Tests.Matrix.fs b/QuadTree.Tests/Tests.Matrix.fs index 816b4a2..b65c53d 100644 --- a/QuadTree.Tests/Tests.Matrix.fs +++ b/QuadTree.Tests/Tests.Matrix.fs @@ -642,3 +642,357 @@ let ``Fold sum`` () = let actual = foldAssociative op_add None m1 |> Option.get Assert.Equal(expected, actual) + +[] +let ``Matrix.filter all pass, none changed`` () = + let m = Matrix.fromCoordinateList ( + CoordinateList(4UL, 4UL, + [ (0UL, 0UL, 1) + (0UL, 1UL, 2) + (1UL, 0UL, 3) + (1UL, 1UL, 4) ])) + let actual = Matrix.filter m (fun x -> x > 0) + Assert.Equal(m, actual) + +[] +let ``Matrix.filter none pass, all reset to zero`` () = + let m = Matrix.fromCoordinateList ( + CoordinateList(4UL, 4UL, + [ (0UL, 0UL, 1) + (0UL, 1UL, 2) + (1UL, 0UL, 3) + (1UL, 1UL, 4) ])) + let expected = Matrix.fromCoordinateList ( + CoordinateList(4UL, 4UL, [])) + let actual = Matrix.filter m (fun x -> x < 0) + + Assert.Equal(expected, actual) + +[] +let ``Matrix.filter length is not a power of 2, all reset to zero`` () = + let m = Matrix.fromCoordinateList ( + CoordinateList(3UL, 3UL, + [ (0UL, 0UL, 1) + (0UL, 1UL, 2) + (0UL, 2UL, 3) + (1UL, 0UL, 4) + (1UL, 1UL, 5) + (1UL, 2UL, 6) + (2UL, 0UL, 7) + (2UL, 1UL, 8) + (2UL, 2UL, 9) ])) + let expected = Matrix.fromCoordinateList ( + CoordinateList(3UL, 3UL, [])) + let actual = Matrix.filter m (fun x -> x < 0) + + Assert.Equal(expected, actual) + +[] +let ``Matrix.filter some pass, odd set to zero`` () = + let m = Matrix.fromCoordinateList ( + CoordinateList(4UL, 4UL, + [ (0UL, 0UL, 1) + (0UL, 1UL, 2) + (0UL, 2UL, 3) + (0UL, 3UL, 4) + (1UL, 0UL, 5) + (1UL, 1UL, 6) + (1UL, 2UL, 7) + (1UL, 3UL, 8) + (2UL, 0UL, 9) + (2UL, 1UL, 10) + (2UL, 2UL, 11) + (2UL, 3UL, 12) + (3UL, 0UL, 13) + (3UL, 1UL, 14) + (3UL, 2UL, 15) + (3UL, 3UL, 16) ])) + let expected = Matrix.fromCoordinateList ( + CoordinateList(4UL, 4UL, + [ (0UL, 1UL, 2) + (0UL, 3UL, 4) + (1UL, 1UL, 6) + (1UL, 3UL, 8) + (2UL, 1UL, 10) + (2UL, 3UL, 12) + (3UL, 1UL, 14) + (3UL, 3UL, 16) ])) + let actual = Matrix.filter m (fun x -> x % 2 = 0) + + Assert.Equal(expected, actual) + +[] +let ``Matrix.filter length is not a power of 2, not all reset to zero`` () = + let m = Matrix.fromCoordinateList ( + CoordinateList(3UL, 3UL, + [ (0UL, 0UL, 1) + (0UL, 1UL, 2) + (0UL, 2UL, 3) + (1UL, 0UL, 4) + (1UL, 1UL, 5) + (1UL, 2UL, 6) + (2UL, 0UL, 7) + (2UL, 1UL, 8) + (2UL, 2UL, 9) ])) + let expected = Matrix.fromCoordinateList ( + CoordinateList(3UL, 3UL, + [ (0UL, 1UL, 2) + (1UL, 0UL, 4) + (1UL, 2UL, 6) + (2UL, 1UL, 8) ])) + let actual = Matrix.filter m (fun x -> x % 2 = 0) + + Assert.Equal(expected, actual) + +[] +let ``Matrix.filter none pass, length is not a power of 2, none changed`` () = + let m = Matrix.fromCoordinateList ( + CoordinateList(3UL, 3UL, [])) + let actual = Matrix.filter m (fun x -> x > 0) + Assert.Equal(m, actual) + +[] +let ``Matrix.filter none pass, length is a power of 2, none changed`` () = + let m = Matrix.fromCoordinateList ( + CoordinateList(4UL, 4UL, [])) + let actual = Matrix.filter m (fun x -> x > 0) + Assert.Equal(m, actual) + +[] +let ``Matrix.filter single element, passes`` () = + let m = Matrix.fromCoordinateList ( + CoordinateList(1UL, 1UL, + [ (0UL, 0UL, 1) ])) + let expected = Matrix.fromCoordinateList ( + CoordinateList(1UL, 1UL, + [ (0UL, 0UL, 1) ])) + let actual = Matrix.filter m (fun x -> x > 0) + + Assert.Equal(expected, actual) + +[] +let ``Matrix.filter single element, fails`` () = + let m = Matrix.fromCoordinateList ( + CoordinateList(1UL, 1UL, + [ (0UL, 0UL, 1) ])) + let expected = Matrix.fromCoordinateList ( + CoordinateList(1UL, 1UL, [])) + let actual = Matrix.filter m (fun x -> x < 0) + + Assert.Equal(expected, actual) + +[] +let ``Matrix.exists the first element fits, length is a power of 2`` () = + let m = Matrix.fromCoordinateList ( + CoordinateList(4UL, 4UL, + [ (0UL, 0UL, 1) + (0UL, 1UL, 2) + (1UL, 0UL, 3) + (1UL, 1UL, 4) ])) + Assert.True (Matrix.exists m (fun x -> x = 1)) + Assert.False (Matrix.exists m (fun x -> x = 10)) + +[] +let ``Matrix.exists the first element fits, length is not a power of 2`` () = + let m = Matrix.fromCoordinateList ( + CoordinateList(3UL, 3UL, + [ (0UL, 0UL, 1) + (0UL, 1UL, 2) + (0UL, 2UL, 3) + (1UL, 0UL, 4) + (1UL, 1UL, 5) + (1UL, 2UL, 6) ])) + Assert.True (Matrix.exists m (fun x -> x = 1)) + +[] +let ``Matrix.exists no element fits, length is a power of 2`` () = + let m = Matrix.fromCoordinateList ( + CoordinateList(4UL, 4UL, + [ (0UL, 0UL, 1) + (0UL, 1UL, 2) + (1UL, 0UL, 3) + (1UL, 1UL, 4) ])) + Assert.False (Matrix.exists m (fun x -> x = 10)) + +[] +let ``Matrix.exists no element fits, length is not a power of 2`` () = + let m = Matrix.fromCoordinateList ( + CoordinateList(3UL, 3UL, + [ (0UL, 0UL, 1) + (0UL, 1UL, 2) + (0UL, 2UL, 3) + (1UL, 0UL, 4) + (1UL, 1UL, 5) + (1UL, 2UL, 6) ])) + Assert.False (Matrix.exists m (fun x -> x = 10)) + +[] +let ``Matrix.exists empty matrix`` () = + let m = Matrix.fromCoordinateList ( + CoordinateList(3UL, 3UL, [])) + Assert.False (Matrix.exists m (fun x -> x = 1)) + +[] +let ``Matrix.exists single element, fits`` () = + let m = Matrix.fromCoordinateList ( + CoordinateList(1UL, 1UL, + [ (0UL, 0UL, 1) ])) + Assert.True (Matrix.exists m (fun x -> x = 1)) + +[] +let ``Matrix.exists single element, does not fit`` () = + let m = Matrix.fromCoordinateList ( + CoordinateList(1UL, 1UL, + [ (0UL, 0UL, 1) ])) + Assert.False (Matrix.exists m (fun x -> x = 10)) + +[] +let ``Matrix.exists all elements fit,length is a power of 2`` () = + let m = Matrix.fromCoordinateList ( + CoordinateList(4UL, 4UL, + [ (0UL, 0UL, 1) + (0UL, 1UL, 2) + (1UL, 0UL, 3) + (1UL, 1UL, 4) ])) + Assert.True (Matrix.exists m (fun x -> x > 0)) + +[] +let ``Matrix.exists all elements fit,length is not a power of 2`` () = + let m = Matrix.fromCoordinateList ( + CoordinateList(3UL, 3UL, + [ (0UL, 0UL, 1) + (0UL, 1UL, 2) + (0UL, 2UL, 3) + (1UL, 0UL, 4) + (1UL, 1UL, 5) + (1UL, 2UL, 6) ])) + Assert.True (Matrix.exists m (fun x -> x > 0)) +[] +let ``Matrix.exists some elements fit, length is a power of 2`` () = + let m = Matrix.fromCoordinateList ( + CoordinateList(4UL, 4UL, + [ (0UL, 0UL, 1) + (0UL, 1UL, 2) + (1UL, 0UL, 3) + (1UL, 1UL, 4) ])) + Assert.True (Matrix.exists m (fun x -> x = 2 || x = 10)) + +[] +let ``Matrix.exists some elements fit, length is not a power of 2`` () = + let m = Matrix.fromCoordinateList ( + CoordinateList(3UL, 3UL, + [ (0UL, 0UL, 1) + (0UL, 1UL, 2) + (0UL, 2UL, 3) + (1UL, 0UL, 4) + (1UL, 1UL, 5) + (1UL, 2UL, 6) ])) + Assert.True (Matrix.exists m (fun x -> x = 2 || x = 10)) + +[] +let ``Matrix.forall the first element fits, length is a power of 2`` () = + let m = Matrix.fromCoordinateList ( + CoordinateList(4UL, 4UL, + [ (0UL, 0UL, 1) + (0UL, 1UL, 2) + (1UL, 0UL, 3) + (1UL, 1UL, 4) ])) + Assert.True (Matrix.forall m (fun x -> x > 0)) + +[] +let ``Matrix.forall the first element fits, length is not a power of 2`` () = + let m = Matrix.fromCoordinateList ( + CoordinateList(3UL, 3UL, + [ (0UL, 0UL, 1) + (0UL, 1UL, 2) + (0UL, 2UL, 3) + (1UL, 0UL, 4) + (1UL, 1UL, 5) + (1UL, 2UL, 6) ])) + Assert.True (Matrix.forall m (fun x -> x > 0)) + +[] +let ``Matrix.forall no element fits, length is a power of 2`` () = + let m = Matrix.fromCoordinateList ( + CoordinateList(4UL, 4UL, + [ (0UL, 0UL, 1) + (0UL, 1UL, 2) + (1UL, 0UL, 3) + (1UL, 1UL, 4) ])) + Assert.False (Matrix.forall m (fun x -> x > 10)) + +[] +let ``Matrix.forall no element fits, length is not a power of 2`` () = + let m = Matrix.fromCoordinateList ( + CoordinateList(3UL, 3UL, + [ (0UL, 0UL, 1) + (0UL, 1UL, 2) + (0UL, 2UL, 3) + (1UL, 0UL, 4) + (1UL, 1UL, 5) + (1UL, 2UL, 6) ])) + Assert.False (Matrix.forall m (fun x -> x > 10)) + +[] +let ``Matrix.forall all elements fit,length is a power of 2`` () = + let m = Matrix.fromCoordinateList ( + CoordinateList(4UL, 4UL, + [ (0UL, 0UL, 1) + (0UL, 1UL, 2) + (1UL, 0UL, 3) + (1UL, 1UL, 4) ])) + Assert.True (Matrix.forall m (fun x -> x > 0)) + +[] +let ``Matrix.forall all elements fit,length is not a power of 2`` () = + let m = Matrix.fromCoordinateList ( + CoordinateList(3UL, 3UL, + [ (0UL, 0UL, 1) + (0UL, 1UL, 2) + (0UL, 2UL, 3) + (1UL, 0UL, 4) + (1UL, 1UL, 5) + (1UL, 2UL, 6) ])) + Assert.True (Matrix.forall m (fun x -> x > 0)) + +[] +let ``Matrix.forall some elements fit, length is a power of 2`` () = + let m = Matrix.fromCoordinateList ( + CoordinateList(4UL, 4UL, + [ (0UL, 0UL, 1) + (0UL, 1UL, 2) + (1UL, 0UL, 3) + (1UL, 1UL, 4) ])) + Assert.False (Matrix.forall m (fun x -> x = 2 || x = 10)) + +[] +let ``Matrix.forall some elements fit, length is not a power of 2`` () = + let m = Matrix.fromCoordinateList ( + CoordinateList(3UL, 3UL, + [ (0UL, 0UL, 1) + (0UL, 1UL, 2) + (0UL, 2UL, 3) + (1UL, 0UL, 4) + (1UL, 1UL, 5) + (1UL, 2UL, 6) ])) + Assert.False (Matrix.forall m (fun x -> x = 2 || x = 10)) + +[] +let ``Matrix.forall empty matrix`` () = + let m = Matrix.fromCoordinateList ( + CoordinateList(3UL, 3UL, [])) + Assert.True (Matrix.forall m (fun x -> x = 1)) + +[] +let ``Matrix.forall single element, fits`` () = + let m = Matrix.fromCoordinateList ( + CoordinateList(1UL, 1UL, + [ (0UL, 0UL, 1) ])) + Assert.True (Matrix.forall m (fun x -> x = 1)) + +[] +let ``Matrix.forall single element, does not fit`` () = + let m = Matrix.fromCoordinateList ( + CoordinateList(1UL, 1UL, + [ (0UL, 0UL, 1) ])) + Assert.False (Matrix.forall m (fun x -> x = 10)) \ No newline at end of file diff --git a/QuadTree.Tests/Tests.Vector.fs b/QuadTree.Tests/Tests.Vector.fs index 4eb1af9..6ef2cbd 100644 --- a/QuadTree.Tests/Tests.Vector.fs +++ b/QuadTree.Tests/Tests.Vector.fs @@ -903,3 +903,386 @@ let ``Init vector`` () = let actual = Vector.init 3UL (fun i -> Some(int i)) Assert.Equal(expected, actual) + + +[] +let ``Vector.filter all pass, none changed`` () = + let v = Vector.fromCoordinateList ( + CoordinateList(8UL, + [ (0UL, 1) + (1UL, 2) + (2UL, 3) + (3UL, 4) + (4UL, 5) + (5UL, 6) + (6UL, 7) + (7UL, 8) ])) + let actual = Vector.filter v (fun x -> x > 0) + Assert.Equal(v, actual) + +[] +let ``Vector.filter none pass, all reset to zero`` () = + let v = Vector.fromCoordinateList ( + CoordinateList(8UL, + [ (0UL, 1) + (1UL, 2) + (2UL, 3) + (3UL, 4) + (4UL, 5) + (5UL, 6) + (6UL, 7) + (7UL, 8) ])) + let expected = + Vector.empty 8UL + let actual = Vector.filter v (fun x -> x < 0) + + Assert.Equal(expected, actual) + +[] +let ``Vector.filter length is not a power of 2, all reset to zero`` () = + let v = Vector.fromCoordinateList ( + CoordinateList(6UL, + [ (0UL, 1) + (1UL, 2) + (2UL, 3) + (3UL, 4) + (4UL, 5) + (5UL, 6) ])) + let expected = + Vector.empty 6UL + let actual = Vector.filter v (fun x -> x < 0) + + Assert.Equal(expected, actual) + +[] +let ``Vector.filter some pass, odd set to zero`` () = + let v = Vector.fromCoordinateList ( + CoordinateList(8UL, + [ (0UL, 1) + (1UL, 2) + (2UL, 3) + (3UL, 4) + (4UL, 5) + (5UL, 6) + (6UL, 7) + (7UL, 8) ])) + let expected = + Vector.fromCoordinateList ( + CoordinateList(8UL, + [ (1UL, 2) + (3UL, 4) + (5UL, 6) + (7UL, 8) ])) + let actual = Vector.filter v (fun x -> ((x % 2) = 0)) + + Assert.Equal(expected, actual) + +[] +let ``Vector.filter length is not a power of 2, not all reset to zero`` () = + let v = Vector.fromCoordinateList ( + CoordinateList(6UL, + [ (0UL, 1) + (1UL, 2) + (2UL, 3) + (3UL, 4) + (4UL, 5) + (5UL, 6) ])) + let expected = + Vector.fromCoordinateList ( + CoordinateList(6UL, + [ (1UL, 2) + (3UL, 4) + (5UL, 6) ])) + let actual = Vector.filter v (fun x -> ((x % 2) = 0)) + + Assert.Equal(expected, actual) + +[] +let ``Vector.filter none pass, length is not a power of 2, none changed`` () = + let v = Vector.fromCoordinateList ( + CoordinateList(6UL, + [])) + let actual = Vector.filter v (fun x -> x > 0) + Assert.Equal(v, actual) + +[] +let ``Vector.filter none pass, length is a power of 2, none changed`` () = + let v = Vector.fromCoordinateList ( + CoordinateList(8UL, + [])) + let actual = Vector.filter v (fun x -> x > 0) + Assert.Equal(v, actual) + +[] +let ``Vector.filter single element, passes`` () = + let v = Vector.fromCoordinateList ( + CoordinateList(1UL, + [ (0UL, 1) ])) + let expected = + Vector.fromCoordinateList ( + CoordinateList(1UL, + [ (0UL, 1) ])) + let actual = Vector.filter v (fun x -> x > 0) + + Assert.Equal(expected, actual) + +[] +let ``Vector.filter single element, fails`` () = + let v = Vector.fromCoordinateList ( + CoordinateList(1UL, + [ (0UL, 1) ])) + let expected = + Vector.empty 1UL + let actual = Vector.filter v (fun x -> x < 0) + + Assert.Equal(expected, actual) + +[] +let ``Vector.exists the first element fits, length is a power of 2`` () = + let v = Vector.fromCoordinateList ( + CoordinateList(8UL, + [ (0UL, 1) + (1UL, 2) + (2UL, 3) + (3UL, 4) + (4UL, 5) + (5UL, 6) + (6UL, 7) + (7UL, 8) ])) + Assert.True (Vector.exists v (fun x -> x > 0)) + +[] +let ``Vector.exists the first element fits, length is not a power of 2`` () = + let v = Vector.fromCoordinateList ( + CoordinateList(6UL, + [ (0UL, 1) + (1UL, 2) + (2UL, 3) + (3UL, 4) + (4UL, 5) + (5UL, 6) ])) + Assert.True (Vector.exists v (fun x -> x > 0)) + +[] +let ``Vector.exists the last item fits, length is a power of 2`` () = + let v = Vector.fromCoordinateList ( + CoordinateList(8UL, + [ (0UL, 1) + (1UL, 2) + (2UL, 3) + (3UL, 4) + (4UL, 5) + (5UL, 6) + (6UL, 7) + (7UL, 8) ])) + Assert.True (Vector.exists v (fun x -> x > 7)) + +[] +let ``Vector.exists the last item fits, length is not a power of 2`` () = + let v = Vector.fromCoordinateList ( + CoordinateList(6UL, + [ (0UL, 1) + (1UL, 2) + (2UL, 3) + (3UL, 4) + (4UL, 5) + (5UL, 6) ])) + Assert.True (Vector.exists v (fun x -> x > 5)) + +[] +let ``Vector.exists empty list`` () = + let v = Vector.fromCoordinateList ( + CoordinateList(8UL, + [])) + Assert.False (Vector.exists v (fun x -> x > 0)) + +[] +let ``Vector.exists no matching elements, length is a power of 2`` () = + let v = Vector.fromCoordinateList ( + CoordinateList(8UL, + [ (0UL, 1) + (1UL, 2) + (2UL, 3) + (3UL, 4) + (4UL, 5) + (5UL, 6) + (6UL, 7) + (7UL, 8) ])) + Assert.False (Vector.exists v (fun x -> x > 8)) + +[] +let ``Vector.exists no matching elements, length is not a power of 2`` () = + let v = Vector.fromCoordinateList ( + CoordinateList(6UL, + [ (0UL, 1) + (1UL, 2) + (2UL, 3) + (3UL, 4) + (4UL, 5) + (5UL, 6) ])) + Assert.False (Vector.exists v (fun x -> x > 6)) + +[] +let ``Vector.exists single element matches`` () = + let v = Vector.fromCoordinateList ( + CoordinateList(1UL, + [ (0UL, 1) ])) + Assert.True (Vector.exists v (fun x -> x = 1)) + +[] +let ``Vector.exists single element does not match`` () = + let v = Vector.fromCoordinateList ( + CoordinateList(1UL, + [ (0UL, 1) ])) + Assert.False (Vector.exists v (fun x -> x = 2)) + +[] +let ``Vector.exists all elements match, length is a power of 2`` () = + let v = Vector.fromCoordinateList ( + CoordinateList(8UL, + [ (0UL, 1) + (1UL, 2) + (2UL, 3) + (3UL, 4) + (4UL, 5) + (5UL, 6) + (6UL, 7) + (7UL, 8) ])) + Assert.True (Vector.exists v (fun x -> x > 0)) + +[] +let ``Vector.exists all elements match, length is not a power of 2`` () = + let v = Vector.fromCoordinateList ( + CoordinateList(6UL, + [ (0UL, 1) + (1UL, 2) + (2UL, 3) + (3UL, 4) + (4UL, 5) + (5UL, 6) ])) + Assert.True (Vector.exists v (fun x -> x > 0)) + +[] +let ``Vector.forall the first element not fits, length is a power of 2`` () = + let v = Vector.fromCoordinateList ( + CoordinateList(8UL, + [ (0UL, 1) + (1UL, 2) + (2UL, 3) + (3UL, 4) + (4UL, 5) + (5UL, 6) + (6UL, 7) + (7UL, 8) ])) + Assert.False (Vector.forall v (fun x -> x > 1)) + +[] +let ``Vector.forall the first element not fits, length is not a power of 2`` () = + let v = Vector.fromCoordinateList ( + CoordinateList(6UL, + [ (0UL, 1) + (1UL, 2) + (2UL, 3) + (3UL, 4) + (4UL, 5) + (5UL, 6) ])) + Assert.False (Vector.forall v (fun x -> x > 1)) + +[] +let ``Vector.forall the last item not fits, length is a power of 2`` () = + let v = Vector.fromCoordinateList ( + CoordinateList(8UL, + [ (0UL, 1) + (1UL, 2) + (2UL, 3) + (3UL, 4) + (4UL, 5) + (5UL, 6) + (6UL, 7) + (7UL, 8) ])) + Assert.False (Vector.forall v (fun x -> x < 8)) + +[] +let ``Vector.forall the last item not fits, length is not a power of 2`` () = + let v = Vector.fromCoordinateList ( + CoordinateList(6UL, + [ (0UL, 1) + (1UL, 2) + (2UL, 3) + (3UL, 4) + (4UL, 5) + (5UL, 6) ])) + Assert.False (Vector.forall v (fun x -> x < 6)) + +[] +let ``Vector.forall empty list`` () = + let v = Vector.fromCoordinateList ( + CoordinateList(8UL, + [])) + Assert.True (Vector.forall v (fun x -> x > 0)) + +[] +let ``Vector.forall no matching elements, length is a power of 2`` () = + let v = Vector.fromCoordinateList ( + CoordinateList(8UL, + [ (0UL, 1) + (1UL, 2) + (2UL, 3) + (3UL, 4) + (4UL, 5) + (5UL, 6) + (6UL, 7) + (7UL, 8) ])) + Assert.False (Vector.forall v (fun x -> x > 8)) + +[] +let ``Vector.forall no matching elements, length is not a power of 2`` () = + let v = Vector.fromCoordinateList ( + CoordinateList(6UL, + [ (0UL, 1) + (1UL, 2) + (2UL, 3) + (3UL, 4) + (4UL, 5) + (5UL, 6) ])) + Assert.False (Vector.forall v (fun x -> x > 6)) + +[] +let ``Vector.forall single element matches`` () = + let v = Vector.fromCoordinateList ( + CoordinateList(1UL, + [ (0UL, 1) ])) + Assert.True (Vector.forall v (fun x -> x = 1)) + +[] +let ``Vector.forall single element does not match`` () = + let v = Vector.fromCoordinateList ( + CoordinateList(1UL, + [ (0UL, 1) ])) + Assert.False (Vector.forall v (fun x -> x = 2)) + +[] +let ``Vector.forall all elements match, length is a power of 2`` () = + let v = Vector.fromCoordinateList ( + CoordinateList(8UL, + [ (0UL, 1) + (1UL, 2) + (2UL, 3) + (3UL, 4) + (4UL, 5) + (5UL, 6) + (6UL, 7) + (7UL, 8) ])) + Assert.True (Vector.forall v (fun x -> x > 0)) + +[] +let ``Vector.forall all elements match, length is not a power of 2`` () = + let v = Vector.fromCoordinateList ( + CoordinateList(6UL, + [ (0UL, 1) + (1UL, 2) + (2UL, 3) + (3UL, 4) + (4UL, 5) + (5UL, 6) ])) + Assert.True (Vector.forall v (fun x -> x > 0)) diff --git a/QuadTree/Matrix.fs b/QuadTree/Matrix.fs index 68ea7d7..47bade8 100644 --- a/QuadTree/Matrix.fs +++ b/QuadTree/Matrix.fs @@ -394,3 +394,53 @@ let transpose (matrix: SparseMatrix<_>) = let mask (m1: SparseMatrix<'a>) (m2: SparseMatrix<'b>) f = map2 m1 m2 (fun m1 m2 -> if f m2 then m1 else None) + + +let filter (matrix: SparseMatrix<'a>) (predicate: 'a -> bool) : SparseMatrix<'a> = + let rec inner (prow: uint64) (pcol: uint64) (size: uint64) matrix = + match matrix with + | Node(x1, x2, x3, x4) -> + let halfSize = size / 2UL + + let (nwR, nwC), (neR, neC), (swR, swC), (seR, seC) = + getQuadrantCoords (prow, pcol) (uint64 halfSize) + + let t1, nvals1 = inner nwR nwC halfSize x1 + let t2, nvals2 = inner neR neC halfSize x2 + let t3, nvals3 = inner swR swC halfSize x3 + let t4, nvals4 = inner seR seC halfSize x4 + (mkNode t1 t2 t3 t4), nvals1 + nvals2 + nvals3 + nvals4 + | Leaf(Dummy) -> Leaf(Dummy), 0UL + | Leaf(UserValue(None)) -> Leaf(UserValue(None)), 0UL + | Leaf(UserValue(Some(v))) -> + if predicate v then + Leaf(UserValue(Some v)), (uint64 size) * (uint64 size) * 1UL + else + Leaf(UserValue(None)), 0UL + + let storage, nvals = + inner 0UL 0UL matrix.storage.size matrix.storage.data + + SparseMatrix(matrix.nrows, matrix.ncols, nvals, (Storage(matrix.storage.size, storage))) + +let exists (matrix: SparseMatrix<'a>) (predicate: 'a -> bool) : bool = + let rec inner tree = + match tree with + | Leaf(Dummy) -> false + | Leaf(UserValue(None)) -> false + | Leaf(UserValue(Some(v))) -> predicate v + | Node(nw, ne, sw, se) -> + inner nw || inner ne || inner sw || inner se + + inner matrix.storage.data + +let forall (matrix: SparseMatrix<'a>) (predicate: 'a -> bool) : bool = + let rec inner tree = + match tree with + | Leaf(Dummy) -> true + | Leaf(UserValue(None)) -> true + | Leaf(UserValue(Some(v))) -> predicate v + | Node(nw, ne, sw, se) -> + inner nw && inner ne && inner sw && inner se + + inner matrix.storage.data \ No newline at end of file diff --git a/QuadTree/Vector.fs b/QuadTree/Vector.fs index b7bcb44..6ea0ee4 100644 --- a/QuadTree/Vector.fs +++ b/QuadTree/Vector.fs @@ -353,7 +353,7 @@ let toCoordinateList (vector: SparseVector<'a>) = CoordinateList(length, lst) -let empty length = +let empty (length: uint64) = fromCoordinateList (CoordinateList(length, [])) let foldValues (vector: SparseVector<'a>) (f: 'b -> 'a -> 'b) (state: 'b) = @@ -561,3 +561,41 @@ let scatter | Error x -> Error x) (Ok w) | Error x -> Error Error.InconsistentStructureOfStorages + +let filter (vector: SparseVector<'a>) (predicate: 'a -> bool) : SparseVector<'a> = + let rec inner (size: uint64) vector = + match vector with + | Node(x1, x2) -> + let t1, nvals1 = inner (size / 2UL) x1 + let t2, nvals2 = inner (size / 2UL) x2 + (mkNode t1 t2), nvals1 + nvals2 + | Leaf(Dummy) -> Leaf(Dummy), 0UL + | Leaf(UserValue(None)) -> Leaf(UserValue(None)), 0UL + | Leaf(UserValue(Some(v))) -> + if predicate v then + Leaf(UserValue(Some(v))), (uint64 size) * 1UL + else + Leaf(UserValue(None)), 0UL + + let storage, nvals = inner vector.storage.size vector.storage.data + SparseVector(vector.length, nvals, Storage(vector.storage.size, storage)) + +let exists (vector: SparseVector<'a>) (predicate: 'a -> bool) : bool = + let rec inner vector = + match vector with + | Leaf(Dummy) -> false + | Leaf(UserValue(None)) -> false + | Leaf(UserValue(Some(v))) -> predicate v + | Node(x1, x2) -> inner x1 || inner x2 + + inner vector.storage.data + +let forall (vector: SparseVector<'a>) (predicate: 'a -> bool) : bool = + let rec inner vector = + match vector with + | Leaf(Dummy) -> true + | Leaf(UserValue(None)) -> true + | Leaf(UserValue(Some(v))) -> predicate v + | Node(x1, x2) -> inner x1 && inner x2 + + inner vector.storage.data \ No newline at end of file From bbd6db16db7c8c32da0aabadc95d118e1d305e65 Mon Sep 17 00:00:00 2001 From: Narysev Date: Sat, 13 Jun 2026 11:07:42 +0300 Subject: [PATCH 2/2] fix: formatting --- QuadTree.Benchmark/Exists.fs | 55 ++- QuadTree.Benchmark/Filters.fs | 50 ++- QuadTree.Benchmark/Forall.fs | 57 ++- QuadTree.Tests/Tests.Matrix.fs | 614 +++++++++++++++++++------------- QuadTree.Tests/Tests.Vector.fs | 632 +++++++++++++++++++-------------- QuadTree/Matrix.fs | 10 +- QuadTree/Vector.fs | 4 +- 7 files changed, 854 insertions(+), 568 deletions(-) diff --git a/QuadTree.Benchmark/Exists.fs b/QuadTree.Benchmark/Exists.fs index 2fc08a8..6278303 100644 --- a/QuadTree.Benchmark/Exists.fs +++ b/QuadTree.Benchmark/Exists.fs @@ -12,40 +12,59 @@ type Benchmark() = let mutable denseMat = Unchecked.defaultof> let mutable sparseMat = Unchecked.defaultof> - [] + [] member val N = 0 with get, set [] member this.Setup() = - let denseData = [ for i in 0UL .. uint64 this.N - 1UL -> (i * 1UL, int i % 100) ] + let denseData = + [ for i in 0UL .. uint64 this.N - 1UL -> (i * 1UL, int i % 100) ] + denseVec <- Vector.fromCoordinateList (Vector.CoordinateList(uint64 this.N * 1UL, denseData)) - let sparseData = [ for i in 0UL .. 10UL .. uint64 this.N - 1UL -> (i * 1UL, int i % 100) ] - sparseVec <- Vector.fromCoordinateList (Vector.CoordinateList(uint64 this.N * 1UL, sparseData)) + let sparseData = + [ for i in 0UL .. 10UL .. uint64 this.N - 1UL -> (i * 1UL, int i % 100) ] + + sparseVec <- + Vector.fromCoordinateList (Vector.CoordinateList(uint64 this.N * 1UL, sparseData)) + + let denseMatData = + [ for r in 0UL .. uint64 this.N - 1UL do + for c in 0UL .. uint64 this.N - 1UL do + (r * 1UL, c * 1UL, int (r + c) % 100) ] + + denseMat <- + Matrix.fromCoordinateList ( + Matrix.CoordinateList( + uint64 this.N * 1UL, + uint64 this.N * 1UL, + denseMatData + ) + ) - let denseMatData = [ - for r in 0UL .. uint64 this.N - 1UL do - for c in 0UL .. uint64 this.N - 1UL do - (r * 1UL, c * 1UL, int (r + c) % 100) ] - denseMat <- Matrix.fromCoordinateList (Matrix.CoordinateList(uint64 this.N * 1UL, uint64 this.N * 1UL, denseMatData)) + let sparseMatData = + [ for r in 0UL .. 10UL .. uint64 this.N - 1UL do + for c in 0UL .. 10UL .. uint64 this.N - 1UL do + (r * 1UL, c * 1UL, int (r + c) % 100) ] - let sparseMatData = [ - for r in 0UL .. 10UL .. uint64 this.N - 1UL do - for c in 0UL .. 10UL .. uint64 this.N - 1UL do - (r * 1UL, c * 1UL, int (r + c) % 100) ] - sparseMat <- Matrix.fromCoordinateList (Matrix.CoordinateList(uint64 this.N * 1UL, uint64 this.N * 1UL, sparseMatData)) + sparseMat <- + Matrix.fromCoordinateList ( + Matrix.CoordinateList( + uint64 this.N * 1UL, + uint64 this.N * 1UL, + sparseMatData + ) + ) [] - member this.VectorExistsDense() = - Vector.exists denseVec (fun x -> x < 0) + member this.VectorExistsDense() = Vector.exists denseVec (fun x -> x < 0) [] member this.VectorExistsSparse() = Vector.exists sparseVec (fun x -> x < 0) [] - member this.MatrixExistsDense() = - Matrix.exists denseMat (fun x -> x < 0) + member this.MatrixExistsDense() = Matrix.exists denseMat (fun x -> x < 0) [] member this.MatrixExistsSparse() = diff --git a/QuadTree.Benchmark/Filters.fs b/QuadTree.Benchmark/Filters.fs index 075e339..b733fa4 100644 --- a/QuadTree.Benchmark/Filters.fs +++ b/QuadTree.Benchmark/Filters.fs @@ -11,29 +11,49 @@ type Benchmark() = let mutable denseMat = Unchecked.defaultof> let mutable sparseMat = Unchecked.defaultof> - // [] - [] + [] member val N = 0 with get, set [] member this.Setup() = - let denseData = [ for i in 0UL .. uint64 this.N - 1UL -> (i * 1UL, int i % 100) ] + let denseData = + [ for i in 0UL .. uint64 this.N - 1UL -> (i * 1UL, int i % 100) ] + denseVec <- Vector.fromCoordinateList (Vector.CoordinateList(uint64 this.N * 1UL, denseData)) - let sparseData = [ for i in 0UL .. 10UL .. uint64 this.N - 1UL -> (i * 1UL, int i % 100) ] - sparseVec <- Vector.fromCoordinateList (Vector.CoordinateList(uint64 this.N * 1UL, sparseData)) + let sparseData = + [ for i in 0UL .. 10UL .. uint64 this.N - 1UL -> (i * 1UL, int i % 100) ] + + sparseVec <- + Vector.fromCoordinateList (Vector.CoordinateList(uint64 this.N * 1UL, sparseData)) + + let denseMatData = + [ for r in 0UL .. uint64 this.N - 1UL do + for c in 0UL .. uint64 this.N - 1UL do + (r * 1UL, c * 1UL, int (r + c) % 100) ] + + denseMat <- + Matrix.fromCoordinateList ( + Matrix.CoordinateList( + uint64 this.N * 1UL, + uint64 this.N * 1UL, + denseMatData + ) + ) - let denseMatData = [ - for r in 0UL .. uint64 this.N - 1UL do - for c in 0UL .. uint64 this.N - 1UL do - (r * 1UL, c * 1UL, int (r + c) % 100) ] - denseMat <- Matrix.fromCoordinateList (Matrix.CoordinateList(uint64 this.N * 1UL, uint64 this.N * 1UL, denseMatData)) + let sparseMatData = + [ for r in 0UL .. 10UL .. uint64 this.N - 1UL do + for c in 0UL .. 10UL .. uint64 this.N - 1UL do + (r * 1UL, c * 1UL, int (r + c) % 100) ] - let sparseMatData = [ - for r in 0UL .. 10UL .. uint64 this.N - 1UL do - for c in 0UL .. 10UL .. uint64 this.N - 1UL do - (r * 1UL, c * 1UL, int (r + c) % 100) ] - sparseMat <- Matrix.fromCoordinateList (Matrix.CoordinateList(uint64 this.N * 1UL, uint64 this.N * 1UL, sparseMatData)) + sparseMat <- + Matrix.fromCoordinateList ( + Matrix.CoordinateList( + uint64 this.N * 1UL, + uint64 this.N * 1UL, + sparseMatData + ) + ) [] member this.VectorFilterDense() = diff --git a/QuadTree.Benchmark/Forall.fs b/QuadTree.Benchmark/Forall.fs index 8d8ad46..af40d24 100644 --- a/QuadTree.Benchmark/Forall.fs +++ b/QuadTree.Benchmark/Forall.fs @@ -12,41 +12,62 @@ type Benchmark() = let mutable denseMat = Unchecked.defaultof> let mutable sparseMat = Unchecked.defaultof> - [] + [] member val N = 0 with get, set [] member this.Setup() = - let denseData = [ for i in 0UL .. uint64 this.N - 1UL -> (i * 1UL, int i % 100) ] + let denseData = + [ for i in 0UL .. uint64 this.N - 1UL -> (i * 1UL, int i % 100) ] + denseVec <- Vector.fromCoordinateList (Vector.CoordinateList(uint64 this.N * 1UL, denseData)) - let sparseData = [ for i in 0UL .. 10UL .. uint64 this.N - 1UL -> (i * 1UL, int i % 100) ] - sparseVec <- Vector.fromCoordinateList (Vector.CoordinateList(uint64 this.N * 1UL, sparseData)) + let sparseData = + [ for i in 0UL .. 10UL .. uint64 this.N - 1UL -> (i * 1UL, int i % 100) ] + + sparseVec <- + Vector.fromCoordinateList (Vector.CoordinateList(uint64 this.N * 1UL, sparseData)) + + let denseMatData = + [ for r in 0UL .. uint64 this.N - 1UL do + for c in 0UL .. uint64 this.N - 1UL do + (r * 1UL, c * 1UL, int (r + c) % 100) ] + + denseMat <- + Matrix.fromCoordinateList ( + Matrix.CoordinateList( + uint64 this.N * 1UL, + uint64 this.N * 1UL, + denseMatData + ) + ) - let denseMatData = [ - for r in 0UL .. uint64 this.N - 1UL do - for c in 0UL .. uint64 this.N - 1UL do - (r * 1UL, c * 1UL, int (r + c) % 100) ] - denseMat <- Matrix.fromCoordinateList (Matrix.CoordinateList(uint64 this.N * 1UL, uint64 this.N * 1UL, denseMatData)) + let sparseMatData = + [ for r in 0UL .. 10UL .. uint64 this.N - 1UL do + for c in 0UL .. 10UL .. uint64 this.N - 1UL do + (r * 1UL, c * 1UL, int (r + c) % 100) ] - let sparseMatData = [ - for r in 0UL .. 10UL .. uint64 this.N - 1UL do - for c in 0UL .. 10UL .. uint64 this.N - 1UL do - (r * 1UL, c * 1UL, int (r + c) % 100) ] - sparseMat <- Matrix.fromCoordinateList (Matrix.CoordinateList(uint64 this.N * 1UL, uint64 this.N * 1UL, sparseMatData)) + sparseMat <- + Matrix.fromCoordinateList ( + Matrix.CoordinateList( + uint64 this.N * 1UL, + uint64 this.N * 1UL, + sparseMatData + ) + ) [] member this.VectorForallDense() = - Vector.forall denseVec (fun x -> x > 0) + Vector.forall denseVec (fun x -> x >= 0) [] member this.VectorForallSparse() = - Vector.forall sparseVec (fun x -> x > 0) + Vector.forall sparseVec (fun x -> x >= 0) [] member this.MatrixForallDense() = - Matrix.forall denseMat (fun x -> x > 0) + Matrix.forall denseMat (fun x -> x >= 0) [] member this.MatrixForallSparse() = - Matrix.forall sparseMat (fun x -> x > 0) + Matrix.forall sparseMat (fun x -> x >= 0) diff --git a/QuadTree.Tests/Tests.Matrix.fs b/QuadTree.Tests/Tests.Matrix.fs index b65c53d..e0192a8 100644 --- a/QuadTree.Tests/Tests.Matrix.fs +++ b/QuadTree.Tests/Tests.Matrix.fs @@ -645,354 +645,492 @@ let ``Fold sum`` () = [] let ``Matrix.filter all pass, none changed`` () = - let m = Matrix.fromCoordinateList ( - CoordinateList(4UL, 4UL, - [ (0UL, 0UL, 1) - (0UL, 1UL, 2) - (1UL, 0UL, 3) - (1UL, 1UL, 4) ])) + let m = + Matrix.fromCoordinateList ( + CoordinateList( + 4UL, + 4UL, + [ (0UL, 0UL, 1) + (0UL, 1UL, 2) + (1UL, 0UL, 3) + (1UL, 1UL, 4) ] + ) + ) + let actual = Matrix.filter m (fun x -> x > 0) Assert.Equal(m, actual) [] let ``Matrix.filter none pass, all reset to zero`` () = - let m = Matrix.fromCoordinateList ( - CoordinateList(4UL, 4UL, - [ (0UL, 0UL, 1) - (0UL, 1UL, 2) - (1UL, 0UL, 3) - (1UL, 1UL, 4) ])) - let expected = Matrix.fromCoordinateList ( - CoordinateList(4UL, 4UL, [])) + let m = + Matrix.fromCoordinateList ( + CoordinateList( + 4UL, + 4UL, + [ (0UL, 0UL, 1) + (0UL, 1UL, 2) + (1UL, 0UL, 3) + (1UL, 1UL, 4) ] + ) + ) + + let expected = + Matrix.fromCoordinateList (CoordinateList(4UL, 4UL, [])) + let actual = Matrix.filter m (fun x -> x < 0) Assert.Equal(expected, actual) [] let ``Matrix.filter length is not a power of 2, all reset to zero`` () = - let m = Matrix.fromCoordinateList ( - CoordinateList(3UL, 3UL, - [ (0UL, 0UL, 1) - (0UL, 1UL, 2) - (0UL, 2UL, 3) - (1UL, 0UL, 4) - (1UL, 1UL, 5) - (1UL, 2UL, 6) - (2UL, 0UL, 7) - (2UL, 1UL, 8) - (2UL, 2UL, 9) ])) - let expected = Matrix.fromCoordinateList ( - CoordinateList(3UL, 3UL, [])) + let m = + Matrix.fromCoordinateList ( + CoordinateList( + 3UL, + 3UL, + [ (0UL, 0UL, 1) + (0UL, 1UL, 2) + (0UL, 2UL, 3) + (1UL, 0UL, 4) + (1UL, 1UL, 5) + (1UL, 2UL, 6) + (2UL, 0UL, 7) + (2UL, 1UL, 8) + (2UL, 2UL, 9) ] + ) + ) + + let expected = + Matrix.fromCoordinateList (CoordinateList(3UL, 3UL, [])) + let actual = Matrix.filter m (fun x -> x < 0) Assert.Equal(expected, actual) [] let ``Matrix.filter some pass, odd set to zero`` () = - let m = Matrix.fromCoordinateList ( - CoordinateList(4UL, 4UL, - [ (0UL, 0UL, 1) - (0UL, 1UL, 2) - (0UL, 2UL, 3) - (0UL, 3UL, 4) - (1UL, 0UL, 5) - (1UL, 1UL, 6) - (1UL, 2UL, 7) - (1UL, 3UL, 8) - (2UL, 0UL, 9) - (2UL, 1UL, 10) - (2UL, 2UL, 11) - (2UL, 3UL, 12) - (3UL, 0UL, 13) - (3UL, 1UL, 14) - (3UL, 2UL, 15) - (3UL, 3UL, 16) ])) - let expected = Matrix.fromCoordinateList ( - CoordinateList(4UL, 4UL, - [ (0UL, 1UL, 2) - (0UL, 3UL, 4) - (1UL, 1UL, 6) - (1UL, 3UL, 8) - (2UL, 1UL, 10) - (2UL, 3UL, 12) - (3UL, 1UL, 14) - (3UL, 3UL, 16) ])) + let m = + Matrix.fromCoordinateList ( + CoordinateList( + 4UL, + 4UL, + [ (0UL, 0UL, 1) + (0UL, 1UL, 2) + (0UL, 2UL, 3) + (0UL, 3UL, 4) + (1UL, 0UL, 5) + (1UL, 1UL, 6) + (1UL, 2UL, 7) + (1UL, 3UL, 8) + (2UL, 0UL, 9) + (2UL, 1UL, 10) + (2UL, 2UL, 11) + (2UL, 3UL, 12) + (3UL, 0UL, 13) + (3UL, 1UL, 14) + (3UL, 2UL, 15) + (3UL, 3UL, 16) ] + ) + ) + + let expected = + Matrix.fromCoordinateList ( + CoordinateList( + 4UL, + 4UL, + [ (0UL, 1UL, 2) + (0UL, 3UL, 4) + (1UL, 1UL, 6) + (1UL, 3UL, 8) + (2UL, 1UL, 10) + (2UL, 3UL, 12) + (3UL, 1UL, 14) + (3UL, 3UL, 16) ] + ) + ) + let actual = Matrix.filter m (fun x -> x % 2 = 0) Assert.Equal(expected, actual) [] let ``Matrix.filter length is not a power of 2, not all reset to zero`` () = - let m = Matrix.fromCoordinateList ( - CoordinateList(3UL, 3UL, - [ (0UL, 0UL, 1) - (0UL, 1UL, 2) - (0UL, 2UL, 3) - (1UL, 0UL, 4) - (1UL, 1UL, 5) - (1UL, 2UL, 6) - (2UL, 0UL, 7) - (2UL, 1UL, 8) - (2UL, 2UL, 9) ])) - let expected = Matrix.fromCoordinateList ( - CoordinateList(3UL, 3UL, - [ (0UL, 1UL, 2) - (1UL, 0UL, 4) - (1UL, 2UL, 6) - (2UL, 1UL, 8) ])) + let m = + Matrix.fromCoordinateList ( + CoordinateList( + 3UL, + 3UL, + [ (0UL, 0UL, 1) + (0UL, 1UL, 2) + (0UL, 2UL, 3) + (1UL, 0UL, 4) + (1UL, 1UL, 5) + (1UL, 2UL, 6) + (2UL, 0UL, 7) + (2UL, 1UL, 8) + (2UL, 2UL, 9) ] + ) + ) + + let expected = + Matrix.fromCoordinateList ( + CoordinateList( + 3UL, + 3UL, + [ (0UL, 1UL, 2) + (1UL, 0UL, 4) + (1UL, 2UL, 6) + (2UL, 1UL, 8) ] + ) + ) + let actual = Matrix.filter m (fun x -> x % 2 = 0) Assert.Equal(expected, actual) [] let ``Matrix.filter none pass, length is not a power of 2, none changed`` () = - let m = Matrix.fromCoordinateList ( - CoordinateList(3UL, 3UL, [])) + let m = Matrix.fromCoordinateList (CoordinateList(3UL, 3UL, [])) let actual = Matrix.filter m (fun x -> x > 0) Assert.Equal(m, actual) [] let ``Matrix.filter none pass, length is a power of 2, none changed`` () = - let m = Matrix.fromCoordinateList ( - CoordinateList(4UL, 4UL, [])) + let m = Matrix.fromCoordinateList (CoordinateList(4UL, 4UL, [])) let actual = Matrix.filter m (fun x -> x > 0) Assert.Equal(m, actual) [] let ``Matrix.filter single element, passes`` () = - let m = Matrix.fromCoordinateList ( - CoordinateList(1UL, 1UL, - [ (0UL, 0UL, 1) ])) - let expected = Matrix.fromCoordinateList ( - CoordinateList(1UL, 1UL, - [ (0UL, 0UL, 1) ])) + let m = + Matrix.fromCoordinateList (CoordinateList(1UL, 1UL, [ (0UL, 0UL, 1) ])) + + let expected = + Matrix.fromCoordinateList (CoordinateList(1UL, 1UL, [ (0UL, 0UL, 1) ])) + let actual = Matrix.filter m (fun x -> x > 0) Assert.Equal(expected, actual) [] let ``Matrix.filter single element, fails`` () = - let m = Matrix.fromCoordinateList ( - CoordinateList(1UL, 1UL, - [ (0UL, 0UL, 1) ])) - let expected = Matrix.fromCoordinateList ( - CoordinateList(1UL, 1UL, [])) + let m = + Matrix.fromCoordinateList (CoordinateList(1UL, 1UL, [ (0UL, 0UL, 1) ])) + + let expected = + Matrix.fromCoordinateList (CoordinateList(1UL, 1UL, [])) + let actual = Matrix.filter m (fun x -> x < 0) Assert.Equal(expected, actual) [] let ``Matrix.exists the first element fits, length is a power of 2`` () = - let m = Matrix.fromCoordinateList ( - CoordinateList(4UL, 4UL, - [ (0UL, 0UL, 1) - (0UL, 1UL, 2) - (1UL, 0UL, 3) - (1UL, 1UL, 4) ])) - Assert.True (Matrix.exists m (fun x -> x = 1)) - Assert.False (Matrix.exists m (fun x -> x = 10)) + let m = + Matrix.fromCoordinateList ( + CoordinateList( + 4UL, + 4UL, + [ (0UL, 0UL, 1) + (0UL, 1UL, 2) + (1UL, 0UL, 3) + (1UL, 1UL, 4) ] + ) + ) + + Assert.True(Matrix.exists m (fun x -> x = 1)) + Assert.False(Matrix.exists m (fun x -> x = 10)) [] let ``Matrix.exists the first element fits, length is not a power of 2`` () = - let m = Matrix.fromCoordinateList ( - CoordinateList(3UL, 3UL, - [ (0UL, 0UL, 1) - (0UL, 1UL, 2) - (0UL, 2UL, 3) - (1UL, 0UL, 4) - (1UL, 1UL, 5) - (1UL, 2UL, 6) ])) - Assert.True (Matrix.exists m (fun x -> x = 1)) + let m = + Matrix.fromCoordinateList ( + CoordinateList( + 3UL, + 3UL, + [ (0UL, 0UL, 1) + (0UL, 1UL, 2) + (0UL, 2UL, 3) + (1UL, 0UL, 4) + (1UL, 1UL, 5) + (1UL, 2UL, 6) ] + ) + ) + + Assert.True(Matrix.exists m (fun x -> x = 1)) [] let ``Matrix.exists no element fits, length is a power of 2`` () = - let m = Matrix.fromCoordinateList ( - CoordinateList(4UL, 4UL, - [ (0UL, 0UL, 1) - (0UL, 1UL, 2) - (1UL, 0UL, 3) - (1UL, 1UL, 4) ])) - Assert.False (Matrix.exists m (fun x -> x = 10)) + let m = + Matrix.fromCoordinateList ( + CoordinateList( + 4UL, + 4UL, + [ (0UL, 0UL, 1) + (0UL, 1UL, 2) + (1UL, 0UL, 3) + (1UL, 1UL, 4) ] + ) + ) + + Assert.False(Matrix.exists m (fun x -> x = 10)) [] let ``Matrix.exists no element fits, length is not a power of 2`` () = - let m = Matrix.fromCoordinateList ( - CoordinateList(3UL, 3UL, - [ (0UL, 0UL, 1) - (0UL, 1UL, 2) - (0UL, 2UL, 3) - (1UL, 0UL, 4) - (1UL, 1UL, 5) - (1UL, 2UL, 6) ])) - Assert.False (Matrix.exists m (fun x -> x = 10)) + let m = + Matrix.fromCoordinateList ( + CoordinateList( + 3UL, + 3UL, + [ (0UL, 0UL, 1) + (0UL, 1UL, 2) + (0UL, 2UL, 3) + (1UL, 0UL, 4) + (1UL, 1UL, 5) + (1UL, 2UL, 6) ] + ) + ) + + Assert.False(Matrix.exists m (fun x -> x = 10)) [] let ``Matrix.exists empty matrix`` () = - let m = Matrix.fromCoordinateList ( - CoordinateList(3UL, 3UL, [])) - Assert.False (Matrix.exists m (fun x -> x = 1)) + let m = Matrix.fromCoordinateList (CoordinateList(3UL, 3UL, [])) + Assert.False(Matrix.exists m (fun x -> x = 1)) [] let ``Matrix.exists single element, fits`` () = - let m = Matrix.fromCoordinateList ( - CoordinateList(1UL, 1UL, - [ (0UL, 0UL, 1) ])) - Assert.True (Matrix.exists m (fun x -> x = 1)) + let m = + Matrix.fromCoordinateList (CoordinateList(1UL, 1UL, [ (0UL, 0UL, 1) ])) + + Assert.True(Matrix.exists m (fun x -> x = 1)) [] let ``Matrix.exists single element, does not fit`` () = - let m = Matrix.fromCoordinateList ( - CoordinateList(1UL, 1UL, - [ (0UL, 0UL, 1) ])) - Assert.False (Matrix.exists m (fun x -> x = 10)) + let m = + Matrix.fromCoordinateList (CoordinateList(1UL, 1UL, [ (0UL, 0UL, 1) ])) + + Assert.False(Matrix.exists m (fun x -> x = 10)) [] let ``Matrix.exists all elements fit,length is a power of 2`` () = - let m = Matrix.fromCoordinateList ( - CoordinateList(4UL, 4UL, - [ (0UL, 0UL, 1) - (0UL, 1UL, 2) - (1UL, 0UL, 3) - (1UL, 1UL, 4) ])) - Assert.True (Matrix.exists m (fun x -> x > 0)) + let m = + Matrix.fromCoordinateList ( + CoordinateList( + 4UL, + 4UL, + [ (0UL, 0UL, 1) + (0UL, 1UL, 2) + (1UL, 0UL, 3) + (1UL, 1UL, 4) ] + ) + ) + + Assert.True(Matrix.exists m (fun x -> x > 0)) [] let ``Matrix.exists all elements fit,length is not a power of 2`` () = - let m = Matrix.fromCoordinateList ( - CoordinateList(3UL, 3UL, - [ (0UL, 0UL, 1) - (0UL, 1UL, 2) - (0UL, 2UL, 3) - (1UL, 0UL, 4) - (1UL, 1UL, 5) - (1UL, 2UL, 6) ])) - Assert.True (Matrix.exists m (fun x -> x > 0)) + let m = + Matrix.fromCoordinateList ( + CoordinateList( + 3UL, + 3UL, + [ (0UL, 0UL, 1) + (0UL, 1UL, 2) + (0UL, 2UL, 3) + (1UL, 0UL, 4) + (1UL, 1UL, 5) + (1UL, 2UL, 6) ] + ) + ) + + Assert.True(Matrix.exists m (fun x -> x > 0)) + [] let ``Matrix.exists some elements fit, length is a power of 2`` () = - let m = Matrix.fromCoordinateList ( - CoordinateList(4UL, 4UL, - [ (0UL, 0UL, 1) - (0UL, 1UL, 2) - (1UL, 0UL, 3) - (1UL, 1UL, 4) ])) - Assert.True (Matrix.exists m (fun x -> x = 2 || x = 10)) + let m = + Matrix.fromCoordinateList ( + CoordinateList( + 4UL, + 4UL, + [ (0UL, 0UL, 1) + (0UL, 1UL, 2) + (1UL, 0UL, 3) + (1UL, 1UL, 4) ] + ) + ) + + Assert.True(Matrix.exists m (fun x -> x = 2 || x = 10)) [] let ``Matrix.exists some elements fit, length is not a power of 2`` () = - let m = Matrix.fromCoordinateList ( - CoordinateList(3UL, 3UL, - [ (0UL, 0UL, 1) - (0UL, 1UL, 2) - (0UL, 2UL, 3) - (1UL, 0UL, 4) - (1UL, 1UL, 5) - (1UL, 2UL, 6) ])) - Assert.True (Matrix.exists m (fun x -> x = 2 || x = 10)) + let m = + Matrix.fromCoordinateList ( + CoordinateList( + 3UL, + 3UL, + [ (0UL, 0UL, 1) + (0UL, 1UL, 2) + (0UL, 2UL, 3) + (1UL, 0UL, 4) + (1UL, 1UL, 5) + (1UL, 2UL, 6) ] + ) + ) + + Assert.True(Matrix.exists m (fun x -> x = 2 || x = 10)) [] let ``Matrix.forall the first element fits, length is a power of 2`` () = - let m = Matrix.fromCoordinateList ( - CoordinateList(4UL, 4UL, - [ (0UL, 0UL, 1) - (0UL, 1UL, 2) - (1UL, 0UL, 3) - (1UL, 1UL, 4) ])) - Assert.True (Matrix.forall m (fun x -> x > 0)) + let m = + Matrix.fromCoordinateList ( + CoordinateList( + 4UL, + 4UL, + [ (0UL, 0UL, 1) + (0UL, 1UL, 2) + (1UL, 0UL, 3) + (1UL, 1UL, 4) ] + ) + ) + + Assert.True(Matrix.forall m (fun x -> x > 0)) [] let ``Matrix.forall the first element fits, length is not a power of 2`` () = - let m = Matrix.fromCoordinateList ( - CoordinateList(3UL, 3UL, - [ (0UL, 0UL, 1) - (0UL, 1UL, 2) - (0UL, 2UL, 3) - (1UL, 0UL, 4) - (1UL, 1UL, 5) - (1UL, 2UL, 6) ])) - Assert.True (Matrix.forall m (fun x -> x > 0)) + let m = + Matrix.fromCoordinateList ( + CoordinateList( + 3UL, + 3UL, + [ (0UL, 0UL, 1) + (0UL, 1UL, 2) + (0UL, 2UL, 3) + (1UL, 0UL, 4) + (1UL, 1UL, 5) + (1UL, 2UL, 6) ] + ) + ) + + Assert.True(Matrix.forall m (fun x -> x > 0)) [] let ``Matrix.forall no element fits, length is a power of 2`` () = - let m = Matrix.fromCoordinateList ( - CoordinateList(4UL, 4UL, - [ (0UL, 0UL, 1) - (0UL, 1UL, 2) - (1UL, 0UL, 3) - (1UL, 1UL, 4) ])) - Assert.False (Matrix.forall m (fun x -> x > 10)) + let m = + Matrix.fromCoordinateList ( + CoordinateList( + 4UL, + 4UL, + [ (0UL, 0UL, 1) + (0UL, 1UL, 2) + (1UL, 0UL, 3) + (1UL, 1UL, 4) ] + ) + ) + + Assert.False(Matrix.forall m (fun x -> x > 10)) [] let ``Matrix.forall no element fits, length is not a power of 2`` () = - let m = Matrix.fromCoordinateList ( - CoordinateList(3UL, 3UL, - [ (0UL, 0UL, 1) - (0UL, 1UL, 2) - (0UL, 2UL, 3) - (1UL, 0UL, 4) - (1UL, 1UL, 5) - (1UL, 2UL, 6) ])) - Assert.False (Matrix.forall m (fun x -> x > 10)) + let m = + Matrix.fromCoordinateList ( + CoordinateList( + 3UL, + 3UL, + [ (0UL, 0UL, 1) + (0UL, 1UL, 2) + (0UL, 2UL, 3) + (1UL, 0UL, 4) + (1UL, 1UL, 5) + (1UL, 2UL, 6) ] + ) + ) + + Assert.False(Matrix.forall m (fun x -> x > 10)) [] let ``Matrix.forall all elements fit,length is a power of 2`` () = - let m = Matrix.fromCoordinateList ( - CoordinateList(4UL, 4UL, - [ (0UL, 0UL, 1) - (0UL, 1UL, 2) - (1UL, 0UL, 3) - (1UL, 1UL, 4) ])) - Assert.True (Matrix.forall m (fun x -> x > 0)) + let m = + Matrix.fromCoordinateList ( + CoordinateList( + 4UL, + 4UL, + [ (0UL, 0UL, 1) + (0UL, 1UL, 2) + (1UL, 0UL, 3) + (1UL, 1UL, 4) ] + ) + ) + + Assert.True(Matrix.forall m (fun x -> x > 0)) [] let ``Matrix.forall all elements fit,length is not a power of 2`` () = - let m = Matrix.fromCoordinateList ( - CoordinateList(3UL, 3UL, - [ (0UL, 0UL, 1) - (0UL, 1UL, 2) - (0UL, 2UL, 3) - (1UL, 0UL, 4) - (1UL, 1UL, 5) - (1UL, 2UL, 6) ])) - Assert.True (Matrix.forall m (fun x -> x > 0)) + let m = + Matrix.fromCoordinateList ( + CoordinateList( + 3UL, + 3UL, + [ (0UL, 0UL, 1) + (0UL, 1UL, 2) + (0UL, 2UL, 3) + (1UL, 0UL, 4) + (1UL, 1UL, 5) + (1UL, 2UL, 6) ] + ) + ) + + Assert.True(Matrix.forall m (fun x -> x > 0)) [] let ``Matrix.forall some elements fit, length is a power of 2`` () = - let m = Matrix.fromCoordinateList ( - CoordinateList(4UL, 4UL, - [ (0UL, 0UL, 1) - (0UL, 1UL, 2) - (1UL, 0UL, 3) - (1UL, 1UL, 4) ])) - Assert.False (Matrix.forall m (fun x -> x = 2 || x = 10)) + let m = + Matrix.fromCoordinateList ( + CoordinateList( + 4UL, + 4UL, + [ (0UL, 0UL, 1) + (0UL, 1UL, 2) + (1UL, 0UL, 3) + (1UL, 1UL, 4) ] + ) + ) + + Assert.False(Matrix.forall m (fun x -> x = 2 || x = 10)) [] let ``Matrix.forall some elements fit, length is not a power of 2`` () = - let m = Matrix.fromCoordinateList ( - CoordinateList(3UL, 3UL, - [ (0UL, 0UL, 1) - (0UL, 1UL, 2) - (0UL, 2UL, 3) - (1UL, 0UL, 4) - (1UL, 1UL, 5) - (1UL, 2UL, 6) ])) - Assert.False (Matrix.forall m (fun x -> x = 2 || x = 10)) + let m = + Matrix.fromCoordinateList ( + CoordinateList( + 3UL, + 3UL, + [ (0UL, 0UL, 1) + (0UL, 1UL, 2) + (0UL, 2UL, 3) + (1UL, 0UL, 4) + (1UL, 1UL, 5) + (1UL, 2UL, 6) ] + ) + ) + + Assert.False(Matrix.forall m (fun x -> x = 2 || x = 10)) [] let ``Matrix.forall empty matrix`` () = - let m = Matrix.fromCoordinateList ( - CoordinateList(3UL, 3UL, [])) - Assert.True (Matrix.forall m (fun x -> x = 1)) + let m = Matrix.fromCoordinateList (CoordinateList(3UL, 3UL, [])) + Assert.True(Matrix.forall m (fun x -> x = 1)) [] let ``Matrix.forall single element, fits`` () = - let m = Matrix.fromCoordinateList ( - CoordinateList(1UL, 1UL, - [ (0UL, 0UL, 1) ])) - Assert.True (Matrix.forall m (fun x -> x = 1)) + let m = + Matrix.fromCoordinateList (CoordinateList(1UL, 1UL, [ (0UL, 0UL, 1) ])) + + Assert.True(Matrix.forall m (fun x -> x = 1)) [] let ``Matrix.forall single element, does not fit`` () = - let m = Matrix.fromCoordinateList ( - CoordinateList(1UL, 1UL, - [ (0UL, 0UL, 1) ])) - Assert.False (Matrix.forall m (fun x -> x = 10)) \ No newline at end of file + let m = + Matrix.fromCoordinateList (CoordinateList(1UL, 1UL, [ (0UL, 0UL, 1) ])) + + Assert.False(Matrix.forall m (fun x -> x = 10)) diff --git a/QuadTree.Tests/Tests.Vector.fs b/QuadTree.Tests/Tests.Vector.fs index 6ef2cbd..9349656 100644 --- a/QuadTree.Tests/Tests.Vector.fs +++ b/QuadTree.Tests/Tests.Vector.fs @@ -907,382 +907,472 @@ let ``Init vector`` () = [] let ``Vector.filter all pass, none changed`` () = - let v = Vector.fromCoordinateList ( - CoordinateList(8UL, - [ (0UL, 1) - (1UL, 2) - (2UL, 3) - (3UL, 4) - (4UL, 5) - (5UL, 6) - (6UL, 7) - (7UL, 8) ])) + let v = + Vector.fromCoordinateList ( + CoordinateList( + 8UL, + [ (0UL, 1) + (1UL, 2) + (2UL, 3) + (3UL, 4) + (4UL, 5) + (5UL, 6) + (6UL, 7) + (7UL, 8) ] + ) + ) + let actual = Vector.filter v (fun x -> x > 0) Assert.Equal(v, actual) [] let ``Vector.filter none pass, all reset to zero`` () = - let v = Vector.fromCoordinateList ( - CoordinateList(8UL, - [ (0UL, 1) - (1UL, 2) - (2UL, 3) - (3UL, 4) - (4UL, 5) - (5UL, 6) - (6UL, 7) - (7UL, 8) ])) - let expected = - Vector.empty 8UL + let v = + Vector.fromCoordinateList ( + CoordinateList( + 8UL, + [ (0UL, 1) + (1UL, 2) + (2UL, 3) + (3UL, 4) + (4UL, 5) + (5UL, 6) + (6UL, 7) + (7UL, 8) ] + ) + ) + + let expected = Vector.empty 8UL let actual = Vector.filter v (fun x -> x < 0) - + Assert.Equal(expected, actual) [] let ``Vector.filter length is not a power of 2, all reset to zero`` () = - let v = Vector.fromCoordinateList ( - CoordinateList(6UL, - [ (0UL, 1) - (1UL, 2) - (2UL, 3) - (3UL, 4) - (4UL, 5) - (5UL, 6) ])) - let expected = - Vector.empty 6UL + let v = + Vector.fromCoordinateList ( + CoordinateList( + 6UL, + [ (0UL, 1) + (1UL, 2) + (2UL, 3) + (3UL, 4) + (4UL, 5) + (5UL, 6) ] + ) + ) + + let expected = Vector.empty 6UL let actual = Vector.filter v (fun x -> x < 0) Assert.Equal(expected, actual) [] let ``Vector.filter some pass, odd set to zero`` () = - let v = Vector.fromCoordinateList ( - CoordinateList(8UL, - [ (0UL, 1) - (1UL, 2) - (2UL, 3) - (3UL, 4) - (4UL, 5) - (5UL, 6) - (6UL, 7) - (7UL, 8) ])) - let expected = + let v = Vector.fromCoordinateList ( - CoordinateList(8UL, - [ (1UL, 2) + CoordinateList( + 8UL, + [ (0UL, 1) + (1UL, 2) + (2UL, 3) (3UL, 4) + (4UL, 5) (5UL, 6) - (7UL, 8) ])) + (6UL, 7) + (7UL, 8) ] + ) + ) + + let expected = + Vector.fromCoordinateList ( + CoordinateList(8UL, [ (1UL, 2); (3UL, 4); (5UL, 6); (7UL, 8) ]) + ) + let actual = Vector.filter v (fun x -> ((x % 2) = 0)) - + Assert.Equal(expected, actual) [] let ``Vector.filter length is not a power of 2, not all reset to zero`` () = - let v = Vector.fromCoordinateList ( - CoordinateList(6UL, - [ (0UL, 1) - (1UL, 2) - (2UL, 3) - (3UL, 4) - (4UL, 5) - (5UL, 6) ])) - let expected = + let v = Vector.fromCoordinateList ( - CoordinateList(6UL, - [ (1UL, 2) + CoordinateList( + 6UL, + [ (0UL, 1) + (1UL, 2) + (2UL, 3) (3UL, 4) - (5UL, 6) ])) + (4UL, 5) + (5UL, 6) ] + ) + ) + + let expected = + Vector.fromCoordinateList ( + CoordinateList(6UL, [ (1UL, 2); (3UL, 4); (5UL, 6) ]) + ) + let actual = Vector.filter v (fun x -> ((x % 2) = 0)) - + Assert.Equal(expected, actual) [] let ``Vector.filter none pass, length is not a power of 2, none changed`` () = - let v = Vector.fromCoordinateList ( - CoordinateList(6UL, - [])) + let v = Vector.fromCoordinateList (CoordinateList(6UL, [])) let actual = Vector.filter v (fun x -> x > 0) Assert.Equal(v, actual) - + [] let ``Vector.filter none pass, length is a power of 2, none changed`` () = - let v = Vector.fromCoordinateList ( - CoordinateList(8UL, - [])) + let v = Vector.fromCoordinateList (CoordinateList(8UL, [])) let actual = Vector.filter v (fun x -> x > 0) Assert.Equal(v, actual) - + [] let ``Vector.filter single element, passes`` () = - let v = Vector.fromCoordinateList ( - CoordinateList(1UL, - [ (0UL, 1) ])) + let v = + Vector.fromCoordinateList (CoordinateList(1UL, [ (0UL, 1) ])) + let expected = - Vector.fromCoordinateList ( - CoordinateList(1UL, - [ (0UL, 1) ])) + Vector.fromCoordinateList (CoordinateList(1UL, [ (0UL, 1) ])) + let actual = Vector.filter v (fun x -> x > 0) - + Assert.Equal(expected, actual) [] let ``Vector.filter single element, fails`` () = - let v = Vector.fromCoordinateList ( - CoordinateList(1UL, - [ (0UL, 1) ])) - let expected = - Vector.empty 1UL + let v = + Vector.fromCoordinateList (CoordinateList(1UL, [ (0UL, 1) ])) + + let expected = Vector.empty 1UL let actual = Vector.filter v (fun x -> x < 0) Assert.Equal(expected, actual) [] let ``Vector.exists the first element fits, length is a power of 2`` () = - let v = Vector.fromCoordinateList ( - CoordinateList(8UL, - [ (0UL, 1) - (1UL, 2) - (2UL, 3) - (3UL, 4) - (4UL, 5) - (5UL, 6) - (6UL, 7) - (7UL, 8) ])) - Assert.True (Vector.exists v (fun x -> x > 0)) + let v = + Vector.fromCoordinateList ( + CoordinateList( + 8UL, + [ (0UL, 1) + (1UL, 2) + (2UL, 3) + (3UL, 4) + (4UL, 5) + (5UL, 6) + (6UL, 7) + (7UL, 8) ] + ) + ) + + Assert.True(Vector.exists v (fun x -> x > 0)) [] let ``Vector.exists the first element fits, length is not a power of 2`` () = - let v = Vector.fromCoordinateList ( - CoordinateList(6UL, - [ (0UL, 1) - (1UL, 2) - (2UL, 3) - (3UL, 4) - (4UL, 5) - (5UL, 6) ])) - Assert.True (Vector.exists v (fun x -> x > 0)) + let v = + Vector.fromCoordinateList ( + CoordinateList( + 6UL, + [ (0UL, 1) + (1UL, 2) + (2UL, 3) + (3UL, 4) + (4UL, 5) + (5UL, 6) ] + ) + ) + + Assert.True(Vector.exists v (fun x -> x > 0)) [] let ``Vector.exists the last item fits, length is a power of 2`` () = - let v = Vector.fromCoordinateList ( - CoordinateList(8UL, - [ (0UL, 1) - (1UL, 2) - (2UL, 3) - (3UL, 4) - (4UL, 5) - (5UL, 6) - (6UL, 7) - (7UL, 8) ])) - Assert.True (Vector.exists v (fun x -> x > 7)) + let v = + Vector.fromCoordinateList ( + CoordinateList( + 8UL, + [ (0UL, 1) + (1UL, 2) + (2UL, 3) + (3UL, 4) + (4UL, 5) + (5UL, 6) + (6UL, 7) + (7UL, 8) ] + ) + ) + + Assert.True(Vector.exists v (fun x -> x > 7)) [] let ``Vector.exists the last item fits, length is not a power of 2`` () = - let v = Vector.fromCoordinateList ( - CoordinateList(6UL, - [ (0UL, 1) - (1UL, 2) - (2UL, 3) - (3UL, 4) - (4UL, 5) - (5UL, 6) ])) - Assert.True (Vector.exists v (fun x -> x > 5)) + let v = + Vector.fromCoordinateList ( + CoordinateList( + 6UL, + [ (0UL, 1) + (1UL, 2) + (2UL, 3) + (3UL, 4) + (4UL, 5) + (5UL, 6) ] + ) + ) + + Assert.True(Vector.exists v (fun x -> x > 5)) [] let ``Vector.exists empty list`` () = - let v = Vector.fromCoordinateList ( - CoordinateList(8UL, - [])) - Assert.False (Vector.exists v (fun x -> x > 0)) + let v = Vector.fromCoordinateList (CoordinateList(8UL, [])) + Assert.False(Vector.exists v (fun x -> x > 0)) [] let ``Vector.exists no matching elements, length is a power of 2`` () = - let v = Vector.fromCoordinateList ( - CoordinateList(8UL, - [ (0UL, 1) - (1UL, 2) - (2UL, 3) - (3UL, 4) - (4UL, 5) - (5UL, 6) - (6UL, 7) - (7UL, 8) ])) - Assert.False (Vector.exists v (fun x -> x > 8)) + let v = + Vector.fromCoordinateList ( + CoordinateList( + 8UL, + [ (0UL, 1) + (1UL, 2) + (2UL, 3) + (3UL, 4) + (4UL, 5) + (5UL, 6) + (6UL, 7) + (7UL, 8) ] + ) + ) + + Assert.False(Vector.exists v (fun x -> x > 8)) [] let ``Vector.exists no matching elements, length is not a power of 2`` () = - let v = Vector.fromCoordinateList ( - CoordinateList(6UL, - [ (0UL, 1) - (1UL, 2) - (2UL, 3) - (3UL, 4) - (4UL, 5) - (5UL, 6) ])) - Assert.False (Vector.exists v (fun x -> x > 6)) + let v = + Vector.fromCoordinateList ( + CoordinateList( + 6UL, + [ (0UL, 1) + (1UL, 2) + (2UL, 3) + (3UL, 4) + (4UL, 5) + (5UL, 6) ] + ) + ) + + Assert.False(Vector.exists v (fun x -> x > 6)) [] let ``Vector.exists single element matches`` () = - let v = Vector.fromCoordinateList ( - CoordinateList(1UL, - [ (0UL, 1) ])) - Assert.True (Vector.exists v (fun x -> x = 1)) + let v = + Vector.fromCoordinateList (CoordinateList(1UL, [ (0UL, 1) ])) + + Assert.True(Vector.exists v (fun x -> x = 1)) [] let ``Vector.exists single element does not match`` () = - let v = Vector.fromCoordinateList ( - CoordinateList(1UL, - [ (0UL, 1) ])) - Assert.False (Vector.exists v (fun x -> x = 2)) + let v = + Vector.fromCoordinateList (CoordinateList(1UL, [ (0UL, 1) ])) + + Assert.False(Vector.exists v (fun x -> x = 2)) [] let ``Vector.exists all elements match, length is a power of 2`` () = - let v = Vector.fromCoordinateList ( - CoordinateList(8UL, - [ (0UL, 1) - (1UL, 2) - (2UL, 3) - (3UL, 4) - (4UL, 5) - (5UL, 6) - (6UL, 7) - (7UL, 8) ])) - Assert.True (Vector.exists v (fun x -> x > 0)) - -[] -let ``Vector.exists all elements match, length is not a power of 2`` () = - let v = Vector.fromCoordinateList ( - CoordinateList(6UL, - [ (0UL, 1) - (1UL, 2) - (2UL, 3) - (3UL, 4) - (4UL, 5) - (5UL, 6) ])) - Assert.True (Vector.exists v (fun x -> x > 0)) + let v = + Vector.fromCoordinateList ( + CoordinateList( + 8UL, + [ (0UL, 1) + (1UL, 2) + (2UL, 3) + (3UL, 4) + (4UL, 5) + (5UL, 6) + (6UL, 7) + (7UL, 8) ] + ) + ) + + Assert.True(Vector.exists v (fun x -> x > 0)) + +[] +let ``Vector.exists all elements match, length is not a power of 2`` () = + let v = + Vector.fromCoordinateList ( + CoordinateList( + 6UL, + [ (0UL, 1) + (1UL, 2) + (2UL, 3) + (3UL, 4) + (4UL, 5) + (5UL, 6) ] + ) + ) + + Assert.True(Vector.exists v (fun x -> x > 0)) [] let ``Vector.forall the first element not fits, length is a power of 2`` () = - let v = Vector.fromCoordinateList ( - CoordinateList(8UL, - [ (0UL, 1) - (1UL, 2) - (2UL, 3) - (3UL, 4) - (4UL, 5) - (5UL, 6) - (6UL, 7) - (7UL, 8) ])) - Assert.False (Vector.forall v (fun x -> x > 1)) + let v = + Vector.fromCoordinateList ( + CoordinateList( + 8UL, + [ (0UL, 1) + (1UL, 2) + (2UL, 3) + (3UL, 4) + (4UL, 5) + (5UL, 6) + (6UL, 7) + (7UL, 8) ] + ) + ) + + Assert.False(Vector.forall v (fun x -> x > 1)) [] let ``Vector.forall the first element not fits, length is not a power of 2`` () = - let v = Vector.fromCoordinateList ( - CoordinateList(6UL, - [ (0UL, 1) - (1UL, 2) - (2UL, 3) - (3UL, 4) - (4UL, 5) - (5UL, 6) ])) - Assert.False (Vector.forall v (fun x -> x > 1)) + let v = + Vector.fromCoordinateList ( + CoordinateList( + 6UL, + [ (0UL, 1) + (1UL, 2) + (2UL, 3) + (3UL, 4) + (4UL, 5) + (5UL, 6) ] + ) + ) + + Assert.False(Vector.forall v (fun x -> x > 1)) [] let ``Vector.forall the last item not fits, length is a power of 2`` () = - let v = Vector.fromCoordinateList ( - CoordinateList(8UL, - [ (0UL, 1) - (1UL, 2) - (2UL, 3) - (3UL, 4) - (4UL, 5) - (5UL, 6) - (6UL, 7) - (7UL, 8) ])) - Assert.False (Vector.forall v (fun x -> x < 8)) + let v = + Vector.fromCoordinateList ( + CoordinateList( + 8UL, + [ (0UL, 1) + (1UL, 2) + (2UL, 3) + (3UL, 4) + (4UL, 5) + (5UL, 6) + (6UL, 7) + (7UL, 8) ] + ) + ) + + Assert.False(Vector.forall v (fun x -> x < 8)) [] let ``Vector.forall the last item not fits, length is not a power of 2`` () = - let v = Vector.fromCoordinateList ( - CoordinateList(6UL, - [ (0UL, 1) - (1UL, 2) - (2UL, 3) - (3UL, 4) - (4UL, 5) - (5UL, 6) ])) - Assert.False (Vector.forall v (fun x -> x < 6)) + let v = + Vector.fromCoordinateList ( + CoordinateList( + 6UL, + [ (0UL, 1) + (1UL, 2) + (2UL, 3) + (3UL, 4) + (4UL, 5) + (5UL, 6) ] + ) + ) + + Assert.False(Vector.forall v (fun x -> x < 6)) [] let ``Vector.forall empty list`` () = - let v = Vector.fromCoordinateList ( - CoordinateList(8UL, - [])) - Assert.True (Vector.forall v (fun x -> x > 0)) + let v = Vector.fromCoordinateList (CoordinateList(8UL, [])) + Assert.True(Vector.forall v (fun x -> x > 0)) [] let ``Vector.forall no matching elements, length is a power of 2`` () = - let v = Vector.fromCoordinateList ( - CoordinateList(8UL, - [ (0UL, 1) - (1UL, 2) - (2UL, 3) - (3UL, 4) - (4UL, 5) - (5UL, 6) - (6UL, 7) - (7UL, 8) ])) - Assert.False (Vector.forall v (fun x -> x > 8)) + let v = + Vector.fromCoordinateList ( + CoordinateList( + 8UL, + [ (0UL, 1) + (1UL, 2) + (2UL, 3) + (3UL, 4) + (4UL, 5) + (5UL, 6) + (6UL, 7) + (7UL, 8) ] + ) + ) + + Assert.False(Vector.forall v (fun x -> x > 8)) [] let ``Vector.forall no matching elements, length is not a power of 2`` () = - let v = Vector.fromCoordinateList ( - CoordinateList(6UL, - [ (0UL, 1) - (1UL, 2) - (2UL, 3) - (3UL, 4) - (4UL, 5) - (5UL, 6) ])) - Assert.False (Vector.forall v (fun x -> x > 6)) + let v = + Vector.fromCoordinateList ( + CoordinateList( + 6UL, + [ (0UL, 1) + (1UL, 2) + (2UL, 3) + (3UL, 4) + (4UL, 5) + (5UL, 6) ] + ) + ) + + Assert.False(Vector.forall v (fun x -> x > 6)) [] let ``Vector.forall single element matches`` () = - let v = Vector.fromCoordinateList ( - CoordinateList(1UL, - [ (0UL, 1) ])) - Assert.True (Vector.forall v (fun x -> x = 1)) + let v = + Vector.fromCoordinateList (CoordinateList(1UL, [ (0UL, 1) ])) + + Assert.True(Vector.forall v (fun x -> x = 1)) [] let ``Vector.forall single element does not match`` () = - let v = Vector.fromCoordinateList ( - CoordinateList(1UL, - [ (0UL, 1) ])) - Assert.False (Vector.forall v (fun x -> x = 2)) + let v = + Vector.fromCoordinateList (CoordinateList(1UL, [ (0UL, 1) ])) + + Assert.False(Vector.forall v (fun x -> x = 2)) [] let ``Vector.forall all elements match, length is a power of 2`` () = - let v = Vector.fromCoordinateList ( - CoordinateList(8UL, - [ (0UL, 1) - (1UL, 2) - (2UL, 3) - (3UL, 4) - (4UL, 5) - (5UL, 6) - (6UL, 7) - (7UL, 8) ])) - Assert.True (Vector.forall v (fun x -> x > 0)) - -[] -let ``Vector.forall all elements match, length is not a power of 2`` () = - let v = Vector.fromCoordinateList ( - CoordinateList(6UL, - [ (0UL, 1) - (1UL, 2) - (2UL, 3) - (3UL, 4) - (4UL, 5) - (5UL, 6) ])) - Assert.True (Vector.forall v (fun x -> x > 0)) + let v = + Vector.fromCoordinateList ( + CoordinateList( + 8UL, + [ (0UL, 1) + (1UL, 2) + (2UL, 3) + (3UL, 4) + (4UL, 5) + (5UL, 6) + (6UL, 7) + (7UL, 8) ] + ) + ) + + Assert.True(Vector.forall v (fun x -> x > 0)) + +[] +let ``Vector.forall all elements match, length is not a power of 2`` () = + let v = + Vector.fromCoordinateList ( + CoordinateList( + 6UL, + [ (0UL, 1) + (1UL, 2) + (2UL, 3) + (3UL, 4) + (4UL, 5) + (5UL, 6) ] + ) + ) + + Assert.True(Vector.forall v (fun x -> x > 0)) diff --git a/QuadTree/Matrix.fs b/QuadTree/Matrix.fs index 47bade8..e78adf7 100644 --- a/QuadTree/Matrix.fs +++ b/QuadTree/Matrix.fs @@ -417,7 +417,7 @@ let filter (matrix: SparseMatrix<'a>) (predicate: 'a -> bool) : SparseMatrix<'a> Leaf(UserValue(Some v)), (uint64 size) * (uint64 size) * 1UL else Leaf(UserValue(None)), 0UL - + let storage, nvals = inner 0UL 0UL matrix.storage.size matrix.storage.data @@ -429,8 +429,7 @@ let exists (matrix: SparseMatrix<'a>) (predicate: 'a -> bool) : bool = | Leaf(Dummy) -> false | Leaf(UserValue(None)) -> false | Leaf(UserValue(Some(v))) -> predicate v - | Node(nw, ne, sw, se) -> - inner nw || inner ne || inner sw || inner se + | Node(nw, ne, sw, se) -> inner nw || inner ne || inner sw || inner se inner matrix.storage.data @@ -440,7 +439,6 @@ let forall (matrix: SparseMatrix<'a>) (predicate: 'a -> bool) : bool = | Leaf(Dummy) -> true | Leaf(UserValue(None)) -> true | Leaf(UserValue(Some(v))) -> predicate v - | Node(nw, ne, sw, se) -> - inner nw && inner ne && inner sw && inner se + | Node(nw, ne, sw, se) -> inner nw && inner ne && inner sw && inner se - inner matrix.storage.data \ No newline at end of file + inner matrix.storage.data diff --git a/QuadTree/Vector.fs b/QuadTree/Vector.fs index 6ea0ee4..00e7f2a 100644 --- a/QuadTree/Vector.fs +++ b/QuadTree/Vector.fs @@ -589,7 +589,7 @@ let exists (vector: SparseVector<'a>) (predicate: 'a -> bool) : bool = | Node(x1, x2) -> inner x1 || inner x2 inner vector.storage.data - + let forall (vector: SparseVector<'a>) (predicate: 'a -> bool) : bool = let rec inner vector = match vector with @@ -598,4 +598,4 @@ let forall (vector: SparseVector<'a>) (predicate: 'a -> bool) : bool = | Leaf(UserValue(Some(v))) -> predicate v | Node(x1, x2) -> inner x1 && inner x2 - inner vector.storage.data \ No newline at end of file + inner vector.storage.data