From 026cf067c872b74252ae1f82e26b5891b043adcb Mon Sep 17 00:00:00 2001 From: "bob.hongbo.zhang" Date: Tue, 26 Mar 2024 22:53:47 +0800 Subject: [PATCH 1/2] split types --- queue/queue.mbt | 17 +---------------- queue/types.mbt | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 16 deletions(-) create mode 100644 queue/types.mbt diff --git a/queue/queue.mbt b/queue/queue.mbt index 68f64c2c6..7aa9450dc 100644 --- a/queue/queue.mbt +++ b/queue/queue.mbt @@ -12,21 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -struct Cont[T] { - content : T - mut next : Cell[T] -} - -enum Cell[T] { - Nil - Cons(Cont[T]) -} - -struct Queue[T] { - mut length : Int - mut first : Cell[T] - mut last : Cell[T] -} /// Creates a new empty queue. /// @@ -64,7 +49,7 @@ pub fn Queue::from_array[T](arr : Array[T]) -> Queue[T] { } /// Tests if two queue cells are equal. -pub fn op_equal[T : Eq](self : Cell[T], other : Cell[T]) -> Bool { +fn op_equal[T : Eq](self : Cell[T], other : Cell[T]) -> Bool { loop self, other { Nil, Nil => true Cons({ content: x, next: xs }), Cons({ content: y, next: ys }) => { diff --git a/queue/types.mbt b/queue/types.mbt new file mode 100644 index 000000000..5eea82ab6 --- /dev/null +++ b/queue/types.mbt @@ -0,0 +1,29 @@ +// Copyright 2024 International Digital Economy Academy +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +priv struct Cont[T] { + content : T + mut next : Cell[T] +} + +priv enum Cell[T] { + Nil + Cons(Cont[T]) +} + +struct Queue[T] { + mut length : Int + mut first : Cell[T] + mut last : Cell[T] +} \ No newline at end of file From 6b95f921e714869758495e9b7aa61840b44c2e68 Mon Sep 17 00:00:00 2001 From: "bob.hongbo.zhang" Date: Tue, 26 Mar 2024 22:58:05 +0800 Subject: [PATCH 2/2] tweak --- queue/queue.mbt | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/queue/queue.mbt b/queue/queue.mbt index 7aa9450dc..3066b3141 100644 --- a/queue/queue.mbt +++ b/queue/queue.mbt @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. - /// Creates a new empty queue. /// /// # Example @@ -52,12 +51,12 @@ pub fn Queue::from_array[T](arr : Array[T]) -> Queue[T] { fn op_equal[T : Eq](self : Cell[T], other : Cell[T]) -> Bool { loop self, other { Nil, Nil => true - Cons({ content: x, next: xs }), Cons({ content: y, next: ys }) => { - if x != y { - break false + Cons({ content: x, next: xs }), Cons({ content: y, next: ys }) => + if x == y { + continue xs, ys + } else { + false } - continue xs, ys - } _, _ => false } }