void main() { SLList<int> obj; obj.addToHead(1); obj.addToHead(6); obj.addToHead(2); obj.addToHead(4); obj.addToHead(3); obj.addToHead(0); obj.addToHead(7); obj.addToHead(5); obj.printAll(); cout<<endl; // sortiranje// cout<<"After sorting"<<endl; obj.insertionSort(); obj.printAll(); cout<<endl; // Adding to tail // obj.addToTail(1234); obj.printAll(); }
void Foam::faPatch::calcPointLabels() const { SLList<label> labels; UList<edge> edges = patchSlice(boundaryMesh().mesh().edges()); forAll(edges, edgeI) { bool existStart = false; bool existEnd = false; for ( SLList<label>::iterator iter = labels.begin(); iter != labels.end(); ++iter ) { if(*iter == edges[edgeI].start()) { existStart = true; } if(*iter == edges[edgeI].end()) { existEnd = true; } } if(!existStart) { labels.append(edges[edgeI].start()); } if(!existEnd) { labels.append(edges[edgeI].end()); } }
int main(int argc, char* argv[]){ SLList* pSLList = new SLList(); pSLList->addToHead("tom", 1); pSLList->addToTail("jerry", 2); pSLList->addToTail("thomas", 3); int* pa = pSLList->deleteFromHead(); printf("the deleted head value should be tom's, %d\n", *pa); pa = pSLList->deleteFromTail(); printf("the deleted tail value should be thomas', %d\n", *pa); pSLList->deleteNode("jerry", 2); if(pSLList->isEmpty()){ printf("yes, it is empty now\n"); } delete pSLList; pSLList=0; return 0; }
int main() { SLList<int> tester; tester.add(1); tester.add(2); tester.add(3); tester.add(4); tester.PrintList(); cout << tester.secondLast() << endl; cout << tester.get(4) << endl; cout << tester.set(4, 10) << endl; tester.PrintList(); cout << tester.get(1) << endl; cout << tester.set(3, 7) << endl; tester.PrintList(); cout << tester.get(3) << endl; cout << tester.secondLast() << endl; cout << tester.add(20, 12) << endl; tester.PrintList(); cout << tester.remove(3) << endl; tester.PrintList(); DLList<int> tester2; tester2.add(1); tester2.add(2); tester2.add(3); tester2.add(4); tester2.add(5); tester2.add(6); tester2.add(7); tester2.add(8); tester2.PrintList(); tester2.reverse(); tester2.PrintList(); tester2.Truncate(7); tester2.PrintList(); tester2.Replace(4, 6); tester2.PrintList(); return 0; }
// For testing (DO NOT ALTER) void UnitTest() { cout << string(40, '-') << endl; cout << "UNIT TEST:\n" << string(40, '-') << endl; if (num_of_tests != 0) cout << "Total Number of Tests: " << num_of_tests << endl; string yours = "", actual = ""; // Tests SLList list; std::stringstream full_head_list, half_head_list, full_tail_list, half_tail_list; for (int i = 999; i > 0; i--) { full_head_list << i << ", "; if (i < 500) half_head_list << i << ", "; } full_head_list << 0; half_head_list << 0; for (int i = 0; i < 999; i++) { full_tail_list << i << ", "; if (i < 499) half_tail_list << i << ", "; } full_tail_list << 999; half_tail_list << 499; Test(list.size() == 0, __LINE__, "Default Constructor & size()"); yours = list.ToString(); actual = ""; Test(yours == actual, __LINE__, "ToString()", yours, actual); Test(list.GetHead() == 0, __LINE__, "GetHead()"); Test(list.GetTail() == 0, __LINE__, "GetTail()"); list.RemoveHead(); Test(list.size() == 0, __LINE__, "RemoveHead() & size()"); list.RemoveTail(); Test(list.size() == 0, __LINE__, "RemoveTail() & size()"); list.InsertHead(1); Test(list.size() == 1, __LINE__, "InsertHead(1) & size()"); yours = list.ToString(); actual = "1"; Test(yours == actual, __LINE__, "ToString()", yours, actual); list.RemoveHead(); Test(list.size() == 0, __LINE__, "RemoveHead() & size()"); yours = list.ToString(); actual = ""; Test(yours == actual, __LINE__, "ToString()", yours, actual); list.InsertTail(5); Test(list.size() == 1, __LINE__, "InsertTail(5) & size()"); yours = list.ToString(); actual = "5"; Test(yours == actual, __LINE__, "ToString()", yours, actual); list.RemoveTail(); Test(list.size() == 0, __LINE__, "RemoveTail() & size()"); yours = list.ToString(); actual = ""; Test(yours == actual, __LINE__, "ToString()", yours, actual); list.InsertHead(10); list.InsertHead(20); Test(list.size() == 2, __LINE__, "InsertHead(10), InsertHead(20) & size()"); yours = list.ToString(); actual = "20, 10"; Test(yours == actual, __LINE__, "ToString()", yours, actual); list.RemoveHead(); Test(list.size() == 1, __LINE__, "RemoveHead() & size()"); yours = list.ToString(); actual = "10"; Test(yours == actual, __LINE__, "ToString()", yours, actual); list.InsertHead(20); list.RemoveTail(); Test(list.size() == 1, __LINE__, "InsertHead(20), RemoveTail() & size()"); yours = list.ToString(); actual = "20"; Test(yours == actual, __LINE__, "ToString()", yours, actual); list.InsertHead(5); Test(list.size() == 2, __LINE__, "InsertHead(5) & size()"); yours = list.ToString(); actual = "5, 20"; Test(yours == actual, __LINE__, "ToString()", yours, actual); list.Clear(); Test(list.size() == 0, __LINE__, "Clear() & size()"); yours = list.ToString(); actual = ""; Test(yours == actual, __LINE__, "ToString()", yours, actual); list.InsertHead(10); list.InsertHead(5); list.InsertTail(20); list.InsertTail(25); Test( list.size() == 4, __LINE__, "InsertHead(10), InsertHead(5), InsertTail(20), InsertTail(25) & size()"); yours = list.ToString(); actual = "5, 10, 20, 25"; Test(yours == actual, __LINE__, "ToString()", yours, actual); Test(list.GetHead() == 5, __LINE__, "GetHead()"); Test(list.GetTail() == 25, __LINE__, "GetTail()"); Test(list.size() == 4, __LINE__, "size()"); yours = list.ToString(); actual = "5, 10, 20, 25"; Test(yours == actual, __LINE__, "ToString()", yours, actual); list.RemoveHead(); list.RemoveTail(); list.RemoveHead(); list.RemoveTail(); Test(list.size() == 0, __LINE__, "RemoveHead(), RemoveTail(), RemoveHead(), RemoveTail() & size()"); yours = list.ToString(); actual = ""; Test(yours == actual, __LINE__, "ToString()", yours, actual); for (unsigned int i = 0; i < 1000; i++) list.InsertHead(i); Test(list.size() == 1000, __LINE__, "InsertHead() \"HIGH LOAD\" & size()"); yours = list.ToString(); actual = full_head_list.str(); Test(yours == actual, __LINE__, "ToString()", yours, actual); for (unsigned int i = 0; i < 500; i++) list.RemoveHead(); Test(list.size() == 500, __LINE__, "RemoveHead() \"HIGH LOAD / 2\" & size()"); yours = list.ToString(); actual = half_head_list.str(); Test(yours == actual, __LINE__, "ToString()", yours, actual); for (unsigned int i = 0; i < 600; i++) list.RemoveHead(); Test(list.size() == 0, __LINE__, "RemoveHead() \"HIGH LOAD / 2\" & size()"); yours = list.ToString(); actual = ""; Test(yours == actual, __LINE__, "ToString()", yours, actual); for (unsigned int i = 0; i < 1000; i++) list.InsertTail(i); Test(list.size() == 1000, __LINE__, "InsertTail() \"HIGH LOAD\" & size()"); yours = list.ToString(); actual = full_tail_list.str(); Test(yours == actual, __LINE__, "ToString()", yours, actual); for (unsigned int i = 0; i < 500; i++) list.RemoveTail(); Test(list.size() == 500, __LINE__, "RemoveTail() \"HIGH LOAD / 2\" & size()"); yours = list.ToString(); actual = half_tail_list.str(); Test(yours == actual, __LINE__, "ToString()", yours, actual); for (unsigned int i = 0; i < 600; i++) list.RemoveTail(); Test(list.size() == 0, __LINE__, "RemoveTail() \"HIGH LOAD / 2\" & size()"); yours = list.ToString(); actual = ""; Test(yours == actual, __LINE__, "ToString()", yours, actual); cout << string(40, '-') << endl; cout << "Passed: " << ut_passed << " / " << ut_total << endl; OutputFailedTests(); cout << string(40, '-') << endl; cout << "END OF UNIT TEST!\n"; cout << string(40, '-') << endl; cout << "Be sure to run 'make style' to check for any style errors.\n" << "Please note that 'make style' does NOT check variable names or" << " indentation" << endl << endl; }
// Constructor from components Foam::labelList Foam::bandCompression(const labelListList& cellCellAddressing) { labelList newOrder(cellCellAddressing.size()); // the business bit of the renumbering SLList<label> nextCell; labelList visited(cellCellAddressing.size()); label currentCell; label cellInOrder = 0; // reset the visited cells list forAll (visited, cellI) { visited[cellI] = 0; } // loop over the cells forAll (visited, cellI) { // find the first cell that has not been visited yet if (visited[cellI] == 0) { currentCell = cellI; // use this cell as a start nextCell.append(currentCell); // loop through the nextCell list. Add the first cell into the // cell order if it has not already been visited and ask for its // neighbours. If the neighbour in question has not been visited, // add it to the end of the nextCell list while (nextCell.size()) { currentCell = nextCell.removeHead(); if (visited[currentCell] == 0) { visited[currentCell] = 1; // add into cellOrder newOrder[cellInOrder] = currentCell; cellInOrder++; // find if the neighbours have been visited const labelList& neighbours = cellCellAddressing[currentCell]; forAll (neighbours, nI) { if (visited[neighbours[nI]] == 0) { // not visited, add to the list nextCell.append(neighbours[nI]); } } } } }
// Find a good edge forAll(singleEdges, edgeI) { SLList<label> pointChain; bool blockHead = false; bool blockTail = false; if (!singleEdgeUsage[edgeI]) { // found a new edge singleEdgeUsage[edgeI] = true; label newEdgeStart = singleEdges[edgeI].start(); label newEdgeEnd = singleEdges[edgeI].end(); pointChain.insert(newEdgeStart); pointChain.append(newEdgeEnd); # ifdef DEBUG_CHAIN Info<< "found edge to start with: " << singleEdges[edgeI] << endl; # endif // Check if head or tail are blocked forAll(cellPoints, pointI) { if (cellPoints[pointI] == newEdgeStart) { if (pointUsage[pointI] > 2) { # ifdef DEBUG_CHAIN Info<< "start head blocked" << endl; # endif blockHead = true; } } else if (cellPoints[pointI] == newEdgeEnd) { if (pointUsage[pointI] > 2) { # ifdef DEBUG_CHAIN Info<< "start tail blocked" << endl; # endif blockTail = true; } } } bool stopSearching = false; // Go through the unused edges and try to chain them up do { stopSearching = false; forAll(singleEdges, addEdgeI) { if (!singleEdgeUsage[addEdgeI]) { // Grab start and end of the candidate label addStart = singleEdges[addEdgeI].start(); label addEnd = singleEdges[addEdgeI].end(); # ifdef DEBUG_CHAIN Info<< "Trying candidate " << singleEdges[addEdgeI] << endl; # endif // Try to add the edge onto the head if (!blockHead) { if (pointChain.first() == addStart) { // Added at start mark as used pointChain.insert(addEnd); singleEdgeUsage[addEdgeI] = true; } else if (pointChain.first() == addEnd) { pointChain.insert(addStart); singleEdgeUsage[addEdgeI] = true; } } // Try the other end only if the first end // did not add it if (!blockTail && !singleEdgeUsage[addEdgeI]) { if (pointChain.last() == addStart) { // Added at start mark as used pointChain.append(addEnd); singleEdgeUsage[addEdgeI] = true; } else if (pointChain.last() == addEnd) { pointChain.append(addStart); singleEdgeUsage[addEdgeI] = true; } } // check if the new head or tail are blocked label curEdgeStart = pointChain.first(); label curEdgeEnd = pointChain.last(); # ifdef DEBUG_CHAIN Info<< "curEdgeStart: " << curEdgeStart << " curEdgeEnd: " << curEdgeEnd << endl; # endif forAll(cellPoints, pointI) { if (cellPoints[pointI] == curEdgeStart) { if (pointUsage[pointI] > 2) { # ifdef DEBUG_CHAIN Info<< "head blocked" << endl; # endif blockHead = true; } } else if (cellPoints[pointI] == curEdgeEnd) { if (pointUsage[pointI] > 2) { # ifdef DEBUG_CHAIN Info<< "tail blocked" << endl; # endif blockTail = true; } } } // Check if the loop is closed if (curEdgeStart == curEdgeEnd) { # ifdef DEBUG_CHAIN Info<< "closed loop" << endl; # endif pointChain.removeHead(); blockHead = true; blockTail = true; stopSearching = true; } # ifdef DEBUG_CHAIN Info<< "current pointChain: " << pointChain << endl; # endif if (stopSearching) break; } } } while (stopSearching); }
void Foam::immersedBoundaryFvPatch::makeTriAddressing() const { if (debug) { InfoIn("void immersedBoundaryFvPatch::makeTriAddressing() const") << "creating tri addressing for immersed boundary " << name() << endl; } // It is an error to attempt to recalculate // if the pointer is already set if (cellsToTriAddrPtr_ || cellsToTriWeightsPtr_) { FatalErrorIn("immersedBoundaryFvPatch::makeTriAddressing() const") << "tri addressing already exist" << "for immersed boundary" << name() << abort(FatalError); } // Get reference to tri patch and hit faces const triSurface& triPatch = ibPolyPatch_.ibMesh(); const vectorField& triCentres = triPatch.faceCentres(); const labelList& hf = hitFaces(); const vectorField& ibp = ibPoints(); // Create a markup field and mark all tris containing an ib point with its // index labelList hitTris(triPatch.size(), -1); forAll (hf, hfI) { hitTris[hf[hfI]] = hfI; } // Allocate storage cellsToTriAddrPtr_ = new labelListList(triPatch.size()); labelListList& addr = *cellsToTriAddrPtr_; cellsToTriWeightsPtr_ = new scalarListList(triPatch.size()); scalarListList& w = *cellsToTriWeightsPtr_; // Algorithm: // For each tri face, check if it contains an IB point // - if so, set the addressing to the index of IB point and weight to 1 // - if not, search the neighbouring faces of the visited faces until // at least 3 IB points are found, or the neighbourhood is exhausted. // When a sufficient number of points is found, calculate the weights // using inverse distance weighting // Get addressing from the triangular patch const labelListList& pf = triPatch.pointFaces(); forAll (triPatch, triI) { if (hitTris[triI] > -1) { // Triangle contains IB point addr[triI].setSize(1); w[triI].setSize(1); addr[triI] = hitTris[triI]; w[triI] = 1; } else { // No direct hit. Start a neighbourhood search // Record already visited faces labelHashSet visited; // Collect new faces to visit SLList<label> nextToVisit; // Collect IB points for interpolation labelHashSet ibPointsToUse; // Initialise with the original tri nextToVisit.insert(triI); do { const label curTri = nextToVisit.removeHead(); // Discard tri if already visited if (visited[curTri]) continue; visited.insert(curTri); const triFace& curTriPoints = triPatch[curTri]; // For all current points of face, pick up neighbouring faces forAll (curTriPoints, tpI) { const labelList curNbrs = pf[curTriPoints[tpI]]; forAll (curNbrs, nbrI) { if (!visited.found(curNbrs[nbrI])) { // Found a face which is not visited. Add it to // the list of faces to visit nextToVisit.append(curNbrs[nbrI]); if (hitTris[curNbrs[nbrI]] > -1) { // Found a neighbour with a hit: use this // IB point ibPointsToUse.insert(hitTris[curNbrs[nbrI]]); } } } } } while ( ibPointsToUse.size() < 3 && !nextToVisit.empty() ); // Found neighbourhood: collect addressing and weights addr[triI] = ibPointsToUse.toc(); w[triI].setSize(addr[triI].size()); labelList& curAddr = addr[triI]; scalarList& curW = w[triI]; vector curTriCentre = triCentres[triI]; scalar sumW = 0; forAll (curAddr, ibI) { curW[ibI] = 1/mag(curTriCentre - ibp[curAddr[ibI]]); sumW += curW[ibI]; } // Divide weights by sum distance forAll (curW, ibI) { curW[ibI] /= sumW; } } }