struct dMatrix *dataFromTable(char *tableName, char *query) /* Read data from table name with rowname. */ { struct sqlConnection *conn = hAllocConn(); struct slName *colNames = sqlListFields(conn, tableName); struct slName *name = NULL; int count = 0; struct dMatrix *dM = NULL; struct sqlResult *sr = NULL; char **row = NULL; int colIx = 0; /* Allocate some initial memory. */ AllocVar(dM); dM->colCount = slCount(colNames) -1; dM->rowCount = 1; AllocArray(dM->matrix, dM->rowCount); AllocArray(dM->rowNames, dM->rowCount); AllocArray(dM->matrix[0], dM->colCount); AllocArray(dM->colNames, dM->colCount); for(name = colNames->next; name != NULL; name = name->next) dM->colNames[count++] = cloneString(name->name); /* Execute our query. */ sr = sqlGetResult(conn, query); count = 0; /* Read out the data. */ while((row = sqlNextRow(sr)) != NULL) { /* Expand matrix data as necessary. */ if(count > 0) { ExpandArray(dM->matrix, dM->rowCount, dM->rowCount+1); AllocArray(dM->matrix[dM->rowCount], dM->colCount); ExpandArray(dM->rowNames, dM->rowCount, dM->rowCount+1); dM->rowCount++; } dM->rowNames[count] = cloneString(row[0]); for(colIx = 0; colIx < dM->colCount; colIx++) dM->matrix[count][colIx] = atof(row[colIx+1]); count++; } if(count == 0) errAbort("Didn't find any results for query:\n%s", query); sqlFreeResult(&sr); hFreeConn(&conn); return dM; }
void sqlStringStaticArray(char *s, char ***retArray, int *retSize) /* Convert comma separated list of strings to an array which will be * overwritten next call to this function or to sqlStringDynamicArray, * but need not be freed. */ { static char **array = NULL; static int alloc = 0; int count = 0; for (;;) { char *e; if (s == NULL || s[0] == 0) break; e = strchr(s, ','); if (e != NULL) *e++ = 0; if (count >= alloc) { if (alloc == 0) alloc = 64; else alloc <<= 1; ExpandArray(array, count, alloc); } array[count++] = s; s = e; } *retSize = count; *retArray = array; }
void sqlLongLongStaticArray(char *s, long long **retArray, int *retSize) /* Convert comma separated list of numbers to an array which will be * overwritten next call to this function, but need not be freed. */ { static long long *array = NULL; static unsigned alloc = 0; unsigned count = 0; for (;;) { char *e; if (s == NULL || s[0] == 0) break; e = strchr(s, ','); if (e != NULL) *e++ = 0; if (count >= alloc) { if (alloc == 0) alloc = 64; else alloc <<= 1; ExpandArray(array, count, alloc); } array[count++] = sqlLongLong(s); s = e; } *retSize = count; *retArray = array; }
void ave(char *fileName) /* ave - Compute average and basic stats. */ { int count = 0; size_t alloc = 1024; double *array; struct lineFile *lf = lineFileOpen(fileName, TRUE); char *words[128], *word; int wordCount; int wordIx = col-1; AllocArray(array, alloc); while ((wordCount = lineFileChop(lf, words)) > 0) { if (count >= alloc) { alloc <<= 1; ExpandArray(array, count, alloc); } word = words[wordIx]; if (word[0] == '-' || isdigit(word[0])) { array[count++] = atof(word); } } if (count == 0) errAbort("No numerical data column %d of %s", col, fileName); qsort(array, count, sizeof(array[0]), cmpDouble); showStats(array, count); }
ALERROR CIntArray::InsertRange (CIntArray *pList, int iStart, int iEnd, int iPos) // InsertRange // // Inserts a range of integers from another array { ALERROR error; int i, iCount; if (iPos < 0 || iPos > m_iLength) iPos = m_iLength; iCount = iEnd - iStart + 1; if (error = ExpandArray(iPos, iCount)) return error; // Copy the data for (i = 0; i < iCount; i++) m_pData[iPos + i] = pList->GetElement(iStart + i); return NOERROR; }
static void expandQuickHeap(struct quickHeap *h) /* Heap needs more space, double the size of the array preserving elements */ { int newSize = h->heapMax * 2; ExpandArray(h->heap, h->heapMax, newSize); h->heapMax = newSize; }
static void growRow(struct rowReader *rr, int needCols) /* grow buffer so it can hold the need number of columns */ { int newSpace = rr->colSpace; while (newSpace < needCols) newSpace *= 2; ExpandArray(rr->row, rr->colSpace, newSpace); rr->colSpace = newSpace; }
struct wigSection *wigSectionRead(struct lineFile *lf) /* Parse out next section of wig. */ { static double *vals = NULL; static int valAlloc = 0; /* Get "fixedStep" line and parse it. */ char *line; if (!lineFileNextReal(lf, &line)) return NULL; char *pattern = "fixedStep "; int patSize = 10; if (!startsWith(pattern, line)) errAbort("Expecting fixedStep line %d of %s", lf->lineIx, lf->fileName); line += patSize; struct hash *varHash = hashVarLine(line, lf->lineIx); int step = sqlUnsigned(requiredVal(lf, varHash, "step")); int start = sqlUnsigned(requiredVal(lf, varHash, "start")); char *chrom = cloneString(requiredVal(lf, varHash, "chrom")); hashFree(&varHash); /* Parse out numbers until next fixedStep. */ int valCount = 0; int i; for (;;) { if (!lineFileNextReal(lf, &line)) break; if (startsWith(pattern, line)) { lineFileReuse(lf); break; } for (i=0; i<step; ++i) { if (valCount >= valAlloc) { int newAlloc = valAlloc + 1024; ExpandArray(vals, valAlloc, newAlloc); valAlloc = newAlloc; } vals[valCount] = lineFileNeedDouble(lf, &line, 0); ++valCount; } } /* Create wigSection. */ struct wigSection *section; AllocVar(section); section->chrom = chrom; section->chromStart = start; section->chromEnd = start + valCount; section->vals = CloneArray(vals, valCount); return section; }
TrapEdgeList *TrapsList::AddEdgeList(void) { if (Used >= CurrentSize) // If we've run out of room in the array { if (!ExpandArray()) // Try to allocate more return(NULL); // And return NULl if we failed } TrapEdgeList *pObject = new TrapEdgeList(this); if (pObject == NULL) return(NULL); pTraps[Used] = pObject; Used++; return(pObject); }
void removeTempDir(struct genomeBit *target, struct genomeBit *align) /* remove the files in the temp dir and then the temp dir itself */ { struct genomeBit *gb = NULL; char *file = NULL; char buff[2048]; int retVal = 0; int i=0; char *alignSuffixes[] = { ".fa.aat", ".fa.minfo", ".fa.mout" }; for(i=0; i<ArraySize(fileSuffixes); i++) { file = fileNameFromGenomeBit(outputRoot, fileSuffixes[i], target); remove(file); freez(&file); } for(i=0; i<ArraySize(fileSuffixes); i++) { file = fileNameFromGenomeBit(outputRoot, fileSuffixes[i], align); remove(file); freez(&file); } for(i=0; i<ArraySize(alignSuffixes); i++) { char *tmpFile = NULL; file = fileNameFromGenomeBit(outputRoot, "", target); tmpFile = cloneString(file); ExpandArray(file, strlen(file), (2*strlen(file)+100)); snprintf(file, (2*strlen(file)+100), "%s_%s:%u-%u%s", tmpFile, align->chrom, align->chromStart, align->chromEnd,alignSuffixes[i]); remove(file); freez(&file); freez(&file); } snprintf(buff, sizeof(buff),"%score", outputRoot); remove(buff); snprintf(buff, sizeof(buff),"%s*", outputRoot); remove(buff); rmdir(outputRoot); snprintf(buff, sizeof(buff), "rm -rf %s", outputRoot); warn("Executing '%s' just to be sure.", buff); retVal = system(buff); warn( "%s exited with value %d", buff, retVal); }
void heapMinInsert(struct heap *h, void *val) /* Insert val into the heap. */ { struct heapEl *el = NULL; int index = 0; assert(h); index = h->count; AllocVar(el); if(index +1 >= h->capacity) { ExpandArray(h->array, h->capacity, h->capacity * 2); h->capacity = h->capacity *2; } el->val = val; h->array[index] = el; h->count++; while(index > 0 && (h->cmpItems(h->array[_heapParent(index)]->val, h->array[index]->val) > 0)) { _heapExchange(h, _heapParent(index), index); index = _heapParent(index); } }
ALERROR CIntArray::InsertElement (int iElement, int iPos, int *retiIndex) // InsertElement // // Inserts data into the list. iPos is the position in the list to insert at. // If -1, the data is inserted at the end of the list. { ALERROR error; if (iPos < 0 || iPos > m_iLength) iPos = m_iLength; if (error = ExpandArray(iPos, 1)) return error; m_pData[iPos] = iElement; if (retiIndex) *retiIndex = iPos; return NOERROR; }
BOOL TrapEdgeList::AddEdge(DocCoord *pPoint, TrapJoinType JoinType) { // --- Make sure we have room to add the new point to our array DocCoord Temp; // Temporary safe storage for the point if (Used >= CurrentSize) { // I pass in a pointer to the coord for efficiency, to save copying points until // absolutely necessary. This is safe under normal circumstances, except when // we realloc the array and the input pointer points INTO that array! Most of // the time, it works fine, but every now and then NT maps out the memory page // and we get a nasty access violation! Rather than copying the Coord every // time, I prefer therefore to copy it into a temporary point on those few // occasions where we actually have to realloc the array. Temp = *pPoint; // Copy the point into safe, temporary storage pPoint = &Temp; // And point the input value pointer at the safe copy if (!ExpandArray()) // Try to allocate more return FALSE; // And return if we failed } // --- Find the new entry and allocate it TrapEdge *pEdge = &pEdges[Used]; // --- Record the point & join type pEdge->Centre = *pPoint; pEdge->PrevTrapJoin = JoinType; // --- Calculate position, and check for repeating strokes if (Used > 0) { TrapEdge *pPrevEdge = &pEdges[Used-1]; const double dx = (double) (pPrevEdge->Centre.x - pPoint->x); const double dy = (double) (pPrevEdge->Centre.y - pPoint->y); double Travel = sqrt(dx*dx + dy*dy); if (Travel < 1.0) // Make sure all positions increment slightly to keep everyone happy Travel = 1.0; pEdge->Position = pPrevEdge->Position + Travel; // Now check for (and handle) repeating strokes const INT32 RepeatLength = (pParentList == NULL) ? 0 : pParentList->GetRepeatLength(); if (RepeatLength > 0 && JoinType == TrapJoin_None) { // If it's a repeating stroke, and we're not in the middle of a join, then // we compare the current path travel to the repeat distance to see if we've gone // past the end of this repeat. If we have, then we calculate the point where the // (first) repeat in this trapezoid should happen, and break this trapezoid there, // ending the current TrapEdgeList, and starting a new TrapEdgeList. // Note: We recursively call AddEdge on each new TrapEdgeList so that if the // trapezoid contains many repeats, we will subdivide it further. if (pEdge->Position >= (double)RepeatLength) { // We've gone too far. Time to subdivide. ERROR3IF(pEdge->Position - pPrevEdge->Position <= 0.0, "Position calculation is screwed up!"); const double Split = (((double)RepeatLength) - pPrevEdge->Position) / (pEdge->Position - pPrevEdge->Position); // Calculate the split point & position value into our last Edge pEdge->Centre.x = (INT32) (((1.0 - Split) * (double)pPrevEdge->Centre.x) + (Split * (double)pPoint->x)); pEdge->Centre.y = (INT32) (((1.0 - Split) * (double)pPrevEdge->Centre.y) + (Split * (double)pPoint->y)); pEdge->Position = (double) RepeatLength; // And create a new TrapEdgelist, and initialise it to go from the split point to the // edge which we were orignally passed - NOTE that this may RECURSE. TrapEdgeList *pNewEdgeList = pParentList->AddEdgeList(); if (pNewEdgeList != NULL) { pNewEdgeList->AddEdge(&pEdge->Centre, TrapJoin_None); // Add intersection point pNewEdgeList->AddEdge(pPoint, TrapJoin_None); // then add the original end Edge } } } } else pEdge->Position = 0.0; // Finally, increment Used to move on to the next TrapEdge entry in our array Used++; return TRUE; }
/** * Appends one string to another without having to * worry about going over. Assumes that the total length * of dest is the strlen(dest) + 1. Wasteful if you want to * use it lots of times in a row as it reallocates memory every time */ void dynamicStrncat(char **dest, const char *src) { ExpandArray(*dest, (strlen(*dest)+1), (strlen(*dest) +strlen(src) + 1)); strncat(*dest, src, strlen(src)); }