Skip to content

Commit

Permalink
add iterator part of vector
Browse files Browse the repository at this point in the history
  • Loading branch information
ailisp committed May 19, 2022
1 parent 4465772 commit 015cc0d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/collections/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { LookupMap } from "./lookup-map";
import { Vector } from "./vector";

export {
LookupMap
LookupMap,
Vector
}
30 changes: 30 additions & 0 deletions src/collections/vector.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,34 @@ export class Vector {
this.push(element)
}
}

iter() {
return new VectorIterator(this)
}

toArray() {
let ret = []
let iterator = this.iter()
while (iterator.hasNext()) {
ret.push(this.iterator.next())
}
return ret
}
}

class VectorIterator {
constructor(vector) {
this.current = 0
this.vector = vector
}

hasNext() {
return this.current < this.vector.len()
}

next() {
let value = this.vector.get(this.current)
this.current += 1
return value
}
}

0 comments on commit 015cc0d

Please sign in to comment.