Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
T
typon
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Tom Niget
typon
Commits
5b953fba
Commit
5b953fba
authored
Aug 18, 2023
by
Tom Niget
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Finish implementation of range
parent
7e04915c
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
21 additions
and
2 deletions
+21
-2
rt/include/python/builtins/range.hpp
rt/include/python/builtins/range.hpp
+21
-2
No files found.
rt/include/python/builtins/range.hpp
View file @
5b953fba
...
@@ -7,16 +7,35 @@
...
@@ -7,16 +7,35 @@
#include <ranges>
#include <ranges>
namespace
view
=
std
::
views
;
#include <python/basedef.hpp>
#include <python/basedef.hpp>
auto
stride
=
[](
int
n
)
{
return
[
s
=
-
1
,
n
](
auto
const
&
)
mutable
{
s
=
(
s
+
1
)
%
n
;
return
!
s
;
};
};
// todo: proper range support
// todo: proper range support
struct
range_s
:
TyBuiltin
<
range_s
>
struct
range_s
:
TyBuiltin
<
range_s
>
{
{
template
<
typename
T
>
template
<
typename
T
>
auto
sync
(
T
stop
)
{
return
s
td
::
views
::
iota
(
0
,
stop
);
}
auto
sync
(
T
stop
)
{
return
s
ync
(
0
,
stop
);
}
template
<
typename
T
>
template
<
typename
T
>
auto
sync
(
T
start
,
T
stop
)
{
return
std
::
views
::
iota
(
start
,
stop
);
}
auto
sync
(
T
start
,
T
stop
,
T
step
=
1
)
{
// https://www.modernescpp.com/index.php/c-20-pythons-map-function/
if
(
step
==
0
)
{
throw
std
::
invalid_argument
(
"Step cannot be 0"
);
}
auto
Step
=
start
<
stop
?
step
:
-
step
;
auto
Begin
=
std
::
min
(
start
,
stop
);
auto
End
=
Step
<
0
?
Begin
:
std
::
max
(
start
,
stop
);
return
view
::
iota
(
Begin
,
End
)
|
view
::
filter
(
stride
(
std
::
abs
(
Step
)))
|
view
::
transform
([
start
,
stop
](
std
::
size_t
i
){
return
start
<
stop
?
i
:
stop
-
(
i
-
start
);
});
}
}
range
;
}
range
;
#endif // TYPON_RANGE_HPP
#endif // TYPON_RANGE_HPP
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment