001 /* 002 * Copyright (C) 2008-2010 by Holger Arndt 003 * 004 * This file is part of the Universal Java Matrix Package (UJMP). 005 * See the NOTICE file distributed with this work for additional 006 * information regarding copyright ownership and licensing. 007 * 008 * UJMP is free software; you can redistribute it and/or modify 009 * it under the terms of the GNU Lesser General Public License as 010 * published by the Free Software Foundation; either version 2 011 * of the License, or (at your option) any later version. 012 * 013 * UJMP is distributed in the hope that it will be useful, 014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 016 * GNU Lesser General Public License for more details. 017 * 018 * You should have received a copy of the GNU Lesser General Public 019 * License along with UJMP; if not, write to the 020 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, 021 * Boston, MA 02110-1301 USA 022 */ 023 024 package org.ujmp.core.collections; 025 026 import java.util.BitSet; 027 import java.util.Collection; 028 import java.util.Comparator; 029 import java.util.Iterator; 030 import java.util.Set; 031 import java.util.SortedSet; 032 033 public class BitSetSet implements Set<Integer>, SortedSet<Integer> { 034 035 BitSet bitset = new BitSet(); 036 037 public BitSetSet() { 038 } 039 040 public BitSetSet(BitSetSet source) { 041 bitset.or(source.bitset); 042 } 043 044 public boolean add(Integer e) { 045 bitset.set(e); 046 return true; 047 } 048 049 public void set(int start, int end) { 050 bitset.set(start, end); 051 } 052 053 public void clear(int index) { 054 bitset.clear(index); 055 } 056 057 public boolean addAll(Collection<? extends Integer> c) { 058 if (c instanceof BitSetSet) { 059 addAll((BitSetSet) c); 060 } else { 061 for (Integer i : c) { 062 bitset.set(i); 063 } 064 } 065 return true; 066 } 067 068 public void addAll(BitSetSet bss) { 069 bitset.or(bss.bitset); 070 } 071 072 public BitSetSet clone() { 073 return new BitSetSet(this); 074 } 075 076 public void clear() { 077 bitset.clear(); 078 } 079 080 public boolean contains(Object o) { 081 return bitset.get((Integer) o); 082 } 083 084 public boolean containsAll(Collection<?> c) { 085 for (Object o : c) { 086 if (!bitset.get((Integer) o)) 087 return false; 088 } 089 return true; 090 } 091 092 public boolean isEmpty() { 093 return bitset.isEmpty(); 094 } 095 096 public Iterator<Integer> iterator() { 097 return new BitIterator(); 098 } 099 100 class BitIterator implements Iterator<Integer> { 101 102 int index = 0; 103 104 public BitIterator() { 105 106 } 107 108 public boolean hasNext() { 109 return index != -1 && bitset.nextSetBit(index) != -1; 110 } 111 112 public Integer next() { 113 int ret = bitset.nextSetBit(index); 114 index = bitset.nextSetBit(ret + 1); 115 return ret; 116 } 117 118 public void remove() { 119 (new Exception("not implemented")).printStackTrace(); 120 } 121 122 } 123 124 public boolean remove(Object o) { 125 bitset.clear((Integer) o); 126 return true; 127 } 128 129 public boolean removeAll(Collection<?> c) { 130 for (Object o : c) { 131 int i = (Integer) o; 132 if (i >= 0) { 133 bitset.clear(i); 134 } 135 } 136 return true; 137 } 138 139 public String toString() { 140 return bitset.toString(); 141 } 142 143 public boolean retainAll(Collection<?> c) { 144 (new Exception("not implemented")).printStackTrace(); 145 return false; 146 } 147 148 public int size() { 149 return bitset.cardinality(); 150 } 151 152 public Object[] toArray() { 153 (new Exception("not implemented")).printStackTrace(); 154 return null; 155 } 156 157 public <T> T[] toArray(T[] a) { 158 (new Exception("not implemented")).printStackTrace(); 159 return null; 160 } 161 162 public Comparator<? super Integer> comparator() { 163 Comparator<Integer> comparator = new Comparator<Integer>() { 164 165 public int compare(Integer o1, Integer o2) { 166 return o1.compareTo(o2); 167 } 168 169 }; 170 return comparator; 171 } 172 173 public Integer first() { 174 return bitset.nextSetBit(0); 175 } 176 177 public SortedSet<Integer> headSet(Integer toElement) { 178 (new Exception("not implemented")).printStackTrace(); 179 return null; 180 } 181 182 public Integer last() { 183 return bitset.length() - 1; 184 } 185 186 public SortedSet<Integer> subSet(Integer fromElement, Integer toElement) { 187 (new Exception("not implemented")).printStackTrace(); 188 return null; 189 } 190 191 public SortedSet<Integer> tailSet(Integer fromElement) { 192 (new Exception("not implemented")).printStackTrace(); 193 return null; 194 } 195 196 }