-
Notifications
You must be signed in to change notification settings - Fork 5
/
GraphImpl.js
111 lines (87 loc) · 2.45 KB
/
GraphImpl.js
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
var Class = require('../ext/Class');
var HashSet = require('../util/collection/HashSet');
var Triple = require('./Triple');
var TripleUtils = require('./TripleUtils');
/**
* Simple set based graph implementation with O(n) complexity for lookups
*
*/
var GraphImpl = Class.create({
initialize: function(triplesSet) {
this.triples = triplesSet || new HashSet();
var self = this;
// Object.defineProperty(this, 'length', {
// get: function() {
// return self.triples.length;
// }
// });
},
isEmpty: function() {
var result = this.triples.isEmpty();
return result;
},
equals: function() {
throw new Error('Not implemented yet');
},
hashCode: function() {
var result = this.triples.hashCode();
return result;
},
add: function(triple) {
var result = this.triples.add(triple);
return result;
},
remove: function(triple) {
var result = this.triples.remove(triple);
return result;
},
removeAll: function(triples) {
this.triples.removeAll(triples);
},
removeMatch: function(pattern) {
//var pattern = new Triple(s, p, o);
var removals = [];
this.triples.forEach(function(triple) {
var isMatch = TripleUtils.matches(pattern, triple);
if(isMatch) {
removals.push(triple);
}
});
//console.log('removals: ', removals);
this.removeAll(removals);
},
forEach: function(callback) {
this.triples.forEach(callback);
},
map: function(callback) {
var result = this.triples.map(callback);
return result;
},
contains: function(triple) {
var result = this.triples.contains(triple);
return result;
},
// filter
// every
// match
// merge (just a convenience for x = new Graph(); x.addAll(otherGraph)
// Adds items from a .forEach-able object (i.e. graph or array of triples)
addAll: function(triples) {
var self = this;
triples.forEach(function(triple) {
self.add(triple);
});
},
clear: function() {
this.triples.clear();
},
toArray: function() {
var result = this.triples.entries();
return result;
},
size: function() {
var result = this.triples.size();
return result;
}
});
module.exports = GraphImpl;