use of jcog.data.byt.DynBytes in project narchy by automenta.
the class FastCompound method get.
public static FastCompound get(Op o, int subs, Iterable<Term> subterms) {
ObjectByteHashMap<Term> atoms = new ObjectByteHashMap();
DynBytes shadow = new DynBytes(256);
// UncheckedBytes shadow = new UncheckedBytes(Bytes.wrapForWrite(new byte[256]));
shadow.writeUnsignedByte(o.ordinal());
shadow.writeUnsignedByte(subs);
final byte[] numAtoms = { 0 };
ByteFunction0 nextUniqueAtom = () -> numAtoms[0]++;
int structure = o.bit, hashCode = 1;
byte volume = 1;
for (Term x : subterms) {
x.recurseTerms((child) -> {
shadow.writeUnsignedByte((byte) child.op().ordinal());
if (child.op().atomic) {
int aid = atoms.getIfAbsentPut(child, nextUniqueAtom);
shadow.writeUnsignedByte((byte) aid);
} else {
shadow.writeUnsignedByte(child.subs());
// TODO use last bit of the subs byte to indicate presence or absence of subsequent 'dt' value (32 bits)
}
});
structure |= x.structure();
hashCode = Util.hashCombine(hashCode, x.hashCode());
volume += x.volume();
}
hashCode = Util.hashCombine(hashCode, o.id);
assert (volume < 127);
// TODO calculate normalized by the encountered sequence of variable id's - whether it is monotonically increasing by 1 each time the max value does increase or something
boolean normalized = false;
// TODO sort atoms to canonicalize its dictionary for sharing with other terms
FastCompound y;
// {
// byte[][] a = new byte[atoms.size()][];
// for (ObjectBytePair<Term> p : atoms.keyValuesView()) {
// a[p.getTwo()] = IO.termToBytes(p.getOne());
// }
// y = new FastCompoundSerializedAtoms(a, shadow.toByteArray(), x.hashCode(), x.hashCodeSubTerms(), (byte) x.volume(), x.isNormalized());
// }
{
Term[] a = new Term[atoms.size()];
for (ObjectBytePair<Term> p : atoms.keyValuesView()) {
a[p.getTwo()] = p.getOne();
}
y = new FastCompoundInstancedAtoms(a, shadow.toByteArray(), structure, hashCode, volume, normalized);
}
return y;
}
use of jcog.data.byt.DynBytes in project narchy by automenta.
the class IO method termToBytes.
public static byte[] termToBytes(Term t) {
if (t instanceof Atomic) {
return ((Atomic) t).bytes();
}
// bb = ArrayPool.bytes().
DynBytes d = new DynBytes(t.volume() * 4);
// ByteArrayOutputStream bs = new ByteArrayOutputStream();
t.append((ByteArrayDataOutput) d);
// bs.toByteArray();
return d.array();
}
use of jcog.data.byt.DynBytes in project narchy by automenta.
the class IO method writeUTF8WithPreLen.
// public static void writeUTF8(String s, DataOutput o) throws IOException {
// new Utf8Writer(o).write(s);
// }
public static void writeUTF8WithPreLen(String s, DataOutput o) throws IOException {
DynBytes d = new DynBytes(s.length());
new Utf8Writer(d).write(s);
o.writeShort(d.length());
d.appendTo(o);
}
Aggregations