搜尋套件、儲存庫與 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!

所屬分類

原始碼儲存庫

使用此套件的專案