搜索插件、代码仓与 APP 分类

插件详情v8.0.1

ordered_set

一个简单的 Dart 有序集合实现,允许具有相同优先级的多个项目。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
16.8万
喜欢数
35
Pub 分数
160

安装插件

flutter pub add ordered_set

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

一个 Dart 包,提供支持具有相同优先级的多个项目的有序集合实现。它为具有相同优先级的元素保持插入顺序,使在需要有序集合的应用程序中能够进行可预测且高效的排序。适用于预期存在重复优先级但必须保持顺序的场景。该包轻量级,可在所有主要平台上兼容,并可通过 pub.dev 与 Flutter 项目无缝集成。

适合场景

  • 基于优先级的任务调度
  • 维护带有重复项的有序日志
  • 使用优先级队列实现优先级冲突解决
  • 以同等紧迫性管理事件序列
  • 数据管道中的自定义排序
  • 实时数据处理与订单约束

主要特点

  • 支持具有相同优先级的多个项目
  • 保留相同优先级内的插入顺序
  • 跨平台兼容性
  • 轻量且易于集成
  • 高效的查找和插入
  • 无外部依赖

适合对象

  • 需要有序集合且具有重复优先级的开发人员
  • 需要确定性排序的应用程序
  • 具有复杂数据排序需求的 Flutter 应用
  • 管理时间敏感或优先级驱动的工作流程的系统
  • 工程师正在构建自定义队列或缓冲系统

使用建议

  • 由于性能权衡,不适用于高频更新
  • 仅限单键排序(不支持多级排序)
  • 需要手动处理优先级更改
  • 最适合中等大小的数据集
  • 不原生支持反向迭代
  • 当存在大量重复项时,所消耗的内存可能比标准集合更多

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

ordered_set

Pub Version Build Status Coverage Status

A simple implementation for an Ordered Set for Dart.

It accepts either a comparator function that compares items for their priority or a mapper function that maps items to their priority.

Unlike Dart's SplayTreeSet or SplayTreeMap classes, it allows for several different elements with the same "priority" to be added.

It also implements Iterable, allowing you to iterate the contents (in order) in O(n) (no additional overhead).

Usage

A simple usage example:

  import 'package:ordered_set/ordered_set.dart';

  main() {
    final items = OrderedSet.simple<num, int>();
    items.add(2);
    items.add(1);
    print(items.toList()); // [1, 2]
  }

But it can accept multiple items with the same priority:

  import 'package:ordered_set/ordered_set.dart';

  main() {
    final items = OrderedSet.mapping<String, Person>((p) => p.name);
    items.add(Person('Alice', 'Engineering'));
    items.add(Person('Bob', 'Accounting'));
    items.add(Person('Alice', 'Marketing'));
    print(items.toList()); // [Alice, Alice, Bob]
  }

Comparing

In order to assist the creation of Comparators, the Comparing class can be used:

  // sort by name length
  final people = OrderedSet.comparing<Person>(Comparing.on((p) => p.name.length));

  // sort by name desc
  final people = OrderedSet.comparing<Person>(Comparing.reverse(Comparing.on((p) => p.name)));

  // sort by role and then by name
  final people = OrderedSet.comparing<Person>(Comparing.join([(p) => p.role, (p) => p.name]));

Note that you could instead just create a MappingOrderedSet instead:

  final people = OrderedSet.mapping<num, Person>((p) => p.name.length);
  // ...

Mapping vs Comparing

There are two main implementations of the OrderedSet interface:

  • ComparingOrderedSet: the simplest implementation, takes in a Comparator and does not cache priorities. It uses Dart's SplayTreeSet as a backing implementation.
  • MappingOrderedSet: a slightly more advanced implementation that takes in a mapper function (maps elements to their priorities) and caches them. It uses Dart's SplayTreeMap as a backing implementation.

In order to create an OrderedSet, however, you can just use the static methods on the interface itself:

  • OrderedSet.comparing<E>([comparator]): creates a ComparingOrderedSet with the given Comparator.
  • OrderedSet.mapping<K, E>([mapper]): creates a MappingOrderedSet with the given mapper function.
  • OrderedSet.comparable<K, E>(): if E extends Comparable<K>, this is a simpler way of creating a MappingOrderedSet with identity mapping.
  • OrderedSet.simple<E>(): if E extends Comparable<E>, this is an even simpler way of creating a MappingOrderedSet with identity mapping.

Querying

You can [register] a set of queries, i.e., predefined sub-types, whose results, i.e., subsets of this set, are then cached. Since the queries have to be type checks, and types are runtime constants, this can be vastly optimized.

You can then filter by type by using the [query] method (or using [whereType]; which is overridden).

Note that you can change [strictMode] to allow for querying for unregistered types; if you do so, the registration cost is payed on the first query.

Contributing

All contributions are very welcome! Please feel free to create Issues, help us with PR's or comment your suggestions, feature requests, bugs, et cetera. Give us a star if you liked it!

所属分类

源码仓库

使用此插件的项目