📄 hashclique.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 java.util.HashSet;import salvo.jesus.graph.Edge;import salvo.jesus.graph.VertexImpl;import java.util.Set;import java.util.Collection;import java.util.AbstractSet;import java.util.Iterator;import java.util.HashSet;import java.util.AbstractCollection;import java.util.Collections;import java.util.LinkedList;import java.util.ArrayList;import java.util.Arrays;import gnu.trove.THashSet;import java.util.Iterator;import edu.umass.cs.mallet.base.types.Matrixn;/** * A clique is a collection of nodes in a graph that are all * adjacent. We implement it cheaply by delegating to a HashSet. * * Created: Wed Sep 17 12:50:01 2003 * * @author <a href="mailto:casutton@cs.umass.edu">Charles Sutton</a> * @version $Id: HashClique.java,v 1.2 2004/08/13 19:17:37 casutton Exp $ */// xxx Perhaps this should just use an alphabet and not implement Set.public class HashClique extends VertexImpl implements Clique { private THashSet verts = new THashSet(); private ArrayList vertsList = new ArrayList (); /** * Create an empty clique. */ public HashClique() { super (); } // Clique constructor /** * Create a two-clique given an edge in a graph. */ public HashClique (Edge e) { super (); add (e.getVertexA()); add (e.getVertexB()); } /** * Create a Clique given a Collection of nodes. */ public HashClique (Collection c) { super(); addAll (c); } public HashClique (Variable[] vars) { super(); addAll (Arrays.asList (vars)); } public Variable get (int idx) { return (Variable) vertsList.get (idx); } public String getLabel () { return toString (); } /** * Returns the intersection of two cliques. */ public Set intersection (Clique c) { THashSet set = new THashSet (vertices()); set.retainAll (c.vertices()); return set; }// Code for delegation of java.util.AbstractSet methods to verts public int hashCode() { return verts.hashCode(); } public boolean equals(Object object) { return verts.equals(object); } public boolean removeAll(Collection collection) { return verts.removeAll(collection); } public Set vertices() { return Collections.unmodifiableSet (verts); } public Variable[] toVariableArray () { // Cannot just do (Variable[]) vertsList.toArray() because that // would cause a ClassCastException. I suppose that's why // toArray is overloaded... return (Variable[]) vertsList.toArray (new Variable[] {}); }// Code for delegation of java.util.AbstractCollection methods to verts public String toString() { String val = "(C"; for (Iterator it = vertsList.iterator(); it.hasNext();) { val += " "; val += it.next().toString(); } val += ")"; return val; } public boolean addAll(Collection collection) { vertsList.addAll(collection); return verts.addAll(collection); } /** Returns the variables in this clique as an array. If the * clique is not modified, then the ordering will remain consistent * across calls. */ public Object[] toArray(Object[] objectArray) { // Using vertsList here assures that toArray() always returns the // same ordering. return vertsList.toArray(objectArray); } /** Returns the variables in this clique as an array. If the * clique is not modified, then the ordering will remain consistent * across calls. */ public Object[] toArray() { // Using vertsList here assures that toArray() always returns the // same ordering. return vertsList.toArray(); } public boolean containsAll(Collection collection) { return verts.containsAll(collection); } public boolean retainAll(Collection collection) { return verts.retainAll(collection); } // Code for delegation of java.util.HashSet methods to verts public Object clone() { return verts.clone(); } public boolean add(Object object) { vertsList.add (object); return verts.add (object); } public boolean contains(Object object) { return verts.contains(object); } public int size() { return verts.size(); } // Returns the total size of a dense discrete variable over this clique. public int weight () { int tot = 1; for (int i = 0; i < vertsList.size(); i++) { Variable var = (Variable) vertsList.get (i); tot *= var.getNumOutcomes (); } return tot; } public Iterator iterator() { return verts.iterator(); } public boolean remove(Object object) { return verts.remove(object); } public void clear() { verts.clear(); } public boolean isEmpty() { return verts.isEmpty(); } // Iterating over assignments public AssignmentIterator assignmentIterator () { return new AssignmentIterator (vertsList); } } // Clique
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -