private static sbyte[] CreateBitCompactedArray(ShortArray type, object array, int offsetBytes) { Type componentType = array.GetType().GetElementType(); bool isPrimitiveByteArray = componentType.Equals(Byte.TYPE); bool isByteArray = componentType.Equals(typeof(Byte)) || isPrimitiveByteArray; int arrayLength = Array.getLength(array); int requiredBits = isByteArray ? (sizeof(sbyte) * 8) : type.calculateRequiredBitsForArray(array, arrayLength); int totalBits = requiredBits * arrayLength; int bitsUsedInLastByte = totalBits % 8; bitsUsedInLastByte = bitsUsedInLastByte == 0 ? 8 : bitsUsedInLastByte; if (isByteArray) { return(CreateBitCompactedByteArray(type, isPrimitiveByteArray, array, bitsUsedInLastByte, requiredBits, offsetBytes)); } else { int numberOfBytes = (totalBits - 1) / 8 + 1; numberOfBytes += NUMBER_HEADER_SIZE; // type + rest + requiredBits header. TODO no need to use full bytes Bits bits = Bits.bits(numberOfBytes); bits.Put(( sbyte )type.intValue()); bits.Put(( sbyte )bitsUsedInLastByte); bits.Put(( sbyte )requiredBits); type.writeAll(array, arrayLength, requiredBits, bits); return(bits.AsBytes(offsetBytes)); } }
private static sbyte[] CreateUncompactedArray(ShortArray type, object array, int offsetBytes) { int arrayLength = Array.getLength(array); int bytesPerElement = type.maxBits / 8; sbyte[] bytes = new sbyte[NUMBER_HEADER_SIZE + bytesPerElement * arrayLength + offsetBytes]; bytes[offsetBytes + 0] = ( sbyte )type.intValue(); bytes[offsetBytes + 1] = ( sbyte )8; bytes[offsetBytes + 2] = ( sbyte )type.maxBits; type.writeAll(array, bytes, NUMBER_HEADER_SIZE + offsetBytes); return(bytes); }
private static sbyte[] CreateBitCompactedByteArray(ShortArray type, bool isPrimitiveByteArray, object array, int bitsUsedInLastByte, int requiredBits, int offsetBytes) { int arrayLength = Array.getLength(array); sbyte[] bytes = new sbyte[NUMBER_HEADER_SIZE + arrayLength + offsetBytes]; bytes[offsetBytes + 0] = ( sbyte )type.intValue(); bytes[offsetBytes + 1] = ( sbyte )bitsUsedInLastByte; bytes[offsetBytes + 2] = ( sbyte )requiredBits; if (isPrimitiveByteArray) { arraycopy(array, 0, bytes, NUMBER_HEADER_SIZE + offsetBytes, arrayLength); } else { sbyte?[] source = ( sbyte?[] )array; for (int i = 0; i < source.Length; i++) { bytes[NUMBER_HEADER_SIZE + offsetBytes + i] = source[i].Value; } } return(bytes); }