Skip to content

Commit

Permalink
Use enum instead of strings as keys
Browse files Browse the repository at this point in the history
  • Loading branch information
OliviaYtterbrink committed Jan 16, 2018
1 parent 9a774c4 commit aa6a431
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 38 deletions.
Expand Up @@ -19,11 +19,15 @@
*/ */
package org.neo4j.gis.spatial.index.curves; package org.neo4j.gis.spatial.index.curves;


import java.util.HashMap; import java.util.EnumMap;
import java.util.LinkedHashMap;


import org.neo4j.gis.spatial.index.Envelope; import org.neo4j.gis.spatial.index.Envelope;


import static org.neo4j.gis.spatial.index.curves.HilbertSpaceFillingCurve2D.Direction2D.DOWN;
import static org.neo4j.gis.spatial.index.curves.HilbertSpaceFillingCurve2D.Direction2D.LEFT;
import static org.neo4j.gis.spatial.index.curves.HilbertSpaceFillingCurve2D.Direction2D.RIGHT;
import static org.neo4j.gis.spatial.index.curves.HilbertSpaceFillingCurve2D.Direction2D.UP;

public class HilbertSpaceFillingCurve2D extends SpaceFillingCurve public class HilbertSpaceFillingCurve2D extends SpaceFillingCurve
{ {


Expand All @@ -40,28 +44,28 @@ private HilbertCurve2D( int... npointValues )
assert npointValues[0] == 0 || npointValues[0] == 3; assert npointValues[0] == 0 || npointValues[0] == 3;
} }


public char direction( int end ) private Direction2D direction( int end )
{ {
int start = npointValues[0]; int start = npointValues[0];
end -= start; end -= start;
switch ( end ) switch ( end )
{ {
case 1: case 1:
return 'U'; // move up 00->01 return UP; // move up 00->01
case 2: case 2:
return 'R'; // move right 00->10 return RIGHT; // move right 00->10
case -2: case -2:
return 'L'; // move left 11->01 return LEFT; // move left 11->01
case -1: case -1:
return 'D'; // move down 11->10 return DOWN; // move down 11->10
default: default:
return '-'; throw new IllegalArgumentException("Illegal direction: " + end);
} }
} }


String name() private Direction2D name()
{ {
return String.valueOf( direction( npointValues[1] ) ); return direction( npointValues[1] );
} }


private void setChildren( CurveRule... children ) private void setChildren( CurveRule... children )
Expand All @@ -78,23 +82,28 @@ public CurveRule childAt( int npoint )
@Override @Override
public String toString() public String toString()
{ {
return name(); return String.valueOf( name() );
} }
} }


private static HashMap<String,HilbertCurve2D> curves = new LinkedHashMap<>(); enum Direction2D
{
UP, RIGHT, LEFT, DOWN
}

private static EnumMap<Direction2D,HilbertCurve2D> curves = new EnumMap<>( Direction2D.class );


private static void addCurveRule( int... npointValues ) private static void addCurveRule( int... npointValues )
{ {
HilbertCurve2D curve = new HilbertCurve2D( npointValues ); HilbertCurve2D curve = new HilbertCurve2D( npointValues );
String name = curve.name(); Direction2D name = curve.name();
if ( !curves.containsKey( name ) ) if ( !curves.containsKey( name ) )
{ {
curves.put( name, curve ); curves.put( name, curve );
} }
} }


private static void setChildren( String parent, String... children ) private static void setChildren( Direction2D parent, Direction2D... children )
{ {
HilbertCurve2D curve = curves.get( parent ); HilbertCurve2D curve = curves.get( parent );
HilbertCurve2D[] childCurves = new HilbertCurve2D[children.length]; HilbertCurve2D[] childCurves = new HilbertCurve2D[children.length];
Expand All @@ -113,11 +122,11 @@ private static void setChildren( String parent, String... children )
addCurveRule( 0, 2, 3, 1 ); addCurveRule( 0, 2, 3, 1 );
addCurveRule( 3, 1, 0, 2 ); addCurveRule( 3, 1, 0, 2 );
addCurveRule( 3, 2, 0, 1 ); addCurveRule( 3, 2, 0, 1 );
setChildren( "U", "R", "U", "U", "L" ); setChildren( UP, RIGHT, UP, UP, LEFT );
setChildren( "R", "U", "R", "R", "D" ); setChildren( RIGHT, UP, RIGHT, RIGHT, DOWN );
setChildren( "D", "L", "D", "D", "R" ); setChildren( DOWN, LEFT, DOWN, DOWN, RIGHT );
setChildren( "L", "D", "L", "L", "U" ); setChildren( LEFT, DOWN, LEFT, LEFT, UP );
curveUp = curves.get( "U" ); curveUp = curves.get( UP );
} }


public static final int MAX_LEVEL = 63 / 2 - 1; public static final int MAX_LEVEL = 63 / 2 - 1;
Expand Down
Expand Up @@ -24,6 +24,13 @@


import org.neo4j.gis.spatial.index.Envelope; import org.neo4j.gis.spatial.index.Envelope;


import static org.neo4j.gis.spatial.index.curves.HilbertSpaceFillingCurve3D.Direction3D.BACK;
import static org.neo4j.gis.spatial.index.curves.HilbertSpaceFillingCurve3D.Direction3D.DOWN;
import static org.neo4j.gis.spatial.index.curves.HilbertSpaceFillingCurve3D.Direction3D.FRONT;
import static org.neo4j.gis.spatial.index.curves.HilbertSpaceFillingCurve3D.Direction3D.LEFT;
import static org.neo4j.gis.spatial.index.curves.HilbertSpaceFillingCurve3D.Direction3D.RIGHT;
import static org.neo4j.gis.spatial.index.curves.HilbertSpaceFillingCurve3D.Direction3D.UP;

public class HilbertSpaceFillingCurve3D extends SpaceFillingCurve public class HilbertSpaceFillingCurve3D extends SpaceFillingCurve
{ {


Expand Down Expand Up @@ -86,32 +93,33 @@ static String binaryString( int value )
return binary.substring( binary.length() - 3, binary.length() ); return binary.substring( binary.length() - 3, binary.length() );
} }


private char direction( int start, int end ) private Direction3D direction( int start, int end )
{ {
end -= start; end -= start;
switch ( end ) switch ( end )
{ {
case 1: case 1:
return 'F'; // move forward 000->001 return FRONT; // move forward 000->001
case 2: case 2:
return 'U'; // move up 000->010 return UP; // move up 000->010
case 4: case 4:
return 'R'; // move right 000->100 return RIGHT; // move right 000->100
case -4: case -4:
return 'L'; // move left 111->011 return LEFT; // move left 111->011
case -2: case -2:
return 'D'; // move down 111->101 return DOWN; // move down 111->101
case -1: case -1:
return 'B'; // move back 111->110 return BACK; // move back 111->110
default: default:
return '-'; throw new IllegalArgumentException("Illegal direction: " + end);
} }
} }


String name() SubCurve3D name()
{ {
return String.valueOf( direction( npointValues[0], npointValues[1] ) ) + direction( npointValues[1], npointValues[2] ) + return new SubCurve3D( direction( npointValues[0], npointValues[1] ),
direction( npointValues[0], npointValues[length() - 1] ); direction( npointValues[1], npointValues[2] ),
direction( npointValues[0], npointValues[length() - 1] ));
} }


/** /**
Expand Down Expand Up @@ -175,7 +183,7 @@ private HilbertCurve3D rotateAboutX()


private HilbertCurve3D singleTon( HilbertCurve3D curve ) private HilbertCurve3D singleTon( HilbertCurve3D curve )
{ {
String name = curve.name(); SubCurve3D name = curve.name();
if ( curves.containsKey( name ) ) if ( curves.containsKey( name ) )
{ {
return curves.get( name ); return curves.get( name );
Expand Down Expand Up @@ -213,16 +221,58 @@ public CurveRule childAt( int npoint )
@Override @Override
public String toString() public String toString()
{ {
return name(); return name().toString();
}
}

enum Direction3D
{
UP, RIGHT, LEFT, DOWN, FRONT, BACK;
}

static class SubCurve3D
{
private final Direction3D firstMove;
private final Direction3D secondMove;
private final Direction3D overallDirection;

SubCurve3D( Direction3D firstMove, Direction3D secondMove, Direction3D overallDirection )
{
this.firstMove = firstMove;
this.secondMove = secondMove;
this.overallDirection = overallDirection;
}

@Override
public int hashCode()
{
return java.util.Objects.hash( firstMove, secondMove, overallDirection );
}

@Override
public boolean equals( Object obj )
{
if ( obj == null || this.getClass() != obj.getClass() )
{
return false;
}
SubCurve3D other = (SubCurve3D) obj;
return this.firstMove == other.firstMove && this.secondMove == other.secondMove && this.overallDirection == other.overallDirection;
}

@Override
public String toString()
{
return firstMove.toString() + secondMove.toString() + overallDirection.toString();
} }
} }


static HashMap<String,HilbertCurve3D> curves = new LinkedHashMap<>(); static HashMap<SubCurve3D,HilbertCurve3D> curves = new LinkedHashMap<>();


private static HilbertCurve3D addCurveRule( int... npointValues ) private static HilbertCurve3D addCurveRule( int... npointValues )
{ {
HilbertCurve3D curve = new HilbertCurve3D( npointValues ); HilbertCurve3D curve = new HilbertCurve3D( npointValues );
String name = curve.name(); SubCurve3D name = curve.name();
if ( !curves.containsKey( name ) ) if ( !curves.containsKey( name ) )
{ {
curves.put( name, curve ); curves.put( name, curve );
Expand Down
Expand Up @@ -35,6 +35,8 @@


import org.neo4j.gis.spatial.index.Envelope; import org.neo4j.gis.spatial.index.Envelope;
import org.neo4j.gis.spatial.index.curves.HilbertSpaceFillingCurve3D.HilbertCurve3D; import org.neo4j.gis.spatial.index.curves.HilbertSpaceFillingCurve3D.HilbertCurve3D;
import org.neo4j.gis.spatial.index.curves.HilbertSpaceFillingCurve3D.SubCurve3D;

import static org.neo4j.gis.spatial.index.curves.HilbertSpaceFillingCurve3D.BinaryCoordinateRotationUtils3D.rotateNPointLeft; import static org.neo4j.gis.spatial.index.curves.HilbertSpaceFillingCurve3D.BinaryCoordinateRotationUtils3D.rotateNPointLeft;
import static org.neo4j.gis.spatial.index.curves.HilbertSpaceFillingCurve3D.BinaryCoordinateRotationUtils3D.rotateNPointRight; import static org.neo4j.gis.spatial.index.curves.HilbertSpaceFillingCurve3D.BinaryCoordinateRotationUtils3D.rotateNPointRight;


Expand Down Expand Up @@ -676,11 +678,11 @@ private void populateChildren( SpaceFillingCurve.CurveRule c, int level )


private void printMapping() private void printMapping()
{ {
HashMap<Integer,Map<String,HilbertCurve3D>> map = new HashMap<>(); HashMap<Integer,Map<SubCurve3D,HilbertCurve3D>> map = new HashMap<>();
for ( Map.Entry<String,HilbertCurve3D> entry : HilbertSpaceFillingCurve3D.curves.entrySet() ) for ( Map.Entry<SubCurve3D,HilbertCurve3D> entry : HilbertSpaceFillingCurve3D.curves.entrySet() )
{ {
int start = entry.getValue().npointForIndex( 0 ); int start = entry.getValue().npointForIndex( 0 );
Map<String,HilbertCurve3D> mapEntry; Map<SubCurve3D,HilbertCurve3D> mapEntry;
if ( map.containsKey( start ) ) if ( map.containsKey( start ) )
{ {
mapEntry = map.get( start ); mapEntry = map.get( start );
Expand All @@ -698,7 +700,7 @@ private void printMapping()
for ( Integer start : sortedKeys ) for ( Integer start : sortedKeys )
{ {
System.out.println( HilbertCurve3D.binaryString( start ) + ":\t" + map.get( start ).size() ); System.out.println( HilbertCurve3D.binaryString( start ) + ":\t" + map.get( start ).size() );
for ( Map.Entry<String,HilbertCurve3D> mapEntry : map.get( start ).entrySet() ) for ( Map.Entry<SubCurve3D,HilbertCurve3D> mapEntry : map.get( start ).entrySet() )
{ {
System.out.println( "\t" + mapEntry.getKey() + ":\t" + Arrays.toString( mapEntry.getValue().children ) ); System.out.println( "\t" + mapEntry.getKey() + ":\t" + Arrays.toString( mapEntry.getValue().children ) );
} }
Expand Down

0 comments on commit aa6a431

Please sign in to comment.