Add reserve functions to ListGraph and SmartGraph (#311)
authorPeter Kovacs <kpeter@inf.elte.hu>
Sun, 23 Aug 2009 11:10:40 +0200
changeset 7362e20aad15754
parent 735 853fcddcf282
child 737 9d6c3e8b2421
Add reserve functions to ListGraph and SmartGraph (#311)
ListDigraph and SmartDigraph already have such functions.
lemon/list_graph.h
lemon/smart_graph.h
test/digraph_test.cc
test/graph_test.cc
     1.1 --- a/lemon/list_graph.h	Sun Aug 23 11:09:22 2009 +0200
     1.2 +++ b/lemon/list_graph.h	Sun Aug 23 11:10:40 2009 +0200
     1.3 @@ -1311,6 +1311,26 @@
     1.4        Parent::clear();
     1.5      }
     1.6  
     1.7 +    /// Reserve memory for nodes.
     1.8 +
     1.9 +    /// Using this function, it is possible to avoid superfluous memory
    1.10 +    /// allocation: if you know that the graph you want to build will
    1.11 +    /// be large (e.g. it will contain millions of nodes and/or edges),
    1.12 +    /// then it is worth reserving space for this amount before starting
    1.13 +    /// to build the graph.
    1.14 +    /// \sa reserveEdge()
    1.15 +    void reserveNode(int n) { nodes.reserve(n); };
    1.16 +
    1.17 +    /// Reserve memory for edges.
    1.18 +
    1.19 +    /// Using this function, it is possible to avoid superfluous memory
    1.20 +    /// allocation: if you know that the graph you want to build will
    1.21 +    /// be large (e.g. it will contain millions of nodes and/or edges),
    1.22 +    /// then it is worth reserving space for this amount before starting
    1.23 +    /// to build the graph.
    1.24 +    /// \sa reserveNode()
    1.25 +    void reserveEdge(int m) { arcs.reserve(2 * m); };
    1.26 +
    1.27      /// \brief Class to make a snapshot of the graph and restore
    1.28      /// it later.
    1.29      ///
     2.1 --- a/lemon/smart_graph.h	Sun Aug 23 11:09:22 2009 +0200
     2.2 +++ b/lemon/smart_graph.h	Sun Aug 23 11:10:40 2009 +0200
     2.3 @@ -691,6 +691,26 @@
     2.4        Parent::clear();
     2.5      }
     2.6  
     2.7 +    /// Reserve memory for nodes.
     2.8 +
     2.9 +    /// Using this function, it is possible to avoid superfluous memory
    2.10 +    /// allocation: if you know that the graph you want to build will
    2.11 +    /// be large (e.g. it will contain millions of nodes and/or edges),
    2.12 +    /// then it is worth reserving space for this amount before starting
    2.13 +    /// to build the graph.
    2.14 +    /// \sa reserveEdge()
    2.15 +    void reserveNode(int n) { nodes.reserve(n); };
    2.16 +
    2.17 +    /// Reserve memory for edges.
    2.18 +
    2.19 +    /// Using this function, it is possible to avoid superfluous memory
    2.20 +    /// allocation: if you know that the graph you want to build will
    2.21 +    /// be large (e.g. it will contain millions of nodes and/or edges),
    2.22 +    /// then it is worth reserving space for this amount before starting
    2.23 +    /// to build the graph.
    2.24 +    /// \sa reserveNode()
    2.25 +    void reserveEdge(int m) { arcs.reserve(2 * m); };
    2.26 +
    2.27    public:
    2.28  
    2.29      class Snapshot;
     3.1 --- a/test/digraph_test.cc	Sun Aug 23 11:09:22 2009 +0200
     3.2 +++ b/test/digraph_test.cc	Sun Aug 23 11:10:40 2009 +0200
     3.3 @@ -35,6 +35,9 @@
     3.4    checkGraphNodeList(G, 0);
     3.5    checkGraphArcList(G, 0);
     3.6  
     3.7 +  G.reserveNode(3);
     3.8 +  G.reserveArc(4);
     3.9 +
    3.10    Node
    3.11      n1 = G.addNode(),
    3.12      n2 = G.addNode(),
     4.1 --- a/test/graph_test.cc	Sun Aug 23 11:09:22 2009 +0200
     4.2 +++ b/test/graph_test.cc	Sun Aug 23 11:10:40 2009 +0200
     4.3 @@ -38,6 +38,9 @@
     4.4    checkGraphEdgeList(G, 0);
     4.5    checkGraphArcList(G, 0);
     4.6  
     4.7 +  G.reserveNode(3);
     4.8 +  G.reserveEdge(3);
     4.9 +
    4.10    Node
    4.11      n1 = G.addNode(),
    4.12      n2 = G.addNode(),