diff --git a/alib2algo_experimental/src/graph/datastructs/Array.h b/alib2algo_experimental/src/graph/datastructs/Array.h index 277f0873fd897ea4cf9ea4bb689dc8c3f0c24c20..eb0bd04355f682bc5e9806b1fcc6dcddf5676913 100644 --- a/alib2algo_experimental/src/graph/datastructs/Array.h +++ b/alib2algo_experimental/src/graph/datastructs/Array.h @@ -5,36 +5,34 @@ * Author: Jan Broz */ -typedef unsigned int uint; - // std::vector is quite not what i'm looking for template< typename elem_t > class Array { public: - Array( uint len ) { _Carray = new elem_t [ len ]; _length = len; } + Array( size_t len ) { _Carray = new elem_t [ len ]; _length = len; } ~Array() { delete [] _Carray; } - inline elem_t & operator[](uint index) { return _Carray[ index ]; } - inline const elem_t & operator[](uint index) const { return _Carray[ index ]; } + inline elem_t & operator[](size_t index) { return _Carray[ index ]; } + inline const elem_t & operator[](size_t index) const { return _Carray[ index ]; } - void resize(uint len) + void resize(size_t len) { elem_t * newArr = new elem_t [ len ]; - for (uint i = 0; i < _length && i < len; i++) + for (unsigned i = 0; i < _length && i < len; i++) newArr[i] = std::move( _Carray[i] ); delete [] _Carray; _Carray = newArr; _length = len; } - inline uint length() const { return _length; } + inline size_t length() const { return _length; } inline const elem_t * Carray() const { return _Carray; } protected: elem_t * _Carray; - uint _length; + size_t _length; }; diff --git a/alib2algo_experimental/src/graph/datastructs/FibonacciHeap.h b/alib2algo_experimental/src/graph/datastructs/FibonacciHeap.h index f7d7dc065e7556bf5aa10cef6b156aa36141aa2a..02b6d50472fc21ba2cb43ca0371f1e4f0813adea 100644 --- a/alib2algo_experimental/src/graph/datastructs/FibonacciHeap.h +++ b/alib2algo_experimental/src/graph/datastructs/FibonacciHeap.h @@ -29,24 +29,24 @@ class FibonacciHeap { /// merges this heap with another heap (!! this is a DESTRUCTIVE merge, heap in argument will be cleared !!) void mergeWith( FibonacciHeap<elem_t> && that ); - uint size() const { return _size; } + size_t size() const { return _size; } protected: struct Node { elem_t value; - uint degree; + unsigned degree; bool mark; Node * parent; Node * child; Node * prev; Node * next; - Node(const elem_t & val, uint deg = 0, Node * par = NULL, Node * chld = NULL, Node * pr = NULL, Node * ne = NULL) + Node(const elem_t & val, unsigned deg = 0, Node * par = NULL, Node * chld = NULL, Node * pr = NULL, Node * ne = NULL) : value(val), degree(deg), mark(false), parent(par), child(chld), prev(pr), next(ne) {} }; Node * _max; ///< pointer to cyclic doubly linked list of trees - uint _size; ///< count of elements stored in the heap + size_t _size; ///< count of elements stored in the heap int (* _compare)( const elem_t &, const elem_t & ); ///< user-defined comparator function protected: @@ -184,9 +184,9 @@ typename FibonacciHeap<elem_t>::Node * FibonacciHeap<elem_t>::mergeHeaps( Node * template< typename elem_t > void FibonacciHeap<elem_t>::consolidate() { - uint maxDegree; + unsigned maxDegree; Node * * rootsByDegree; - uint i, degree; + unsigned i, degree; Node * actual, * other; maxDegree = log2( _size ); diff --git a/alib2algo_experimental/src/graph/spanningtree/Edmonds.cpp b/alib2algo_experimental/src/graph/spanningtree/Edmonds.cpp index e394ea26ad34601b017be618afb37f6c6394b3ff..1172cbfc1786bc4bf50411f0d7896c9e82434c84 100644 --- a/alib2algo_experimental/src/graph/spanningtree/Edmonds.cpp +++ b/alib2algo_experimental/src/graph/spanningtree/Edmonds.cpp @@ -31,15 +31,15 @@ namespace spanningtree { /// wrapper for directed edge with additional info struct Edge { DirectedEdge e; - uint from; - uint to; + unsigned from; + unsigned to; int weight; int origWeight; Edge * fParent; std::vector<Edge*> fChildren; bool deleted; - Edge( const DirectedEdge & edge, uint f, uint t, int w ) + Edge( const DirectedEdge & edge, unsigned f, unsigned t, int w ) : e(edge), from(f), to(t), weight(w), origWeight(w), fParent(NULL), deleted(false) {} }; @@ -90,18 +90,18 @@ static AdjacencyListDirectedGraph edmonds_impl( const DirectedGraph & graph ) { AdjacencyListDirectedGraph spanningTree; // spanning tree - uint nodeCnt = graph.nodeCount(); - uint edgeCnt = graph.edgeCount(); + unsigned nodeCnt = graph.nodeCount(); + unsigned edgeCnt = graph.edgeCount(); std::list<Edge> edges; // just to ease deallocation - std::queue<uint> roots; ///< silne komponenty, ktere jsou koreny v aktualnim grafu (nevede do nich zadny uzel) + std::queue<unsigned> roots; ///< silne komponenty, ktere jsou koreny v aktualnim grafu (nevede do nich zadny uzel) Array<EdgeQueue> queues( nodeCnt ); ///< vstupni hrany do danych uzlu (razene dle priority) Array<Edge*> enter( nodeCnt ); ///< vstupni hrany do silnych komponent Array<std::vector<Edge*>> h( nodeCnt ); ///< hrany, ktere mohou byt v kostre - std::vector<uint> rset; ///< rooti vyslednych stromu Tarjanovy implementace + std::vector<unsigned> rset; ///< rooti vyslednych stromu Tarjanovy implementace Components2 strong( nodeCnt ); ///< silne komponenty (reprezentanti) Components2 weak( nodeCnt ); ///< slabe komponenty (reprezentanti) - Array<uint> min( nodeCnt ); ///< urcuje finalni korenove uzly orientovanych koster + Array<unsigned> min( nodeCnt ); ///< urcuje finalni korenove uzly orientovanych koster std::vector<Edge*> fRoots; ///< koreny lesa F Array<std::vector<Edge*>> cycles( nodeCnt ); ///< nalezene cykly Array<Edge*> lambda( nodeCnt ); ///< listy lesa F @@ -109,9 +109,9 @@ static AdjacencyListDirectedGraph edmonds_impl( const DirectedGraph & graph ) // this algorithm works on number identification of nodes and edges std::vector<Node> id2node( nodeCnt ); std::vector<DirectedEdge> id2edge( edgeCnt ); - std::unordered_map<Node,uint> node2id; - std::unordered_map<DirectedEdge,uint> edge2id; - uint id = 0; + std::unordered_map<Node,unsigned> node2id; + std::unordered_map<DirectedEdge,unsigned> edge2id; + unsigned id = 0; for (const Node & node : graph.getNodes()) { id2node[ id ] = node; node2id[ node ] = id; @@ -125,7 +125,7 @@ static AdjacencyListDirectedGraph edmonds_impl( const DirectedGraph & graph ) } */ // fill up data structures - for (uint nodeID = 0; nodeID < nodeCnt; nodeID++) { + for (unsigned nodeID = 0; nodeID < nodeCnt; nodeID++) { roots.push( nodeID ); enter[ nodeID ] = NULL; min[ nodeID ] = nodeID; @@ -145,7 +145,7 @@ static AdjacencyListDirectedGraph edmonds_impl( const DirectedGraph & graph ) } while (roots.size() != 0) { - uint root = roots.front(); roots.pop(); + unsigned root = roots.front(); roots.pop(); Edge * edge = queues[ root ].extractMax(); if (edge != NULL) { if (cycles[ strong.Find( root ) ].size() <= 1) { @@ -201,11 +201,11 @@ static AdjacencyListDirectedGraph edmonds_impl( const DirectedGraph & graph ) std::vector<Edge*> b; - std::vector<uint> r; - uint fRootIndex = 0; + std::vector<unsigned> r; + unsigned fRootIndex = 0; while (!rset.empty() || fRootIndex < fRoots.size()) { - uint v = 0; + unsigned v = 0; if (!rset.empty()) { v = min[ rset[0] ]; rset.erase( rset.begin() ); r.push_back(v); diff --git a/alib2algo_experimental/test-src/graph/TestUtils.h b/alib2algo_experimental/test-src/graph/TestUtils.h index 97da47f94dcd6be94e07921571b0ad0c837bdcab..ad56d57698b33ae9dc1525098b2c278656b4a2d4 100644 --- a/alib2algo_experimental/test-src/graph/TestUtils.h +++ b/alib2algo_experimental/test-src/graph/TestUtils.h @@ -11,8 +11,6 @@ #include <graph/GraphClasses.h> -typedef unsigned int uint; - template< typename Type > inline std::string toString( const Type & val ) { diff --git a/alib2algo_experimental/test-src/graph/maximumflow/FordFulkersonTest.cpp b/alib2algo_experimental/test-src/graph/maximumflow/FordFulkersonTest.cpp index a39cb15032097131a6f7be482c1ab4143725ecc1..bee86ed858a9ba5636013f3a5a48d99ff3292fd9 100644 --- a/alib2algo_experimental/test-src/graph/maximumflow/FordFulkersonTest.cpp +++ b/alib2algo_experimental/test-src/graph/maximumflow/FordFulkersonTest.cpp @@ -10,8 +10,8 @@ CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( GraphFordFulkersonTest, "graph" ); CPPUNIT_TEST_SUITE_REGISTRATION( GraphFordFulkersonTest ); -#define NetworkDef( edgeCount ) const struct { uint nodeCnt; uint edgeCnt; uint source; uint sink; struct { uint node1; uint node2; uint capacity; } edges [ edgeCount ]; } -#define FlowDef( edgeCount ) const struct { uint edgeCnt; struct { uint node1; uint node2; int flow; } edges [ edgeCount ]; } +#define NetworkDef( edgeCount ) const struct { unsigned nodeCnt; unsigned edgeCnt; unsigned source; unsigned sink; struct { unsigned node1; unsigned node2; unsigned capacity; } edges [ edgeCount ]; } +#define FlowDef( edgeCount ) const struct { unsigned edgeCnt; struct { unsigned node1; unsigned node2; int flow; } edges [ edgeCount ]; } NetworkDef(1) network1 = { 2, 1, @@ -146,12 +146,12 @@ FlowDef(10) undirflow4 = { template< typename Type1, typename Type2 > -void testDirNetwork( uint netID, const Type1 * netDef, const Type2 * flowDef ) +void testDirNetwork( unsigned netID, const Type1 * netDef, const Type2 * flowDef ) { graph::AdjacencyListDirectedGraph graph; std::vector<graph::Node> id2node( netDef->nodeCnt ); graph::Node source, sink; - uint i, j; + unsigned i, j; for (i = 0; i < netDef->nodeCnt; i++) { graph::Node node( std::string("n")+toString(i) ); @@ -171,7 +171,7 @@ void testDirNetwork( uint netID, const Type1 * netDef, const Type2 * flowDef ) graph::maximumflow::Flow flow = graph::maximumflow::FordFulkerson::fordfulkerson( graph, source, sink ); - for (uint id = 0; id < flowDef->edgeCnt; id++) { + for (unsigned id = 0; id < flowDef->edgeCnt; id++) { i = flowDef->edges[id].node1; j = flowDef->edges[id].node2; CPPUNIT_ASSERT_EQUAL( flowDef->edges[id].flow, flow[ id2node[i] ][ id2node[j] ] ); @@ -179,12 +179,12 @@ void testDirNetwork( uint netID, const Type1 * netDef, const Type2 * flowDef ) } template< typename Type1, typename Type2 > -void testUndirNetwork( uint netID, const Type1 * netDef, const Type2 * flowDef ) +void testUndirNetwork( unsigned netID, const Type1 * netDef, const Type2 * flowDef ) { graph::AdjacencyListUndirectedGraph graph; std::vector<graph::Node> id2node( netDef->nodeCnt ); graph::Node source, sink; - uint i, j; + unsigned i, j; for (i = 0; i < netDef->nodeCnt; i++) { graph::Node node( std::string("n")+toString(i) ); @@ -204,7 +204,7 @@ void testUndirNetwork( uint netID, const Type1 * netDef, const Type2 * flowDef ) graph::maximumflow::Flow flow = graph::maximumflow::FordFulkerson::fordfulkerson( graph, source, sink ); - for (uint id = 0; id < flowDef->edgeCnt; id++) { + for (unsigned id = 0; id < flowDef->edgeCnt; id++) { i = flowDef->edges[id].node1; j = flowDef->edges[id].node2; CPPUNIT_ASSERT_EQUAL( flowDef->edges[id].flow, flow[ id2node[i] ][ id2node[j] ] ); diff --git a/alib2algo_experimental/test-src/graph/minimumcut/FordFulkersonTest.cpp b/alib2algo_experimental/test-src/graph/minimumcut/FordFulkersonTest.cpp index ffb10823a3081220a782dac928ad3652d86c7faf..68f1f4164a32b335c72201053f0fbfe4031f9ab6 100644 --- a/alib2algo_experimental/test-src/graph/minimumcut/FordFulkersonTest.cpp +++ b/alib2algo_experimental/test-src/graph/minimumcut/FordFulkersonTest.cpp @@ -12,10 +12,10 @@ CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( GraphFordFulkersonCutTest, "graph" ); CPPUNIT_TEST_SUITE_REGISTRATION( GraphFordFulkersonCutTest ); -#define NetworkDef( edgeCount ) const struct { uint nodeCnt; uint edgeCnt; uint source; uint sink; struct { uint node1; uint node2; uint capacity; } edges [ edgeCount ]; } +#define NetworkDef( edgeCount ) const struct { unsigned nodeCnt; unsigned edgeCnt; unsigned source; unsigned sink; struct { unsigned node1; unsigned node2; unsigned capacity; } edges [ edgeCount ]; } struct NodePair { - uint node1ID; - uint node2ID; + unsigned node1ID; + unsigned node2ID; }; NetworkDef(1) network1 = { @@ -105,12 +105,12 @@ NodePair undircut4 [3] = { template< typename Type1, typename Type2 > -void testDirNetwork( uint netID, const Type1 * netDef, const Type2 cutDef, uint cutSize ) +void testDirNetwork( unsigned netID, const Type1 * netDef, const Type2 cutDef, unsigned cutSize ) { graph::AdjacencyListDirectedGraph graph; std::vector<graph::Node> id2node( netDef->nodeCnt ); graph::Node source, sink; - uint i; + unsigned i; for (i = 0; i < netDef->nodeCnt; i++) { graph::Node node( std::string("n")+toString(i) ); @@ -130,19 +130,19 @@ void testDirNetwork( uint netID, const Type1 * netDef, const Type2 cutDef, uint graph::minimumcut::Cut cut = graph::minimumcut::FordFulkerson::fordfulkerson( graph, source, sink ); - CPPUNIT_ASSERT_EQUAL( cutSize, (uint)cut.size() ); + CPPUNIT_ASSERT_EQUAL( cutSize, (unsigned)cut.size() ); for (i = 0; i < cutSize; i++) { CPPUNIT_ASSERT( cut.find( std::make_pair( id2node[cutDef[i].node1ID], id2node[cutDef[i].node2ID] ) ) != cut.end() ); } } template< typename Type1, typename Type2 > -void testUndirNetwork( uint netID, const Type1 * netDef, const Type2 cutDef, uint cutSize ) +void testUndirNetwork( unsigned netID, const Type1 * netDef, const Type2 cutDef, unsigned cutSize ) { graph::AdjacencyListUndirectedGraph graph; std::vector<graph::Node> id2node( netDef->nodeCnt ); graph::Node source, sink; - uint i; + unsigned i; for (i = 0; i < netDef->nodeCnt; i++) { graph::Node node( std::string("n")+toString(i) ); @@ -162,7 +162,7 @@ void testUndirNetwork( uint netID, const Type1 * netDef, const Type2 cutDef, uin graph::minimumcut::Cut cut = graph::minimumcut::FordFulkerson::fordfulkerson( graph, source, sink ); - CPPUNIT_ASSERT_EQUAL( cutSize, (uint)cut.size() ); + CPPUNIT_ASSERT_EQUAL( cutSize, (unsigned)cut.size() ); for (i = 0; i < cutSize; i++) { CPPUNIT_ASSERT( cut.find( std::make_pair( id2node[cutDef[i].node1ID], id2node[cutDef[i].node2ID] ) ) != cut.end() ); } diff --git a/alib2algo_experimental/test-src/graph/spanningtree/EdmondsTest.cpp b/alib2algo_experimental/test-src/graph/spanningtree/EdmondsTest.cpp index 6277bb40c4ec367e2bc4e991da30e0dc1e4ed3e9..d97ee9f654354e635e82aaa6708ccde8ca1b4be8 100644 --- a/alib2algo_experimental/test-src/graph/spanningtree/EdmondsTest.cpp +++ b/alib2algo_experimental/test-src/graph/spanningtree/EdmondsTest.cpp @@ -10,7 +10,7 @@ CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( GraphEdmondsTest, "graph" ); CPPUNIT_TEST_SUITE_REGISTRATION( GraphEdmondsTest ); -#define GraphDef( edgeCount ) const struct { uint nodeCnt; uint edgeCnt; struct { uint node1; uint node2; uint weight; } edges [ edgeCount ]; } +#define GraphDef( edgeCount ) const struct { unsigned nodeCnt; unsigned edgeCnt; struct { unsigned node1; unsigned node2; unsigned weight; } edges [ edgeCount ]; } GraphDef(1) graph1 = { 2, 1, @@ -141,22 +141,22 @@ GraphDef(5) spanningTree7 = { template< typename Type1, typename Type2 > -void testGraph( uint graphID, const Type1 * graphDef, const Type2 * spanTreeDef ) +void testGraph( unsigned graphID, const Type1 * graphDef, const Type2 * spanTreeDef ) { std::vector<graph::Node> id2node( graphDef->nodeCnt ); std::vector<graph::DirectedEdge> id2edge( graphDef->edgeCnt ); - std::unordered_map<graph::Node,uint> node2id; - std::unordered_map<graph::DirectedEdge,uint> edge2id; + std::unordered_map<graph::Node,unsigned> node2id; + std::unordered_map<graph::DirectedEdge,unsigned> edge2id; graph::AdjacencyListDirectedGraph graph; - for (uint i = 0; i < graphDef->nodeCnt; i++) { + for (unsigned i = 0; i < graphDef->nodeCnt; i++) { graph::Node node( std::string("n")+toString(i) ); graph.addNode( node ); id2node[ i ] = node; node2id[ node ] = i; } - for (uint i = 0; i < graphDef->edgeCnt; i++) { + for (unsigned i = 0; i < graphDef->edgeCnt; i++) { graph::DirectedEdge edge( id2node[ graphDef->edges[i].node1 ], id2node[ graphDef->edges[i].node2 ], std::string("e")+toString(i) ); graph.addEdge( edge, graphDef->edges[i].weight ); id2edge[ i ] = edge; @@ -167,9 +167,9 @@ void testGraph( uint graphID, const Type1 * graphDef, const Type2 * spanTreeDef graph::AdjacencyListDirectedGraph spanningTree = graph::spanningtree::Edmonds::edmonds( graph ); - CPPUNIT_ASSERT_EQUAL_MESSAGE( "invalid node count", spanTreeDef->nodeCnt, (uint)spanningTree.nodeCount() ); - CPPUNIT_ASSERT_EQUAL_MESSAGE( "invalid edge count", spanTreeDef->edgeCnt, (uint)spanningTree.edgeCount() ); - for (uint i = 0; i < spanTreeDef->edgeCnt; i++) { + CPPUNIT_ASSERT_EQUAL_MESSAGE( "invalid node count", spanTreeDef->nodeCnt, (unsigned)spanningTree.nodeCount() ); + CPPUNIT_ASSERT_EQUAL_MESSAGE( "invalid edge count", spanTreeDef->edgeCnt, (unsigned)spanningTree.edgeCount() ); + for (unsigned i = 0; i < spanTreeDef->edgeCnt; i++) { CPPUNIT_ASSERT_EQUAL_MESSAGE( "missing edge: "+toString(spanTreeDef->edges[i].node1)+","+toString(spanTreeDef->edges[i].node2), true, spanningTree.hasEdge( id2node[ spanTreeDef->edges[i].node1 ], id2node[ spanTreeDef->edges[i].node2 ] ) ); } diff --git a/alib2algo_experimental/test-src/graph/spanningtree/KruskalTest.cpp b/alib2algo_experimental/test-src/graph/spanningtree/KruskalTest.cpp index f4a83fc740092b9b9de5d34ae43b4c858184efd8..15250c33f3cdd8e0bf12f80d62fcd96d65031385 100644 --- a/alib2algo_experimental/test-src/graph/spanningtree/KruskalTest.cpp +++ b/alib2algo_experimental/test-src/graph/spanningtree/KruskalTest.cpp @@ -9,7 +9,7 @@ CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( GraphKruskalTest, "graph" ); CPPUNIT_TEST_SUITE_REGISTRATION( GraphKruskalTest ); -#define GraphDef( edgeCount ) const struct { uint nodeCnt; uint edgeCnt; struct { uint node1; uint node2; uint weight; } edges [ edgeCount ]; } +#define GraphDef( edgeCount ) const struct { unsigned nodeCnt; unsigned edgeCnt; struct { unsigned node1; unsigned node2; unsigned weight; } edges [ edgeCount ]; } GraphDef(1) graph1 = { 2, 1, @@ -109,22 +109,22 @@ GraphDef(11) spanningTree4 = { template< typename Type1, typename Type2 > -void testGraph( uint graphID, const Type1 * graphDef, const Type2 * spanTreeDef ) +void testGraph( unsigned graphID, const Type1 * graphDef, const Type2 * spanTreeDef ) { std::vector<graph::Node> id2node( graphDef->nodeCnt ); std::vector<graph::UndirectedEdge> id2edge( graphDef->edgeCnt ); - std::unordered_map<graph::Node,uint> node2id; - std::unordered_map<graph::UndirectedEdge,uint> edge2id; + std::unordered_map<graph::Node,unsigned> node2id; + std::unordered_map<graph::UndirectedEdge,unsigned> edge2id; graph::AdjacencyListUndirectedGraph graph; - for (uint i = 0; i < graphDef->nodeCnt; i++) { + for (unsigned i = 0; i < graphDef->nodeCnt; i++) { graph::Node node( std::string("n")+toString(i) ); graph.addNode( node ); id2node[ i ] = node; node2id[ node ] = i; } - for (uint i = 0; i < graphDef->edgeCnt; i++) { + for (unsigned i = 0; i < graphDef->edgeCnt; i++) { graph::UndirectedEdge edge( id2node[ graphDef->edges[i].node1 ], id2node[ graphDef->edges[i].node2 ], std::string("e")+toString(i) ); graph.addEdge( edge, graphDef->edges[i].weight ); id2edge[ i ] = edge; @@ -135,9 +135,9 @@ void testGraph( uint graphID, const Type1 * graphDef, const Type2 * spanTreeDef graph::AdjacencyListUndirectedGraph spanningTree = graph::spanningtree::Kruskal::kruskal( graph ); - CPPUNIT_ASSERT_EQUAL_MESSAGE( "invalid node count", spanTreeDef->nodeCnt, (uint)spanningTree.nodeCount() ); - CPPUNIT_ASSERT_EQUAL_MESSAGE( "invalid edge count", spanTreeDef->edgeCnt, (uint)spanningTree.edgeCount() ); - for (uint i = 0; i < spanTreeDef->edgeCnt; i++) { + CPPUNIT_ASSERT_EQUAL_MESSAGE( "invalid node count", spanTreeDef->nodeCnt, (unsigned)spanningTree.nodeCount() ); + CPPUNIT_ASSERT_EQUAL_MESSAGE( "invalid edge count", spanTreeDef->edgeCnt, (unsigned)spanningTree.edgeCount() ); + for (unsigned i = 0; i < spanTreeDef->edgeCnt; i++) { CPPUNIT_ASSERT_EQUAL_MESSAGE( "missing edge: "+toString(spanTreeDef->edges[i].node1)+","+toString(spanTreeDef->edges[i].node2), true, spanningTree.hasEdge( id2node[ spanTreeDef->edges[i].node1 ], id2node[ spanTreeDef->edges[i].node2 ] ) ); }