From 36621d3cffba34655184563ef3f143535bfcc169 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kasper=20Overg=C3=A5rd=20Nielsen?= Date: Thu, 16 Nov 2023 14:43:41 +0100 Subject: [PATCH 1/5] Update pubspecs --- example/pubspec.yaml | 6 +++--- pubspec.yaml | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/example/pubspec.yaml b/example/pubspec.yaml index 122ecb9..0fbcc9c 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -6,8 +6,8 @@ publish_to: 'none' version: 1.0.0+1 environment: - flutter: ^3.10.0 - sdk: ">=3.0.0 <4.0.0" + flutter: ^3.16.0 + sdk: ^3.2.0 dependencies: flutter: @@ -19,7 +19,7 @@ dependencies: dev_dependencies: flutter_test: sdk: flutter - flutter_lints: ^2.0.0 + flutter_lints: ^3.0.0 flutter: diff --git a/pubspec.yaml b/pubspec.yaml index 7e8d763..30f3cbf 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -9,8 +9,8 @@ version: 1.0.0 homepage: https://github.com/nielsenko/treap environment: - sdk: '>=3.0.0 <4.0.0' + sdk: ^3.2.0 dev_dependencies: - lints: ^2.0.0 + lints: ^3.0.0 test: ^1.17.5 From b3fc386832f8fd0451d6f61f15fe7498e9acb52a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kasper=20Overg=C3=A5rd=20Nielsen?= Date: Thu, 16 Nov 2023 14:44:43 +0100 Subject: [PATCH 2/5] Fix super.key --- example/lib/ui/edit_task_page.dart | 2 +- example/lib/ui/task_hero.dart | 4 ++-- example/lib/ui/task_tile.dart | 4 ++-- example/lib/ui/todo_list_page.dart | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/example/lib/ui/edit_task_page.dart b/example/lib/ui/edit_task_page.dart index f76b2f5..5fc6ce6 100644 --- a/example/lib/ui/edit_task_page.dart +++ b/example/lib/ui/edit_task_page.dart @@ -7,7 +7,7 @@ import 'task_hero.dart'; class EditTaskPage extends StatefulWidget { final Task task; - const EditTaskPage({Key? key, required this.task}) : super(key: key); + const EditTaskPage({super.key, required this.task}); @override State createState() => _EditTaskPageState(); diff --git a/example/lib/ui/task_hero.dart b/example/lib/ui/task_hero.dart index 0116e0c..bd896bd 100644 --- a/example/lib/ui/task_hero.dart +++ b/example/lib/ui/task_hero.dart @@ -7,10 +7,10 @@ class TaskHero extends StatelessWidget { final double? radius; const TaskHero({ - Key? key, + super.key, required this.task, this.radius, - }) : super(key: key); + }); @override Widget build(BuildContext context) { diff --git a/example/lib/ui/task_tile.dart b/example/lib/ui/task_tile.dart index e29cf88..f33921f 100644 --- a/example/lib/ui/task_tile.dart +++ b/example/lib/ui/task_tile.dart @@ -14,13 +14,13 @@ class TaskTile extends StatelessWidget { final Animation animation; const TaskTile({ - Key? key, + super.key, required this.task, required this.onCompletionChanged, required this.animation, this.onDismissed, this.onTap, - }) : super(key: key); + }); @override Widget build(BuildContext context) { diff --git a/example/lib/ui/todo_list_page.dart b/example/lib/ui/todo_list_page.dart index 23a8af1..949b492 100644 --- a/example/lib/ui/todo_list_page.dart +++ b/example/lib/ui/todo_list_page.dart @@ -10,7 +10,7 @@ import 'task_tile.dart'; class TodoListPage extends StatefulWidget { final String title; - const TodoListPage({Key? key, required this.title}) : super(key: key); + const TodoListPage({super.key, required this.title}); @override State createState() => _TodoListPageState(); From 3e21a012904d1a01784f3cc2cf189510d1258b07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kasper=20Overg=C3=A5rd=20Nielsen?= Date: Thu, 16 Nov 2023 15:34:23 +0100 Subject: [PATCH 3/5] Comment public interface (with GPT4 - a bit verbose) to get prefect PANA score --- lib/src/treap_base.dart | 51 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 2 deletions(-) diff --git a/lib/src/treap_base.dart b/lib/src/treap_base.dart index e12b4d8..105f4e6 100644 --- a/lib/src/treap_base.dart +++ b/lib/src/treap_base.dart @@ -4,60 +4,107 @@ import 'node.dart'; final _rnd = Random(42); +// Treap class +// A treap is a type of binary search tree data structure that maintains a dynamic +// set of ordered keys and allows binary search tree operations in addition to operations +// like add, find, and erase in O(log n) time. The name 'Treap' is a portmanteau +// of tree and heap, as the tree maintains its shape using heap properties. class Treap> { final Node? _root; const Treap._(this._root); const Treap() : this._(null); - // TODO: This is O(N log(N))! An O(N) algorithm exists, if items are sorted. - // The advantage is, this is simpler, and it works even in the unsorted case. + // The build method takes an iterable of items and constructs a treap from them. + // It does this by folding over the items, adding each one to the treap in turn. + // This method is O(N log(N)) in complexity. An O(N) algorithm exists if the items + // are sorted. However, this method is simpler and works even if the items are not + // sorted. factory Treap.build(Iterable items) => items.fold(Treap(), (acc, i) => acc.add(i)); + // Adds an item to the treap. Creates a new node with the item and a random priority. + // The new node is then added to the root of the treap. Returns a new treap with the + // added node. Treap add(T item) => Treap._(_root.add(Node(item, _rnd.nextInt(1 << 32)))); + // Erases an item from the treap. Returns a new treap without the erased item. Treap erase(T item) => Treap._(_root.erase(item)); + // Checks if the treap is empty. Returns true if the treap is empty, false otherwise. bool get isEmpty => _root == null; + // Returns the size of the treap. int get size => _root.size; + // Finds an item in the treap. Returns the item if found, null otherwise. T? find(T item) => _root.find(item)?.item; + + // Checks if an item exists in the treap. Returns true if the item exists, false + // otherwise. bool has(T item) => find(item) != null; + // Returns the rank of an item in the treap. int rank(T item) => _root.rank(item); + // Selects an item in the treap by its rank. Returns the selected item. T select(int rank) => _root.select(rank).item; + // Returns all the values in the treap. Iterable get values => _root.values; + // Returns the first item in the treap, or null if the treap is empty. T? get firstOrDefault => _root?.min; + + // Returns the last item in the treap, or null if the treap is empty. T? get lastOrDefault => _root?.max; + // Returns the first item in the treap. Throws an error if the treap is empty. T get first => firstOrDefault ?? (throw StateError('No element')); + + // Returns the last item in the treap. Throws an error if the treap is empty. T get last => lastOrDefault ?? (throw StateError('No element')); + // Returns the previous item in the treap for a given item. T prev(T item) => _root.select(rank(item) - 1).item; + + // Returns the next item in the treap for a given item. T next(T item) => _root.select(rank(item) + 1).item; + // Returns a new treap with the first n items. If n is greater than the size of the treap, returns the original treap. Treap take(int n) => n < size ? Treap._(_root.split(_root.select(n).item).low) : this; + + // Skips the first n items and returns a new treap with the remaining items. If n is greater than or equal to the size of the treap, returns an empty treap. Treap skip(int n) { if (n >= size) return Treap(); // empty final split = _root.split(_root.select(n).item); return Treap._(split.high.union(split.middle)); } + // Returns a new treap that is the union of this treap and another treap. Treap union(Treap other) => Treap._(_root.union(other._root)); + + // Returns a new treap that is the intersection of this treap and another treap. Treap intersection(Treap other) => Treap._(_root.intersection(other._root)); + + // Returns a new treap that is the difference of this treap and another treap. Treap difference(Treap other) => Treap._(_root.difference(other._root)); + // Operator overloading for adding an item to the treap. Returns a new treap with the added item. Treap operator +(T item) => add(item); + + // Operator overloading for the union of this treap and another treap. Returns a new treap that is the union of the two treaps. Treap operator |(Treap other) => union(other); + + // Operator overloading for the intersection of this treap and another treap. Returns a new treap that is the intersection of the two treaps. Treap operator &(Treap other) => intersection(other); + + // Operator overloading for the difference of this treap and another treap. Returns a new treap that is the difference of the two treaps. Treap operator -(Treap other) => difference(other); + + // Operator overloading for selecting an item in the treap by its rank. Returns the selected item. T operator [](int i) => select(i); } From 0dbf36fe163a8bd3719c951bd61bf3789cc325ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kasper=20Overg=C3=A5rd=20Nielsen?= Date: Thu, 16 Nov 2023 15:41:19 +0100 Subject: [PATCH 4/5] Fixup generated comments --- lib/src/treap_base.dart | 83 ++++++++++++++++++++++------------------- 1 file changed, 45 insertions(+), 38 deletions(-) diff --git a/lib/src/treap_base.dart b/lib/src/treap_base.dart index 105f4e6..944f1ab 100644 --- a/lib/src/treap_base.dart +++ b/lib/src/treap_base.dart @@ -4,107 +4,114 @@ import 'node.dart'; final _rnd = Random(42); -// Treap class -// A treap is a type of binary search tree data structure that maintains a dynamic -// set of ordered keys and allows binary search tree operations in addition to operations -// like add, find, and erase in O(log n) time. The name 'Treap' is a portmanteau -// of tree and heap, as the tree maintains its shape using heap properties. +/// Treap class +/// A treap is a type of binary search tree data structure that maintains a dynamic +/// set of ordered keys and allows binary search tree operations in addition to operations +/// like add, find, and erase in O(log n) time. The name 'Treap' is a portmanteau +/// of tree and heap, as the tree maintains its shape using heap properties. class Treap> { final Node? _root; const Treap._(this._root); const Treap() : this._(null); - // The build method takes an iterable of items and constructs a treap from them. - // It does this by folding over the items, adding each one to the treap in turn. - // This method is O(N log(N)) in complexity. An O(N) algorithm exists if the items - // are sorted. However, this method is simpler and works even if the items are not - // sorted. + /// The build method takes an iterable of items and constructs a treap from them. + /// It does this by folding over the items, adding each one to the treap in turn. + /// This method is O(N log(N)) in complexity. An O(N) algorithm exists if the items + /// are sorted. However, this method is simpler and works even if the items are not + /// sorted. factory Treap.build(Iterable items) => items.fold(Treap(), (acc, i) => acc.add(i)); - // Adds an item to the treap. Creates a new node with the item and a random priority. - // The new node is then added to the root of the treap. Returns a new treap with the - // added node. + /// Adds an item to the treap. Creates a new node with the item and a random priority. + /// The new node is then added to the root of the treap. Returns a new treap with the + /// added node. Treap add(T item) => Treap._(_root.add(Node(item, _rnd.nextInt(1 << 32)))); - // Erases an item from the treap. Returns a new treap without the erased item. + /// Erases an item from the treap. Returns a new treap without the erased item. Treap erase(T item) => Treap._(_root.erase(item)); - // Checks if the treap is empty. Returns true if the treap is empty, false otherwise. + /// Checks if the treap is empty. Returns true if the treap is empty, false otherwise. bool get isEmpty => _root == null; - // Returns the size of the treap. + /// Returns the size of the treap. int get size => _root.size; - // Finds an item in the treap. Returns the item if found, null otherwise. + /// Finds an item in the treap. Returns the item if found, null otherwise. T? find(T item) => _root.find(item)?.item; - // Checks if an item exists in the treap. Returns true if the item exists, false - // otherwise. + /// Checks if an item exists in the treap. Returns true if the item exists, false + /// otherwise. bool has(T item) => find(item) != null; - // Returns the rank of an item in the treap. + /// Returns the rank of an item in the treap. int rank(T item) => _root.rank(item); - // Selects an item in the treap by its rank. Returns the selected item. + /// Selects an item in the treap by its rank. Returns the selected item. T select(int rank) => _root.select(rank).item; - // Returns all the values in the treap. + /// Returns all the values in the treap. Iterable get values => _root.values; - // Returns the first item in the treap, or null if the treap is empty. + /// Returns the first item in the treap, or null if the treap is empty. T? get firstOrDefault => _root?.min; - // Returns the last item in the treap, or null if the treap is empty. + /// Returns the last item in the treap, or null if the treap is empty. T? get lastOrDefault => _root?.max; - // Returns the first item in the treap. Throws an error if the treap is empty. + /// Returns the first item in the treap. Throws an error if the treap is empty. T get first => firstOrDefault ?? (throw StateError('No element')); - // Returns the last item in the treap. Throws an error if the treap is empty. + /// Returns the last item in the treap. Throws an error if the treap is empty. T get last => lastOrDefault ?? (throw StateError('No element')); - // Returns the previous item in the treap for a given item. + /// Returns the previous item in the treap for a given item. T prev(T item) => _root.select(rank(item) - 1).item; - // Returns the next item in the treap for a given item. + /// Returns the next item in the treap for a given item. T next(T item) => _root.select(rank(item) + 1).item; - // Returns a new treap with the first n items. If n is greater than the size of the treap, returns the original treap. + /// Returns a new treap with the first n items. If n is greater than the size of + /// the treap, returns the original treap. Treap take(int n) => n < size ? Treap._(_root.split(_root.select(n).item).low) : this; - // Skips the first n items and returns a new treap with the remaining items. If n is greater than or equal to the size of the treap, returns an empty treap. + /// Skips the first n items and returns a new treap with the remaining items. + /// If n is greater than or equal to the size of the treap, returns an empty treap. Treap skip(int n) { if (n >= size) return Treap(); // empty final split = _root.split(_root.select(n).item); return Treap._(split.high.union(split.middle)); } - // Returns a new treap that is the union of this treap and another treap. + /// Returns a new treap that is the union of this treap and another treap. Treap union(Treap other) => Treap._(_root.union(other._root)); - // Returns a new treap that is the intersection of this treap and another treap. + /// Returns a new treap that is the intersection of this treap and another treap. Treap intersection(Treap other) => Treap._(_root.intersection(other._root)); - // Returns a new treap that is the difference of this treap and another treap. + /// Returns a new treap that is the difference of this treap and another treap. Treap difference(Treap other) => Treap._(_root.difference(other._root)); - // Operator overloading for adding an item to the treap. Returns a new treap with the added item. + /// Operator overloading for adding an item to the treap. Returns a new treap with + /// the added item. Treap operator +(T item) => add(item); - // Operator overloading for the union of this treap and another treap. Returns a new treap that is the union of the two treaps. + /// Operator overloading for the union of this treap and another treap. + /// Returns a new treap that is the union of the two treaps. Treap operator |(Treap other) => union(other); - // Operator overloading for the intersection of this treap and another treap. Returns a new treap that is the intersection of the two treaps. + /// Operator overloading for the intersection of this treap and another treap. + /// Returns a new treap that is the intersection of the two treaps. Treap operator &(Treap other) => intersection(other); - // Operator overloading for the difference of this treap and another treap. Returns a new treap that is the difference of the two treaps. + /// Operator overloading for the difference of this treap and another treap. + /// Returns a new treap that is the difference of the two treaps. Treap operator -(Treap other) => difference(other); - // Operator overloading for selecting an item in the treap by its rank. Returns the selected item. + /// Operator overloading for selecting an item in the treap by its rank. + /// Returns the selected item. T operator [](int i) => select(i); } From ffd2ef3c4216d2112fef398226c321e49137f8bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kasper=20Overg=C3=A5rd=20Nielsen?= Date: Thu, 16 Nov 2023 15:41:51 +0100 Subject: [PATCH 5/5] .. treaps a word --- .vscode/settings.json | 1 + 1 file changed, 1 insertion(+) diff --git a/.vscode/settings.json b/.vscode/settings.json index 2906b9f..7a7fbb0 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,6 +1,7 @@ { "cSpell.words": [ "Treap", + "treaps", "upsert" ] } \ No newline at end of file