bitstringlist.java

来自「Java版的SAT求解器」· Java 代码 · 共 535 行

JAVA
535
字号
/* * BitStringList.java	1.0 05/04/28 * * Copyright 2004-2005 Positronic Software. * * */package positronic.satisfiability.bitstringlist;import java.util.Collection;import java.util.Iterator;import java.util.List;import java.util.ListIterator;import java.util.Set;import positronic.satisfiability.bitstring.BitString;import positronic.satisfiability.bitstring.IBitString;import positronic.satisfiability.elements.BooleanVariable;import positronic.satisfiability.elements.IBooleanVariable;import positronic.satisfiability.exceptions.BitStringListException;import positronic.util.ArrayListSet;public class BitStringList implements IBitStringList{  private static int bSLCount;  public static IBitStringList add(IBitStringList first,IBitStringList second) throws Exception  {    return BitStringList.add("BitStringList-"+ bSLCount++,first,second);  }    public static IBitStringList add(String name,IBitStringList first,IBitStringList second) throws Exception  {  	if(name==null)      throw new BitStringListException("Passed null String to constructor.");  	if(first==null || second==null)      throw new BitStringListException("Passed null String to constructor.");        Collection a=first.toList();    Collection b=second.toList();    a.addAll(b);    IBitString[] newArray=(IBitString[])a.toArray(new IBitString[0]);    IBitStringList res=new BitStringList(name,newArray);    return res;  }    private ArrayListSet listData;  private String name;    public BitStringList() throws Exception  {    this.setName("BitStringList-"+ bSLCount++);  }    public BitStringList(boolean[][] bdata) throws Exception  {    this("BitStringList-"+ bSLCount++,bdata);  }    public BitStringList(IBitString[] data) throws Exception  {    this("BitStringList-"+ bSLCount++,data);  }    public BitStringList(IBitStringList list) throws Exception  {    this("BitStringList-"+ bSLCount++,(IBitString[])list.toArray(new IBitString[0]));  }    public BitStringList(int n) throws Exception  {    this("BitStringList-"+ bSLCount++,n);  }    public BitStringList(String name, boolean[][] bdata) throws Exception  {  	if(name==null)      throw new BitStringListException("Passed null String to constructor.");    if(bdata==null)      throw new BitStringListException("Passed null boolean[][] to constructor.");    if(bdata.length==0)      throw new BitStringListException("Passed boolean[][] with bdata.length==0 to constructor.");    this.setName(name);    this.setData(new ArrayListSet(bdata.length));    for(int i=0;i<bdata.length;i++)    {      Object o=new BitString(name+"_"+i,new IBooleanVariable[bdata[i].length]);      this.getData().add(o);      for(int j=0;j<this.getBitString(i).size();j++)        this.getBitString(i).setBooleanVariable(j,          BooleanVariable.getBooleanVariable(name+"_"+i+"_"+j,bdata[i][j]));    }  }  public BitStringList(String name, IBitString[] data) throws Exception  {  	if(name==null)      throw new BitStringListException("Passed null String to constructor.");    if(data==null)      throw new BitStringListException("Passed null IBitString[] to constructor.");    this.setName(name);    boolean[][] bdata=new boolean[data.length][];    for(int i=0;i<bdata.length;i++)    {      if(data[i]==null)        throw new BitStringListException("Attempted to initialize with null IBitString.");      bdata[i]=new boolean[data[i].size()];      for(int j=0;j<bdata[i].length;j++)        bdata[i][j]=data[i].getBooleanVariable(j).getValue();    }    this.setData(new BitStringList(name,bdata).getData());  }  public BitStringList(String name, int n) throws Exception  {    this.setName(name);    this.setData(new ArrayListSet(n));    for(int i=0;i<n;i++)    {      //Object o=new BitString(name+"_"+i,(boolean[])null);    	Object o=new BitString(name+"_"+i,new boolean[0]);      this.getData().add(o);    }  }  public IBitStringList add(IBitStringList list) throws Exception  {    return BitStringList.add(this.getName(),this,list);  }  public void add(int n, Object o)  {    try		{			((ArrayListSet)(this.getData())).add(n,o);		} catch (Exception e)		{			e.printStackTrace();		}  }  public boolean add(Object o)  {    try		{			return this.getData().add(o);		} catch (Exception e)		{			e.printStackTrace();			return false;		}  }  public boolean addAll(Collection c)  {    try		{			return this.getData().addAll(c);		} catch (Exception e)		{			e.printStackTrace();			return false;		}  }  public boolean addAll(int n,Collection c)  {    try		{			return ((ArrayListSet)this.getData()).addAll(n,c);		} catch (Exception e)		{			e.printStackTrace();			return false;		}  }  public void clear()  {    try		{			this.getData().clear();		} catch (Exception e)		{			e.printStackTrace();		}  }  public Object clone() throws CloneNotSupportedException  {    try    {      return new BitStringList(this.getName(),(IBitString[])this.getData().toArray(new IBitString[0]));    }    catch(CloneNotSupportedException err)    {      throw err;    }    catch(Exception err)    {      System.err.println("Attempt to clone BitStringList failed.");      return null;    }  }  public boolean contains(Object o)  {    try		{			return this.getData().contains(o);		} catch (Exception e)		{			e.printStackTrace();			return false;		}  }  public boolean containsAll(Collection c)  {    try		{			return this.getData().containsAll(c);		} catch (Exception e)		{			e.printStackTrace();			return false;		}  }  public boolean equals(Object anObject)  {  	if(anObject==null)  		return false; //this is never equal to null.    if(!(anObject instanceof BitStringList))      return false;    try		{			if(!this.getData().containsAll(((BitStringList)anObject).getData()))			  return false;		} catch (Exception e)		{			e.printStackTrace();		}    try		{			if(!((BitStringList)anObject).getData().containsAll(this.getData()))			  return false;		} catch (Exception e)		{			e.printStackTrace();		}    return true;  }  public Object get(int n)  {    try		{			return ((ArrayListSet)this.getData()).get(n);		} catch (Exception e)		{			e.printStackTrace();			return null;		}  }  public IBitString getBitString(int i) throws Exception  {  	if(i<0 || i>this.size()-1)  		throw new BitStringListException("Attempted to index out of range.");    return (IBitString)(((ArrayListSet)this.getData()).get(i));  }  public Set getData() throws Exception  {  	if(this.listData==null)  		throw new BitStringListException("Attempted to access listData while listData==null.");  	return this.listData;  }  public String getName()  {    return name;  }  public int indexOf(Object o)  {    try		{			return ((ArrayListSet)this.getData()).indexOf(o);		} catch (Exception e)		{			e.printStackTrace();			return -1;		}  }  public boolean isEmpty()  {    try		{			return this.getData()==null;		} catch (Exception e)		{			e.printStackTrace();			return false;		}  }  public Iterator iterator()  {    try		{			return this.getData().iterator();		} catch (Exception e)		{			e.printStackTrace();			return null;		}  }  public int lastIndexOf(Object o)  {    try		{			return ((ArrayListSet)this.getData()).lastIndexOf(o);		} catch (Exception e)		{			e.printStackTrace();			return -1;		}  }  public ListIterator listIterator()  {    try		{			return ((ArrayListSet)this.getData()).listIterator();		} catch (Exception e)		{			e.printStackTrace();			return null;		}  }  public ListIterator listIterator(int n)  {    try		{			return ((ArrayListSet)this.getData()).listIterator(n);		} catch (Exception e)		{			e.printStackTrace();			return null;		}  }  public Object remove(int n)  {    try		{			return ((ArrayListSet)this.getData()).remove(n);		} catch (Exception e)		{			e.printStackTrace();			return null;		}  }  public boolean remove(Object o)  {    try		{			return this.getData().remove(o);		} catch (Exception e)		{			e.printStackTrace();			return false;		}  }  public boolean removeAll(Collection c)  {    try		{			return this.getData().removeAll(c);		} catch (Exception e)		{			e.printStackTrace();			return false;		}  }  public boolean retainAll(Collection c)  {    try		{			return this.getData().retainAll(c);		} catch (Exception e)		{			e.printStackTrace();			return false;		}  }  public Object set(int n, Object o)  {  	try {			if(n<0 || n>this.getData().size()-1)				throw new BitStringListException("Attempted to index out of range.");		} catch (BitStringListException e1) {			// TODO Auto-generated catch block			e1.printStackTrace();		} catch (Exception e1) {			// TODO Auto-generated catch block			e1.printStackTrace();		}    try		{			return ((ArrayListSet)this.getData()).set(n,o);		} catch (Exception e)		{			e.printStackTrace();			return null;		}  }  public void setBitString(int i,IBitString bitString) throws Exception  {  	if(i<0 || i>this.size()-1)  		throw new BitStringListException("Attempted to index out of range.");  	if(bitString==null)  		throw new BitStringListException("Passed null IBitString to setBitString method.");  	((ArrayListSet)this.getData()).set(i,bitString);  }  public void setData(Set data) throws Exception  {  	if(data==null)  		throw new BitStringListException("Passed null ArrayListSet to setData method.");    this.listData = (ArrayListSet)data;  }  public void setName(String name) throws Exception  {  	if(name==null)  		throw new BitStringListException("Passed null String to setName method.");      this.name = name;  }  public int size()  {    try		{			return this.getData().size();		} catch (Exception e)		{			e.printStackTrace();			return 0;		}  }  public List subList(int m, int n)  {    try		{			return ((ArrayListSet)this.getData()).subList(m,n);		} catch (Exception e)		{			e.printStackTrace();			return null;		}  }  public Object[] toArray()  {    try		{			if(this.getData()!=null)			  return (IBitString[])this.getData().toArray(new IBitString[0]);			else			  return null;		} catch (Exception e)		{			e.printStackTrace();			return null;		}  }  public Object[] toArray(Object[] o)  {    try		{			return this.getData().toArray(o);		} catch (Exception e)		{			e.printStackTrace();			return null;		}  }	public List toList() throws Exception  {    return (ArrayListSet)this.getData();  }	public String toString()  {    String res="[";    if(this.size()>0)    {      try			{				res+=this.getBitString(0).toString();			}       catch (Exception e)			{				// TODO Auto-generated catch block				e.printStackTrace();			}      for(int i=1;i<this.size();i++)				try				{					res+=","+this.getBitString(i);				}       	catch (Exception e)				{					// TODO Auto-generated catch block					e.printStackTrace();				}    }    return res+"]";  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?