public PureFactory() { super(); final ATermListImpl protoList = new ATermListImpl(this); protoList.init(FIX_POINT, null, null); _empty = build(protoList); ((ATermListImpl) _empty).init(FIX_POINT, null, null); }
@Override public ATerm getSubTerm(final int index) { return elementAt(index); }
protected ATermListImpl(final PureFactory factory, final ATerm first, final ATermList next) { super(factory); _first = first; _next = next; if (first == null && next == null) _length = 0; else _length = 1 + next.getLength(); setHashCode(hashFunction()); }
@Override public ATermList getPrefix() { if (isEmpty()) return this; ATermList cur = this; final List<ATerm> elems = new ArrayList<>(); while (true) { if (cur.getNext().isEmpty()) { cur = getPureFactory().getEmpty(); for (int i = elems.size() - 1; i >= 0; i--) cur = cur.insert(elems.get(i)); return cur; } elems.add(cur.getFirst()); cur = cur.getNext(); } }
@Override public SList<ATerm> concat(final SList<ATerm> rhs) { if (isEmpty()) return rhs; if (_next.isEmpty()) return rhs.insert(_first); return _next.concat(rhs).insert(_first); }
@Override public ATermList dictPut(final ATerm key, final ATerm value) { if (isEmpty()) { final ATermList pair = getEmpty().insert(value).insert(key); return getEmpty().insert(pair); } ATermList pair = (ATermList) _first; if (key.equals(pair.getFirst())) { pair = getEmpty().insert(value).insert(pair); return _next.insert(pair); } return _next.dictPut(key, value).insert(_first); }
@Override public ATermList getEmpty() { return getPureFactory().makeList(); }
@Override public ATermList makeList(final ATerm first, final ATermList next) { return build(new ATermListImpl(this, first, next)); }
@Override public ATermList insertAt(final ATerm el, final int i) { if (0 > i || i > _length) throw new IllegalArgumentException(_illegalListIndex + i); if (i == 0) return insert(el); return _next.insertAt(el, i - 1).insert(_first); }
@Override public boolean equivalent(final SharedObject obj) { if (obj instanceof ATermList) { final ATermList peer = (ATermList) obj; if (peer.getType() != getType()) return false; return peer.getFirst() == _first && peer.getNext() == _next; } return false; }
@Override public ATermList reverse() { ATermList cur = this; ATermList reverse = getEmpty(); while (!cur.isEmpty()) { reverse = reverse.insert(cur.getFirst()); cur = cur.getNext(); } return reverse; }
/** * depricated Use the new constructor instead. * * @param annos x * @param _first x * @param _next x */ protected void initHashCode(final ATerm first, final ATermList next) { _first = first; _next = next; setHashCode(hashFunction()); if (first == null && next == null) _length = 0; else _length = 1 + next.getLength(); }
@Override public ATermList dictRemove(final ATerm key) { if (isEmpty()) return this; final ATermList pair = (ATermList) _first; if (key.equals(pair.getFirst())) return _next; return _next.dictRemove(key).insert(_first); }
@Override public ATermList insert(final ATerm head) { return getPureFactory().makeList(head, this); }
@Override public ATermList concat(final ATermList rhs) { if (isEmpty()) return rhs; if (_next.isEmpty()) return rhs.insert(_first); return _next.concat(rhs).insert(_first); }
protected ATermList make(final ATerm head, final ATermList tail) { return getPureFactory().makeList(head, tail); }
@Override public ATerm dictGet(final ATerm key) { if (isEmpty()) return null; final ATermList pair = (ATermList) _first; if (key.equals(pair.getFirst())) return pair.getNext().getFirst(); return _next.dictGet(key); }
@Override public ATermList getSlice(final int start, final int end) { int i; final int size = end - start; ATermList list = this; for (i = 0; i < start; i++) list = list.getNext(); final List<ATerm> buffer = new ArrayList<>(size); for (i = 0; i < size; i++) { buffer.add(list.getFirst()); list = list.getNext(); } ATermList result = getPureFactory().getEmpty(); for (--i; i >= 0; i--) result = result.insert(buffer.get(i)); return result; }