Skip to content

Commit 1f27096

Browse files
authored
Merge branch 'main' into pragma
2 parents e9ace85 + 0ebe01e commit 1f27096

File tree

2 files changed

+118
-5
lines changed

2 files changed

+118
-5
lines changed

deque/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,27 @@ let dq : Deque[Int] = Deque::new()
3838
dq.is_empty() // true
3939
```
4040

41+
You can use `reserve_capacity` to reserve capacity, ensures that it can hold at least the number of elements
42+
specified by the `capacity` argument.
43+
44+
```moonbit
45+
let dq = Deque::[1]
46+
dq.reserve_capacity(10)
47+
println(dq.capacity()) // 10
48+
```
49+
50+
Also, you can use `shrink_to_fit` to shrink the capacity of the deque.
51+
52+
```moonbit
53+
let dq = Deque::with_capacity(10)
54+
dq.push_back(1)
55+
dq.push_back(2)
56+
dq.push_back(3)
57+
println(dq.capacity()) // 10
58+
dq.shrink_to_fit()
59+
println(dq.capacity()) // 3
60+
```
61+
4162
### Front & Back & Get
4263

4364
You can use `front()` and `back()` to get the head and tail elements of the queue, respectively. Since the queue may be empty, their return values are both `Option`, or `None` if the queue is empty.

deque/deque.mbt

Lines changed: 97 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ test "front_and_back" {
148148
let dq = Deque::[1, 2, 3, 4, 5]
149149
@assertion.assert_eq(dq.back(), Some(5))?
150150
@assertion.assert_eq(dq.front(), Some(1))?
151-
let dq: Deque[Int] = Deque::[]
151+
let dq : Deque[Int] = Deque::[]
152152
@assertion.assert_eq(dq.back(), None)?
153153
@assertion.assert_eq(dq.front(), None)?
154154
}
@@ -273,19 +273,18 @@ test "push_and_pop" {
273273
@assertion.assert_eq(dq.length(), 6)?
274274
@assertion.assert_eq(dq.pop_back(), Some(8))?
275275
@assertion.assert_eq(dq.back(), Some(7))?
276-
277276
let dq = Deque::[1]
278277
@assertion.assert_eq(dq.pop_front(), Some(1))?
279278
@assertion.assert_eq(dq.pop_front(), None)?
280279
@assertion.assert_eq(dq.pop_back(), None)?
281280
}
282281
283282
test "push_realloc" {
284-
let dq: Deque[Int] = Deque::with_capacity(0)
283+
let dq : Deque[Int] = Deque::with_capacity(0)
285284
dq.push_front(1)
286285
@assertion.assert_eq(dq.pop_front(), Some(1))?
287286
@assertion.assert_true(dq.capacity() > 0)?
288-
let dq: Deque[Int] = Deque::with_capacity(0)
287+
let dq : Deque[Int] = Deque::with_capacity(0)
289288
dq.push_back(1)
290289
@assertion.assert_eq(dq.pop_back(), Some(1))?
291290
@assertion.assert_true(dq.capacity() > 0)?
@@ -355,7 +354,7 @@ pub fn op_set[T](self : Deque[T], index : Int, value : T) -> Unit {
355354
}
356355
357356
test "op_set" {
358-
let dq = Deque::[1,2,3,4,5]
357+
let dq = Deque::[1, 2, 3, 4, 5]
359358
dq[1] = 3
360359
@assertion.assert_eq(dq[1], 3)?
361360
}
@@ -639,3 +638,96 @@ test "contains" {
639638
@assertion.assert_true(dq.contains(5))?
640639
@assertion.assert_false(dq.contains(6))?
641640
}
641+
642+
/// Reserves capacity to ensure that it can hold at least the number of elements
643+
/// specified by the `capacity` argument.
644+
///
645+
/// # Example
646+
///
647+
/// ```
648+
/// let dq = Deque::[1]
649+
/// dq.reserve_capacity(10)
650+
/// println(dq.capacity()) // 10
651+
/// ```
652+
pub fn reserve_capacity[T](self : Deque[T], capacity : Int) -> Unit {
653+
if self.capacity() >= capacity {
654+
return
655+
}
656+
let new_buf : UninitializedArray[T] = UninitializedArray::make(capacity)
657+
let { buf, len, head, .. } = self
658+
self.buf = new_buf
659+
self.head = 0
660+
self.tail = 0
661+
for i = 0; i < len; i = i + 1 {
662+
let idx = (head + i) % buf.0.length()
663+
self.buf[i] = buf[idx]
664+
self.tail += 1
665+
}
666+
}
667+
668+
test "reserve_capacity" {
669+
let dq = Deque::[1, 2, 3, 4, 5]
670+
dq.pop_front() |> ignore() // 2, 3, 4, 5
671+
dq.push_back(1) // 2, 3, 4, 5, 1
672+
dq.pop_front() |> ignore() // 3, 4, 5, 1
673+
dq.pop_front() |> ignore() // 4, 5, 1
674+
dq.reserve_capacity(10)
675+
@assertion.assert_true(dq.capacity() == 10)?
676+
@assertion.assert_eq(dq[0], 4)?
677+
@assertion.assert_eq(dq[1], 5)?
678+
@assertion.assert_eq(dq[2], 1)?
679+
}
680+
681+
test "reserve_capacity_2" {
682+
let dq = Deque::[1, 2, 3, 4, 5]
683+
dq.reserve_capacity(3)
684+
@assertion.assert_true(dq.capacity() == 5)?
685+
}
686+
687+
/// Shrinks the capacity of the deque as much as possible.
688+
///
689+
/// # Example
690+
///
691+
/// ```
692+
/// let dq = Deque::with_capacity(10)
693+
/// dq.push_back(1)
694+
/// dq.push_back(2)
695+
/// dq.push_back(3)
696+
/// println(dq.capacity()) // >= 10
697+
/// dq.shrink_to_fit()
698+
/// println(dq.capacity()) // >= 3
699+
/// ```
700+
pub fn shrink_to_fit[T](self : Deque[T]) -> Unit {
701+
if self.capacity() <= self.length() {
702+
return
703+
}
704+
let { buf, len, head, .. } = self
705+
self.buf = UninitializedArray::make(len)
706+
self.head = 0
707+
self.tail = 0
708+
for i = 0; i < len; i = i + 1 {
709+
let idx = (head + i) % buf.0.length()
710+
self.buf[i] = buf[idx]
711+
}
712+
}
713+
714+
test "shrink_to_fit" {
715+
let dq : Deque[Int] = Deque::[1, 2, 3, 4, 5]
716+
dq.pop_front() |> ignore()
717+
dq.push_back(1)
718+
dq.pop_front() |> ignore()
719+
dq.pop_front() |> ignore()
720+
@assertion.assert_true(dq.capacity() == 5)?
721+
dq.shrink_to_fit()
722+
@assertion.assert_true(dq.capacity() == 3)?
723+
@assertion.assert_eq(dq[0], 4)?
724+
@assertion.assert_eq(dq[1], 5)?
725+
@assertion.assert_eq(dq[2], 1)?
726+
}
727+
728+
test "shrink_to_fit_2" {
729+
let dq : Deque[Int] = Deque::[1, 2, 3]
730+
@assertion.assert_true(dq.capacity() == 3)?
731+
dq.shrink_to_fit()
732+
@assertion.assert_true(dq.capacity() == 3)?
733+
}

0 commit comments

Comments
 (0)