동일한 우선순위를 가진 여러 항목을 지원하는 순서 있는 집합 구현을 제공하는 Dart 패키지입니다. 동일한 우선순위를 가진 요소에 대해 삽입 순서를 유지하여 순서가 필요한 응용 프로그램에서 예측 가능하고 효율적인 정렬을 가능하게 합니다. 중복된 우선순위가 예상되지만 순서 유지가 필수적인 경우에 적합합니다. 이 패키지는 가볍고 모든 주요 플랫폼과 호환되며 pub.dev를 통해 Flutter 프로젝트와 원활하게 통합됩니다.
사용 사례
우선순위 기반 작업 스케줄링
중복을 포함한 정렬된 로그 유지
티 브레이킹을 사용한 우선순위 큐 구현
동등한 긴급성으로 이벤트 시퀀스 관리
데이터 파이프라인에서의 사용자 정의 정렬
주문 제약 조건을 갖춘 실시간 데이터 처리
주요 기능
동일한 우선순위를 가진 여러 항목을 지원
동일한 우선 순위 내의 삽입 순서를 유지합니다
크로스 플랫폼 호환성
경량이며 통합이 쉬움
효율적인 검색 및 삽입
외부 종속성 없음
적합한 대상
중복 우선순위가 있는 정렬된 컬렉션을 필요로 하는 개발자
결정론적 순서가 필요한 응용 프로그램
복잡한 데이터 정렬이 필요한 플러터 앱
시간 민감성 또는 우선순위 기반 워크플로우를 관리하는 시스템
맞춤형 대기열 또는 버퍼 시스템을 구축하는 엔지니어
고려 사항
성능 상의 트레이드오프로 인해 고주기 업데이트에 적합하지 않습니다
단일 키 정렬에 제한됨(다중 레벨 정렬 불가)
우선 순위 변경을 수동으로 처리해야 합니다
중간 규모의 데이터 세트에 가장 적합
역방향 반복을 기본적으로 지원하지 않습니다
중복 항목이 많을 경우 표준 세트보다 더 많은 메모리를 사용할 수 있습니다
원본 README
연결된 소스 저장소의 README 원문 스냅샷입니다. 최신 내용은 저장소에서 확인하세요.
ordered_set
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).
In order to assist the creation of Comparators, the Comparing class can be used:
// sort by name lengthfinal people = OrderedSet.comparing<Person>(Comparing.on((p) => p.name.length));
// sort by name descfinal people = OrderedSet.comparing<Person>(Comparing.reverse(Comparing.on((p) => p.name)));
// sort by role and then by namefinal 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!