static void splitIntoAdjacenciesStubsAndChains(stList *subCycle, stList *adjacencyEdges, stList *stubEdges, stList *chainEdges, stList **subAdjacencyEdges, stList **subStubEdges, stList **subChainEdges) { /* * Splits run into cycles and chains.. */ *subStubEdges = stList_construct(); *subChainEdges = stList_construct(); for (int64_t j = 0; j < stList_length(subCycle); j++) { stIntTuple *edge = stList_get(subCycle, j); if (stList_contains(stubEdges, edge)) { stList_append(*subStubEdges, edge); } else if (stList_contains(chainEdges, edge)) { stList_append(*subChainEdges, edge); } } *subAdjacencyEdges = stList_construct(); stSortedSet *nodes = getNodeSetOfEdges(subCycle); for (int64_t j = 0; j < stList_length(adjacencyEdges); j++) { stIntTuple *edge = stList_get(adjacencyEdges, j); if (nodeInSet(nodes, stIntTuple_get(edge, 0)) && nodeInSet( nodes, stIntTuple_get(edge, 1))) { stList_append(*subAdjacencyEdges, edge); } } stSortedSet_destruct(nodes); }
void test_stList_removeItem(CuTest *testCase) { setup(); CuAssertTrue(testCase, stList_contains(list, strings[2])); stList_removeItem(list, strings[2]); CuAssertTrue(testCase, !stList_contains(list, strings[2])); stList_removeItem(list, strings[2]); CuAssertTrue(testCase, !stList_contains(list, strings[2])); teardown(); }
static void doBestMergeOfTwoSimpleCycles(stList *cycles, stList *nonZeroWeightAdjacencyEdges, stSortedSet *allAdjacencyEdges) { /* * Merge two simple cycles, using the best possible adjacency switch. Modifies components list in place, * destroying two old components and adding a new one. If new adjacency edges are needed then they are * added to the adjacency edges list. */ assert(stList_length(cycles) > 1); /* * Get the best adjacency switch. */ AdjacencySwitch *adjacencySwitch = getBestAdjacencySwitch(cycles, nonZeroWeightAdjacencyEdges, allAdjacencyEdges); assert(adjacencySwitch != NULL); /* * Find the two components to merge. */ stList *cyclesToMerge = stList_construct3(0, (void(*)(void *)) stList_destruct); for (int64_t i = 0; i < stList_length(cycles); i++) { stList *cycle = stList_get(cycles, i); if (stList_contains(cycle, adjacencySwitch->oldEdge1)) { assert(!stList_contains(cycle, adjacencySwitch->oldEdge2)); stList_append(cyclesToMerge, cycle); } else if (stList_contains(cycle, adjacencySwitch->oldEdge2)) { stList_append(cyclesToMerge, cycle); } } /* * Now construct the new component and modify the list of components in place. */ assert(stList_length(cyclesToMerge) == 2); stList *newComponent = stList_join(cyclesToMerge); assert(!stList_contains(newComponent, NULL)); //Cleanup the old components assert(stList_contains(cycles, stList_get(cyclesToMerge, 0))); stList_removeItem(cycles, stList_get(cyclesToMerge, 0)); assert(stList_contains(cycles, stList_get(cyclesToMerge, 1))); stList_removeItem(cycles, stList_get(cyclesToMerge, 1)); stList_destruct(cyclesToMerge); //Now remove the old edges and add the new ones assert(stList_contains(newComponent, adjacencySwitch->oldEdge1)); stList_removeItem(newComponent, adjacencySwitch->oldEdge1); assert(stList_contains(newComponent, adjacencySwitch->oldEdge2)); stList_removeItem(newComponent, adjacencySwitch->oldEdge2); assert(!stList_contains(newComponent, adjacencySwitch->newEdge1)); stList_append(newComponent, adjacencySwitch->newEdge1); assert(!stList_contains(newComponent, adjacencySwitch->newEdge2)); stList_append(newComponent, adjacencySwitch->newEdge2); adjacencySwitch_destruct(adjacencySwitch); //Clean the adjacency switch. //Finally add the component to the list of components stList_append(cycles, newComponent); }
void test_stList_contains(CuTest *testCase) { setup(); int64_t i; for(i=0; i<stringNumber; i++) { CuAssertTrue(testCase, stList_contains(list, strings[i])); } CuAssertTrue(testCase, !stList_contains(list, "something")); CuAssertTrue(testCase, !stList_contains(list, NULL)); teardown(); }
stList *mergeSimpleCycles(stList *cycles, stList *nonZeroWeightAdjacencyEdges, stSortedSet *allAdjacencyEdges) { /* * Takes a set of simple cycles (containing only the adjacency edges). * Returns a single simple cycle, as a list of edges, by doing length(components)-1 * calls to doBestMergeOfTwoSimpleCycles. */ /* * Build a hash of nodes to adjacency edges. */ cycles = stList_copy(cycles, NULL); for (int64_t i = 0; i < stList_length(cycles); i++) { //Clone the complete list assert(stList_length(stList_get(cycles, i)) > 0); assert(!stList_contains(stList_get(cycles, i), NULL)); stList_set(cycles, i, stList_copy(stList_get(cycles, i), NULL)); } while (stList_length(cycles) > 1) { doBestMergeOfTwoSimpleCycles(cycles, nonZeroWeightAdjacencyEdges, allAdjacencyEdges); } assert(stList_length(cycles) == 1); stList *mergedComponent = stList_get(cycles, 0); stList_destruct(cycles); return mergedComponent; }
/* * Recursive function which fills a givenlist with the * connected nodes within a module */ static void buildFaces_fillTopNodeList(Cap * cap, stList *list, stHash *liftedEdgesTable) { stList *liftedEdges; int64_t index; // Limit of recursion if (stList_contains(list, cap)) return; // Actual filling st_logInfo("Adding cap %p to face\n", cap); stList_append(list, cap); // Recursion through lifted edges if ((liftedEdges = stHash_search(liftedEdgesTable, cap))) for (index = 0; index < stList_length(liftedEdges); index++) buildFaces_fillTopNodeList( ((LiftedEdge *) stList_get(liftedEdges, index))->destination, list, liftedEdgesTable); // Recursion through adjacency if (cap_getAdjacency(cap)) buildFaces_fillTopNodeList(cap_getAdjacency(cap),list, liftedEdgesTable); }
/* * Constructs a face from a given Cap */ static void buildFaces_constructFromCap(Cap * startingCap, stHash *liftedEdgesTable, Flower * flower) { Face *face = face_construct(flower); stList *topNodes = stList_construct3(16, NULL); stList *liftedEdges; Cap *cap, *bottomNode, *ancestor; int64_t index, index2; printf("Constructing new face"); // Establishlist of top nodes buildFaces_fillTopNodeList(startingCap, topNodes, liftedEdgesTable); #ifndef NDEBUG // What, no top nodes!? if (stList_length(topNodes) == 0) abort(); #endif // Initialize data structure face_allocateSpace(face, stList_length(topNodes)); // For every top node for (index = 0; index < stList_length(topNodes); index++) { cap = stList_get(topNodes, index); face_setTopNode(face, index, cap); liftedEdges = stHash_search(liftedEdgesTable, cap); if (!liftedEdges) { face_setBottomNodeNumber(face, index, 0); continue; } face_setBottomNodeNumber(face, index, stList_length(liftedEdges)); // For every bottom node of that top node for (index2 = 0; index2 < stList_length(liftedEdges); index2++) { bottomNode = ((LiftedEdge *) stList_get(liftedEdges, index2))->bottomNode; face_addBottomNode(face, index, bottomNode); ancestor = cap_getTopCap(cap_getPositiveOrientation( cap_getAdjacency(bottomNode))); if (cap_getAdjacency(cap) != ancestor) face_setDerivedDestination(face, index, index2, ancestor); else face_setDerivedDestination(face, index, index2, NULL); #ifndef NDEBUG // If bottom nodes part of top nodes assert(!stList_contains(topNodes, cap_getPositiveOrientation( ((LiftedEdge*) stList_get(liftedEdges, index2))->bottomNode))); #endif } } // Clean up stList_destruct(topNodes); }
static void test_stSet_testGetKeys(CuTest *testCase) { testSetup(); stList *list = stSet_getKeys(set1); CuAssertTrue(testCase, stList_length(list) == 6); CuAssertTrue(testCase, stList_contains(list, one)); CuAssertTrue(testCase, stList_contains(list, two)); CuAssertTrue(testCase, stList_contains(list, three)); CuAssertTrue(testCase, stList_contains(list, four)); CuAssertTrue(testCase, stList_contains(list, five)); CuAssertTrue(testCase, stList_contains(list, six)); stList_destruct(list); testTeardown(); }
static void test_st_randomChoice(CuTest *testCase) { /* * Excercies the random int function. */ stList *list = stList_construct3(0, (void (*)(void *))stIntTuple_destruct); stTry { st_randomChoice(list); } stCatch(except) { CuAssertTrue(testCase, stExcept_getId(except) == RANDOM_EXCEPTION_ID); } stTryEnd for(int32_t i = 0; i < 10; i++) { stList_append(list, stIntTuple_construct(1, i)); } for(int32_t i = 0; i < 100; i++) { CuAssertTrue(testCase, stList_contains(list, st_randomChoice(list))); } stList_destruct(list); }
/* * Recursive function which fills a givenlist with the * connected nodes within a module and fills their lifted * edges in the same pass */ static void buildFaces_fillTopNodeList2(Cap * cap, stList *list, stHash *liftedEdgesTable) { stList *liftedEdges = stList_construct3(2, buildFaces_stList_destructElem); int64_t index; // Orientation check cap = cap_getPositiveOrientation(cap); // Limit of recursion if (stList_contains(list, cap)) return; // Actual filling st_logInfo("Adding cap %p to face\n", cap); stList_append(list, cap); // Compute lifted edges for (index = 0; index < cap_getChildNumber(cap); index++) buildFaces_computeLiftedEdgesAtTopNode(cap_getChild(cap, index), liftedEdges); // If emptylist... if (stList_length(liftedEdges) == 0) stList_destruct(liftedEdges); // Recursion through lifted edges else { stHash_insert(liftedEdgesTable, cap, liftedEdges); for (index = 0; index < stList_length(liftedEdges); index++) buildFaces_fillTopNodeList2( ((LiftedEdge *) stList_get(liftedEdges, index))->destination, list, liftedEdgesTable); } // Recursion through adjacency if (cap_getAdjacency(cap)) buildFaces_fillTopNodeList2(cap_getAdjacency(cap),list, liftedEdgesTable); }