6 internal class InternalUtils {
7 public static T[] check_arr_len<T>(T[] arr, int length) {
8 if (arr != null && arr.Length != length) {
9 throw new ArgumentException("Array must be of fixed size " + length + " but was of length " + arr.Length);
14 public static byte[] convUInt5Array(UInt5[] u5s) {
15 if (u5s == null) return null;
16 byte[] res = new byte[u5s.Length];
17 for (int i = 0; i < u5s.Length; i++) {
18 res[i] = u5s[i].getVal();
23 public static T[] mapArray<F, T>(F[] arr, Func<F, T> f) {
24 if (arr == null) return null;
25 T[] ret = new T[arr.Length];
26 for (int i = 0; i < arr.Length; i++) ret[i] = f(arr[i]);
30 public static string decodeString(long strptr) {
31 byte[] bytes = decodeUint8Array(strptr);
32 return Encoding.UTF8.GetString(bytes);
35 public static long encodeString(string s) {
36 byte[] bytes = Encoding.UTF8.GetBytes(s);
37 return encodeUint8Array(bytes);
41 public static int getArrayLength(long arrptr) {
44 long* arrlen = (long*) arrptr;
47 if (len > 0xffffffffL) {
48 throw new ArgumentException("Array length was out of bounds");
54 public static long getU64ArrayElem(long arrptr, int idx) {
56 long* arr = (long*) (arrptr + 8);
60 public static int getU32ArrayElem(long arrptr, int idx) {
62 int* arr = (int*) (arrptr + 8);
66 public static short getU16ArrayElem(long arrptr, int idx) {
68 short* arr = (short*) (arrptr + 8);
72 public static byte getU8ArrayElem(long arrptr, int idx) {
74 byte* arr = (byte*) (arrptr + 8);
80 public static long encodeUint8Array(byte[] arr) {
81 long buf = bindings.allocate_buffer(arr.Length + 8);
82 unsafe { *((long*)buf) = (long)arr.Length; }
83 for (int i = 0; i < arr.Length; i++) {
85 ((byte*)(buf + 8))[i] = arr[i];
90 public static long encodeUint16Array(short[] arr) {
91 long buf = bindings.allocate_buffer(arr.Length * 2 + 8);
92 unsafe { *((long*)buf) = (long)arr.Length; }
93 for (int i = 0; i < arr.Length; i++) {
95 ((short*)(buf + 8))[i] = arr[i];
100 public static long encodeUint32Array(int[] arr) {
101 long buf = bindings.allocate_buffer(arr.Length * 4 + 8);
102 unsafe { *((long*)buf) = (long)arr.Length; }
103 for (int i = 0; i < arr.Length; i++) {
105 ((int*)(buf + 8))[i] = arr[i];
110 public static long encodeUint64Array(long[] arr) {
111 long buf = bindings.allocate_buffer(arr.Length * 8 + 8);
112 unsafe { *((long*)buf) = (long)arr.Length; }
113 for (int i = 0; i < arr.Length; i++) {
115 ((long*)(buf + 8))[i] = arr[i];
122 public static byte[] decodeUint8Array(long arrptr) {
123 int len = getArrayLength(arrptr);
124 byte[] res = new byte[len];
125 for (int i = 0; i < len; i++)
126 res[i] = getU8ArrayElem(arrptr, i);
127 bindings.free_buffer(arrptr);
130 public static short[] decodeUint16Array(long arrptr) {
131 int len = getArrayLength(arrptr);
132 short[] res = new short[len];
133 for (int i = 0; i < len; i++)
134 res[i] = getU16ArrayElem(arrptr, i);
135 bindings.free_buffer(arrptr);
138 public static long[] decodeUint64Array(long arrptr) {
139 int len = getArrayLength(arrptr);
140 long[] res = new long[len];
141 for (int i = 0; i < len; i++)
142 res[i] = getU64ArrayElem(arrptr, i);
143 bindings.free_buffer(arrptr);