|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| UnionSet.java | 0% | 0% | 0% | 0% |
|
||||||||||||||
| 1 | /*BEGIN_COPYRIGHT_BLOCK* | |
| 2 | ||
| 3 | PLT Utilities BSD License | |
| 4 | ||
| 5 | Copyright (c) 2007-2010 JavaPLT group at Rice University | |
| 6 | All rights reserved. | |
| 7 | ||
| 8 | Developed by: Java Programming Languages Team | |
| 9 | Rice University | |
| 10 | http://www.cs.rice.edu/~javaplt/ | |
| 11 | ||
| 12 | Redistribution and use in source and binary forms, with or without modification, are permitted | |
| 13 | provided that the following conditions are met: | |
| 14 | ||
| 15 | - Redistributions of source code must retain the above copyright notice, this list of conditions | |
| 16 | and the following disclaimer. | |
| 17 | - Redistributions in binary form must reproduce the above copyright notice, this list of | |
| 18 | conditions and the following disclaimer in the documentation and/or other materials provided | |
| 19 | with the distribution. | |
| 20 | - Neither the name of the JavaPLT group, Rice University, nor the names of the library's | |
| 21 | contributors may be used to endorse or promote products derived from this software without | |
| 22 | specific prior written permission. | |
| 23 | ||
| 24 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR | |
| 25 | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
| 26 | FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND | |
| 27 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
| 28 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 29 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER | |
| 30 | IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | |
| 31 | OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 32 | ||
| 33 | *END_COPYRIGHT_BLOCK*/ | |
| 34 | ||
| 35 | package edu.rice.cs.plt.collect; | |
| 36 | ||
| 37 | import java.util.Set; | |
| 38 | import java.util.Iterator; | |
| 39 | import java.io.Serializable; | |
| 40 | import edu.rice.cs.plt.iter.ImmutableIterator; | |
| 41 | import edu.rice.cs.plt.iter.ComposedIterator; | |
| 42 | import edu.rice.cs.plt.iter.IterUtil; | |
| 43 | import edu.rice.cs.plt.object.Composite; | |
| 44 | import edu.rice.cs.plt.object.ObjectUtil; | |
| 45 | ||
| 46 | /** | |
| 47 | * The union of two sets, lazily constructed and updated dynamically. | |
| 48 | */ | |
| 49 | public class UnionSet<E> extends AbstractPredicateSet<E> implements Composite, Serializable { | |
| 50 | private final Set<? extends E> _set1; | |
| 51 | private final Set<? extends E> _set2; | |
| 52 | private final Set<? extends E> _set2Extras; | |
| 53 | ||
| 54 | /** | |
| 55 | * For best performance of {@link #size}, {@code set2} should be the smaller | |
| 56 | * of the two sets (this is not handled automatically because calculating sizes may be expensive). | |
| 57 | */ | |
| 58 | 0 | public UnionSet(Set<? extends E> set1, Set<? extends E> set2) { |
| 59 | 0 | _set1 = set1; |
| 60 | 0 | _set2 = set2; |
| 61 | 0 | _set2Extras = new ComplementSet<E>(set2, set1); |
| 62 | } | |
| 63 | ||
| 64 | 0 | public int compositeHeight() { return ObjectUtil.compositeHeight(_set1, _set2) + 1; } |
| 65 | 0 | public int compositeSize() { return ObjectUtil.compositeSize(_set1, _set2) + 1; } |
| 66 | ||
| 67 | 0 | public boolean contains(Object o) { |
| 68 | 0 | return _set1.contains(o) || _set2.contains(o); |
| 69 | } | |
| 70 | ||
| 71 | 0 | public Iterator<E> iterator() { |
| 72 | 0 | return new ImmutableIterator<E>(new ComposedIterator<E>(_set1.iterator(), _set2Extras.iterator())); |
| 73 | } | |
| 74 | ||
| 75 | 0 | public boolean isInfinite() { return IterUtil.isInfinite(_set1) || IterUtil.isInfinite(_set2); } |
| 76 | 0 | public boolean hasFixedSize() { return IterUtil.hasFixedSize(_set1) && IterUtil.hasFixedSize(_set2); } |
| 77 | 0 | public boolean isStatic() { return IterUtil.isStatic(_set1) && IterUtil.isStatic(_set2); } |
| 78 | ||
| 79 | ||
| 80 | /** Linear in the size of {@code set2}. */ | |
| 81 | 0 | @Override public int size() { |
| 82 | 0 | return _set1.size() + _set2Extras.size(); |
| 83 | } | |
| 84 | ||
| 85 | 0 | @Override public int size(int bound) { |
| 86 | 0 | int size1 = IterUtil.sizeOf(_set1, bound); |
| 87 | 0 | int bound2 = bound - size1; |
| 88 | 0 | int size2 = (bound2 > 0) ? IterUtil.sizeOf(_set2Extras, bound) : 0; |
| 89 | 0 | return size1 + size2; |
| 90 | } | |
| 91 | ||
| 92 | 0 | @Override public boolean isEmpty() { |
| 93 | 0 | return _set1.isEmpty() && _set2.isEmpty(); |
| 94 | } | |
| 95 | ||
| 96 | } |
|
||||||||||