public static void Expand(Vec *ptr, int elemNum) { byte *oldBlob = ptr->blob; int newSz; int oldSz = (ptr->elNm * ptr->elSz); int minSz = (elemNum * ptr->elSz); if (oldSz * 2 > (minSz + oldSz)) // MAX function { newSz = oldSz * 2; } else { newSz = (minSz + oldSz) & (-4); } byte *newBlob = (byte *)malloc(newSz); for (int i = 0; i < oldSz; i++) { *(newBlob + i) = *(oldBlob + i); } free((void *)oldBlob); ptr->blob = newBlob; ptr->elNm = newSz / ptr->elSz; }
public static Vec *newVector(int elemSiz, int elemNum) { Vec *ptr = (Vec *)malloc(sizeof(Vec)); ptr->elSz = elemSiz; ptr->elNm = (elemNum + 3) & (-4); ptr->blob = (byte *)malloc(elemSiz * ptr->elNm); ptr->high = -1; return(ptr); }
public static void CatArr(Vec *ptr, byte *str, uint sHi) { int iHi = (int)sHi; Trim(ptr); if (ptr->high + iHi >= ptr->elNm) { Expand(ptr, ptr->high + iHi + 1); } ByteAppend(ptr, str, iHi); }
// // Need string concatenations also ... // VecConcat, StrConcat, ChrConcat // public static void CatVec(Vec *vec1, Vec *vec2) { Trim(vec1); int dlta = Len(vec2); if (vec1->high + dlta >= vec1->elNm) { Expand(vec1, vec1->high + dlta + 1); } ByteAppend(vec1, vec2->blob, dlta); }
// // Return length of an m2-char vector // private static int Len(Vec *ptr) { // Assert: this vector is of m2-char for (int idx = 0; idx <= ptr->high; idx++) { if (*(ptr->blob + idx) == (byte)0) { return(idx); } } return(ptr->high); }
// // Trim a m2-char vector so that 'high' is the last non-nul character // private static void Trim(Vec *ptr) { // Assert: this vector is of m2-char for (int idx = 0; idx <= ptr->high; idx++) { byte chr = *(ptr->blob + idx); if (chr == (byte)0) { ptr->high = idx; return; } } }
public static void CatChr(Vec *ptr, byte chr) { Trim(ptr); if (chr != (byte)0) // append of chr { ptr->high++; if (ptr->high >= ptr->elNm) { Expand(ptr, ptr->elNm + 1); } *(ptr->blob + ptr->high) = chr; } }
// // Append m2-chars up to but not including the first nul character. // private static void ByteAppend(Vec *ptr, byte *src2, int maxL) { int idx; int jdx = ptr->high; for (idx = 0; idx < maxL; idx++) { byte chr = *(src2 + idx); if (chr == (byte)0) { ptr->high = jdx; return; } jdx++; *(ptr->blob + jdx) = chr; ptr->high = jdx; } }
public static void Dispose(Vec *ptr) { free((void *)ptr->blob); free((void *)ptr); }