📄 bitsetclique.java
字号:
/* Copyright (C) 2003 Univ. of Massachusetts Amherst, Computer Science Dept. This file is part of "MALLET" (MAchine Learning for LanguagE Toolkit). http://www.cs.umass.edu/~mccallum/mallet This software is provided under the terms of the Common Public License, version 1.0, as published by http://www.opensource.org. For further information, see the file `LICENSE' included with this distribution. */package edu.umass.cs.mallet.grmm;import edu.umass.cs.mallet.base.types.Alphabet;import java.util.*;import gnu.trove.THashSet;/** * A clique that uses very little time and memory based on the flyweight * pattern. The owner supplies an Alphabet of vertices and a BitSet, * and the clique contains the vertices in the Alphabet, masked by the BitSet. * * @author Charles Sutton * @version $Id: BitSetClique.java,v 1.2 2004/08/13 19:17:37 casutton Exp $ */public class BitSetClique extends AbstractSet implements Clique { private Alphabet vertexMap; private BitSet bitset; /** * Creates a BitSet clique given an alphabet of Variables, * and a bitset that says which variables in the alphabet * to include in the clique. Neither is copied, but * neither is modified, either. * * @param vertexMap * @param included Bit mask that indicates which variables to include */ BitSetClique (Alphabet vertexMap, BitSet included) { this.vertexMap = vertexMap; this.bitset = included; } BitSetClique (Alphabet vertexMap, Collection included) { this.vertexMap = vertexMap; this.bitset = new BitSet (vertexMap.size()); java.util.Iterator it = included.iterator(); while (it.hasNext()) { bitset.set (vertexMap.lookupIndex (it.next())); } } public boolean add (Object o) { int idx = vertexMap.lookupIndex (o); if (idx == -1) throw new UnsupportedOperationException(); bitset.set (idx); return true; } public Variable get(int idx) { int mapIdx = 0; for (int i = 0; i < idx; i++) { mapIdx = bitset.nextSetBit (mapIdx); if (mapIdx == -1) throw new IndexOutOfBoundsException("Index "+idx+" in BitSetClique"); } return (Variable) vertexMap.lookupObject (mapIdx); } public Set vertices() { return null; } public Variable[] toVariableArray() { return (Variable[]) toArray (new Variable[0]); }// FIXME cache not updated on changes to the clique private int cachedWeight = -1; public int weight() { if (cachedWeight == -1) { int weight = 1; Iterator it = new Iterator(); while (it.hasNext()) { Variable var = (Variable) it.next(); weight *= var.getNumOutcomes(); } cachedWeight = weight; } return cachedWeight; } public AssignmentIterator assignmentIterator() { return new AssignmentIterator (Arrays.asList(toArray ())); } public int size() { return bitset.cardinality(); } public boolean isEmpty() { return bitset.isEmpty(); } public boolean contains(Object o) { return bitset.get(vertexMap.lookupIndex(o, false)); } private class Iterator implements java.util.Iterator { int nextIdx; public Iterator () { nextIdx = bitset.nextSetBit (0); } public boolean hasNext() { return (nextIdx >= 0); } public Object next() { int thisIdx = nextIdx; nextIdx = bitset.nextSetBit (thisIdx + 1); return vertexMap.lookupObject (thisIdx); } public void remove() { throw new UnsupportedOperationException("Removal from BitSetClique not permitted"); } } public java.util.Iterator iterator() { return new Iterator(); } public int hashCode () { return bitset.hashCode (); } public boolean containsAll(Collection c) { if (c instanceof BitSetClique) { return containsAll ((BitSetClique) c); } else { return super.containsAll (c); } } /** * Efficient version of containsAll() for BitSetCliques. */ public boolean containsAll (BitSetClique bsc) { assert vertexMap == bsc.vertexMap; for(int i=bsc.bitset.nextSetBit(0); i>=0; i=bsc.bitset.nextSetBit(i+1)) { if (!bitset.get (i)) { return false; } } return true; } public Set intersection (Clique c) { if (c instanceof BitSetClique) { // Efficient implementation BitSetClique bsc = (BitSetClique) c; BitSet newBitSet = (BitSet) bitset.clone(); newBitSet.and (bsc.bitset); return new BitSetClique (vertexMap, newBitSet); } else { // Grossly inefficient implementation THashSet hset = new THashSet (this); hset.retainAll (c); return hset; } } int intersectionSize (BitSetClique bsc) { assert vertexMap == bsc.vertexMap; int size = 0; for(int i=bsc.bitset.nextSetBit(0); i>=0; i=bsc.bitset.nextSetBit(i+1)) { if (bitset.get (i)) { size++; } } return size; } public void clear() { bitset.clear(); } public boolean hasLabel() { return true; } public String getLabel() { return toString (); } public String toString () { String foo = "(C"; Iterator it = new Iterator (); while (it.hasNext()) { Variable var = (Variable) it.next(); foo = foo + " " + var; } foo = foo + ")"; return foo; } public void setLabel(String s) { throw new UnsupportedOperationException(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -