Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cython_plus_experiments
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
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Julien Jerphanion
cython_plus_experiments
Commits
7516a324
Commit
7516a324
authored
Apr 15, 2021
by
Julien Jerphanion
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[WIP] KDTree implementation
parent
85f51247
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
60 additions
and
27 deletions
+60
-27
kdtree/main.pyx
kdtree/main.pyx
+60
-27
No files found.
kdtree/main.pyx
View file @
7516a324
# distutils: language = c++
from
cython.view
cimport
array
as
cvarray
import
numpy
as
np
from
libcythonplus.list
cimport
cyplist
from
runtime.runtime
cimport
BatchMailBox
,
NullResult
,
Scheduler
from
libc.stdio
cimport
(
fprintf
,
fopen
,
fclose
,
fread
,
fwrite
,
FILE
,
stdout
,
printf
,
ferror
)
from
libc.stdlib
cimport
malloc
,
free
from
stdlib.stat
cimport
Stat
,
dev_t
from
stdlib.fmt
cimport
sprintf
from
stdlib.string
cimport
string
...
...
@@ -19,57 +24,85 @@ cdef lock Scheduler scheduler
cdef
cypclass
Node
activable
:
"""A KDTree Node"""
cyplist
[
int
]
point
active
Node
left
_child
active
Node
right
_child
double
[:
]
point
active
Node
left
active
Node
right
__init__
(
self
):
self
.
_active_result_class
=
NullResult
self
.
_active_queue_class
=
consume
BatchMailBox
(
scheduler
)
self
.
left_child
=
NULL
self
.
right_child
=
NULL
void
build_node
(
self
,
lock
cyplist
[
cyplist
[
int
]]
points
,
int
depth
):
self
.
left
=
NULL
self
.
right
=
NULL
void
build_node
(
self
,
double
*
points
,
int
n
,
int
depth
,
int
dims
,
int
dim_for_split
):
cdef
int
i
if
(
depth
<
0
):
return
printf
(
"Depth %d
\
n
"
,
depth
)
self
.
left_child
=
activate
(
consume
Node
())
self
.
right_child
=
activate
(
consume
Node
())
printf
(
"Dim %d
\
n
"
,
dim_for_split
)
for
i
in
range
(
n
):
printf
(
"X[%d, %d] = %f
\
n
"
,
i
,
dim_for_split
,
points
[
i
*
dims
+
dim_for_split
])
self
.
left_child
.
build_node
(
NULL
,
consume
points
,
depth
-
1
)
self
.
right_child
.
build_node
(
NULL
,
consume
points
,
depth
-
1
)
printf
(
"
\
n
"
)
# TODO: find a way to partitions indices here
self
.
left
=
activate
(
consume
Node
())
self
.
right
=
activate
(
consume
Node
())
cdef
int
start
(
string
path
)
nogil
:
global
scheduler
scheduler
=
Scheduler
()
points
=
cyplist
[
cyplist
[
int
]]()
self
.
left
.
build_node
(
NULL
,
points
,
depth
-
1
,
n
,
dims
,
(
dim_for_split
+
1
)
%
dims
)
self
.
right
.
build_node
(
NULL
,
points
,
depth
-
1
,
n
,
dims
,
(
dim_for_split
+
1
%
dims
))
# TODO: integrate here
# [7, 2],
# [5, 4],
# [9, 6],
# [4, 7],
# [8, 1],
# [2, 3]
cdef
int
start
()
nogil
:
global
scheduler
scheduler
=
Scheduler
()
cdef
int
i
cdef
int
n
=
12
cdef
int
d
=
2
# TODO: use memory view for convenience
# cdef double p[12][2]
# cdef double [:, ::1] points_views = p
# Use Golden Spiral for the layout
cdef
double
golden_ratio
=
(
1
+
5
**
0.5
)
/
2
cdef
double
*
points
=
<
double
*>
malloc
(
n
*
d
*
sizeof
(
double
))
for
i
in
range
(
n
):
points
[
i
*
d
]
=
(
i
/
golden_ratio
)
%
1
points
[
i
*
d
+
1
]
=
i
/
n
printf
(
"Before
\
n
"
)
node
=
consume
Node
()
if
node
is
NULL
:
return
-
1
root
=
activate
(
consume
node
)
root
.
build_node
(
NULL
,
consume
points
,
4
)
root
.
build_node
(
NULL
,
points
,
n
,
5
,
d
,
0
)
scheduler
.
finish
()
del
scheduler
free
(
points
)
return
0
cdef
public
int
main
()
nogil
:
return
start
(
<
char
*>
'.'
)
return
start
()
def
python_main
():
start
(
<
char
*>
'.'
)
start
()
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