Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
Molstar
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Michal Malý
Molstar
Commits
d48e496c
Commit
d48e496c
authored
7 years ago
by
David Sehnal
Browse files
Options
Downloads
Patches
Plain Diff
simplified iterators
parent
a91b5123
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/structure/collections/int-pair.ts
+1
-1
1 addition, 1 deletion
src/structure/collections/int-pair.ts
src/structure/collections/iterator.ts
+30
-22
30 additions, 22 deletions
src/structure/collections/iterator.ts
with
31 additions
and
23 deletions
src/structure/collections/int-pair.ts
+
1
−
1
View file @
d48e496c
...
...
@@ -28,4 +28,4 @@ namespace IntPair {
}
}
export
default
IntPair
export
default
IntPair
\ No newline at end of file
This diff is collapsed.
Click to expand it.
src/structure/collections/iterator.ts
+
30
−
22
View file @
d48e496c
...
...
@@ -5,64 +5,72 @@
*/
/**
* ES6 compatible
mutable
iterator.
Use with care.
* ES6 compatible iterator.
*
* "Idiomatic" usage
:
* "Idiomatic" usage
is to use the move function, because it does not make any allocations.
*
* const it = ...;
* for (let v = it.
nextValu
e();
!
it.
done
; v = it.
nextValu
e()) { ... }
* for (let v = it.
mov
e(); it.
hasNext
; v = it.
mov
e()) { ... }
*/
interface
Iterator
<
T
>
{
[
Symbol
.
iterator
]():
Iterator
<
T
>
,
readonly
done
:
boolean
,
readonly
value
:
T
,
readonly
hasNext
:
boolean
,
next
():
{
done
:
boolean
,
value
:
T
},
nextValu
e
():
T
mov
e
():
T
}
class
ArrayIteratorImpl
<
T
>
implements
Iterator
<
T
>
{
private
xs
:
ArrayLike
<
T
>
=
[];
private
index
:
number
=
-
1
;
private
length
:
number
=
0
;
private
lastValue
:
T
;
[
Symbol
.
iterator
]()
{
return
this
;
};
done
=
true
;
value
:
T
=
void
0
as
any
;
hasNext
:
boolean
;
next
()
{
const
index
=
++
this
.
index
;
if
(
index
<
this
.
length
)
this
.
value
=
this
.
xs
[
index
];
else
this
.
done
=
true
;
return
this
;
const
value
=
this
.
move
();
return
{
value
,
done
:
!
this
.
hasNext
};
}
nextValue
()
{
return
this
.
next
().
value
;
}
move
()
{
const
index
=
++
this
.
index
;
if
(
index
<
this
.
length
)
this
.
lastValue
=
this
.
xs
[
index
];
else
this
.
hasNext
=
false
;
return
this
.
lastValue
;
}
constructor
(
xs
:
ArrayLike
<
T
>
)
{
this
.
length
=
xs
.
length
;
this
.
done
=
false
;
this
.
hasNext
=
xs
.
length
>
0
;
this
.
xs
=
xs
;
this
.
index
=
-
1
;
// try to avoid deoptimization with undefined values
this
.
lastValue
=
xs
.
length
>
0
?
xs
[
0
]
:
void
0
as
any
;
return
this
;
}
}
class
RangeIteratorImpl
implements
Iterator
<
number
>
{
private
value
:
number
;
[
Symbol
.
iterator
]()
{
return
this
;
};
done
=
true
;
value
:
number
;
hasNext
:
boolean
;
next
()
{
++
this
.
value
;
this
.
done
=
this
.
value
>
this
.
max
;
return
this
;
const
value
=
this
.
value
;
return
{
value
,
done
:
!
this
.
hasNext
}
}
nextValue
()
{
return
this
.
next
().
value
;
}
move
()
{
++
this
.
value
;
this
.
hasNext
=
this
.
value
<=
this
.
max
;
return
this
.
value
;
}
constructor
(
min
:
number
,
private
max
:
number
)
{
this
.
value
=
min
-
1
;
this
.
done
=
false
;
this
.
hasNext
=
max
>=
min
;
return
this
;
}
}
...
...
@@ -75,7 +83,7 @@ namespace Iterator {
export
function
toArray
<
T
>
(
it
:
Iterator
<
T
>
):
T
[]
{
const
ret
=
[];
for
(
let
v
=
it
.
nextValu
e
();
!
it
.
done
;
v
=
it
.
nextValu
e
())
ret
[
ret
.
length
]
=
v
;
for
(
let
v
=
it
.
mov
e
();
it
.
hasNext
;
v
=
it
.
mov
e
())
ret
[
ret
.
length
]
=
v
;
return
ret
;
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment