-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.erl
117 lines (95 loc) · 2.92 KB
/
util.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
-module(util).
-include_lib("eunit/include/eunit.hrl").
-compile(export_all).
%%Records einbinden
-include_lib("datastructures.hrl").
%%Ersetzt ein Element in
replace_edge(List, Old_elem, New_elem) ->
List_tmp = List -- [Old_elem],
List_tmp ++ [New_elem].
get_edge_by_neighbour_edge(Edge_list, {Weight, Nodex, Nodey}) ->
TestEdge = #edge { weight = Weight, node_1 = Nodey, node_2 = Nodex },
List_tmp = lists:filter(fun(Elem) -> are_edges_equal(Elem, TestEdge) end, Edge_list),
case length(List_tmp) > 0 of
true -> lists:nth(1, List_tmp);
false -> not_found
end.
are_edges_equal(Edge1, Edge2) ->
if
(Edge1 == undefined) or (Edge2 == undefined) ->
false;
true ->
(Edge1#edge.weight == Edge2#edge.weight)
and (Edge1#edge.node_1 == Edge2#edge.node_1)
and (Edge1#edge.node_2 == Edge2#edge.node_2)
end.
log(Format, Data) ->
io:format(Format ++ "~n", Data).
get_best_edge_from_basic_edges(EdgeList) ->
Edges = lists:filter(
fun(Edge) -> Edge#edge.type == basic end,
EdgeList),
[ FirstEdge | _ ] = Edges,
lists:foldl(
fun(Edge, Akmg) -> case Edge#edge.weight < Akmg#edge.weight of
true -> Edge;
false -> Akmg
end
end,
FirstEdge,
Edges
).
%%Testcases
replace_edge_test()->
Edge_1 = #edge {
node_1 = eins,
node_2 = zwei,
weight = 1,
type = basic
},
Edge_2 = #edge {
node_1 = drei,
node_2 = vier,
weight = 4,
type = rejected
},
Edge_1_changed = #edge {
node_1 = eins,
node_2 = zwei,
weight = 1,
type = branch
},
Edge_2_changed = #edge {
node_1 = drei,
node_2 = vier,
weight = 4,
type = rejected
},
Source_list = [Edge_1, Edge_2],
Destination_list_1 = [Edge_1, Edge_2_changed],
Destination_list_2 = [Edge_2, Edge_1_changed],
?assertEqual(Destination_list_1, replace_edge(Source_list, Edge_2, Edge_2_changed)),
?assertEqual(Destination_list_2, replace_edge(Source_list, Edge_1, Edge_1_changed)).
get_edge_by_neighbour_edge_test() ->
%Edge_list, {Weight, Nodex, Nodey}
Edge_1 = #edge {
node_1 = eins,
node_2 = zwei,
weight = 1,
type = basic
},
Edge_2 = #edge {
node_1 = drei,
node_2 = vier,
weight = 4,
type = rejected
},
Edge_list = [Edge_1, Edge_2],
?assertEqual(Edge_1, get_edge_by_neighbour_edge(Edge_list, {1, zwei, eins})),
?assertEqual(Edge_2, get_edge_by_neighbour_edge(Edge_list, {4, vier, drei})).
get_akmg_from_basic_edges_test() ->
Edge_1 = #edge {node_1 = eins, node_2 = zwei, weight = 2, type = basic},
Edge_2 = #edge {node_1 = drei, node_2 = vier, weight = 4, type = branch},
Edge_3 = #edge {node_1 = fuenf, node_2 = sechs, weight = 6, type = basic},
EdgeList = [Edge_1, Edge_2, Edge_3],
?assertEqual(Edge_1, get_best_edge_from_basic_edges(EdgeList)).