[Lemon-commits] Peter Kovacs: Add reserve functions to ListGraph...

Lemon HG hg at lemon.cs.elte.hu
Wed Sep 30 08:52:47 CEST 2009


details:   http://lemon.cs.elte.hu/hg/lemon/rev/2e20aad15754
changeset: 787:2e20aad15754
user:      Peter Kovacs <kpeter [at] inf.elte.hu>
date:      Sun Aug 23 11:10:40 2009 +0200
description:
	Add reserve functions to ListGraph and SmartGraph (#311) ListDigraph
	and SmartDigraph already have such functions.

diffstat:

 lemon/list_graph.h   |  20 ++++++++++++++++++++
 lemon/smart_graph.h  |  20 ++++++++++++++++++++
 test/digraph_test.cc |   3 +++
 test/graph_test.cc   |   3 +++
 4 files changed, 46 insertions(+), 0 deletions(-)

diffs (86 lines):

diff --git a/lemon/list_graph.h b/lemon/list_graph.h
--- a/lemon/list_graph.h
+++ b/lemon/list_graph.h
@@ -1311,6 +1311,26 @@
       Parent::clear();
     }
 
+    /// Reserve memory for nodes.
+
+    /// Using this function, it is possible to avoid superfluous memory
+    /// allocation: if you know that the graph you want to build will
+    /// be large (e.g. it will contain millions of nodes and/or edges),
+    /// then it is worth reserving space for this amount before starting
+    /// to build the graph.
+    /// \sa reserveEdge()
+    void reserveNode(int n) { nodes.reserve(n); };
+
+    /// Reserve memory for edges.
+
+    /// Using this function, it is possible to avoid superfluous memory
+    /// allocation: if you know that the graph you want to build will
+    /// be large (e.g. it will contain millions of nodes and/or edges),
+    /// then it is worth reserving space for this amount before starting
+    /// to build the graph.
+    /// \sa reserveNode()
+    void reserveEdge(int m) { arcs.reserve(2 * m); };
+
     /// \brief Class to make a snapshot of the graph and restore
     /// it later.
     ///
diff --git a/lemon/smart_graph.h b/lemon/smart_graph.h
--- a/lemon/smart_graph.h
+++ b/lemon/smart_graph.h
@@ -691,6 +691,26 @@
       Parent::clear();
     }
 
+    /// Reserve memory for nodes.
+
+    /// Using this function, it is possible to avoid superfluous memory
+    /// allocation: if you know that the graph you want to build will
+    /// be large (e.g. it will contain millions of nodes and/or edges),
+    /// then it is worth reserving space for this amount before starting
+    /// to build the graph.
+    /// \sa reserveEdge()
+    void reserveNode(int n) { nodes.reserve(n); };
+
+    /// Reserve memory for edges.
+
+    /// Using this function, it is possible to avoid superfluous memory
+    /// allocation: if you know that the graph you want to build will
+    /// be large (e.g. it will contain millions of nodes and/or edges),
+    /// then it is worth reserving space for this amount before starting
+    /// to build the graph.
+    /// \sa reserveNode()
+    void reserveEdge(int m) { arcs.reserve(2 * m); };
+
   public:
 
     class Snapshot;
diff --git a/test/digraph_test.cc b/test/digraph_test.cc
--- a/test/digraph_test.cc
+++ b/test/digraph_test.cc
@@ -35,6 +35,9 @@
   checkGraphNodeList(G, 0);
   checkGraphArcList(G, 0);
 
+  G.reserveNode(3);
+  G.reserveArc(4);
+
   Node
     n1 = G.addNode(),
     n2 = G.addNode(),
diff --git a/test/graph_test.cc b/test/graph_test.cc
--- a/test/graph_test.cc
+++ b/test/graph_test.cc
@@ -38,6 +38,9 @@
   checkGraphEdgeList(G, 0);
   checkGraphArcList(G, 0);
 
+  G.reserveNode(3);
+  G.reserveEdge(3);
+
   Node
     n1 = G.addNode(),
     n2 = G.addNode(),



More information about the Lemon-commits mailing list