-
Notifications
You must be signed in to change notification settings - Fork 0
/
adi_trie.dart
86 lines (69 loc) · 1.99 KB
/
adi_trie.dart
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
/*
NODE BASED, GENERIC IMPLEMENTATION
class ADINode<E> {
E _data;
int _relations;
ADINode(E datum){
this._data = datum;
this._relations = 0;
}
E getData() => this._data;
void setData(E datum) => this._data = datum;
int getRelations() => this._relations;
void setRelations(int relation) => this._relations |= relation;
}
class ADIGraph {
List<ADINode<String>> nodes;
void addPerson({name: String}){
int duplicates = 0;
nodes.forEach((node){
node.getData() == name ? duplicates++ : null;
});
duplicates > 0 ? print('$name is already in the graph.') : nodes.add(ADINode(name));
}
void connectFriends({name: String, friends: List}){
friends.forEach((friend){
})
}
*/
class ADIGraph {
List<int> relations = [];
List<String> people = [];
void addPerson({name: String}){
if(people.contains(name)){
print('$name is already in the graph.');
}
else{
relations.add(0);
people.add(name);
}
}
void connectFriends({name: String, friends: List}){
if(!people.contains(name)){
print('$name not found, adding $name to graph.');
this.addPerson(name: name);
}
for(int i = 0; i < friends.length; i++){
if(people.contains(friends[i])){
int friendPosition = (1<<people.indexOf(friends[i]));
relations[people.indexOf(name)] |= friendPosition;
} else {
this.addPerson(name: friends[i]);
int friendPosition = 1<<people.indexOf(friends[i]);
relations[people.indexOf(name)] |= friendPosition;
}
}
}
}
void main() {
ADIGraph sanctum = new ADIGraph();
List<String> roomies = ['Adelya', 'Alex', 'Andre', 'Arjun'];
List<String> addysFriends = ['Eli', 'Steph', 'Rain', 'Alex'];
for(String person in roomies){
sanctum.addPerson(name: person);
}
sanctum.connectFriends(name: 'Adelya', friends: addysFriends);
print(sanctum.people);
print(sanctum.relations);
print(sanctum.relations[0].toRadixString(2));
}