-
Notifications
You must be signed in to change notification settings - Fork 0
/
conceptual.js
79 lines (79 loc) · 2.63 KB
/
conceptual.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
/**
* Example of Concrete Iterators which implement the traversal algorithms.
* These store the current traversal position always.
*/
var ConcreteIterator = /** @class */ (function () {
function ConcreteIterator(collection, reverse) {
if (reverse === void 0) { reverse = false; }
// Storing current position of traversal (may be other feilds for iteration state, depending on collection type)
this.position = 0;
// Variable for traversal direction
this.reverse = false;
this.collection = collection;
this.reverse = reverse;
if (reverse) {
this.position = collection.getCount() - 1;
}
}
ConcreteIterator.prototype.rewind = function () {
this.position = this.reverse ?
this.collection.getCount() - 1 :
0;
};
ConcreteIterator.prototype.current = function () {
return this.collection.getItems()[this.position];
};
ConcreteIterator.prototype.key = function () {
return this.position;
};
ConcreteIterator.prototype.next = function () {
var item = this.collection.getItems()[this.position];
this.position += this.reverse ? -1 : 1;
return item;
};
ConcreteIterator.prototype.valid = function () {
if (this.reverse) {
return this.position >= 0;
}
return this.position < this.collection.getCount();
};
return ConcreteIterator;
}());
// Concrete Collections provide methods for retreiving new iterator instances for said collection.
var WordsCollection = /** @class */ (function () {
function WordsCollection() {
this.items = [];
}
WordsCollection.prototype.getItems = function () {
return this.items;
};
WordsCollection.prototype.getCount = function () {
return this.items.length;
};
WordsCollection.prototype.addItem = function (item) {
this.items.push(item);
};
WordsCollection.prototype.getIterator = function () {
return new ConcreteIterator(this);
};
WordsCollection.prototype.getReverseIterator = function () {
return new ConcreteIterator(this, true);
};
return WordsCollection;
}());
// Client code
var collection = new WordsCollection();
collection.addItem('First');
collection.addItem('Second');
collection.addItem('Third');
var iterator = collection.getIterator();
console.log('Straight traversal:');
while (iterator.valid()) {
console.log(iterator.next());
}
console.log('');
console.log('Reverse traversal:');
var reverseIterator = collection.getReverseIterator();
while (reverseIterator.valid()) {
console.log(reverseIterator.next());
}