ordered_set 1.1.2

ordered_set #

Build Status Coverage Status

A simple implementation for an ordered set for Dart.

It accepts a compare function that compares items for their priority.

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

It also implements Iterable, so you can iterate it in O(n).

Usage #

A simple usage example:

  import 'package:ordered_set/ordered_set.dart';

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

Comparing #

In order to assist the creation of OrderedSet's, there is a Comparing class to easily create Comparables:

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

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

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

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!

1.1.0 #

  • Improving documentation via dartdocs

1.0.0 #

  • Adding Comparing class

0.1.0 #

  • Initial version, adding Ordered Set with basic operations

example/ordered_set_example.dart

import 'package:ordered_set/ordered_set.dart';
import 'package:ordered_set/comparing.dart';

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

  OrderedSet<Person> a = new OrderedSet((a, b) => a.age - b.age);
  a.add(new Person(12, 'Klaus'));
  a.add(new Person(1, 'Sunny'));
  a.add(new Person(14, 'Violet'));
  print(a.elementAt(0).name); // Sunny
  print(a.elementAt(2).name); // Violet

  a.add(new Person(13, 'Isadora'));
  a.add(new Person(13, 'Duncan'));
  a.add(new Person(13, 'Quigley'));
  print(a.toList().map((e) => e.name));
  // Sunny, Klaus, Isadora, Duncan, Quigley, Violet

  // use Comparing for simpler creation:
  // sort by age desc and then name asc
  OrderedSet<Person> b =
      new OrderedSet(Comparing.join([(p) => -p.age, (p) => p.name]));
  b.addAll(a.toList());
  print(b.toList().map((e) => e.name));
  // Violet, Duncan, Isadora, Quigley, Klaus, Sunny
}

class Person {
  int age;
  String name;
  Person(this.age, this.name);
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  ordered_set: ^1.1.2

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:ordered_set/ordered_set.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
68
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
84
Learn more about scoring.

We analyzed this package on Aug 19, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.4.0
  • pana: 0.12.19

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:ordered_set/ordered_set.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.54.0 <3.0.0
Dev dependencies
test ^1.3.0