Skip to content

Commit

Permalink
Broke array writing and comparison into static methods
Browse files Browse the repository at this point in the history
  • Loading branch information
fickludd committed May 31, 2017
1 parent 57b0bbb commit f9feda4
Show file tree
Hide file tree
Showing 14 changed files with 174 additions and 94 deletions.
Expand Up @@ -19,7 +19,7 @@
*/
package org.neo4j.values;

abstract class ArrayValue extends Value
abstract class DirectArray extends Value
{
abstract int length();

Expand Down
Expand Up @@ -23,7 +23,7 @@

import static java.lang.String.format;

final class DirectBooleanArray extends ArrayValue implements ValueGroup.VBooleanArray
final class DirectBooleanArray extends DirectArray implements ValueGroup.VBooleanArray
{
private final boolean[] value;

Expand Down Expand Up @@ -130,12 +130,7 @@ static int hash( boolean[] value )
@Override
void writeTo( ValueWriter writer )
{
writer.beginArray( value.length, ValueWriter.ArrayType.BOOLEAN );
for ( boolean x : value )
{
writer.writeBoolean( x );
}
writer.endArray();
PrimitiveArrayWriting.writeTo( writer, value );
}

@Override
Expand Down
Expand Up @@ -84,12 +84,7 @@ boolean equals( double[] x )
@Override
void writeTo( ValueWriter writer )
{
writer.beginArray( value.length, ValueWriter.ArrayType.BYTE );
for ( byte x : value )
{
writer.writeInteger( x );
}
writer.endArray();
PrimitiveArrayWriting.writeTo( writer, value );
}

@Override
Expand Down
Expand Up @@ -23,7 +23,7 @@

import static java.lang.String.format;

final class DirectCharArray extends ArrayValue
final class DirectCharArray extends DirectArray
{
final char[] value;

Expand Down
Expand Up @@ -84,12 +84,7 @@ boolean equals( double[] x )
@Override
void writeTo( ValueWriter writer )
{
writer.beginArray( value.length, ValueWriter.ArrayType.DOUBLE );
for ( double x : value )
{
writer.writeFloatingPoint( x );
}
writer.endArray();
PrimitiveArrayWriting.writeTo( writer, value );
}

@Override
Expand Down
Expand Up @@ -84,12 +84,7 @@ boolean equals( double[] x )
@Override
void writeTo( ValueWriter writer )
{
writer.beginArray( value.length, ValueWriter.ArrayType.FLOAT );
for ( float x : value )
{
writer.writeFloatingPoint( x );
}
writer.endArray();
PrimitiveArrayWriting.writeTo( writer, value );
}

@Override
Expand Down
Expand Up @@ -19,7 +19,7 @@
*/
package org.neo4j.values;

abstract class DirectFloatingPointArray extends ArrayValue implements ValueGroup.VFloatingPointArray
abstract class DirectFloatingPointArray extends DirectArray implements ValueGroup.VFloatingPointArray
{
public abstract double doubleValue( int index );

Expand Down Expand Up @@ -77,29 +77,11 @@ public final int hashCode()

public int compareTo( ValueGroup.VIntegerArray other )
{
int i = 0;
int length = length();
int x = length - other.length();

while ( x == 0 && i < length )
{
x = NumberValues.compareDoubleAgainstLong( doubleValue( i ), other.longValue( i ) );
i++;
}
return x;
return -NumberValues.compareIntegerVsFloatArrays( other, this );
}

public int compareTo( ValueGroup.VFloatingPointArray other )
{
int i = 0;
int length = length();
int x = length - other.length();

while ( x == 0 && i < length )
{
x = Double.compare( doubleValue( i ), other.doubleValue( i ) );
i++;
}
return x;
return NumberValues.compareFloatArrays( this, other );
}
}
Expand Up @@ -84,12 +84,7 @@ boolean equals( double[] x )
@Override
void writeTo( ValueWriter writer )
{
writer.beginArray( value.length, ValueWriter.ArrayType.INT );
for ( int x : value )
{
writer.writeInteger( x );
}
writer.endArray();
PrimitiveArrayWriting.writeTo( writer, value );
}

@Override
Expand Down
Expand Up @@ -19,7 +19,7 @@
*/
package org.neo4j.values;

abstract class DirectIntegralArray extends ArrayValue implements ValueGroup.VIntegerArray
abstract class DirectIntegralArray extends DirectArray implements ValueGroup.VIntegerArray
{
@Override
boolean equals( boolean[] x )
Expand Down Expand Up @@ -76,30 +76,13 @@ public final int hashCode()
@Override
public int compareTo( ValueGroup.VIntegerArray other )
{
int i = 0;
int length = length();
int x = length - other.length();

while ( x == 0 && i < length )
{
x = Long.compare( longValue( i ), other.longValue( i ) );
i++;
}
return x;
return NumberValues.compareIntegerArrays( this, other );
}

@Override
public int compareTo( ValueGroup.VFloatingPointArray other )
{
int i = 0;
int length = length();
int x = length - other.length();

while ( x == 0 && i < length )
{
x = NumberValues.compareLongAgainstDouble( longValue( i ), other.doubleValue( i ) );
i++;
}
return x;
return NumberValues.compareIntegerVsFloatArrays( this, other );
}

}
Expand Up @@ -84,12 +84,7 @@ boolean equals( double[] x )
@Override
void writeTo( ValueWriter writer )
{
writer.beginArray( value.length, ValueWriter.ArrayType.LONG );
for ( long x : value )
{
writer.writeInteger( x );
}
writer.endArray();
PrimitiveArrayWriting.writeTo( writer, value );
}

@Override
Expand Down
Expand Up @@ -84,12 +84,7 @@ boolean equals( double[] x )
@Override
void writeTo( ValueWriter writer )
{
writer.beginArray( value.length, ValueWriter.ArrayType.SHORT );
for ( short x : value )
{
writer.writeInteger( x );
}
writer.endArray();
PrimitiveArrayWriting.writeTo( writer, value );
}

@Override
Expand Down
Expand Up @@ -23,7 +23,7 @@

import static java.lang.String.format;

final class DirectStringArray extends ArrayValue
final class DirectStringArray extends DirectArray
{
final String[] value;

Expand Down Expand Up @@ -114,12 +114,7 @@ public int hashCode()
@Override
void writeTo( ValueWriter writer )
{
writer.beginArray( value.length, ValueWriter.ArrayType.STRING );
for ( String x : value )
{
writer.writeString( x );
}
writer.endArray();
PrimitiveArrayWriting.writeTo( writer, value );
}

@Override
Expand Down
42 changes: 42 additions & 0 deletions community/values/src/main/java/org/neo4j/values/NumberValues.java
Expand Up @@ -240,4 +240,46 @@ public static boolean numbersEqual( ValueGroup.VFloatingPointArray fps, ValueGro
}
return true;
}

public static int compareIntegerArrays( ValueGroup.VIntegerArray a, ValueGroup.VIntegerArray b )
{
int i = 0;
int length = a.length();
int x = length - b.length();

while ( x == 0 && i < length )
{
x = Long.compare( a.longValue( i ), b.longValue( i ) );
i++;
}
return x;
}

public static int compareIntegerVsFloatArrays( ValueGroup.VIntegerArray a, ValueGroup.VFloatingPointArray b )
{
int i = 0;
int length = a.length();
int x = length - b.length();

while ( x == 0 && i < length )
{
x = compareLongAgainstDouble( a.longValue( i ), b.doubleValue( i ) );
i++;
}
return x;
}

public static int compareFloatArrays( ValueGroup.VFloatingPointArray a, ValueGroup.VFloatingPointArray b )
{
int i = 0;
int length = a.length();
int x = length - b.length();

while ( x == 0 && i < length )
{
x = Double.compare( a.doubleValue( i ), b.doubleValue( i ) );
i++;
}
return x;
}
}
@@ -0,0 +1,113 @@
/*
* Copyright (c) 2002-2017 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.values;

public class PrimitiveArrayWriting
{
public static void writeTo( ValueWriter writer, byte[] values )
{
writer.beginArray( values.length, ValueWriter.ArrayType.BYTE );
for ( byte x : values )
{
writer.writeInteger( x );
}
writer.endArray();
}

public static void writeTo( ValueWriter writer, short[] values )
{
writer.beginArray( values.length, ValueWriter.ArrayType.SHORT );
for ( short x : values )
{
writer.writeInteger( x );
}
writer.endArray();
}

public static void writeTo( ValueWriter writer, int[] values )
{
writer.beginArray( values.length, ValueWriter.ArrayType.INT );
for ( int x : values )
{
writer.writeInteger( x );
}
writer.endArray();
}

public static void writeTo( ValueWriter writer, long[] values )
{
writer.beginArray( values.length, ValueWriter.ArrayType.LONG );
for ( long x : values )
{
writer.writeInteger( x );
}
writer.endArray();
}

public static void writeTo( ValueWriter writer, float[] values )
{
writer.beginArray( values.length, ValueWriter.ArrayType.FLOAT );
for ( float x : values )
{
writer.writeFloatingPoint( x );
}
writer.endArray();
}

public static void writeTo( ValueWriter writer, double[] values )
{
writer.beginArray( values.length, ValueWriter.ArrayType.DOUBLE );
for ( double x : values )
{
writer.writeFloatingPoint( x );
}
writer.endArray();
}

public static void writeTo( ValueWriter writer, boolean[] values )
{
writer.beginArray( values.length, ValueWriter.ArrayType.BOOLEAN );
for ( boolean x : values )
{
writer.writeBoolean( x );
}
writer.endArray();
}

public static void writeTo( ValueWriter writer, char[] values )
{
writer.beginArray( values.length, ValueWriter.ArrayType.CHAR );
for ( char x : values )
{
writer.writeString( x );
}
writer.endArray();
}

public static void writeTo( ValueWriter writer, String[] values )
{
writer.beginArray( values.length, ValueWriter.ArrayType.STRING );
for ( String x : values )
{
writer.writeString( x );
}
writer.endArray();
}
}

0 comments on commit f9feda4

Please sign in to comment.