From 0a6480cbd26acbad0f732e9c80927a34f9ea763f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rados=C5=82aw=20Wa=C5=9Bko?= Date: Thu, 4 Aug 2022 17:05:35 +0200 Subject: [PATCH] Remove set_at --- .../Base/0.0.0-dev/src/Data/Array.enso | 16 ---- .../Base/0.0.0-dev/src/Data/Vector.enso | 92 +++++++++---------- .../enso/interpreter/runtime/data/Array.java | 7 -- test/Tests/src/Data/Array_Spec.enso | 7 -- 4 files changed, 44 insertions(+), 78 deletions(-) diff --git a/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Array.enso b/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Array.enso index 21b349b569845..f284f50fe1243 100644 --- a/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Array.enso +++ b/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Array.enso @@ -23,22 +23,6 @@ type Array at : Integer -> Any at self index = @Builtin_Method "Array.at" - ## Set the cell at the specified index to the provided value, returning - the array. - - Arguments: - - index: The position in the array to set. - - value: The value to set at position index. - - The array is mutated in place, and only returned to facilitate a natural - programming style in Enso. - - ? Safety - If index < 0 or index >= self.length, then this operation will result - in an Invalid_Array_Index_Error exception. - set_at : Integer -> Any -> Array - set_at self index value = @Builtin_Method "Array.set_at" - ## Gets the length of the array this. > Example diff --git a/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Vector.enso b/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Vector.enso index e56555e676f73..6d3172e18b1cb 100644 --- a/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Vector.enso +++ b/distribution/lib/Standard/Base/0.0.0-dev/src/Data/Vector.enso @@ -4,6 +4,9 @@ import Standard.Base.Runtime.Unsafe from Standard.Base.Data.Index_Sub_Range import While, By_Index, Sample, Every import Standard.Base.Random +polyglot java import java.util.ArrayList +polyglot java import java.lang.IndexOutOfBoundsException + ## Creates a new vector of the given length, initializing elements using the provided constructor function. @@ -25,9 +28,9 @@ import Standard.Base.Random Vector.new my_vec.length (ix -> my_vec.at ix) new : Number -> (Number -> Any) -> Vector Any new length constructor = - arr = Array.new length - 0.up_to length . each ix-> arr.set_at ix (constructor ix) - Vector arr + builder = new_builder length + 0.up_to length . each ix-> builder.append (constructor ix) + builder.to_vector ## Creates a new vector of the given length, filling the elements with the provided constant. @@ -45,9 +48,9 @@ new length constructor = Vector.fill length=50 item=42 fill : Number -> Any -> Vector Any fill length ~item = - arr = Array.new length - 0.up_to length . each ix-> arr.set_at ix item - Vector arr + builder = new_builder length + 0.up_to length . each _-> builder.append item + builder.to_vector ## Creates a new vector builder instance. @@ -983,8 +986,7 @@ type Builder A builder type for Enso vectors. Arguments: - - to_array: The accumulator for the new vector. - - length: The current length of the vector being built. + - java_builder: The accumulator for the new vector. A vector builder is a mutable data structure, that allows to gather a number of elements and then convert them to a vector. This is @@ -1004,7 +1006,7 @@ type Builder @Tail_Call do_build new_builder start+1 stop builder = do_build Vector.new_builder 1 10 builder.to_vector - type Builder to_array length + type Builder java_builder ## Creates a new builder. @@ -1016,21 +1018,11 @@ type Builder Vector.new_builder new : Integer->Builder - new (capacity=1) = Builder (Array.new capacity) 0 - - ## Returns the current capacity (i.e. the size of the underlying storage) - of this builder. - - > Example - Get the capacity of a new builder. - - Vector.new_builder.capacity - capacity : Integer - capacity self = self.to_array.length + new (capacity=1) = Builder (ArrayList.new capacity) ## Checks if this builder is empty. is_empty : Boolean - is_empty self = self.length == 0 + is_empty self = self.java_builder.isEmpty ## Checks if this builder is not empty. not_empty : Boolean @@ -1053,6 +1045,24 @@ type Builder self.unsafe_append item self + ## Appends a part of a given vector to this builder + + Arguments: + - vector: The vector from which the elements are sourced. + - start: The start index of the range to append. + - end: The end index (the first index after the last element to be + appended) of the range to be appended. + + > Example + Append a part of the vector. + + builder = Vector.new_builder + builder . append_vector_range [20, 30, 40, 50] 1 3 . to_vector == [30, 40] + append_vector_range : Vector Any ! Error -> Builder ! Error + append_vector_range self vector start end = + subrange = vector.slice start end + self.java_builder.addAll subrange.to_array + ## PRIVATE Appends a new element into this builder. @@ -1072,17 +1082,7 @@ type Builder Vector.new_builder.unsafe_append 10 unsafe_append : Any -> Nothing - unsafe_append self item = case self.capacity > self.length of - True -> - self.to_array.set_at self.length item - Unsafe.set_atom_field self 1 (self.length + 1) - False -> - old_array = self.to_array - new_array = Array.new old_array.length*2 - Array.copy old_array 0 new_array 0 old_array.length - Unsafe.set_atom_field self 0 new_array - self.append item - Nothing + unsafe_append self item = self.java_builder.add item ## Gets an element from the vector at a specified index (0-based). @@ -1093,7 +1093,7 @@ type Builder at : Integer -> Any ! Index_Out_Of_Bounds_Error at self index = actual_index = if index < 0 then self.length + index else index - Panic.catch Invalid_Array_Index_Error (self.to_array.at actual_index) _-> + Panic.catch IndexOutOfBoundsException (self.java_builder.get actual_index) _-> Error.throw (Index_Out_Of_Bounds_Error index self.length) ## Checks whether a predicate holds for at least one element of this builder. @@ -1104,7 +1104,7 @@ type Builder exists : (Any -> Boolean) -> Boolean exists self predicate = - 0.up_to self.length . exists (idx -> (predicate (self.to_array.at idx))) + 0.up_to self.length . exists (idx -> (predicate (self.java_builder.get idx))) ## Converts this builder to a vector containing all the appended elements. @@ -1119,9 +1119,9 @@ type Builder bldr.to_vector to_vector : Vector Any to_vector self = - old_array = self.to_array - new_array = Array.new self.length - Array.copy old_array 0 new_array 0 self.length + ## This creates a fresh copy of the builders storage, so any future + changes to the builder will not affect the returned vector. + new_array = self.java_builder.toArray Vector new_array ## UNSTABLE @@ -1208,21 +1208,17 @@ slice_many_ranges vector ranges = new_length = ranges.fold 0 acc-> descriptor-> case descriptor of Integer -> acc+1 Range _ _ _ -> acc+descriptor.length - arr = Array.new new_length - ranges.fold 0 start_ix-> descriptor-> case descriptor of + builder = Vector.new_builder new_length + ranges.each descriptor-> case descriptor of Integer -> - arr.set_at start_ix (vector.unsafe_at descriptor) - start_ix+1 + builder.append (vector.unsafe_at descriptor) Range start end step -> case step == 1 of True -> - len = end-start - Array.copy vector.to_array start arr start_ix len - start_ix+len + builder.append_vector_range vector start end False -> - descriptor.each_with_index within_range_ix-> descriptor_ix-> - arr.set_at start_ix+within_range_ix (vector.unsafe_at descriptor_ix) - start_ix+descriptor.length - Vector arr + descriptor.each ix-> + builder.append (vector.unsafe_at ix) + builder.to_vector ## PRIVATE Takes a list of descriptors and returns a new one where ranges with diff --git a/engine/runtime/src/main/java/org/enso/interpreter/runtime/data/Array.java b/engine/runtime/src/main/java/org/enso/interpreter/runtime/data/Array.java index e65a8fcfb8603..bc5b0420839cb 100644 --- a/engine/runtime/src/main/java/org/enso/interpreter/runtime/data/Array.java +++ b/engine/runtime/src/main/java/org/enso/interpreter/runtime/data/Array.java @@ -111,13 +111,6 @@ public Object get(long index) { return getItems()[(int) index]; } - @Builtin.Method(name = "setAt", description = "Gets an array element at the given index.") - @Builtin.WrapException(from = IndexOutOfBoundsException.class, to = InvalidArrayIndexError.class) - public Object set(long index, @AcceptsError Object value) { - getItems()[(int) index] = value; - return this; - } - /** * Exposes an index validity check through the polyglot API. * diff --git a/test/Tests/src/Data/Array_Spec.enso b/test/Tests/src/Data/Array_Spec.enso index 17bb3beeeaa4d..62e0a54a8941b 100644 --- a/test/Tests/src/Data/Array_Spec.enso +++ b/test/Tests/src/Data/Array_Spec.enso @@ -17,17 +17,10 @@ spec = Test.group "Arrays" <| arr.at 0 . should_equal 1 arr.at 2 . should_equal 3 - Test.specify "should allow setting elements" <| - arr = [1, 2, 3] . to_array - arr.set_at 1 10 - arr.at 1 . should_equal 10 - Vector.from_array arr . should_equal [1, 10, 3] - Test.specify "should panic on out of bounds access" <| arr = [1, 2, 3] . to_array Test.expect_panic_with (arr.at -1) Invalid_Array_Index_Error Test.expect_panic_with (arr.at 3) Invalid_Array_Index_Error - Test.expect_panic_with (arr.set_at 3 100) Invalid_Array_Index_Error Test.specify "should allow for functional dispatch on a method defined in this module" arr = [1, 2, 3] . to_array