Line data Source code
1 : // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
2 : // for details. All rights reserved. Use of this source code is governed by a
3 : // BSD-style license that can be found in the LICENSE file.
4 :
5 : import 'dart:collection';
6 :
7 : import 'package:collection/collection.dart';
8 :
9 : /// An unmodifiable [Set] view backed by an arbitrary [Iterable].
10 : ///
11 : /// Note that contrary to most APIs that take iterables, this does not convert
12 : /// its argument to another collection before use. This means that if it's
13 : /// lazily-generated, that generation will happen for every operation.
14 : ///
15 : /// Note also that set operations that are usually expected to be `O(1)` or
16 : /// `O(log(n))`, such as [contains], may be `O(n)` for many underlying iterable
17 : /// types. As such, this should only be used for small iterables.
18 : class IterableSet<E> extends SetMixin<E> with UnmodifiableSetMixin<E> {
19 : /// The base iterable that set operations forward to.
20 : final Iterable<E> _base;
21 :
22 10 : int get length => _base.length;
23 :
24 10 : Iterator<E> get iterator => _base.iterator;
25 :
26 : /// Creates a [Set] view of [base].
27 5 : IterableSet(this._base);
28 :
29 0 : bool contains(Object element) => _base.contains(element);
30 :
31 : E lookup(Object needle) =>
32 0 : _base.firstWhere((element) => element == needle, orElse: () => null);
33 :
34 0 : Set<E> toSet() => _base.toSet();
35 : }
|