📄 icllist.java
字号:
package com.sri.oaa2.icl;
import java.util.*;
import java.util.zip.CRC32;
public final class IclList extends IclComposite
{
private boolean headTailList = false;
/**
* Create a new IclList using the given children
*/
public IclList(List args)
{
super(OaaPrologVocabTokenTypes.LIST);
setChildren(new ArrayList(args));
}
/**
* @deprecated use IclList(ArrayList)
*/
public IclList(Vector args)
{
super(OaaPrologVocabTokenTypes.LIST);
setChildren(new ArrayList(args));
}
/**
* Create an empty IclList
*/
public IclList()
{
super(OaaPrologVocabTokenTypes.LIST);
setChildren(new ArrayList());
}
/**
* With one term...
*/
public IclList(IclTerm first)
{
super(OaaPrologVocabTokenTypes.LIST);
add(first);
}
/**
* With two terms...
*/
public IclList(IclTerm first, IclTerm second)
{
super(OaaPrologVocabTokenTypes.LIST);
add(first);
add(second);
}
/**
* etc...
*/
public IclList(IclTerm first, IclTerm second, IclTerm third)
{
super(OaaPrologVocabTokenTypes.LIST);
add(first);
add(second);
add(third);
}
/**
* etc...
*/
public IclList(IclTerm first, IclTerm second, IclTerm third, IclTerm fourth)
{
super(OaaPrologVocabTokenTypes.LIST);
add(first);
add(second);
add(third);
add(fourth);
}
/** Accept the visitor. **/
protected final Object accept(OaaPrologVisitor visitor, Object data) {
return visitor.visit(this, data);
}
/**
* Sort the list with the given Comparator
*/
public final void sort(Comparator comparator) {
Collections.sort(children, comparator);
}
public void add(IclTerm t)
{
if(!this.hasEmptyTail()) {
throw new IllegalStateException("Can't add to a list with a tail");
}
super.add(t);
}
public void add(int n, IclTerm t)
{
if(n == (this.size() - 1)) {
if(!this.hasEmptyTail()) {
throw new IllegalStateException("Can't add to a list with a tail");
}
}
super.add(n, t);
}
/**
* Add the contents of another list. If it is not a list (list.isList()), this will
* fail to do anything.
*/
public final void addList(IclTerm list)
{
if(!this.hasEmptyTail()) {
throw new IllegalStateException("Can't add to a list with a tail");
}
if(!list.isList()) {
return;
}
if(list.size() == 0) {
return;
}
ListIterator li = list.listIterator();
while(li.hasNext()) {
add((IclTerm)li.next());
}
if(!((IclList)list).hasEmptyTail()) {
this.setHeadTailList(true);
}
}
static private final String EXTRACRC = "_LIST";
void addExtraCrcData(CRC32 inCrc)
{
inCrc.update(EXTRACRC.getBytes());
}
/**
* @deprecated use getStarter
*/
public final char iclStarter()
{
return '[';
}
/**
* @deprecated use IclList()
*/
public static final IclList iclGetEmptyList()
{
return new IclList();
}
/**
* @deprecated use addList
*/
public final void iclUnion(IclTerm inList)
{
this.addList(inList);
}
/**
* @deprecated use sort
*/
public final void iclSortList(Comparator comparator) {
sort(comparator);
}
/**
* @param headTailList Set whether this is a head tail list.
*/
void setHeadTailList(boolean headTailList)
{
this.headTailList = headTailList;
}
boolean isHeadTailList()
{
return this.headTailList;
}
/**
* Check if this IclList has an empty tail.
*/
public boolean hasEmptyTail()
{
return !this.isHeadTailList();
}
/**
* Returns a new IclList which is a sublist of this list.
*/
public IclList subList(int offset, int length)
{
// TODO: should this be a deep copy?
IclTerm[] terms = new IclTerm[this.size()];
terms = (IclTerm[])this.getChildren().toArray(terms);
IclTerm[] sub = new IclTerm[length];
System.arraycopy(terms, offset, sub, 0, length);
IclList toRet = new IclList(Arrays.asList(sub));
toRet.setHeadTailList(!this.hasEmptyTail());
return toRet;
}
private IclTerm[] subListChildren(int offset, int length)
{
IclTerm[] terms = new IclTerm[this.size()];
terms = (IclTerm[])this.getChildren().toArray(terms);
IclTerm[] sub = new IclTerm[length];
System.arraycopy(terms, offset, sub, 0, length);
return sub;
}
/**
* A list in a tail:
* [a | [b,c]] is equivalent to
* [a, b, c].
* <p>
* This method compresses the tail of
* this list (if any).
*/
void compressTail()
{
if(this.hasEmptyTail()) {
return;
}
if(this.size() < 1) {
this.setHeadTailList(false);
return;
}
IclTerm tail = this.getChild(this.size() - 1);
if(!tail.isList()) {
if(!tail.isVar()) {
this.setHeadTailList(false);
}
return;
}
ArrayList newChildren = new ArrayList(Arrays.asList(this.subListChildren(0, this.size() - 1)));
boolean lastHadTail = false;
while((tail != null) &&
tail.isList()) {
newChildren.addAll(tail.getChildren());
lastHadTail = ((IclList)tail).isHeadTailList();
if(lastHadTail) {
tail = (IclTerm)newChildren.get(newChildren.size() - 1);
newChildren.remove(newChildren.size() - 1);
}
else {
tail = null;
}
}
if(lastHadTail) {
newChildren.add(tail);
}
this.setHeadTailList(lastHadTail);
this.setChildren(newChildren);
}
/**
* Set the tail of this list
*
* @param t
* @return the previos tail (the empty list or a Var)
*/
public IclTerm setTail(IclTerm t)
{
if(!t.isList() &&
!t.isVar()) {
throw new IllegalArgumentException("The tail must be an IclList or an IclVar");
}
IclTerm toRet = null;
if(this.isHeadTailList()) {
toRet = this.getChild(this.size() - 1);
this.removeElement(this.size() - 1);
}
else {
toRet = new IclList();
}
this.add(t);
this.setHeadTailList(true);
this.compressTail();
return toRet;
}
/**
* Get the tail of this IclList. If this IclList returns
* false to this.hasNonEmptyTail(), it will return the empty
* list. Otherwise, it returns the tail (which will always be
* an IclVar).
*/
public IclTerm getTail()
{
if(!this.headTailList) {
return new IclList();
}
else {
return this.getChild(this.size() - 1);
}
}
/* As with super.equals, but check tail as well
* @see java.lang.Object#equals(java.lang.Object)
*/
public boolean equals(Object o)
{
if(!super.equals(o)) {
return false;
} // else fall through
IclList lo = (IclList)o;
return this.hasEmptyTail() == lo.hasEmptyTail();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -