From d53a8abc6019ab8f598c3d94996fb68e10d24520 Mon Sep 17 00:00:00 2001 From: zihang Date: Thu, 28 Nov 2024 15:28:41 +0800 Subject: [PATCH] doc: update segment tree example and add code --- next/sources/segment-tree/.gitignore | 2 + next/sources/segment-tree/LICENSE | 202 ++++++++++++++++++ next/sources/segment-tree/README.md | 3 + next/sources/segment-tree/moon.mod.json | 10 + .../segment-tree/src/part1/moon.pkg.json | 1 + next/sources/segment-tree/src/part1/top.mbt | 82 +++++++ .../segment-tree/src/part2/moon.pkg.json | 1 + next/sources/segment-tree/src/part2/top.mbt | 173 +++++++++++++++ .../example/segment-tree/segment-tree.md | 107 ++++------ .../example/segment-tree/segment-tree2.md | 141 +++--------- 10 files changed, 543 insertions(+), 179 deletions(-) create mode 100644 next/sources/segment-tree/.gitignore create mode 100644 next/sources/segment-tree/LICENSE create mode 100644 next/sources/segment-tree/README.md create mode 100644 next/sources/segment-tree/moon.mod.json create mode 100644 next/sources/segment-tree/src/part1/moon.pkg.json create mode 100644 next/sources/segment-tree/src/part1/top.mbt create mode 100644 next/sources/segment-tree/src/part2/moon.pkg.json create mode 100644 next/sources/segment-tree/src/part2/top.mbt diff --git a/next/sources/segment-tree/.gitignore b/next/sources/segment-tree/.gitignore new file mode 100644 index 00000000..b1283a74 --- /dev/null +++ b/next/sources/segment-tree/.gitignore @@ -0,0 +1,2 @@ +target/ +.mooncakes/ diff --git a/next/sources/segment-tree/LICENSE b/next/sources/segment-tree/LICENSE new file mode 100644 index 00000000..d6456956 --- /dev/null +++ b/next/sources/segment-tree/LICENSE @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + 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. diff --git a/next/sources/segment-tree/README.md b/next/sources/segment-tree/README.md new file mode 100644 index 00000000..f9fa5816 --- /dev/null +++ b/next/sources/segment-tree/README.md @@ -0,0 +1,3 @@ +# Segment Tree + +Check `tutorial/example/segment-tree`. \ No newline at end of file diff --git a/next/sources/segment-tree/moon.mod.json b/next/sources/segment-tree/moon.mod.json new file mode 100644 index 00000000..0e73ecc0 --- /dev/null +++ b/next/sources/segment-tree/moon.mod.json @@ -0,0 +1,10 @@ +{ + "name": "moonbit-community/segment-tree", + "version": "0.1.0", + "readme": "README.md", + "repository": "", + "license": "Apache-2.0", + "keywords": [], + "description": "", + "source": "src" +} \ No newline at end of file diff --git a/next/sources/segment-tree/src/part1/moon.pkg.json b/next/sources/segment-tree/src/part1/moon.pkg.json new file mode 100644 index 00000000..9e26dfee --- /dev/null +++ b/next/sources/segment-tree/src/part1/moon.pkg.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/next/sources/segment-tree/src/part1/top.mbt b/next/sources/segment-tree/src/part1/top.mbt new file mode 100644 index 00000000..a027f267 --- /dev/null +++ b/next/sources/segment-tree/src/part1/top.mbt @@ -0,0 +1,82 @@ +// start node definition +enum Node { + Nil + Node(Int, Node, Node) +} +// end node definition + +impl ToJson for Node with to_json(self) { + match self { + Nil => "Nil" + Node(value, left, right) => { + let left_json = left.to_json() + let right_json = right.to_json() + [Number(value.to_double()), left_json, right_json] + } + } +} + +// start op_add definition +fn op_add(self : Node, v : Node) -> Node { + match (self, v) { + (Node(left, _, _), Node(right, _, _)) => Node(left + right, self, v) + (Node(_), Nil) => self + (Nil, Node(_)) => v + (Nil, Nil) => Nil + } +} +// end op_add definition + +// start build definition +fn build(data : ArrayView[Int]) -> Node { + if data.length() == 1 { + Node(data[0], Nil, Nil) + } else { + let mid = (data.length() + 1) >> 1 + build(data[0:mid]) + build(data[mid:]) + } +} +// end build definition + +// start build test +test { + let tree = build([1, 2, 3, 4, 5][:]) + @json.inspect!( + tree, + content=[ + 15, + [6, [3, [1, "Nil", "Nil"], [2, "Nil", "Nil"]], [3, "Nil", "Nil"]], + [9, [4, "Nil", "Nil"], [5, "Nil", "Nil"]], + ], + ) +} +// end build test + +// start query definition +let empty_node : Node = Node(0, Nil, Nil) + +fn query(self : Node, l : Int, r : Int, query_l : Int, query_r : Int) -> Node { + if query_l > r || l > query_r { + empty_node + } else if query_l <= l && query_r >= r { + self + } else { + guard let Node(_, left, right) = self + let mid = (l + r) >> 1 + left.query(l, mid, query_l, query_r) + + right.query(mid + 1, r, query_l, query_r) + } +} +// end query definition + +// start query test + +test { + let tree = build([1, 2, 3, 4, 5][:]) + let sum = match tree.query(1, 5, 1, 3) { + Node(sum, _, _) => sum + _ => fail!("Expected Node") + } + inspect!(sum, content="6") +} +// end query test \ No newline at end of file diff --git a/next/sources/segment-tree/src/part2/moon.pkg.json b/next/sources/segment-tree/src/part2/moon.pkg.json new file mode 100644 index 00000000..9e26dfee --- /dev/null +++ b/next/sources/segment-tree/src/part2/moon.pkg.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/next/sources/segment-tree/src/part2/top.mbt b/next/sources/segment-tree/src/part2/top.mbt new file mode 100644 index 00000000..837e8f03 --- /dev/null +++ b/next/sources/segment-tree/src/part2/top.mbt @@ -0,0 +1,173 @@ +// start data definition +enum Data { + Data(sum~ : Int, len~ : Int) +} + +enum LazyTag { + Nil + Tag(Int) +} + +enum Node { + Nil + Node(data~ : Data, tag~ : LazyTag, left~ : Node, right~ : Node) +} +// end data definition + +impl ToJson for Data with to_json(data) { + let Data(sum~, len~) = data + [Number(sum.to_double()), Number(len.to_double())] +} + +impl ToJson for LazyTag with to_json(tag) { + match tag { + Nil => "Nil" + Tag(value) => Number(value.to_double()) + } +} + +impl ToJson for Node with to_json(node) { + match node { + Nil => "Nil" + Node(data~, tag~, left~, right~) => { + let data_json = data.to_json() + let tag_json = tag.to_json() + let left_json = left.to_json() + let right_json = right.to_json() + { + "data": data_json, + "tag": tag_json, + "left": left_json, + "right": right_json, + } + } + } +} + +// start op_add definition +fn op_add(self : Data, v : Data) -> Data { + match (self, v) { + (Data(sum=a, len=len_a), Data(sum=b, len=len_b)) => + Data(sum=a + b, len=len_a + len_b) + } +} + +fn op_add(self : Node, v : Node) -> Node { + match (self, v) { + (Node(data=l, ..), Node(data=r, ..)) => + Node(data=l + r, tag=Nil, left=self, right=v) + (Node(_), Nil) => self + (Nil, Node(_)) => v + (Nil, Nil) => Nil + } +} +// end op_add definition + +// start lazytag definition +fn op_add(self : LazyTag, v : LazyTag) -> LazyTag { + match (self, v) { + (Tag(a), Tag(b)) => Tag(a + b) + (Nil, t) | (t, Nil) => t + } +} + +fn apply(self : Node, v : LazyTag) -> Node { + match (self, v) { + (Node(data=Data(sum=a, len=length), tag~, left~, right~), Tag(v) as new_tag) => + Node( + data=Data(sum=a + v * length, len=length), + tag=tag + new_tag, + left~, + right~, + ) + (_, Nil) => self + (Nil, _) => Nil + } +} +// end lazytag definition + +// start build definition +fn build(data : ArrayView[Int]) -> Node { + if data.length() == 1 { + Node(data=Data(sum=data[0], len=1), tag=Nil, left=Nil, right=Nil) + } else { + let mid = (data.length() + 1) >> 1 + build(data[0:mid]) + build(data[mid:]) + } +} +// end build definition + +// start modify definition +fn modify( + self : Node, + l : Int, + r : Int, + modify_l : Int, + modify_r : Int, + tag : LazyTag +) -> Node { + if modify_l > r || l > modify_r { + self + } else if modify_l <= l && modify_r >= r { + self.apply(tag) + } else { + guard let Node(left~, right~, ..) = self + left.apply(tag) + right.apply(tag) + } +} +// end modify definition + +// start query definition +let empty_node : Node = Node( + data=Data(sum=0, len=0), + tag=Nil, + left=Nil, + right=Nil, +) + +fn query(self : Node, l : Int, r : Int, query_l : Int, query_r : Int) -> Node { + if query_l > r || l > query_r { + empty_node + } else if query_l <= l && query_r >= r { + self + } else { + guard let Node(tag~, left~, right~, ..) = self + let mid = (l + r) >> 1 + left.apply(tag).query(l, mid, query_l, query_r) + + right.apply(tag).query(mid + 1, r, query_l, query_r) + } +} +// end query definition + +test { + let tree = build([1, 2, 3, 4, 5][:]) + @json.inspect!( + tree.modify(1, 5, 1, 3, Tag(1)).query(1, 5, 1, 3), + content={ + "data": [9, 3], + "tag": "Nil", + "left": { + "data": [9, 3], + "tag": 1, + "left": { + "data": [3, 2], + "tag": "Nil", + "left": { + "data": [1, 1], + "tag": "Nil", + "left": "Nil", + "right": "Nil", + }, + "right": { + "data": [2, 1], + "tag": "Nil", + "left": "Nil", + "right": "Nil", + }, + }, + "right": { "data": [3, 1], "tag": "Nil", "left": "Nil", "right": "Nil" }, + }, + "right": { "data": [0, 0], "tag": "Nil", "left": "Nil", "right": "Nil" }, + }, + ) +} diff --git a/next/tutorial/example/segment-tree/segment-tree.md b/next/tutorial/example/segment-tree/segment-tree.md index aec57e6d..ddbd525f 100644 --- a/next/tutorial/example/segment-tree/segment-tree.md +++ b/next/tutorial/example/segment-tree/segment-tree.md @@ -47,11 +47,10 @@ This section only discusses the query operation; we will elaborate on the princi We use a classic approach to represent the Segment Tree: -```moonbit -enum Node { - Nil, - Node(Int, Node, Node) -} derive(Show) +```{literalinclude} /sources/segment-tree/src/part1/top.mbt +:language: moonbit +:start-after: start node definition +:end-before: end node definition ``` Here, `Nil` represents an empty tree, while a `Node` contains the stored data (of type Int) and its left and right children. @@ -64,30 +63,20 @@ Building the tree refers to the process of abstracting a linear sequence into a To start, we should write an overloaded `op_add` function for the `Node` type to assist with the tree-building process: -```moonbit -fn op_add(self : Node, v : Node) -> Node { - match (self, v) { - (Node(left, _, _), Node(right, _, _)) => Node(left + right, self, v) - (Node(_), Nil) => self - (Nil, Node(_)) => v - (Nil, Nil) => Nil - } -} +```{literalinclude} /sources/segment-tree/src/part1/top.mbt +:language: moonbit +:start-after: start op_add definition +:end-before: end op_add definition ``` With this operation defined, we can easily merge two `Node` instances while maintaining the segment sums, laying the foundation for building the tree. In some descriptions of Segment Trees, this process is also called `pushup`. We can leverage MoonBit's `ArrayView` feature (known as `slice` in some languages) to recursively build the tree from a segment of a linear structure at a low cost, achieving O(Log N) complexity: -```moonbit -fn build(data : ArrayView[Int]) -> Node { - if data.length() == 1 { - Node(data[0], Nil, Nil) - } else { - let mid = (data.length() + 1) >> 1 - build(data[0:mid]) + build(data[mid:]) - } -} +```{literalinclude} /sources/segment-tree/src/part1/top.mbt +:language: moonbit +:start-after: start build definition +:end-before: end build definition ``` Let’s analyze this code: @@ -97,19 +86,12 @@ Let’s analyze this code: This code is concise, highly readable, and optimization-friendly, serving as a great learning paradigm for other data structures. -Now, let's build a tree and output it: - -```moonbit -fn main { - let tree = build([1, 2, 3, 4, 5][:]) - println(tree) -} -``` - -The output will be: +Now, let's build a tree and test it: -```bash -Node(15, Node(6, Node(3, Node(1, Nil, Nil), Node(2, Nil, Nil)), Node(3, Nil, Nil)), Node(9, Node(4, Nil, Nil), Node(5, Nil, Nil))) +```{literalinclude} /sources/segment-tree/src/part1/top.mbt +:language: moonbit +:start-after: start build test +:end-before: end build test ``` Great! We've successfully built the tree! @@ -118,21 +100,11 @@ Great! We've successfully built the tree! Next, we need to implement the query function. Since the nodes of our Segment Tree maintain segment sums, we can write a `query` function to retrieve these sums: -```moonbit -let empty_node : Node = Node(0, Nil, Nil) - -fn query(self : Node, l : Int, r : Int, query_l : Int, query_r : Int) -> Node { - if query_l > r || l > query_r { - empty_node - } else if query_l <= l && query_r >= r { - self - } else { - let Node(_, left, right) = self - let mid = (l + r) >> 1 - left.query(l, mid, query_l, query_r) + - right.query(mid + 1, r, query_l, query_r) - } -} +```{literalinclude} /sources/segment-tree/src/part1/top.mbt +:language: moonbit +:start-after: start query definition +:end-before: end query definition +:emphasize-lines: 9 ``` Here, `l` and `r` represent the currently queried range, while `query_l` and `query_r` denote the range we need to query. Let's break down this implementation: @@ -141,6 +113,20 @@ Here, `l` and `r` represent the currently queried range, while `query_l` and `qu - If the current range is a subset of the queried range, it fully contributes to the result, so we return it directly. - If the current range overlaps with the queried range, we continue searching downwards to find the exact covering ranges, merging the results of the left and right nodes. +#### Before We Continue + +Notice the highlighted line. When using the `let` to destructure `Node`, we could be sure that the enum being destructured wasn’t `Nil`. However, the compiler couldn't guarantee this, so we would have received a warning for using: + +```moonbit +let Node(x, y) = z +``` + +Although it didn’t affect execution, it was somewhat misleading. With MoonBit’s newly introduced `guard` statement, we can now handle this better using: + +```moonbit +guard let Node(x, y) = z +``` + ### Q&A - **Q:** Why use `Node` as the return value? Can't I destructure and sum the values directly? @@ -151,28 +137,19 @@ Here, `l` and `r` represent the currently queried range, while `query_l` and `qu Now, let's test the query process: -```moonbit -fn main { - let tree = build([1, 2, 3, 4, 5][:]) - let sum = match tree.query(1, 5, 1, 3) { - Node(sum, _, _) => sum - _ => panic() - } - println(sum) -} +```{literalinclude} /sources/segment-tree/src/part1/top.mbt +:language: moonbit +:start-after: start query test +:end-before: end query test ``` -The output will be: - -```bash -6 -``` +The output is `6`. Fantastic! We've obtained the correct output! ### Code -For the complete code, please check the [GitHub repository](https://github.com/moonbit-community/MoonBit-SegmentTree/blob/main/1/main.mbt). +For the complete code, please check the [GitHub repository](https://github.com/moonbitlang/moonbit-docs/tree/main/next/sources/segment-tree/src/part1/top.mbt). ## Conclusion diff --git a/next/tutorial/example/segment-tree/segment-tree2.md b/next/tutorial/example/segment-tree/segment-tree2.md index 45260d84..ba80f0f9 100644 --- a/next/tutorial/example/segment-tree/segment-tree2.md +++ b/next/tutorial/example/segment-tree/segment-tree2.md @@ -36,38 +36,14 @@ Let’s dive into the code! ## Implementation -### Before We Begin - -In the last lesson, when using the `let` to destructure `Node`, we could be sure that the enum being destructured wasn’t `Nil`. However, the compiler couldn't guarantee this, so we received a warning for using: - -```moonbit -let Node(x, y) = z -``` - -Although it didn’t affect execution, it was somewhat misleading. With MoonBit’s newly introduced `guard` statement, we can now handle this better using: - -```moonbit -guard let Node(x, y) = z -``` - ### Basic Definitions In the previous code, we defined the segment tree using `enum`. However, none of the elements were clearly named, which was manageable when the data size was small. Now, we need to add **Tag** and **Length** attributes, so it makes sense to use labeled arguments in the `enum` definition: -```moonbit -enum Data { - Data(~sum: Int, ~len: Int) -} derive(Show) - -enum LazyTag { - Nil - Tag(Int) -} derive(Show) - -enum Node { - Nil - Node(~data: Data, ~tag: LazyTag, ~left: Node, ~right: Node) -} derive(Show) +```{literalinclude} /sources/segment-tree/src/part2/top.mbt +:language: moonbit +:start-after: start data definition +:end-before: end data definition ``` This allows for clearer initialization and pattern matching, making the code easier to follow. We've also abstracted the `Data` type, adding a `len` attribute to represent the length of the current range, which is useful for calculating the node's value. @@ -76,89 +52,42 @@ This allows for clearer initialization and pattern matching, making the code eas Similar to the last lesson, before building the tree, we need to define the addition operations between `Node` types. However, since we’ve abstracted `Data`, we must account for its addition too: -```moonbit -fn op_add(self: Data, v: Data) -> Data { - match (self, v) { - (Data(sum=a, len=len_a), Data(sum=b, len=len_b)) => - Data(sum=a + b, len=len_a + len_b) - } -} - -fn op_add(self: Node, v: Node) -> Node { - match (self, v) { - (Node(data=l, ..), Node(data=r, ..)) => - Node(data=l + r, tag=Nil, left=self, right=v) - (Node(_), Nil) => self - (Nil, Node(_)) => v - (Nil, Nil) => Nil - } -} +```{literalinclude} /sources/segment-tree/src/part2/top.mbt +:language: moonbit +:start-after: start op_add definition +:end-before: end op_add definition ``` Here, we’ve ignored merging LazyTags for now and set the resulting tag to `Nil` because once a node is reached, its parent’s LazyTag no longer applies. Now, we can implement the tree-building function: -```moonbit -fn build(data: ArrayView[Int]) -> Node { - if data.length() == 1 { - Node(data=Data(sum=data[0], len=1), tag=Nil, left=Nil, right=Nil) - } else { - let mid = (data.length() + 1) >> 1 - build(data[0:mid]) + build(data[mid:]) - } -} +```{literalinclude} /sources/segment-tree/src/part2/top.mbt +:language: moonbit +:start-after: start build definition +:end-before: end build definition ``` ### LazyTag and Range Modifications A node receiving a LazyTag is handled by the `apply` function. The key logic here is how the tag is merged and how the value is computed based on the node’s length: -```moonbit -fn op_add(self: LazyTag, v: LazyTag) -> LazyTag { - match (self, v) { - (Tag(a), Tag(b)) => Tag(a + b) - (Nil, t) | (t, Nil) => t - } -} - -fn apply(self: Node, v: LazyTag) -> Node { - match (self, v) { - (Node(data=Data(sum=a, len=length), ~tag, ~left, ~right), Tag(v) as new_tag) => - Node( - data=Data(sum=a + v * length, len=length), - tag=tag + new_tag, - ~left, - ~right, - ) - (_, Nil) => self - (Nil, _) => Nil - } -} + +```{literalinclude} /sources/segment-tree/src/part2/top.mbt +:language: moonbit +:start-after: start lazytag definition +:end-before: end lazytag definition ``` This code allows a node to compute its value based on its range length and the applied LazyTag. It also merges existing tags correctly. Next, we implement range modifications: -```moonbit -fn modify( - self: Node, - l: Int, - r: Int, - modify_l: Int, - modify_r: Int, - tag: LazyTag -) -> Node { - if modify_l > r || l > modify_r { - self - } else if modify_l <= l && modify_r >= r { - self.apply(tag) - } else { - guard let Node(~left, ~right, ..) = self - left.apply(tag) + right.apply(tag) - } -} + +```{literalinclude} /sources/segment-tree/src/part2/top.mbt +:language: moonbit +:start-after: start modify definition +:end-before: end modify definition ``` The logic is similar to the query function from the previous lesson, but now each relevant node applies the necessary LazyTag for the modification. @@ -169,26 +98,10 @@ Interestingly, even with range modifications, this segment tree remains persiste For queries, we need to remember to push the LazyTag downwards: -```moonbit -let empty_node: Node = Node( - data=Data(sum=0, len=0), - tag=Nil, - left=Nil, - right=Nil, -) - -fn query(self: Node, l: Int, r: Int, query_l: Int, query_r: Int) -> Node { - if query_l > r || l > query_r { - empty_node - } else if query_l <= l && query_r >= r { - self - } else { - guard let Node(~tag, ~left, ~right, ..) = self - let mid = (l + r) >> 1 - left.apply(tag).query(l, mid, query_l, query_r) + - right.apply(tag).query(mid + 1, r, query_l, query_r) - } -} +```{literalinclude} /sources/segment-tree/src/part2/top.mbt +:language: moonbit +:start-after: start query definition +:end-before: end query definition ``` ## Conclusion @@ -197,4 +110,4 @@ With this, we have a segment tree that supports range modifications and is much In the next lesson, we’ll add multiplication support to the segment tree and explore some use cases for immutable segment trees. Stay tuned! -Full code is available [here](https://github.com/moonbit-community/MoonBit-SegmentTree/blob/main/2/main.mbt). +Full code is available [here](https://github.com/moonbitlang/moonbit-docs/tree/main/next/sources/segment-tree/src/part2/top.mbt).