Skip to content

Commit

Permalink
feat: Add powerset function and PowersetOnIterableExtension.
Browse files Browse the repository at this point in the history
  • Loading branch information
robmllze committed Nov 10, 2024
1 parent e8fd547 commit a1e1a8d
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 10 deletions.
12 changes: 3 additions & 9 deletions lib/src/_index.g.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
//.title
// ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
//
// GENERATED BY DF GEN - DO NOT MODIFY BY HAND
// GENERATED - DO NOT MODIFY BY HAND
// See: https://github.com/robmllze/df_gen
//
// ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
//.title~

// --- PUBLIC FILES ---
export 'unique_entries.dart';
export 'merge.dart';
export 'maybe_add_to.dart';
Expand All @@ -28,10 +27,5 @@ export 'extensions/traverse_map_on_map_extension.dart';
export 'extensions/generic_type_extension.dart';
export 'extensions/second_to_ninth_element_on_iterable_extension.dart';
export 'extensions/non_nulls_on_map_extension.dart';
export 'extensions/map_filter_extension.dart';

// --- PRIVATE FILES (EXCLUDED) ---
// export '_index.g.dart';

// --- GENERATED FILES (EXCLUDED) ---
// export '_index.g.dart';
export 'extensions/powerset_on_iterable_extension.dart';
export 'extensions/map_filter_extension.dart';
69 changes: 69 additions & 0 deletions lib/src/extensions/powerset_on_iterable_extension.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//.title
// ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
//
// Dart/Flutter (DF) Packages by DevCetra.com & contributors. The use of this
// source code is governed by an MIT-style license described in the LICENSE
// file located in this project's root directory.
//
// See: https://opensource.org/license/mit
//
// ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
//.title~

extension PowersetOnIterableExtension<T> on Iterable<T> {
/// Returns the powerset using the provided [combinator].
///
/// The powerset of a set is the set of all possible subsets of the set.
///
/// The [combinator] is a function that combines two elements of the set.
///
/// For example, given the set {1, 2, 3} and the combinator `+`, the powerset
/// would be {1, 2, 3, 1+2, 1+3, 2+3, 1+2+3}.
Iterable<T> powerset(T Function(T a, T b) combinator) {
return _powerset(
map((e) => {e}).toList(),
combinator,
);
}
}

final _powerset = powerset;

/// Returns the powerset of the given [source] using the provided [combinator].
///
/// The powerset of a set is the set of all possible subsets of the set.
///
/// The [combinator] is a function that combines two elements of the set.
///
/// For example, given the set {1, 2, 3} and the combinator `+`, the powerset
/// would be {1, 2, 3, 1+2, 1+3, 2+3, 1+2+3}.
Iterable<T> powerset<T>(
Iterable<Iterable<T>> source,
T Function(T a, T b) combinator,
) {
// Filter out empty sets to simplify logic
final input = source.where((e) => e.isNotEmpty).toList();

if (input.isEmpty) {
return {};
} else if (input.length == 1) {
return input[0];
} else {
// Combine the first two sets using the provided combinator
final combined = <T>{};
for (final a in input[0]) {
for (final b in input[1]) {
combined.add(combinator(a, b));
}
}

// Recursively process the remaining sets
return powerset(
[
combined,
...input.skip(2),
],
combinator,
);
}
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

name: df_collection
description: A package designed to extend Dart collections by offering additional functionality.
version: 0.6.1
version: 0.6.2
repository: https://github.com/robmllze/df_collection
funding:
- https://www.buymeacoffee.com/robmllze
Expand Down

0 comments on commit a1e1a8d

Please sign in to comment.