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> with SetMixin<E>, UnmodifiableSetMixin<E> { 19 : /// The base iterable that set operations forward to. 20 : final Iterable<E> _base; 21 : 22 0 : @override 23 0 : int get length => _base.length; 24 : 25 0 : @override 26 0 : Iterator<E> get iterator => _base.iterator; 27 : 28 : /// Creates a [Set] view of [base]. 29 0 : IterableSet(this._base); 30 : 31 0 : @override 32 0 : bool contains(Object? element) => _base.contains(element); 33 : 34 0 : @override 35 : E? lookup(Object? element) { 36 0 : for (var e in _base) { 37 0 : if (e == element) return e; 38 : } 39 : } 40 : 41 0 : @override 42 0 : Set<E> toSet() => _base.toSet(); 43 : }