bitstringlinkedlist.java

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

JAVA
530
字号
package positronic.satisfiability.bitstringlist;

import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import positronic.satisfiability.bitstring.BitString;
import positronic.satisfiability.bitstring.IBitString;
import positronic.satisfiability.elements.BooleanVariable;
import positronic.satisfiability.elements.IBooleanVariable;
import positronic.satisfiability.exceptions.BitStringLinkedListException;

public class BitStringLinkedList extends LinkedList implements IBitStringLinkedList
{
	private static int bSLLCount;
	private static final long serialVersionUID = 1L;
	
	public static IBitStringLinkedList add(IBitStringLinkedList first,IBitStringLinkedList second) throws Exception
  {
    return BitStringLinkedList.add("BitStringLinkedList-"+ bSLLCount++,first,second);
  }

	public static IBitStringLinkedList add(String name,IBitStringLinkedList first,IBitStringLinkedList second) throws Exception
  {
  	if(name==null)
      throw new BitStringLinkedListException("Passed null String to constructor.");
  	if(first==null || second==null)
      throw new BitStringLinkedListException("Passed null String to constructor.");
    
    Collection a=first.toList();
    Collection b=second.toList();
    a.addAll(b);
    IBitString[] newArray=(IBitString[])a.toArray(new IBitString[0]);
    IBitStringLinkedList res=new BitStringLinkedList(name,newArray);
    return res;
  }

	private String name;

	public BitStringLinkedList() throws BitStringLinkedListException
  {
    this.setName("BitStringLinkedList-"+ bSLLCount++);
  }
	
	public BitStringLinkedList(boolean[][] bdata) throws Exception
  {
    this("BitStringLinkedList-"+ bSLLCount++,bdata);
  }
	
	public BitStringLinkedList(IBitString[] data) throws Exception
  {
    this("BitStringLinkedList-"+ bSLLCount++,data);
  }
  
  public BitStringLinkedList(IBitStringLinkedList list) throws Exception
  {
    this("BitStringLinkedList-"+ bSLLCount++,(IBitString[])list.toArray(new IBitString[0]));
  }
  
  public BitStringLinkedList(int n) throws Exception
  {
    this("BitStringLinkedList-"+ bSLLCount++,n);
  }

  public BitStringLinkedList(String name, boolean[][] bdata) throws Exception
  {
  	if(name==null)
      throw new BitStringLinkedListException("Passed null String to constructor.");
    if(bdata==null)
      throw new BitStringLinkedListException("Passed null boolean[][] to constructor.");
    if(bdata.length==0)
      throw new BitStringLinkedListException("Passed boolean[][] with bdata.length==0 to constructor.");
    this.setName(name);
    for(int i=0;i<bdata.length;i++)
    {
      Object o=new BitString(name+"_"+i,new IBooleanVariable[bdata[i].length]);
      super.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 BitStringLinkedList(String name, IBitString[] data) throws Exception
  {
  	if(name==null)
      throw new BitStringLinkedListException("Passed null String to constructor.");
    if(data==null)
      throw new BitStringLinkedListException("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 BitStringLinkedListException("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.add(new BitString(bdata[i]));
    }
  }
  
  public BitStringLinkedList(String name, int n) throws Exception
  {
    this.setName(name);
    for(int i=0;i<n;i++)
    {
      Object o=new BitString(name+"_"+i,(boolean[])null);
      super.add(o);
    }
  }
  
  public IBitStringLinkedList add(IBitStringLinkedList list) throws Exception
  {
    return BitStringLinkedList.add(this.getName(),this,list);
  }
  
  public void add(int n, Object o)
  {
    try
		{
    	super.add(n,o);
		} 
    catch (Exception e)
		{
			e.printStackTrace();
		}
  }

  public boolean add(Object o)
  {
    try
		{
			return super.add(o);
		} 
    catch (Exception e)
		{
			e.printStackTrace();
			return false;
		}
  }

  public boolean addAll(Collection c)
  {
    try
		{
			return super.addAll(c);
		} 
    catch (Exception e)
		{
			e.printStackTrace();
			return false;
		}
  }

  public boolean addAll(int n,Collection c)
  {
    try
		{
			return super.addAll(n,c);
		} 
    catch (Exception e)
		{
			e.printStackTrace();
			return false;
		}
  }

  public void clear()
  {
    try
		{
			super.clear();
		} 
    catch (Exception e)
		{
			e.printStackTrace();
		}
  }

  public Object clone()
  {
    try
    {
      return new BitStringLinkedList(this.getName(),(IBitString[])super.toArray(new IBitString[0]));
    }
    catch(CloneNotSupportedException err)
    {
      try
			{
				throw err;
			} catch (CloneNotSupportedException e)
			{
				e.printStackTrace();
				return null;
			}
    }
    catch(Exception err)
    {
      System.err.println("Attempt to clone BitStringLinkedList failed.");
      return null;
    }
  }

  public boolean contains(Object o)
  {
    try
		{
			return super.contains(o);
		} 
    catch (Exception e)
		{
			e.printStackTrace();
			return false;
		}
  }

  public boolean containsAll(Collection c)
  {
    try
		{
			return super.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 BitStringLinkedList))
      return false;
    try
		{
			if(!super.containsAll((Collection)anObject))
			  return false;
		} 
    catch (Exception e)
		{
			e.printStackTrace();
		}
    try
		{
			if(!((Collection)anObject).containsAll((Collection)this))
			  return false;
		} 
    catch (Exception e)
		{
			e.printStackTrace();
		}
    return true;
  }

  public Object get(int n)
  {
    try
		{
			return super.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 BitStringLinkedListException("Attempted to index out of range.");
    return (IBitString)(super.get(i));
  }

  public String getName()
	{
		return this.name;
	}

  public int indexOf(Object o)
  {
    try
		{
			return super.indexOf(o);
		} 
    catch (Exception e)
		{
			e.printStackTrace();
			return -1;
		}
  }

  public boolean isEmpty()
  {
    try
		{
			return super.size()==0;
		} 
    catch (Exception e)
		{
			e.printStackTrace();
			return false;
		}
  }

  public Iterator iterator()
  {
    try
		{
			return super.iterator();
		} 
    catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
  }

  public int lastIndexOf(Object o)
  {
    try
		{
			return super.lastIndexOf(o);
		} 
    catch (Exception e)
		{
			e.printStackTrace();
			return -1;
		}
  }

  public ListIterator listIterator()
  {
    try
		{
			return super.listIterator();
		} 
    catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
  }

  public ListIterator listIterator(int n)
  {
    try
		{
			return super.listIterator(n);
		} 
    catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
  }

  public Object remove(int n)
  {
    try
		{
			return super.remove(n);
		} 
    catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
  }

  public boolean remove(Object o)
  {
    try
		{
			return super.remove(o);
		} 
    catch (Exception e)
		{
			e.printStackTrace();
			return false;
		}
  }

  public boolean removeAll(Collection c)
  {
    try
		{
			return super.removeAll(c);
		} 
    catch (Exception e)
		{
			e.printStackTrace();
			return false;
		}
  }

  public boolean retainAll(Collection c)
  {
    try
		{
			return super.retainAll(c);
		} 
    catch (Exception e)
		{
			e.printStackTrace();
			return false;
		}
  }

  public Object set(int n, Object o)
  {
    try
		{
			return super.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 BitStringLinkedListException("Attempted to index out of range.");
  	if(bitString==null)
  		throw new BitStringLinkedListException("Passed null IBitString to setBitString method.");
  	super.set(i,bitString);
  }

  public void setName(String name) throws BitStringLinkedListException
  {
  	if(name==null)
  		throw new BitStringLinkedListException("Passed null String to setName method.");
  
    this.name = name;
  }

  public int size()
  {
    try
		{
			return super.size();
		} 
    catch (Exception e)
		{
			e.printStackTrace();
			return 0;
		}
  }

  public List subList(int m, int n)
  {
    try
		{
			return super.subList(m,n);
		} 
    catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
  }

  public Object[] toArray()
  {
    try
		{
			if(super.size()!=0)
			  return (IBitString[])super.toArray(new IBitString[0]);
			else
			  return null;
		} 
    catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
  }

  public Object[] toArray(Object[] o)
  {
    try
		{
			return super.toArray(o);
		} 
    catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
  }

  public List toList() throws Exception
	{
		return super.subList(0,super.size()-1);
	}

	public String toString()
  {
    String res="[";
    if(this.size()>0)
    {
      try
			{
				res+=this.getBitString(0).toString();
			} 
      catch (Exception e)
			{
				e.printStackTrace();
			}
      for(int i=1;i<this.size();i++)
				try
				{
					res+=","+this.getBitString(i);
				} 
      	catch (Exception e)
				{
					e.printStackTrace();
				}
    }
    return res+"]";
  }
}

⌨️ 快捷键说明

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