Skip to content

Commit

Permalink
merge T::with_capacity and T::new, rename T::make() to `T::new(…
Browse files Browse the repository at this point in the history
…)` (#593)

* rename `make` to `new` in immut/hashmap

* rename `make` to `new` in immut/hashset

* add capacity optional argument in `devec.new`, remove `devec.with_capacity`

* add `capacity` optional argument for Array::new, remove Array::with_capacity

* continue tidy up (#592)

* move Option::unwrap to builtin (#594)

* with_capacity to new

* Refactor devec test to use new method instead of with_capacity

* add Buffer::new(~size_hint)

---------

Co-authored-by: Hongbo Zhang <[email protected]>
Co-authored-by: Yu Zhang <[email protected]>
Co-authored-by: Neil-Chen <[email protected]>
  • Loading branch information
4 people authored Jun 24, 2024
1 parent f284b13 commit a6c91d6
Show file tree
Hide file tree
Showing 46 changed files with 154 additions and 132 deletions.
6 changes: 3 additions & 3 deletions array/array_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ test "iter" {
}

test "iter_rev" {
let v = with_capacity(3)
let v = Array::new(capacity=3)
v.push(3)
v.push(4)
v.push(5)
Expand Down Expand Up @@ -395,7 +395,7 @@ test "reserve_capacity_2" {
}

test "shrink_to_fit" {
let v = with_capacity(10)
let v = Array::new(capacity=10)
v.push(1)
v.push(2)
v.push(3)
Expand All @@ -410,7 +410,7 @@ test "shrink_to_fit_2" {
}

test "as_iter" {
let buf = Buffer::make(5)
let buf = Buffer::new(size_hint=5)
[1, 2, 3, 4, 5].as_iter().iter(fn { x => buf.write_string(x.to_string()) })
inspect(buf, content="12345")?
buf.reset()
Expand Down
2 changes: 1 addition & 1 deletion array/fixedarray.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -913,7 +913,7 @@ pub fn as_iter[T](self : FixedArray[T]) -> Iter[T] {
test "as_iter" {
let arr : FixedArray[_] = [1, 2, 3, 4, 5]
let iter = arr.as_iter()
let exb = Buffer::make(0)
let exb = Buffer::new()
let mut i = 0
iter.iter(
fn(x) {
Expand Down
4 changes: 2 additions & 2 deletions array/sort_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ fn test_sort(f : (Array[Int]) -> Unit) -> Result[Unit, String] {
f(arr)
@assertion.assert_eq(arr, [1, 2, 3, 4, 5])?
{
let arr = with_capacity(1000)
let arr = Array::new(capacity=1000)
for i = 0; i < 1000; i = i + 1 {
arr.push(1000 - i - 1)
}
for i = 10; i < 1000; i = i + 10 {
arr.swap(i, i - 1)
}
f(arr)
let expected = with_capacity(1000)
let expected = Array::new(capacity=1000)
for i = 0; i < 1000; i = i + 1 {
expected.push(i)
}
Expand Down
2 changes: 1 addition & 1 deletion assertion/assertion.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

fn debug_string[T : Debug](t : T) -> String {
let buf = Buffer::make(50)
let buf = Buffer::new(size_hint=50)
t.debug_write(buf)
buf.to_string()
}
Expand Down
15 changes: 5 additions & 10 deletions builtin/array.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

/// Creates a new, empty vector.
pub fn Array::new[T]() -> Array[T] {
[]
}

/// Creates a new vector from an array.
pub fn Array::from_fixed_array[T](arr : FixedArray[T]) -> Array[T] {
let len = arr.length()
Expand Down Expand Up @@ -239,7 +234,7 @@ pub fn append[T](self : Array[T], other : Array[T]) -> Unit {
///
/// # Example
/// ```
/// let v = Array::with_capacity(3)
/// let v = Array::new(3)
/// v.push(3)
/// v.push(4)
/// v.push(5)
Expand All @@ -262,7 +257,7 @@ pub fn iter_rev[T](self : Array[T], f : (T) -> Unit) -> Unit {
///
/// # Example
/// ```
/// let v = Array::with_capacity(3)
/// let v = Array::new(3)
/// v.push(3)
/// v.push(4)
/// v.push(5)
Expand Down Expand Up @@ -701,7 +696,7 @@ pub fn flatten[T](self : Array[Array[T]]) -> Array[T] {
/// [3, 4].repeat(2)
/// ```
pub fn repeat[T](self : Array[T], times : Int) -> Array[T] {
let v = Array::with_capacity(self.length() * times)
let v = Array::new(capacity=self.length() * times)
for i = 0; i < times; i = i + 1 {
v.append(self)
}
Expand Down Expand Up @@ -841,7 +836,7 @@ pub fn chunks[T](self : Array[T], size : Int) -> Array[Array[T]] {
let chunks = []
let mut i = 0
while i < self.length() {
let chunk = Array::with_capacity(size)
let chunk = Array::new(capacity=size)
for j = 0; j < size && i < self.length(); j = j + 1 {
chunk.push(self[i])
i = i + 1
Expand Down Expand Up @@ -919,7 +914,7 @@ pub fn reserve_capacity[T](self : Array[T], capacity : Int) -> Unit {
/// # Example
///
/// ```
/// let v = Array::with_capacity(10)
/// let v = Array::new(10)
/// v.push(1)
/// v.push(2)
/// v.push(3)
Expand Down
10 changes: 7 additions & 3 deletions builtin/arraycore.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,13 @@ fn Array::make_uninit[T](len : Int) -> Array[T] {
{ buf: UninitializedArray::make(len), len }
}

/// Creates a new, empty vector with a specified initial capacity.
pub fn Array::with_capacity[T](cap : Int) -> Array[T] {
{ buf: UninitializedArray::make(cap), len: 0 }
/// Creates a new vector.
pub fn Array::new[T](~capacity : Int = 0) -> Array[T] {
if capacity == 0 {
[]
} else {
{ buf: UninitializedArray::make(capacity), len: 0 }
}
}

/// Returns the number of elements in the vector.
Expand Down
2 changes: 1 addition & 1 deletion builtin/autoloc.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub fn ArgsLoc::to_string(self : ArgsLoc) -> String {
}

pub fn ArgsLoc::to_json(self : ArgsLoc) -> String {
let buf = Buffer::make(10)
let buf = Buffer::new(size_hint=10)
buf.write_char('[')
for i = 0; i < self.0.length(); i = i + 1 {
if i != 0 {
Expand Down
14 changes: 10 additions & 4 deletions builtin/buffer.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
/// # Usage
///
/// ```
/// let buf = Buffer::make(100)
/// let buf = Buffer::new(size_hint=100)
/// buf.write_string("Tes")
/// buf.write_char("t")
/// println(buf.to_string()) // output: Test
Expand All @@ -41,7 +41,7 @@ pub fn expect(
self.reset()
if current != content {
fn repr(s : String) -> String {
let buf = Buffer::make(10)
let buf = Buffer::new()
s.debug_write(buf)
buf.to_string()
}
Expand Down Expand Up @@ -80,9 +80,15 @@ pub fn to_string(self : Buffer) -> String {
self.bytes.sub_string(0, self.len)
}

/// Create a buffer with initial capacity.
/// Create a buffer with initial capacity.
/// This function is deprecated, use `Buffer::new` instead.
pub fn Buffer::make(initial_capacity : Int) -> Buffer {
let initial = if initial_capacity < 1 { 1 } else { initial_capacity }
Buffer::new(size_hint=initial_capacity)
}

/// Create a buffer with initial capacity.
pub fn Buffer::new(~size_hint : Int = 0) -> Buffer {
let initial = if size_hint < 1 { 1 } else { size_hint }
let bytes = Bytes::make(initial)
{ bytes, len: 0, initial_bytes: bytes }
}
Expand Down
4 changes: 2 additions & 2 deletions builtin/builtin.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl Array {
map_inplace[T](Self[T], (T) -> T) -> Unit
mapi[T, U](Self[T], (Int, T) -> U) -> Self[U]
mapi_inplace[T](Self[T], (Int, T) -> T) -> Unit
new[T]() -> Self[T]
new[T](~capacity : Int = ..) -> Self[T]
op_add[T](Self[T], Self[T]) -> Self[T]
op_as_view[T](Self[T], ~start : Int, ~end : Int) -> ArrayView[T]
op_equal[T : Eq](Self[T], Self[T]) -> Bool
Expand All @@ -98,7 +98,6 @@ impl Array {
to_string[T : Show](Self[T]) -> String
unsafe_blit[A](Self[A], Int, Self[A], Int, Int) -> Unit
unsafe_blit_fixed[A](Self[A], Int, FixedArray[A], Int, Int) -> Unit
with_capacity[T](Int) -> Self[T]
}

type ArrayView
Expand All @@ -114,6 +113,7 @@ type Buffer
impl Buffer {
expect(Self, ~content : String = .., ~loc : SourceLoc = _, ~args_loc : ArgsLoc = _) -> Result[Unit, String]
make(Int) -> Self
new(~size_hint : Int = ..) -> Self
reset(Self) -> Unit
to_bytes(Self) -> Bytes
to_string(Self) -> String
Expand Down
6 changes: 3 additions & 3 deletions builtin/console.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub fn to_string(self : Int64) -> String {
// The min and max value of i64 are -9223372036854775808 and 9223372036854775807,
// so max=20 is enough.

let buf = Buffer::make(20)
let buf = Buffer::new(size_hint=20)
if self < 0L {
buf.write_char('-')
}
Expand Down Expand Up @@ -69,7 +69,7 @@ pub fn to_string(self : Int) -> String {
// The min and max value of i32 are -2147483648 and 2147483647,
// so max=11 is enough.

let buf = Buffer::make(11)
let buf = Buffer::new()
if self < 0 {
buf.write_char('-')
}
Expand Down Expand Up @@ -148,7 +148,7 @@ pub fn inspect(
let actual = obj.to_string()
if actual != content {
fn repr(s : String) -> String {
let buf = Buffer::make(10)
let buf = Buffer::new()
s.debug_write(buf)
buf.to_string()
}
Expand Down
2 changes: 1 addition & 1 deletion builtin/debug.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub trait Debug {

/// print a value to the console using [Debug], with a tailing newline
pub fn debug[X : Debug](x : X) -> Unit {
let buf = Buffer::make(10)
let buf = Buffer::new()
x.debug_write(buf)
println(buf.to_string())
}
Expand Down
2 changes: 1 addition & 1 deletion builtin/intrinsics_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

// fn debug_string[T : Debug](t : T) -> String {
// let buf = Buffer::make(50)
// let buf = Buffer::new(size_hint=50)
// t.debug_write(buf)
// buf.to_string()
// }
Expand Down
Loading

0 comments on commit a6c91d6

Please sign in to comment.