class Vec { protected float[] vec; const char[4] sig = "nVec"; // constructors this(int len, float val) { initRand(); vec.length = len; for (int i = 0; i < vec.length; i++) vec[i] = val; } // a lot of stuff was removed here /** * Export the object to a seekable stream. The format is only * intended for computers, but it should be lossless. * The returned value is the stream address of the start of the object. */ ulong write(Stream s) in { assert (s.isOpen()); assert (s.writeable); assert (s.seekable); } body { ulong oStart = s.position; s.write(cast(uint)0); s.write(char4ToUint(sig)); s.write(cast(uint)(this.vec.length)); foreach(float f; this.vec) s.write(f); s.write(eor); ulong oEnd = s.position; s.position(oStart); s.write(cast(uint)(oEnd - oStart) ); s.position(oEnd); return oStart; } /** * Import the object from a seekable stream. The format is only * intended for computers, but it should be lossless. */ static Vec get(Stream s) in { assert (s.isOpen()); assert (s.readable); assert (s.seekable); } body { ulong oStart = s.position; Vec v = new Vec(); uint len, vlen, veor, tmpSig2; char[] sig2; s.read(len); s.read(tmpSig2); sig2 = uintToChar4(tmpSig2); if (sig != sig2) { s.position(oStart); writefln("original signature = <", sig, ">"); writefln("read in signature = <", sig2, ">"); throw new Exception ("Vec:get: Signature mismatch"); } s.read(cast(uint)(vlen)); v.length = vlen; for(int i = 0; i < vlen; i++) s.read(v.vec[i]); s.read(veor); if (eor != veor) { s.position(oStart); throw new Exception ("Vec:get: eor not detected when expected"); } ulong oEnd = s.position; if (len != cast(uint)(oEnd - oStart) ) { s.position(oStart); writefln("length calculated = %d", len); writefln("length read = %d", cast(uint)(oEnd - oStart)); throw new Exception ("Vec:get: length calculated does not match length read"); } return v; } // get }