Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
dlib
Commits
0fcd3820
Commit
0fcd3820
authored
Apr 30, 2012
by
Davis King
Browse files
Added the copy_graph() routine.
parent
18ab8c7a
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
201 additions
and
0 deletions
+201
-0
dlib/graph_utils/graph_utils.h
dlib/graph_utils/graph_utils.h
+61
-0
dlib/graph_utils/graph_utils_abstract.h
dlib/graph_utils/graph_utils_abstract.h
+23
-0
dlib/test/directed_graph.cpp
dlib/test/directed_graph.cpp
+62
-0
dlib/test/graph.cpp
dlib/test/graph.cpp
+55
-0
No files found.
dlib/graph_utils/graph_utils.h
View file @
0fcd3820
...
@@ -351,6 +351,67 @@ namespace dlib
...
@@ -351,6 +351,67 @@ namespace dlib
}
}
}
}
// ----------------------------------------------------------------------------------------
template
<
typename
graph_type1
,
typename
graph_type2
>
typename
enable_if
<
is_graph
<
graph_type1
>
>::
type
copy_graph
(
const
graph_type1
&
src
,
graph_type2
&
dest
)
{
COMPILE_TIME_ASSERT
(
is_graph
<
graph_type1
>::
value
);
COMPILE_TIME_ASSERT
(
is_graph
<
graph_type2
>::
value
);
if
(
graph_helpers
::
is_same_object
(
src
,
dest
))
return
;
copy_graph_structure
(
src
,
dest
);
// copy all the node and edge content
for
(
unsigned
long
i
=
0
;
i
<
src
.
number_of_nodes
();
++
i
)
{
dest
.
node
(
i
).
data
=
src
.
node
(
i
).
data
;
for
(
unsigned
long
j
=
0
;
j
<
src
.
node
(
i
).
number_of_neighbors
();
++
j
)
{
const
unsigned
long
nidx
=
src
.
node
(
i
).
neighbor
(
j
).
index
();
if
(
nidx
>=
i
)
{
dest
.
node
(
i
).
edge
(
j
)
=
src
.
node
(
i
).
edge
(
j
);
}
}
}
}
template
<
typename
graph_type1
,
typename
graph_type2
>
typename
enable_if
<
is_directed_graph
<
graph_type1
>
>::
type
copy_graph
(
const
graph_type1
&
src
,
graph_type2
&
dest
)
{
COMPILE_TIME_ASSERT
(
is_directed_graph
<
graph_type1
>::
value
);
COMPILE_TIME_ASSERT
(
is_directed_graph
<
graph_type2
>::
value
);
if
(
graph_helpers
::
is_same_object
(
src
,
dest
))
return
;
copy_graph_structure
(
src
,
dest
);
// copy all the node and edge content
for
(
unsigned
long
i
=
0
;
i
<
src
.
number_of_nodes
();
++
i
)
{
dest
.
node
(
i
).
data
=
src
.
node
(
i
).
data
;
for
(
unsigned
long
j
=
0
;
j
<
src
.
node
(
i
).
number_of_children
();
++
j
)
{
dest
.
node
(
i
).
child_edge
(
j
)
=
src
.
node
(
i
).
child_edge
(
j
);
}
}
}
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
template
<
template
<
...
...
dlib/graph_utils/graph_utils_abstract.h
View file @
0fcd3820
...
@@ -183,6 +183,29 @@ namespace dlib
...
@@ -183,6 +183,29 @@ namespace dlib
- #dest.has_edge(i,j) == true
- #dest.has_edge(i,j) == true
!*/
!*/
// ----------------------------------------------------------------------------------------
template
<
typename
src_type
,
typename
dest_type
>
void
copy_graph
(
const
src_type
&
src
,
dest_type
&
dest
);
/*!
requires
- src_type is an implementation of directed_graph/directed_graph_kernel_abstract.h or
src_type is an implementation of graph/graph_kernel_abstract.h
- dest_type is an implementation of directed_graph/directed_graph_kernel_abstract.h or
dest_type is an implementation of graph/graph_kernel_abstract.h
- src_type and dest_type are both the same kind of graph. That is, they
are either both directed or both undirected.
- the node and edge data in the graphs are copyable via operator=().
ensures
- #dest is a complete duplicate of src.
!*/
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
template
<
template
<
...
...
dlib/test/directed_graph.cpp
View file @
0fcd3820
...
@@ -445,6 +445,66 @@ namespace
...
@@ -445,6 +445,66 @@ namespace
}
}
void
test_copy
()
{
{
directed_graph
<
int
,
int
>::
kernel_1a_c
a
,
b
;
a
.
set_number_of_nodes
(
3
);
a
.
node
(
0
).
data
=
1
;
a
.
node
(
1
).
data
=
2
;
a
.
node
(
2
).
data
=
3
;
a
.
add_edge
(
0
,
1
);
a
.
add_edge
(
1
,
0
);
a
.
add_edge
(
0
,
2
);
edge
(
a
,
0
,
1
)
=
4
;
edge
(
a
,
1
,
0
)
=
3
;
edge
(
a
,
0
,
2
)
=
5
;
a
.
add_edge
(
0
,
0
);
edge
(
a
,
0
,
0
)
=
9
;
copy_graph
(
a
,
b
);
DLIB_TEST
(
b
.
number_of_nodes
()
==
3
);
DLIB_TEST
(
b
.
node
(
0
).
data
==
1
);
DLIB_TEST
(
b
.
node
(
1
).
data
==
2
);
DLIB_TEST
(
b
.
node
(
2
).
data
==
3
);
DLIB_TEST
(
edge
(
b
,
0
,
1
)
==
4
);
DLIB_TEST
(
edge
(
b
,
1
,
0
)
==
3
);
DLIB_TEST
(
edge
(
b
,
0
,
2
)
==
5
);
DLIB_TEST
(
edge
(
b
,
0
,
0
)
==
9
);
}
{
directed_graph
<
int
,
int
>::
kernel_1a_c
a
,
b
;
a
.
set_number_of_nodes
(
4
);
a
.
node
(
0
).
data
=
1
;
a
.
node
(
1
).
data
=
2
;
a
.
node
(
2
).
data
=
3
;
a
.
node
(
3
).
data
=
8
;
a
.
add_edge
(
0
,
1
);
a
.
add_edge
(
0
,
2
);
a
.
add_edge
(
2
,
3
);
a
.
add_edge
(
3
,
2
);
edge
(
a
,
0
,
1
)
=
4
;
edge
(
a
,
0
,
2
)
=
5
;
edge
(
a
,
2
,
3
)
=
6
;
edge
(
a
,
3
,
2
)
=
3
;
copy_graph
(
a
,
b
);
DLIB_TEST
(
b
.
number_of_nodes
()
==
4
);
DLIB_TEST
(
b
.
node
(
0
).
data
==
1
);
DLIB_TEST
(
b
.
node
(
1
).
data
==
2
);
DLIB_TEST
(
b
.
node
(
2
).
data
==
3
);
DLIB_TEST
(
b
.
node
(
3
).
data
==
8
);
DLIB_TEST
(
edge
(
b
,
0
,
1
)
==
4
);
DLIB_TEST
(
edge
(
b
,
0
,
2
)
==
5
);
DLIB_TEST
(
edge
(
b
,
2
,
3
)
==
6
);
DLIB_TEST
(
edge
(
b
,
3
,
2
)
==
3
);
}
}
class
directed_graph_tester
:
public
tester
class
directed_graph_tester
:
public
tester
...
@@ -465,6 +525,8 @@ namespace
...
@@ -465,6 +525,8 @@ namespace
void
perform_test
(
void
perform_test
(
)
)
{
{
test_copy
();
dlog
<<
LINFO
<<
"testing kernel_1a_c"
;
dlog
<<
LINFO
<<
"testing kernel_1a_c"
;
directed_graph_test
<
directed_graph
<
int
,
unsigned
short
>::
kernel_1a_c
>
();
directed_graph_test
<
directed_graph
<
int
,
unsigned
short
>::
kernel_1a_c
>
();
...
...
dlib/test/graph.cpp
View file @
0fcd3820
...
@@ -323,6 +323,59 @@ namespace
...
@@ -323,6 +323,59 @@ namespace
}
}
void
test_copy
()
{
{
graph
<
int
,
int
>::
kernel_1a_c
a
,
b
;
a
.
set_number_of_nodes
(
3
);
a
.
node
(
0
).
data
=
1
;
a
.
node
(
1
).
data
=
2
;
a
.
node
(
2
).
data
=
3
;
a
.
add_edge
(
0
,
1
);
a
.
add_edge
(
0
,
2
);
edge
(
a
,
0
,
1
)
=
4
;
edge
(
a
,
0
,
2
)
=
5
;
a
.
add_edge
(
0
,
0
);
edge
(
a
,
0
,
0
)
=
9
;
copy_graph
(
a
,
b
);
DLIB_TEST
(
b
.
number_of_nodes
()
==
3
);
DLIB_TEST
(
b
.
node
(
0
).
data
==
1
);
DLIB_TEST
(
b
.
node
(
1
).
data
==
2
);
DLIB_TEST
(
b
.
node
(
2
).
data
==
3
);
DLIB_TEST
(
edge
(
b
,
0
,
1
)
==
4
);
DLIB_TEST
(
edge
(
b
,
0
,
2
)
==
5
);
DLIB_TEST
(
edge
(
b
,
0
,
0
)
==
9
);
}
{
graph
<
int
,
int
>::
kernel_1a_c
a
,
b
;
a
.
set_number_of_nodes
(
4
);
a
.
node
(
0
).
data
=
1
;
a
.
node
(
1
).
data
=
2
;
a
.
node
(
2
).
data
=
3
;
a
.
node
(
3
).
data
=
8
;
a
.
add_edge
(
0
,
1
);
a
.
add_edge
(
0
,
2
);
a
.
add_edge
(
2
,
3
);
edge
(
a
,
0
,
1
)
=
4
;
edge
(
a
,
0
,
2
)
=
5
;
edge
(
a
,
2
,
3
)
=
6
;
copy_graph
(
a
,
b
);
DLIB_TEST
(
b
.
number_of_nodes
()
==
4
);
DLIB_TEST
(
b
.
node
(
0
).
data
==
1
);
DLIB_TEST
(
b
.
node
(
1
).
data
==
2
);
DLIB_TEST
(
b
.
node
(
2
).
data
==
3
);
DLIB_TEST
(
b
.
node
(
3
).
data
==
8
);
DLIB_TEST
(
edge
(
b
,
0
,
1
)
==
4
);
DLIB_TEST
(
edge
(
b
,
0
,
2
)
==
5
);
DLIB_TEST
(
edge
(
b
,
2
,
3
)
==
6
);
}
}
...
@@ -349,6 +402,8 @@ namespace
...
@@ -349,6 +402,8 @@ namespace
dlog
<<
LINFO
<<
"testing kernel_1a"
;
dlog
<<
LINFO
<<
"testing kernel_1a"
;
graph_test
<
graph
<
int
>::
kernel_1a
>
();
graph_test
<
graph
<
int
>::
kernel_1a
>
();
test_copy
();
}
}
}
a
;
}
a
;
...
...
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