From 5eedd68482dc28ae14187731026b0a5c93e3ccb2 Mon Sep 17 00:00:00 2001 From: Dimi Racordon Date: Mon, 5 Aug 2024 11:29:47 +0200 Subject: [PATCH 1/3] Implement 'Collection.count' --- StandardLibrary/Sources/Core/Collection.hylo | 9 +++++++++ .../TestCases/CollectionTests.hylo | 19 +++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/StandardLibrary/Sources/Core/Collection.hylo b/StandardLibrary/Sources/Core/Collection.hylo index 64c890737..8597b30e6 100644 --- a/StandardLibrary/Sources/Core/Collection.hylo +++ b/StandardLibrary/Sources/Core/Collection.hylo @@ -28,6 +28,15 @@ public trait Collection { public extension Collection { + /// Returns the number of elements in `self`. + /// + /// - Complexity: O(n), where n is the number of elements in `self`. + public fun count() -> Int { + var r = 0 + for let _ in self { &r += 1 } + return r + } + /// Returns the result of applying `combine` on an accumulator, initialized with `initial_value`, /// and each element of `self`, in order. /// diff --git a/Tests/LibraryTests/TestCases/CollectionTests.hylo b/Tests/LibraryTests/TestCases/CollectionTests.hylo index 7fff780a7..57267c21f 100644 --- a/Tests/LibraryTests/TestCases/CollectionTests.hylo +++ b/Tests/LibraryTests/TestCases/CollectionTests.hylo @@ -1,5 +1,23 @@ //- compileAndRun expecting: .success +conformance Int: Collection { + + public typealias Element = Bool + public typealias Position = Int + + public fun start_position() -> Int { 0 } + public fun end_position() -> Int { Int.bit_width() } + public fun position(after p: Int) -> Int { p + 1 } + + public subscript(_ p: Int): Bool { (self & (1 << p)) != 0 } + +} + +fun test_count() { + let a = 0 + precondition(a.count() == 64) +} + fun test_reduce() { var a = Array() &a.append(1) @@ -31,6 +49,7 @@ fun test_all_satisfy() { } public fun main() { + test_count() test_reduce() test_contains_where() test_all_satisfy() From 9338c0e9e18b4b28e0f5db0fb1129f833be29c02 Mon Sep 17 00:00:00 2001 From: Dimi Racordon Date: Mon, 5 Aug 2024 11:50:33 +0200 Subject: [PATCH 2/3] Implement 'Collection.first_position(where:)' --- StandardLibrary/Sources/Core/Collection.hylo | 13 +++++++++++++ Tests/LibraryTests/TestCases/CollectionTests.hylo | 6 ++++++ 2 files changed, 19 insertions(+) diff --git a/StandardLibrary/Sources/Core/Collection.hylo b/StandardLibrary/Sources/Core/Collection.hylo index 8597b30e6..11c6a243b 100644 --- a/StandardLibrary/Sources/Core/Collection.hylo +++ b/StandardLibrary/Sources/Core/Collection.hylo @@ -37,6 +37,19 @@ public extension Collection { return r } + /// Returns the position of the first element of `self` satisfying `predicate`, or + /// `end_position()` if no such element exists. + /// + /// - Complexity: O(n), where n is the number of elements in `self`. + public fun first_position(where predicate: [E](Element) -> Bool) -> Position { + var i = start_position() + let j = end_position() + while (i != j) && !predicate(self[i]) { + &i = self.position(after: i) + } + return i + } + /// Returns the result of applying `combine` on an accumulator, initialized with `initial_value`, /// and each element of `self`, in order. /// diff --git a/Tests/LibraryTests/TestCases/CollectionTests.hylo b/Tests/LibraryTests/TestCases/CollectionTests.hylo index 57267c21f..4c8b97f3c 100644 --- a/Tests/LibraryTests/TestCases/CollectionTests.hylo +++ b/Tests/LibraryTests/TestCases/CollectionTests.hylo @@ -18,6 +18,11 @@ fun test_count() { precondition(a.count() == 64) } +fun test_first_position() { + let a = 4 + precondition(a.first_position(where: fun (_ x) { x.copy() }) == 2) +} + fun test_reduce() { var a = Array() &a.append(1) @@ -50,6 +55,7 @@ fun test_all_satisfy() { public fun main() { test_count() + test_first_position() test_reduce() test_contains_where() test_all_satisfy() From d4eacda41ca790dd7a93ab17ea108a0e40808ce9 Mon Sep 17 00:00:00 2001 From: Dimi Racordon Date: Mon, 5 Aug 2024 11:55:37 +0200 Subject: [PATCH 3/3] Implement 'Collection.first_position(of:)' --- StandardLibrary/Sources/Core/Collection.hylo | 12 ++++++++++++ Tests/LibraryTests/TestCases/CollectionTests.hylo | 1 + 2 files changed, 13 insertions(+) diff --git a/StandardLibrary/Sources/Core/Collection.hylo b/StandardLibrary/Sources/Core/Collection.hylo index 11c6a243b..d7d9ba30d 100644 --- a/StandardLibrary/Sources/Core/Collection.hylo +++ b/StandardLibrary/Sources/Core/Collection.hylo @@ -85,3 +85,15 @@ public extension Collection { } } + +public extension Collection where Element: Equatable { + + /// Returns the position of the first element of `self` that is equal to `needle`, or + /// `end_position()` if no such element exists. + /// + /// - Complexity: O(n), where n is the number of elements in `self`. + public fun first_position(of needle: Element) -> Position { + first_position(where: fun (_ e) { e == needle }) + } + +} diff --git a/Tests/LibraryTests/TestCases/CollectionTests.hylo b/Tests/LibraryTests/TestCases/CollectionTests.hylo index 4c8b97f3c..0b24a76e2 100644 --- a/Tests/LibraryTests/TestCases/CollectionTests.hylo +++ b/Tests/LibraryTests/TestCases/CollectionTests.hylo @@ -21,6 +21,7 @@ fun test_count() { fun test_first_position() { let a = 4 precondition(a.first_position(where: fun (_ x) { x.copy() }) == 2) + precondition(a.first_position(of: true) == 2) } fun test_reduce() {