Skip to content

Commit

Permalink
refactor: private ArraySlice
Browse files Browse the repository at this point in the history
  • Loading branch information
Lampese authored and bobzhang committed Mar 25, 2024
1 parent 904cc9f commit 37e08f5
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions array/slice.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,36 @@
// See the License for the specific language governing permissions and
// limitations under the License.

struct ArraySlice[T] {
priv struct ArraySlice[T] {
array : Array[T]
start : Int
end : Int
}

pub fn length[T](self : ArraySlice[T]) -> Int {
fn length[T](self : ArraySlice[T]) -> Int {
self.end - self.start
}

pub fn op_get[T](self : ArraySlice[T], index : Int) -> T {
fn op_get[T](self : ArraySlice[T], index : Int) -> T {
self.array[self.start + index]
}

pub fn op_set[T](self : ArraySlice[T], index : Int, value : T) -> Unit {
fn op_set[T](self : ArraySlice[T], index : Int, value : T) -> Unit {
self.array[self.start + index] = value
}

pub fn swap[T](self : ArraySlice[T], a : Int, b : Int) -> Unit {
fn swap[T](self : ArraySlice[T], a : Int, b : Int) -> Unit {
self.array.swap(self.start + a, self.start + b)
}

pub fn reverse[T](self : ArraySlice[T]) -> Unit {
fn reverse[T](self : ArraySlice[T]) -> Unit {
let mid_len = self.length() / 2
for i = 0; i < mid_len; i = i + 1 {
let j = self.length() - i - 1
self.swap(i, j)
}
}

pub fn slice[T](self : ArraySlice[T], start : Int, end : Int) -> ArraySlice[T] {
fn slice[T](self : ArraySlice[T], start : Int, end : Int) -> ArraySlice[T] {
{ array: self.array, start: self.start + start, end: self.start + end }
}

0 comments on commit 37e08f5

Please sign in to comment.