/// <summary> /// Write data of any type. Note, consider using type-specific function for performance if the type is known at compile time /// </summary> /// <param name="o">object, any supported type</param> public void write(dynamic o) { // === wrap scalars === if(o is float) o = new octsock5Matrix<float>(o); else if(o is double) o = new octsock5Matrix<double>(o); else if(o is byte) o = new octsock5Matrix<byte>(o); else if(o is sbyte) o = new octsock5Matrix<sbyte>(o); else if(o is UInt16) o = new octsock5Matrix<UInt16>(o); else if(o is Int16) o = new octsock5Matrix<Int16>(o); else if(o is UInt32) o = new octsock5Matrix<UInt32>(o); else if(o is Int32) o = new octsock5Matrix<Int32>(o); else if(o is UInt64) o = new octsock5Matrix<UInt64>(o); else if(o is Int64) o = new octsock5Matrix<Int64>(o); if(o is octsock5Matrix<float>) this.writeWrapped(o as octsock5Matrix<float>); else if(o is octsock5Matrix<double>) this.writeWrapped(o as octsock5Matrix<double>); else if(o is octsock5Matrix<byte>) this.writeWrapped(o as octsock5Matrix<byte>); else if(o is octsock5Matrix<sbyte>) this.writeWrapped(o as octsock5Matrix<sbyte>); else if(o is octsock5Matrix<UInt16>) this.writeWrapped(o as octsock5Matrix<UInt16>); else if(o is octsock5Matrix<Int16>) this.writeWrapped(o as octsock5Matrix<Int16>); else if(o is octsock5Matrix<UInt32>) this.writeWrapped(o as octsock5Matrix<UInt32>); else if(o is octsock5Matrix<Int32>) this.writeWrapped(o as octsock5Matrix<Int32>); else if(o is octsock5Matrix<UInt64>) this.writeWrapped(o as octsock5Matrix<UInt64>); else if(o is octsock5Matrix<Int64>) this.writeWrapped(o as octsock5Matrix<Int64>); else if(o is string) this.writeString(o as string); else if(o is Dictionary<object, object>) this.writeDict(o); else if(o is Array) this.writeTuple(o); else throw new Exception("unsupported type"); }
/// <summary> /// reads octsock5Matrix<typeparamref name="T"/>, once the header has been processed /// </summary> /// <typeparam name="T">type of inbound data (known from header)</typeparam> /// <param name="interleavedComplex">whether inbound data represents complex data</param> /// <returns>octsock5Matrix<typeparamref name="T"/></returns> private object readMatrixOrScalar<T>(bool interleavedComplex = false) { Int64 h0 = BitConverter.ToInt64(this.buf, 0*sizeof(Int64)); // ID Int64 h1 = BitConverter.ToInt64(this.buf, 1*sizeof(Int64)); // nBytes/elem Int64 h2 = BitConverter.ToInt64(this.buf, 2*sizeof(Int64)); // nDims // === determine dimensions === Int64[] dims; Int64 nElems = 1; bool isArray = ((h0 & H0_ARRAY) != 0); if(isArray) { dims = new Int64[h2]; Buffer.BlockCopy(this.buf, 3*sizeof(Int64), dims, 0, (int)h2*sizeof(Int64)); for(int ix = 0; ix < dims.Length; ++ix) nElems *= dims[ix]; } else dims = new Int64[] { }; // empty dims flags scalar // === create new matrix object === octsock5Matrix<T> tmp = new octsock5Matrix<T>(); tmp.dims = dims; Int64 nBytesTotal = nElems*h1; // note: for complex, h1 is already doubled if(interleavedComplex) nElems *= 2; // for interleaved complex, double the number of real elements tmp.colMajorData = new T[nElems]; tmp.interleavedComplex = interleavedComplex; // === read data === this.readToBuf(nBytesTotal); // === copy data === Buffer.BlockCopy(this.buf, 0, tmp.colMajorData, 0, (int)nBytesTotal); // === return array or unwrap scalar === // Note: C# has no if(isArray || interleavedComplex) return tmp; return tmp.colMajorData[0]; }
/// <summary> /// Writes a wrapped matrix type /// </summary> /// <typeparam name="T">type of numeric array</typeparam> /// <param name="o">wrapper object</param> public void writeWrapped<T>(octsock5Matrix<T> o) { int nElemBytes = Marshal.SizeOf(typeof(T)); // === set flags === if(typeof(T) == typeof(float)) this.header[0] = H0_FLOAT; else if(typeof(T) == typeof(double)) this.header[0] = H0_FLOAT; else if(typeof(T) == typeof(byte)) this.header[0] = H0_INTEGER; else if(typeof(T) == typeof(sbyte)) this.header[0] = H0_INTEGER | H0_SIGNED; else if(typeof(T) == typeof(UInt16)) this.header[0] = H0_INTEGER; else if(typeof(T) == typeof(Int16)) this.header[0] = H0_INTEGER | H0_SIGNED; else if(typeof(T) == typeof(UInt32)) this.header[0] = H0_INTEGER; else if(typeof(T) == typeof(Int32)) this.header[0] = H0_INTEGER | H0_SIGNED; else if(typeof(T) == typeof(UInt64)) this.header[0] = H0_INTEGER; else if(typeof(T) == typeof(Int64)) this.header[0] = H0_INTEGER | H0_SIGNED; else throw new Exception("unsupported type"); // === set element size === if(o.interleavedComplex) { this.header[0] |= H0_COMPLEX; this.header[1] = 2*nElemBytes; } else this.header[1] = nElemBytes; // === set dims === if(o.dims == null || o.dims.Length == 0) { this.header[2] = 1; // one dimension this.header[3] = 1; // with size 1 } else { this.header[2] = o.dims.Length; Buffer.BlockCopy(o.dims, 0, this.header, 3*sizeof(Int64), o.dims.Length * sizeof(Int64)); this.header[0] |= H0_ARRAY; } this.sendHeader(); this.sendArray(o.colMajorData); }