Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,17 @@ jobs:
run: |
moon version --all
moon update
moon install
moon check --deny-warn

- name: moon info
run: |
moon info --target all
git diff --exit-code

- name: format diff
run: |
moon fmt
git diff --exit-code

- name: Run tests with coverage
run: |
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
target/
.mooncakes/
.DS_Store
_build/
7 changes: 7 additions & 0 deletions src/snapshot_array/moon.pkg
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {
"BigOrangeQWQ/unification_table/undo_logs" @logs,
}

options(
name: "BigOrangeQWQ/unification_table/snapshot_array",
)
6 changes: 0 additions & 6 deletions src/snapshot_array/moon.pkg.json

This file was deleted.

46 changes: 46 additions & 0 deletions src/snapshot_array/pkg.generated.mbti
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Generated using `moon info`, DON'T EDIT IT
package "BigOrangeQWQ/unification_table/snapshot_array"

import {
"BigOrangeQWQ/unification_table/undo_logs",
}

// Values
pub fn[T] from_iter(Iter[T], capacity? : Int) -> SnapshotArray[T]

pub fn[T] new(capacity? : Int) -> SnapshotArray[T]

pub fn[T] of(FixedArray[T]) -> SnapshotArray[T]

// Errors

// Types and methods
pub struct Snapshot {
undo_count : Int
}

pub struct SnapshotArray[T] {
logs : @undo_logs.UndoLogs[T]
values : Array[T]
}
pub fn[T] SnapshotArray::action_since_snapshot(Self[T], Snapshot) -> ArrayView[@undo_logs.Undo[T]]
pub fn[T] SnapshotArray::commit(Self[T], Snapshot) -> Unit
pub fn[T] SnapshotArray::commit_all(Self[T]) -> Unit
pub fn[T] SnapshotArray::extend(Self[T], Iter[T]) -> Unit
pub fn[T] SnapshotArray::get(Self[T], Int) -> T?
pub fn[T] SnapshotArray::in_snapshot(Self[T]) -> Bool
pub fn[T] SnapshotArray::length(Self[T]) -> Int
pub fn[T] SnapshotArray::op_get(Self[T], Int) -> T
pub fn[T] SnapshotArray::op_set(Self[T], Int, T) -> Unit
pub fn[T] SnapshotArray::push(Self[T], T) -> Unit
pub fn[T] SnapshotArray::record(Self[T], @undo_logs.Undo[T]) -> Bool
pub fn[T] SnapshotArray::reset(Self[T]) -> Unit
pub fn[T] SnapshotArray::rollback_to(Self[T], Snapshot) -> Unit
pub fn[T] SnapshotArray::set(Self[T], Int, T) -> Unit
pub fn[T] SnapshotArray::set_all(Self[T], (T) -> T) -> Unit
pub fn[T] SnapshotArray::start_snapshot(Self[T]) -> Snapshot

// Type aliases

// Traits

89 changes: 56 additions & 33 deletions src/snapshot_array/snapshot_array.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub struct Snapshot {
/// Creates a new `SnapshotArray` with an optional initial capacity.
///
/// Example:
/// ```moonbit
/// ```moonbit nocheck
/// let arr : SnapshotArray[Int] = new(capacity=5)
/// assert_true(arr.length() == 0)
/// ```
Expand All @@ -39,44 +39,44 @@ pub fn[T] of(values : FixedArray[T]) -> SnapshotArray[T] {
/// Returns the number of elements in the `SnapshotArray`.
///
/// Example:
/// ```moonbit
/// ```moonbit nocheck
/// let arr = new(capacity=5)
/// assert_eq(arr.length(), 0)
/// arr.push(1)
/// assert_eq(arr.length(), 1)
/// ```
pub fn[T] length(self : SnapshotArray[T]) -> Int {
pub fn[T] SnapshotArray::length(self : SnapshotArray[T]) -> Int {
self.values.length()
}

///|
/// Returns the value at the specified index
///
/// Example:
/// ```moonbit
/// ```moonbit nocheck
/// let arr = new(capacity=5)
/// arr.push(1)
/// arr.push(2)
/// assert_eq(arr.get(0).unwrap(), 1)
/// assert_eq(arr.get(1).unwrap(), 2)
/// ```
pub fn[T] get(self : SnapshotArray[T], index : Int) -> T? {
pub fn[T] SnapshotArray::get(self : SnapshotArray[T], index : Int) -> T? {
self.values.get(index)
}

///|
/// Revert the last action.
///
/// Example:
/// ```moonbit
/// ```moonbit nocheck
/// let arr = new()
/// let snapshot = arr.start_snapshot()
/// arr.push(1)
/// arr.push(2)
/// arr.rollback_to(snapshot)
/// inspect(arr.length(), content="0")
/// ```
fn[T] rollback(self : SnapshotArray[T]) -> Unit {
fn[T] SnapshotArray::rollback(self : SnapshotArray[T]) -> Unit {
let undo = self.logs.pop().unwrap()
match undo {
NewElem(idx) => {
Expand All @@ -95,13 +95,13 @@ fn[T] rollback(self : SnapshotArray[T]) -> Unit {
///
/// Example:
///
/// ```moonbit
/// ```moonbit nocheck
/// let arr : SnapshotArray[Int] = new()
/// inspect(arr.in_snapshot(), content="false")
/// let _ = arr.start_snapshot()
/// inspect(arr.in_snapshot(), content="true")
/// ```
pub fn[T] in_snapshot(self : SnapshotArray[T]) -> Bool {
pub fn[T] SnapshotArray::in_snapshot(self : SnapshotArray[T]) -> Bool {
self.logs.in_snapshot()
}

Expand All @@ -111,7 +111,7 @@ pub fn[T] in_snapshot(self : SnapshotArray[T]) -> Bool {
///
/// Example:
///
/// ```moonbit
/// ```moonbit nocheck
/// let arr = new()
/// let snapshot = arr.start_snapshot()
/// arr.push(1)
Expand All @@ -120,7 +120,7 @@ pub fn[T] in_snapshot(self : SnapshotArray[T]) -> Bool {
/// arr.rollback_to(snapshot)
/// inspect(arr.length(), content="0")
/// ```
pub fn[T] push(self : SnapshotArray[T], elem : T) -> Unit {
pub fn[T] SnapshotArray::push(self : SnapshotArray[T], elem : T) -> Unit {
let len = self.values.length()
self.values.push(elem)
if self.in_snapshot() {
Expand All @@ -135,15 +135,15 @@ pub fn[T] push(self : SnapshotArray[T], elem : T) -> Unit {
///
/// Example:
///
/// ```moonbit
/// ```moonbit nocheck
/// let arr : SnapshotArray[Int] = new()
/// arr.push(1)
/// arr.push(2)
/// inspect(arr.length(), content="2")
/// arr.reset()
/// inspect(arr.length(), content="0")
/// ```
pub fn[T] reset(self : SnapshotArray[T]) -> Unit {
pub fn[T] SnapshotArray::reset(self : SnapshotArray[T]) -> Unit {
self.values.clear()
self.logs.clear()
}
Expand All @@ -153,13 +153,16 @@ pub fn[T] reset(self : SnapshotArray[T]) -> Unit {
///
/// Example:
///
/// ```moonbit
/// ```moonbit nocheck
/// let arr = new()
/// arr.extend([1, 2, 3].iter())
/// inspect(arr.length(), content="3")
/// inspect(arr.get(0).unwrap(), content="1")
/// ```
pub fn[T] extend(self : SnapshotArray[T], iter : Iter[T]) -> Unit {
pub fn[T] SnapshotArray::extend(
self : SnapshotArray[T],
iter : Iter[T],
) -> Unit {
iter.each(fn(elem) { self.push(elem) })
}

Expand All @@ -171,7 +174,7 @@ pub fn[T] extend(self : SnapshotArray[T], iter : Iter[T]) -> Unit {
///
/// Example:
///
/// ```moonbit
/// ```moonbit nocheck
/// let arr = new()
/// arr.push(1)
/// let snapshot = arr.start_snapshot()
Expand All @@ -180,7 +183,11 @@ pub fn[T] extend(self : SnapshotArray[T], iter : Iter[T]) -> Unit {
/// arr.rollback_to(snapshot)
/// inspect(arr.get(0).unwrap(), content="1")
/// ```
pub fn[T] set(self : SnapshotArray[T], index : Int, value : T) -> Unit {
pub fn[T] SnapshotArray::set(
self : SnapshotArray[T],
index : Int,
value : T,
) -> Unit {
if index >= self.values.length() {
abort("Index out of bounds")
}
Expand All @@ -197,7 +204,7 @@ pub fn[T] set(self : SnapshotArray[T], index : Int, value : T) -> Unit {
/// rollback.
///
/// Example:
/// ```moonbit
/// ```moonbit nocheck
/// let arr = new()
/// arr.extend([1, 2, 3].iter())
/// let snapshot = arr.start_snapshot()
Expand All @@ -206,7 +213,10 @@ pub fn[T] set(self : SnapshotArray[T], index : Int, value : T) -> Unit {
/// arr.rollback_to(snapshot)
/// inspect(arr.get(0).unwrap(), content="1")
/// ```
pub fn[T] set_all(self : SnapshotArray[T], func : (T) -> T) -> Unit {
pub fn[T] SnapshotArray::set_all(
self : SnapshotArray[T],
func : (T) -> T,
) -> Unit {
if self.in_snapshot() {
for i in 0..<self.values.length() {
let new_value = func(self.values[i])
Expand All @@ -228,15 +238,15 @@ pub fn[T] set_all(self : SnapshotArray[T], func : (T) -> T) -> Unit {
///
/// Example:
///
/// ```moonbit
/// ```moonbit nocheck
/// let arr = new()
/// arr.push(1)
/// let snapshot = arr.start_snapshot()
/// arr.push(2)
/// arr.rollback_to(snapshot)
/// inspect(arr.length(), content="1")
/// ```
pub fn[T] start_snapshot(self : SnapshotArray[T]) -> Snapshot {
pub fn[T] SnapshotArray::start_snapshot(self : SnapshotArray[T]) -> Snapshot {
{ undo_count: self.logs.start_snapshot() }
}

Expand All @@ -246,14 +256,14 @@ pub fn[T] start_snapshot(self : SnapshotArray[T]) -> Snapshot {
///
/// Example:
///
/// ```moonbit
/// ```moonbit nocheck
/// let arr = new()
/// let snapshot = arr.start_snapshot()
/// arr.push(1)
/// arr.push(2)
/// inspect(arr.action_since_snapshot(snapshot).length(), content="2")
/// ```
pub fn[T] action_since_snapshot(
pub fn[T] SnapshotArray::action_since_snapshot(
self : SnapshotArray[T],
snapshot : Snapshot,
) -> ArrayView[@logs.Undo[T]] {
Expand All @@ -268,7 +278,7 @@ pub fn[T] action_since_snapshot(
///
/// Example:
///
/// ```moonbit
/// ```moonbit nocheck
/// let arr = new()
/// arr.push(1)
/// let snapshot = arr.start_snapshot()
Expand All @@ -278,7 +288,10 @@ pub fn[T] action_since_snapshot(
/// inspect(arr.length(), content="1")
/// inspect(arr.get(0).unwrap(), content="1")
/// ```
pub fn[T] rollback_to(self : SnapshotArray[T], snapshot : Snapshot) -> Unit {
pub fn[T] SnapshotArray::rollback_to(
self : SnapshotArray[T],
snapshot : Snapshot,
) -> Unit {
let actions = self.action_since_snapshot(snapshot)
let count = actions.length()
for _ in 0..<count {
Expand All @@ -298,13 +311,16 @@ pub fn[T] rollback_to(self : SnapshotArray[T], snapshot : Snapshot) -> Unit {
///
/// Example:
///
/// ```moonbit
/// ```moonbit nocheck
/// let arr : SnapshotArray[Int] = new()
/// let snapshot = arr.start_snapshot()
/// let _ = arr.record(@logs.newElem(0))
/// inspect(arr.action_since_snapshot(snapshot).length(), content="1")
/// ```
pub fn[T] record(self : SnapshotArray[T], undo : @logs.Undo[T]) -> Bool {
pub fn[T] SnapshotArray::record(
self : SnapshotArray[T],
undo : @logs.Undo[T],
) -> Bool {
if self.in_snapshot() {
self.logs.push(undo)
return true
Expand All @@ -328,7 +344,7 @@ pub fn[T] record(self : SnapshotArray[T], undo : @logs.Undo[T]) -> Bool {
///
/// Example:
///
/// ```moonbit
/// ```moonbit nocheck
/// let arr = new()
/// let snapshot1 = arr.start_snapshot()
/// arr.push(1)
Expand All @@ -340,7 +356,10 @@ pub fn[T] record(self : SnapshotArray[T], undo : @logs.Undo[T]) -> Bool {
/// arr.rollback_to(snapshot1)
/// inspect(arr.length(), content="0")
/// ```
pub fn[T] commit(self : SnapshotArray[T], snapshot : Snapshot) -> Unit {
pub fn[T] SnapshotArray::commit(
self : SnapshotArray[T],
snapshot : Snapshot,
) -> Unit {
self.logs.commit(snapshot.undo_count)
}

Expand All @@ -355,7 +374,7 @@ pub fn[T] commit(self : SnapshotArray[T], snapshot : Snapshot) -> Unit {
///
/// Example:
///
/// ```moonbit
/// ```moonbit nocheck
/// let arr = new()
/// let _snapshot = arr.start_snapshot()
/// arr.push(1)
Expand All @@ -365,20 +384,24 @@ pub fn[T] commit(self : SnapshotArray[T], snapshot : Snapshot) -> Unit {
/// inspect(arr.in_snapshot(), content="false")
/// inspect(arr.length(), content="2")
/// ```
pub fn[T] commit_all(self : SnapshotArray[T]) -> Unit {
pub fn[T] SnapshotArray::commit_all(self : SnapshotArray[T]) -> Unit {
self.logs.clear()
}

///|
pub fn[T] op_get(self : SnapshotArray[T], index : Int) -> T {
pub fn[T] SnapshotArray::op_get(self : SnapshotArray[T], index : Int) -> T {
if index < 0 || index >= self.values.length() {
abort("Index out of bounds")
}
self.values[index]
}

///|
pub fn[T] op_set(self : SnapshotArray[T], index : Int, value : T) -> Unit {
pub fn[T] SnapshotArray::op_set(
self : SnapshotArray[T],
index : Int,
value : T,
) -> Unit {
if index < 0 || index >= self.values.length() {
abort("Index out of bounds")
}
Expand Down
Loading
Loading