Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
2XB3FinalProject
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Releases
Model registry
Analyze
Contributor analytics
Repository analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Christopher Schankula
2XB3FinalProject
Commits
7a8710e8
Commit
7a8710e8
authored
7 years ago
by
Christopher Schankula
Browse files
Options
Downloads
Patches
Plain Diff
range searching works?!?!?!?
parent
fd3aa704
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/sort/GeneralRange.java
+11
-0
11 additions, 0 deletions
src/sort/GeneralRange.java
src/sort/KDT.java
+68
-1
68 additions, 1 deletion
src/sort/KDT.java
src/sort/Range.java
+1
-1
1 addition, 1 deletion
src/sort/Range.java
with
80 additions
and
2 deletions
src/sort/GeneralRange.java
0 → 100644
+
11
−
0
View file @
7a8710e8
package
sort
;
public
interface
GeneralRange
<
T
extends
Comparable
<
T
>>
{
/**
* Compare two Comparable elements based on an arbitrary ordering definition.
* @param a1 The first value to be compared.
* @param a2 The second value to be compared
* @return Integer < 0 if a1 is less than a2, 0 if equal and > 0 if a1 is bigger than a2
*/
public
int
isInBounds
(
T
a1
);
}
This diff is collapsed.
Click to expand it.
src/sort/KDT.java
+
68
−
1
View file @
7a8710e8
...
...
@@ -26,9 +26,28 @@ public class KDT<KeyVal extends Comparable<KeyVal>> {
Point
[]
pts
=
{
p1
,
p2
,
p3
,
p4
,
p5
,
p6
};
KDT
<
Point
>
kdt
=
new
KDT
(
axes
,
pts
);
KDT
<
Point
>
kdt
=
new
KDT
<
Point
>
(
axes
,
pts
);
System
.
out
.
println
(
kdt
.
size
());
System
.
out
.
println
(
kdt
.
height
());
//GeneralRange<Point> xRange = p -> 4 <= p.getX() && p.getX() <= 6;
//GeneralRange<Point> yRange = p -> 3 <= p.getY() && p.getY() <= 5;
GeneralRange
<
Point
>
xRange
=
p
->
p
.
getX
()
<
2
?
-
1
:
(
p
.
getX
()
>
7
?
1
:
0
);
GeneralRange
<
Point
>
yRange
=
p
->
0
;
//p.getY() < 3 ? -1 : (p.getY() > 6 ? 1 : 0);
ArrayList
<
GeneralRange
<
Point
>>
ranges
=
new
ArrayList
<
GeneralRange
<
Point
>>();
ranges
.
add
(
xRange
);
ranges
.
add
(
yRange
);
Iterable
<
Point
>
results
=
kdt
.
rangeSearch
(
ranges
);
System
.
out
.
println
(
"Results"
);
for
(
Point
p
:
results
)
{
System
.
out
.
println
(
p
);
}
System
.
out
.
println
(
kdt
.
toString
());
}
private
class
Node
{
...
...
@@ -66,6 +85,41 @@ public class KDT<KeyVal extends Comparable<KeyVal>> {
return
newNode
;
}
public
Iterable
<
KeyVal
>
rangeSearch
(
ArrayList
<
GeneralRange
<
KeyVal
>>
range
){
ArrayList
<
KeyVal
>
result
=
new
ArrayList
<
KeyVal
>();
rangeSearch
(
root
,
range
,
result
,
0
);
return
result
;
}
private
void
rangeSearch
(
Node
x
,
ArrayList
<
GeneralRange
<
KeyVal
>>
range
,
ArrayList
<
KeyVal
>
result
,
int
depth
)
{
if
(
x
==
null
)
return
;
int
axis
=
depth
%
getK
();
GeneralRange
<
KeyVal
>
rg
=
range
.
get
(
axis
);
System
.
out
.
println
(
"Try: "
+
x
.
keyval
);
int
bounds
=
rg
.
isInBounds
((
KeyVal
)
x
.
keyval
);
if
(
bounds
==
0
)
{
System
.
out
.
println
(
pointInside
(
x
.
keyval
,
range
));
if
(
pointInside
(
x
.
keyval
,
range
))
{
result
.
add
(
x
.
keyval
);
}
rangeSearch
(
x
.
left
,
range
,
result
,
depth
+
1
);
rangeSearch
(
x
.
right
,
range
,
result
,
depth
+
1
);
}
else
if
(
bounds
>
0
)
{
rangeSearch
(
x
.
left
,
range
,
result
,
depth
+
1
);
}
else
if
(
bounds
<
0
)
rangeSearch
(
x
.
right
,
range
,
result
,
depth
+
1
);
return
;
}
private
boolean
pointInside
(
KeyVal
pt
,
ArrayList
<
GeneralRange
<
KeyVal
>>
range
)
{
for
(
int
i
=
0
;
i
<
axes
.
size
();
i
++)
if
(
range
.
get
(
i
).
isInBounds
(
pt
)
!=
0
)
return
false
;
return
true
;
}
public
int
size
()
{
return
size
(
root
);
}
...
...
@@ -87,4 +141,17 @@ public class KDT<KeyVal extends Comparable<KeyVal>> {
public
int
getK
()
{
return
axes
.
size
();
}
public
String
toString
()
{
return
toString
(
root
,
""
);
}
private
String
toString
(
Node
x
,
String
depth
)
{
if
(
x
==
null
)
return
depth
+
"null\n"
;
String
result
=
""
;
result
+=
depth
+
x
.
keyval
.
toString
()
+
"\n"
;
result
+=
toString
(
x
.
left
,
depth
+
" "
);
result
+=
toString
(
x
.
right
,
depth
+
" "
);
return
result
;
}
}
This diff is collapsed.
Click to expand it.
src/sort/Range.java
+
1
−
1
View file @
7a8710e8
...
...
@@ -37,7 +37,7 @@ public class Range<Key extends Comparable<Key>> {
this
.
boundType
=
bt
;
}
public
boolean
inBounds
(
Key
key
,
GeneralCompare
<
Key
>
gc
)
{
public
<
T
>
boolean
inBounds
(
T
key
)
{
if
(
boundType
==
Bound
.
ANY
)
return
true
;
else
if
(
boundType
==
Bound
.
LOWER
)
return
gc
.
compare
(
lower
,
key
)
<=
0
;
else
if
(
boundType
==
Bound
.
UPPER
)
return
gc
.
compare
(
upper
,
key
)
>=
0
;
...
...
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