void AddEdgeToVertices(Graph *graph, ULONG edgeIndex) { ULONG v1, v2; Vertex *vertex; ULONG *edgeIndices; v1 = graph->edges[edgeIndex].vertex1; v2 = graph->edges[edgeIndex].vertex2; vertex = & graph->vertices[v1]; edgeIndices = (ULONG *) realloc(vertex->edges, sizeof(ULONG) * (vertex->numEdges + 1)); if (edgeIndices == NULL) OutOfMemoryError("AddEdgeToVertices:edgeIndices1"); edgeIndices[vertex->numEdges] = edgeIndex; vertex->edges = edgeIndices; vertex->numEdges++; if (v1 != v2) { // don't add a self edge twice vertex = & graph->vertices[v2]; edgeIndices = (ULONG *) realloc(vertex->edges, sizeof(ULONG) * (vertex->numEdges + 1)); if (edgeIndices == NULL) OutOfMemoryError("AddEdgeToVertices:edgeIndices2"); edgeIndices[vertex->numEdges] = edgeIndex; vertex->edges = edgeIndices; vertex->numEdges++; } }
ReferenceGraph *AllocateReferenceGraph (ULONG v, ULONG e) { ReferenceGraph *graph; graph = (ReferenceGraph *) malloc (sizeof (ReferenceGraph)); if (graph == NULL) OutOfMemoryError("AllocateReferenceGraph:graph"); graph->numVertices = v; graph->numEdges = e; graph->vertices = NULL; graph->edges = NULL; if (v > 0) { graph->vertices = (ReferenceVertex *) malloc (sizeof (ReferenceVertex) * v); if (graph->vertices == NULL) OutOfMemoryError ("AllocateReferenceGraph:graph->vertices"); } graph->vertexListSize = v; if (e > 0) { graph->edges = (ReferenceEdge *) malloc (sizeof (ReferenceEdge) * e); if (graph->edges == NULL) OutOfMemoryError ("AllocateReferenceGraph:graph->edges"); } graph->edgeListSize = e; return graph; }
Graph *AllocateGraph(ULONG v, ULONG e) { Graph *graph; graph = (Graph *) malloc(sizeof(Graph)); if (graph == NULL) OutOfMemoryError("AllocateGraph:graph"); graph->numVertices = v; graph->numEdges = e; graph->vertices = NULL; graph->edges = NULL; if (v > 0) { graph->vertices = (Vertex *) malloc(sizeof(Vertex) * v); if (graph->vertices == NULL) OutOfMemoryError("AllocateGraph:graph->vertices"); } if (e > 0) { graph->edges = (Edge *) malloc(sizeof(Edge) * e); if (graph->edges == NULL) OutOfMemoryError("AllocateGraph:graph->edges"); } return graph; }
void AddRefEdgeToRefVertices(ReferenceGraph *graph, ULONG edgeIndex) { ULONG v1, v2; ReferenceVertex *vertex; ULONG *edgeIndices; v1 = graph->edges[edgeIndex].vertex1; v2 = graph->edges[edgeIndex].vertex2; vertex = & graph->vertices[v1]; edgeIndices = (ULONG *) realloc(vertex->edges, sizeof (ULONG) * (vertex->numEdges + 1)); if (edgeIndices == NULL) OutOfMemoryError ("AddRefEdgeToRefVertices:edgeIndices1"); edgeIndices[vertex->numEdges] = edgeIndex; vertex->edges = edgeIndices; vertex->vertexValid = TRUE; vertex->numEdges++; if (v1 != v2) // do not add a self edge twice { vertex = & graph->vertices[v2]; edgeIndices = (ULONG *) realloc (vertex->edges, sizeof (ULONG) * (vertex->numEdges + 1)); if (edgeIndices == NULL) OutOfMemoryError ("AddRefEdgeToRefVertices:edgeIndices2"); edgeIndices[vertex->numEdges] = edgeIndex; vertex->edges = edgeIndices; vertex->vertexValid = TRUE; vertex->numEdges++; } }
void AddVertex(Graph *graph, ULONG labelIndex, ULONG *vertexListSize) { Vertex *newVertexList; ULONG numVertices; numVertices = graph->numVertices; // make sure there is enough room for another vertex if (*vertexListSize == graph->numVertices) { *vertexListSize += LIST_SIZE_INC; newVertexList = (Vertex *) realloc(graph->vertices, (sizeof(Vertex) * (*vertexListSize))); if (newVertexList == NULL) OutOfMemoryError("vertex list"); graph->vertices = newVertexList; } // store information in vertex graph->vertices[numVertices].label = labelIndex; graph->vertices[numVertices].numEdges = 0; graph->vertices[numVertices].edges = NULL; graph->vertices[numVertices].map = VERTEX_UNMAPPED; graph->vertices[numVertices].used = FALSE; graph->numVertices++; }
inline void Array::allocate(Size size) { if (pointer_ != 0 && bufferSize_ != 0) delete[] pointer_; if (size <= 0) { pointer_ = 0; } else { n_ = size; bufferSize_ = size+size/10+10; try { pointer_ = new double[bufferSize_]; } catch (...) { pointer_ = 0; } if (pointer_ == 0) { n_ = bufferSize_ = size; try { pointer_ = new double[bufferSize_]; } catch (...) { pointer_ = 0; } if (pointer_ == 0) { n_ = bufferSize_ = 0; throw OutOfMemoryError("Array"); } } } }
void AddReferenceVertex (ReferenceGraph *graph, ULONG labelIndex) { ReferenceVertex *newVertexList; ULONG numVertices = graph->numVertices; ULONG vertexListSize = graph->vertexListSize; // make sure there is enough room for another vertex if (vertexListSize == graph->numVertices) { vertexListSize += LIST_SIZE_INC; newVertexList = (ReferenceVertex *) realloc (graph->vertices, (sizeof (ReferenceVertex) * vertexListSize)); if (newVertexList == NULL) OutOfMemoryError("vertex list"); graph->vertices = newVertexList; graph->vertexListSize = vertexListSize; } // store information in vertex graph->vertices[numVertices].label = labelIndex; graph->vertices[numVertices].numEdges = 0; graph->vertices[numVertices].edges = NULL; graph->vertices[numVertices].map = VERTEX_UNMAPPED; graph->vertices[numVertices].used = FALSE; graph->vertices[numVertices].vertexValid = TRUE; graph->numVertices++; }
void AddReferenceEdge(ReferenceGraph *graph, ULONG sourceVertexIndex, ULONG targetVertexIndex, BOOLEAN directed, ULONG labelIndex, BOOLEAN spansIncrement) { ReferenceEdge *newEdgeList; ULONG edgeListSize = graph->edgeListSize; // make sure there is enough room for another edge in the graph if (edgeListSize == graph->numEdges) { edgeListSize += LIST_SIZE_INC; newEdgeList = (ReferenceEdge *) realloc(graph->edges, (sizeof (ReferenceEdge) * edgeListSize)); if (newEdgeList == NULL) OutOfMemoryError("AddEdge:newEdgeList"); graph->edges = newEdgeList; graph->edgeListSize = edgeListSize; } // add edge to graph graph->edges[graph->numEdges].vertex1 = sourceVertexIndex; graph->edges[graph->numEdges].vertex2 = targetVertexIndex; graph->edges[graph->numEdges].spansIncrement = spansIncrement; graph->edges[graph->numEdges].label = labelIndex; graph->edges[graph->numEdges].directed = directed; graph->edges[graph->numEdges].used = FALSE; graph->edges[graph->numEdges].failed = FALSE; // add index to edge in edge index array of both vertices AddRefEdgeToRefVertices(graph, graph->numEdges); graph->numEdges++; }
void PrintMapping(VertexMap *mapping, ULONG numVertices) { ULONG *sortedMapping; ULONG i; sortedMapping = (ULONG *) malloc(sizeof(ULONG) * numVertices); if (sortedMapping == NULL) OutOfMemoryError("sortedMapping"); for (i = 0; i < numVertices; i++) sortedMapping[mapping[i].v1] = mapping[i].v2; printf("Mapping (vertices of larger graph to smaller):\n"); for (i = 0; i < numVertices; i++) { printf(" %lu -> ", i+1); if (sortedMapping[i] == VERTEX_DELETED) printf("deleted\n"); else if (sortedMapping[i] == VERTEX_UNMAPPED) // this should never happen printf("unmapped\n"); else printf("%lu\n", sortedMapping[i] + 1); } free(sortedMapping); }
void AddInstanceToInstance(Instance *instance1, Instance *instance2) { ULONG v1, v2, e1, e2, i; ULONG nv2, ne2; // search for vertices of instance1 in instance2; if not found, then add // them, keeping instance2's vertices in order v2 = 0; for (v1 = 0; v1 < instance1->numVertices; v1++) { nv2 = instance2->numVertices; while ((v2 < nv2) && (instance2->vertices[v2] < instance1->vertices[v1])) v2++; if ((v2 == nv2) || (instance2->vertices[v2] > instance1->vertices[v1])) { // vertex not in instance2, so make room instance2->vertices = (ULONG *) realloc(instance2->vertices, (sizeof(ULONG) * (nv2 + 1))); if (instance2->vertices == NULL) OutOfMemoryError("AddInstanceToInstance:instance2->vertices"); for (i = nv2; i > v2; i--) instance2->vertices[i] = instance2->vertices[i-1]; instance2->vertices[v2] = instance1->vertices[v1]; instance2->numVertices++; } } // insert new edges from instance1 into instance2 e2 = 0; for (e1 = 0; e1 < instance1->numEdges; e1++) { ne2 = instance2->numEdges; while ((e2 < ne2) && (instance2->edges[e2] < instance1->edges[e1])) e2++; if ((e2 == ne2) || (instance2->edges[e2] > instance1->edges[e1])) { // edge not in instance2, so make room instance2->edges = (ULONG *) realloc(instance2->edges, (sizeof(ULONG) * (ne2 + 1))); if (instance2->edges == NULL) OutOfMemoryError("AddInstanceToInstance:instance2->edges"); for (i = ne2; i > e2; i--) instance2->edges[i] = instance2->edges[i-1]; instance2->edges[e2] = instance1->edges[e1]; instance2->numEdges++; } } }
ULONG *AddVertexIndex(ULONG *vertexIndices, ULONG n, ULONG index) { vertexIndices = (ULONG *) realloc(vertexIndices, sizeof(ULONG) * n); if (vertexIndices == NULL) OutOfMemoryError("AddVertexIndex:vertexIndices"); vertexIndices[n - 1] = index; return vertexIndices; }
void* SystemAlloc(size_t size) { #ifndef MAP_ANONYMOUS #define MAP_ANONYMOUS MAP_ANON #endif void* ptr = 0; ptr = mmap(ptr, size, (PROT_READ | PROT_WRITE), MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); return ptr != MAP_FAILED ? ptr : OutOfMemoryError(size); }
void* fastMalloc( size_t size ) { uchar* udata = (uchar*)malloc(size + sizeof(void*) + CV_MALLOC_ALIGN); if(!udata) return OutOfMemoryError(size); uchar** adata = alignPtr((uchar**)udata + 1, CV_MALLOC_ALIGN); adata[-1] = udata; return adata; }
InstanceList *AllocateInstanceList(void) { InstanceList *instanceList; instanceList = (InstanceList *) malloc(sizeof(InstanceList)); if (instanceList == NULL) OutOfMemoryError("AllocateInstanceList:instanceList"); instanceList->head = NULL; return instanceList; }
SubList *AllocateSubList(void) { SubList *subList; subList = (SubList *) malloc(sizeof(SubList)); if (subList == NULL) OutOfMemoryError("AllocateSubList:subList"); subList->head = NULL; return subList; }
GSL_vector::GSL_vector(const GSL_vector& other) { vec = gsl_vector_alloc(other.size()); if (!vec) throw OutOfMemoryError( "Allocation of GSL_vector of size " + std::to_string(other.size()) + " failed."); gsl_vector_memcpy(vec, other.vec); }
SubListNode *AllocateSubListNode(Substructure *sub) { SubListNode *subListNode; subListNode = (SubListNode *) malloc(sizeof(SubListNode)); if (subListNode == NULL) OutOfMemoryError("SubListNode"); subListNode->sub = sub; subListNode->next = NULL; return subListNode; }
InstanceListNode *AllocateInstanceListNode(Instance *instance) { InstanceListNode *instanceListNode; instanceListNode = (InstanceListNode *) malloc(sizeof(InstanceListNode)); if (instanceListNode == NULL) OutOfMemoryError("AllocateInstanceListNode:InstanceListNode"); instanceListNode->instance = instance; instance->refCount++; instanceListNode->next = NULL; return instanceListNode; }
GSL_vector::GSL_vector(std::size_t size) { if (!size) return; vec = gsl_vector_calloc(size); if (!vec) throw OutOfMemoryError( "Allocation of GSL_vector of size " + std::to_string(size) + " failed."); }
Parameters *GetParameters(int argc, char *argv[]) { Parameters *parameters; parameters = (Parameters *) malloc(sizeof(Parameters)); if (parameters == NULL) OutOfMemoryError("GetParameters:parameters"); // initialize default parameter settings parameters->directed = TRUE; parameters->labelList = AllocateLabelList(); return parameters; }
GSL_vector::GSL_vector(std::initializer_list<double> list) { if (list.size() == 0) return; vec = gsl_vector_alloc(list.size()); if (!vec) throw OutOfMemoryError( "Allocation of GSL_vector of size " + std::to_string(list.size()) + " failed."); std::copy(list.begin(), list.end(), gsl_vector_ptr(vec, 0)); }
Instance *AllocateInstance(ULONG v, ULONG e) { Instance *instance; instance = (Instance *) malloc(sizeof(Instance)); if (instance == NULL) OutOfMemoryError("AllocateInstance:instance"); instance->numVertices = v; instance->numEdges = e; instance->vertices = NULL; instance->edges = NULL; instance->newVertex = 0; instance->newEdge = 0; instance->mappingIndex1 = MAX_UNSIGNED_LONG; instance->mappingIndex2 = MAX_UNSIGNED_LONG; instance->used = FALSE; if (v > 0) { instance->vertices = (ULONG *) malloc(sizeof(ULONG) * v); if (instance->vertices == NULL) OutOfMemoryError("AllocateInstance: instance->vertices"); instance->mapping = (VertexMap *) malloc(sizeof(VertexMap) * v); if (instance->mapping == NULL) OutOfMemoryError("AllocateInstance: instance->mapping"); } if (e > 0) { instance->edges = (ULONG *) malloc(sizeof(ULONG) * e); if (instance->edges == NULL) OutOfMemoryError("AllocateInstance: instance->edges"); } instance->minMatchCost = MAX_DOUBLE; instance->refCount = 0; instance->parentInstance = NULL; return instance; }
ReferenceGraph *CopyReferenceGraph(ReferenceGraph *g) { ReferenceGraph *gCopy; ULONG nv; ULONG ne; ULONG v; ULONG e; ULONG numEdges; nv = g->numVertices; ne = g->numEdges; // allocate graph gCopy = AllocateReferenceGraph(nv, ne); // copy vertices; allocate and copy vertex edge arrays for (v = 0; v < nv; v++) { gCopy->vertices[v].label = g->vertices[v].label; gCopy->vertices[v].map = g->vertices[v].map; gCopy->vertices[v].used = g->vertices[v].used; gCopy->vertices[v].vertexValid = g->vertices[v].vertexValid; numEdges = g->vertices[v].numEdges; gCopy->vertices[v].numEdges = numEdges; gCopy->vertices[v].edges = NULL; if (numEdges > 0) { gCopy->vertices[v].edges = (ULONG *) malloc (numEdges * sizeof (ULONG)); if (gCopy->vertices[v].edges == NULL) OutOfMemoryError ("CopyGraph:edges"); for (e = 0; e < numEdges; e++) gCopy->vertices[v].edges[e] = g->vertices[v].edges[e]; } } // copy edges for (e = 0; e < ne; e++) { gCopy->edges[e].vertex1 = g->edges[e].vertex1; gCopy->edges[e].vertex2 = g->edges[e].vertex2; gCopy->edges[e].label = g->edges[e].label; gCopy->edges[e].directed = g->edges[e].directed; gCopy->edges[e].used = g->edges[e].used; gCopy->edges[e].map = g->edges[e].map; gCopy->edges[e].failed = g->edges[e].failed; } return gCopy; }
BOOL FASTCALL CmdSignatureDlg_SaveSig( HWND hwnd, int index ) { char sz[ _MAX_FNAME ]; HWND hwndList; register int c; HNDFILE fh; hwndList = GetDlgItem( hwnd, IDD_LIST ); ComboBox_GetLBText( hwndList, index, sz ); for( c = 0; sz[ c ]; ++c ) if( !ValidFileNameChr( sz[ c ] ) ) { wsprintf( lpTmpBuf, GS( IDS_STR136 ), sz[ c ] ); fMessageBox( hwnd, 0, lpTmpBuf, MB_OK|MB_ICONEXCLAMATION ); return( FALSE ); } lstrcat( sz, ".sig" ); if( ( fh = Ameol2_CreateFile( sz, DSD_SIG, 0 ) ) == HNDFILE_ERROR ) fMessageBox( hwnd, 0, GS( IDS_STR137 ), MB_OK|MB_ICONEXCLAMATION ); else { WORD wSize; LPSTR lpText; HWND hwndEdit; INITIALISE_PTR(lpText); hwndEdit = GetDlgItem( hwnd, IDD_EDIT ); wSize = Edit_GetTextLength( hwndEdit ) + 1; if( !fNewMemory( &lpText, wSize ) ) OutOfMemoryError( hwnd, FALSE, FALSE ); else { Edit_GetText( hwndEdit, lpText, wSize ); if( Amfile_Write( fh, lpText, wSize ) == wSize ) { FreeMemory( &lpText ); Amfile_Close( fh ); return( TRUE ); } FreeMemory( &lpText ); DiskWriteError( hwnd, sz, FALSE, FALSE ); } Amfile_Close( fh ); Amfile_Delete( sz ); } return( FALSE ); }
Substructure *AllocateSub() { Substructure *sub; sub = (Substructure *) malloc(sizeof(Substructure)); if (sub == NULL) OutOfMemoryError("substructure"); sub->definition = NULL; sub->numInstances = 0; sub->numExamples = 0; sub->instances = NULL; sub->numNegInstances = 0; sub->numNegExamples = 0; sub->negInstances = NULL; sub->value = -1.0; sub->recursive = FALSE; sub->recursiveEdgeLabel = 0; return sub; }
/** * Creates new GSL_vector with the content of the given pointer. * * @param other gsl_vector whose elements are copied */ void GSL_vector::assign(const gsl_vector* other) { if (!other) { gsl_vector_free(vec); vec = nullptr; return; } // avoid free and alloc if other has same size if (size() != other->size) { gsl_vector_free(vec); vec = gsl_vector_alloc(other->size); if (!vec) throw OutOfMemoryError( "Allocation of GSL_vector of size " + std::to_string(other->size) + " failed."); } gsl_vector_memcpy(vec, other); }
double Log2Factorial(ULONG number, Parameters *parameters) { ULONG i; ULONG newSize; if (number >= parameters->log2FactorialSize) { // add enough room to array to encompass desired value and then some newSize = number + LIST_SIZE_INC; parameters->log2Factorial = (double *) realloc(parameters->log2Factorial, newSize * sizeof(double)); if (parameters->log2Factorial == NULL) OutOfMemoryError("Log2Factorial"); // compute new values for (i = parameters->log2FactorialSize; i < newSize; i++) { parameters->log2Factorial[i] = Log2(i) + parameters->log2Factorial[i - 1]; } parameters->log2FactorialSize = newSize; } return parameters->log2Factorial[number]; }
void* SystemAlloc(size_t size) { void* ptr = malloc(size); return ptr ? ptr : OutOfMemoryError(size); }
InstanceList *GetRecursiveInstances(Graph *graph, InstanceList *instances, ULONG numInstances, ULONG recEdgeLabel) { Instance **instanceMap; InstanceList *recInstances; InstanceListNode *instanceListNode1; InstanceListNode *instanceListNode2; Instance *instance1; Instance *instance2; Vertex *vertex1; ULONG i, i1, i2; ULONG v1; ULONG v2Index; ULONG e; Edge *edge; // Allocate instance map, where instanceMap[i]=j implies the original // instance i is now part of possibly new instance j instanceMap = (Instance **) malloc(numInstances * sizeof(Instance *)); if (instanceMap == NULL) OutOfMemoryError("GetRecursiveInstances:instanceMap"); i = 0; instanceListNode1 = instances->head; while (instanceListNode1 != NULL) { instanceMap[i] = instanceListNode1->instance; instanceListNode1 = instanceListNode1->next; i++; } // search instance list for a connected pair i1 = 0; instanceListNode1 = instances->head; while (instanceListNode1 != NULL) { instance1 = instanceListNode1->instance; for (v1 = 0; v1 < instance1->numVertices; v1++) { vertex1 = & graph->vertices[instance1->vertices[v1]]; for (e = 0; e < vertex1->numEdges; e++) { edge = & graph->edges[vertex1->edges[e]]; if ((! edge->used) && (edge->label == recEdgeLabel)) { // search instance list for another instance involving edge v2Index = edge->vertex2; if (edge->vertex2 == instance1->vertices[v1]) v2Index = edge->vertex1; i2 = i1 + 1; instanceListNode2 = instanceListNode1->next; while (instanceListNode2 != NULL) { instance2 = instanceListNode2->instance; if (InstanceContainsVertex(instance2, v2Index)) { // found pair, so update instance map and new instances AddRecursiveInstancePair(i1, i2, instance1, instance2, vertex1->edges[e], edge, numInstances, instanceMap); } i2++; instanceListNode2 = instanceListNode2->next; } } } } i1++; instanceListNode1 = instanceListNode1->next; } recInstances = CollectRecursiveInstances(instanceMap, numInstances); free(instanceMap); return recInstances; }
ReferenceGraph *InstanceToRefGraph(Instance *instance, Graph *graph, Parameters *parameters) { ReferenceGraph *newGraph; Vertex *vertex; Edge *edge; ULONG i, j; ULONG v1, v2; BOOLEAN found1; BOOLEAN found2; ReferenceVertex *refVertex; v1 = 0; v2 = 0; newGraph = AllocateReferenceGraph(instance->numVertices, instance->numEdges); // convert vertices for (i = 0; i < instance->numVertices; i++) { vertex = & graph->vertices[instance->vertices[i]]; newGraph->vertices[i].label = vertex->label; newGraph->vertices[i].numEdges = 0; newGraph->vertices[i].edges = NULL; newGraph->vertices[i].used = FALSE; newGraph->vertices[i].map = instance->vertices[i]; newGraph->vertices[i].vertexValid = TRUE; } // convert edges for (i = 0; i < instance->numEdges; i++) { edge = & graph->edges[instance->edges[i]]; // find new indices for edge vertices j = 0; found1 = FALSE; found2 = FALSE; while ((! found1) || (! found2)) { if (instance->vertices[j] == edge->vertex1) { v1 = j; found1 = TRUE; } if (instance->vertices[j] == edge->vertex2) { v2 = j; found2 = TRUE; } j++; } // set new edge information newGraph->edges[i].vertex1 = v1; newGraph->edges[i].vertex2 = v2; newGraph->edges[i].map = instance->edges[i]; newGraph->edges[i].label = edge->label; newGraph->edges[i].directed = edge->directed; newGraph->edges[i].used = FALSE; newGraph->edges[i].failed = FALSE; // add edge to appropriate vertices refVertex = & newGraph->vertices[v1]; refVertex->vertexValid = TRUE; //this should be unnecessary refVertex->numEdges++; refVertex->edges = (ULONG *) realloc(refVertex->edges, sizeof (ULONG) * refVertex->numEdges); if (refVertex->edges == NULL) OutOfMemoryError ("InstanceToGraph:refVertex1->edges"); refVertex->edges[refVertex->numEdges - 1] = i; if (v1 != v2) { refVertex = &newGraph->vertices[v2]; refVertex->vertexValid = TRUE; // this should be unnecessary refVertex->numEdges++; refVertex->edges = (ULONG *) realloc(refVertex->edges, sizeof (ULONG) * refVertex->numEdges); if (refVertex->edges == NULL) OutOfMemoryError ("InstanceToGraph:refVertex2->edges"); refVertex->edges[refVertex->numEdges - 1] = i; } } // remap instance to refGraph for (i = 0; i < instance->numVertices; i++) instance->vertices[i] = i; for (i = 0; i < instance->numEdges; i++) instance->edges[i] = i; instance->newVertex = 0; instance->newEdge = 0; return newGraph; }