Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
7
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Open sidebar
Tiago Peixoto
graph-tool
Commits
48f1b2e9
Commit
48f1b2e9
authored
Mar 11, 2012
by
Tiago Peixoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Move random_permutation_interator to graph_util.hh
parent
1fd8cd95
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
62 additions
and
60 deletions
+62
-60
src/graph/generation/graph_rewiring.hh
src/graph/generation/graph_rewiring.hh
+0
-60
src/graph/graph_util.hh
src/graph/graph_util.hh
+62
-0
No files found.
src/graph/generation/graph_rewiring.hh
View file @
48f1b2e9
...
...
@@ -29,11 +29,6 @@
#include <iostream>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/random_access_index.hpp>
#include <boost/multi_index/identity.hpp>
#include "graph.hh"
#include "graph_filtering.hh"
#include "graph_util.hh"
...
...
@@ -43,7 +38,6 @@ namespace graph_tool
{
using
namespace
std
;
using
namespace
boost
;
using
namespace
boost
::
multi_index
;
// returns true if vertices u and v are adjacent. This is O(k(u)).
template
<
class
Graph
>
...
...
@@ -84,60 +78,6 @@ target(const pair<size_t, bool>& e,
return
target
(
edges
[
e
.
first
],
g
);
}
// This will iterate over a random permutation of a random access sequence, by
// swapping the values of the sequence as it iterates
template
<
class
RandomAccessIterator
,
class
RNG
,
class
RandomDist
=
tr1
::
uniform_int
<
size_t
>
>
class
random_permutation_iterator
:
public
std
::
iterator
<
input_iterator_tag
,
typename
RandomAccessIterator
::
value_type
>
{
public:
random_permutation_iterator
(
RandomAccessIterator
begin
,
RandomAccessIterator
end
,
RNG
&
rng
)
:
_i
(
begin
),
_end
(
end
),
_rng
(
&
rng
)
{
if
(
_i
!=
_end
)
{
RandomDist
random
(
0
,
_end
-
_i
-
1
);
std
::
iter_swap
(
_i
,
_i
+
random
(
*
_rng
));
}
}
typename
RandomAccessIterator
::
value_type
operator
*
()
{
return
*
_i
;
}
random_permutation_iterator
&
operator
++
()
{
++
_i
;
if
(
_i
!=
_end
)
{
RandomDist
random
(
0
,
_end
-
_i
-
1
);
std
::
iter_swap
(
_i
,
_i
+
random
(
*
_rng
));
}
return
*
this
;
}
bool
operator
==
(
const
random_permutation_iterator
&
ri
)
{
return
_i
==
ri
.
_i
;
}
bool
operator
!=
(
const
random_permutation_iterator
&
ri
)
{
return
_i
!=
ri
.
_i
;
}
size_t
operator
-
(
const
random_permutation_iterator
&
ri
)
{
return
_i
-
ri
.
_i
;
}
private:
RandomAccessIterator
_i
,
_end
;
RNG
*
_rng
;
};
// this functor will swap the source of the edge e with the source of edge se
// and the target of edge e with the target of te
...
...
src/graph/graph_util.hh
View file @
48f1b2e9
...
...
@@ -34,6 +34,12 @@
#include <boost/type_traits/is_convertible.hpp>
#include <string>
#if (GCC_VERSION >= 40400)
# include <tr1/random>
#else
# include <boost/tr1/random.hpp>
#endif
namespace
graph_tool
{
using
namespace
boost
;
...
...
@@ -368,4 +374,60 @@ private:
};
// This will iterate over a random permutation of a random access sequence, by
// swapping the values of the sequence as it iterates
template
<
class
RandomAccessIterator
,
class
RNG
,
class
RandomDist
=
std
::
tr1
::
uniform_int
<
size_t
>
>
class
random_permutation_iterator
:
public
std
::
iterator
<
std
::
input_iterator_tag
,
typename
RandomAccessIterator
::
value_type
>
{
public:
random_permutation_iterator
(
RandomAccessIterator
begin
,
RandomAccessIterator
end
,
RNG
&
rng
)
:
_i
(
begin
),
_end
(
end
),
_rng
(
&
rng
)
{
if
(
_i
!=
_end
)
{
RandomDist
random
(
0
,
_end
-
_i
-
1
);
std
::
iter_swap
(
_i
,
_i
+
random
(
*
_rng
));
}
}
typename
RandomAccessIterator
::
value_type
operator
*
()
{
return
*
_i
;
}
random_permutation_iterator
&
operator
++
()
{
++
_i
;
if
(
_i
!=
_end
)
{
RandomDist
random
(
0
,
_end
-
_i
-
1
);
std
::
iter_swap
(
_i
,
_i
+
random
(
*
_rng
));
}
return
*
this
;
}
bool
operator
==
(
const
random_permutation_iterator
&
ri
)
{
return
_i
==
ri
.
_i
;
}
bool
operator
!=
(
const
random_permutation_iterator
&
ri
)
{
return
_i
!=
ri
.
_i
;
}
size_t
operator
-
(
const
random_permutation_iterator
&
ri
)
{
return
_i
-
ri
.
_i
;
}
private:
RandomAccessIterator
_i
,
_end
;
RNG
*
_rng
;
};
#endif // GRAPH_UTIL_HH
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment