Skip to content

Commit

Permalink
Move slice to ListValue
Browse files Browse the repository at this point in the history
  • Loading branch information
pontusmelke committed May 27, 2018
1 parent 9c62954 commit ecf4f6e
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 27 deletions.
Expand Up @@ -19,9 +19,8 @@
*/ */
package org.neo4j.cypher.internal.runtime.interpreted.commands.expressions package org.neo4j.cypher.internal.runtime.interpreted.commands.expressions


import org.neo4j.cypher.internal.runtime.interpreted.ExecutionContext
import org.neo4j.cypher.internal.runtime.interpreted.{CastSupport, ListSupport}
import org.neo4j.cypher.internal.runtime.interpreted.pipes.QueryState import org.neo4j.cypher.internal.runtime.interpreted.pipes.QueryState
import org.neo4j.cypher.internal.runtime.interpreted.{CastSupport, ExecutionContext, ListSupport}
import org.neo4j.values.AnyValue import org.neo4j.values.AnyValue
import org.neo4j.values.storable.{NumberValue, Values} import org.neo4j.values.storable.{NumberValue, Values}
import org.neo4j.values.virtual.{ListValue, VirtualValues} import org.neo4j.values.virtual.{ListValue, VirtualValues}
Expand All @@ -47,17 +46,17 @@ case class ListSlice(collection: Expression, from: Option[Expression], to: Optio
case (Some(fromValue), Some(toValue)) => case (Some(fromValue), Some(toValue)) =>
val size = collectionValue.size val size = collectionValue.size
if (fromValue >= 0 && toValue >= 0) if (fromValue >= 0 && toValue >= 0)
VirtualValues.slice(collectionValue, fromValue, toValue) collectionValue.slice(fromValue, toValue)
else if (fromValue >= 0) { else if (fromValue >= 0) {
val end = size + toValue val end = size + toValue
VirtualValues.slice(collectionValue, fromValue, end) collectionValue.slice(fromValue, end)
} else if (toValue >= 0) { } else if (toValue >= 0) {
val start = size + fromValue val start = size + fromValue
VirtualValues.slice(collectionValue, start, toValue) collectionValue.slice(start, toValue)
} else { } else {
val start = size + fromValue val start = size + fromValue
val end = size + toValue val end = size + toValue
VirtualValues.slice(collectionValue, start, end) collectionValue.slice(start, end)
} }
} }
} }
Expand Down
Expand Up @@ -35,6 +35,7 @@


import static org.neo4j.values.storable.Values.NO_VALUE; import static org.neo4j.values.storable.Values.NO_VALUE;
import static org.neo4j.values.virtual.ArrayHelpers.containsNull; import static org.neo4j.values.virtual.ArrayHelpers.containsNull;
import static org.neo4j.values.virtual.VirtualValues.EMPTY_LIST;


public abstract class ListValue extends VirtualValue implements SequenceValue, Iterable<AnyValue> public abstract class ListValue extends VirtualValue implements SequenceValue, Iterable<AnyValue>
{ {
Expand Down Expand Up @@ -896,4 +897,18 @@ public ListValue dropNoValues()
{ {
return new DropNoValuesListValue( this ); return new DropNoValuesListValue( this );
} }

public ListValue slice( int from, int to )
{
int f = Math.max( from, 0 );
int t = Math.min( to, size() );
if ( f > t )
{
return EMPTY_LIST;
}
else
{
return new ListSlice( this, f, t );
}
}
} }
Expand Up @@ -63,20 +63,6 @@ public static ListValue fromArray( ArrayValue arrayValue )
return new ListValue.ArrayValueListValue( arrayValue ); return new ListValue.ArrayValueListValue( arrayValue );
} }


public static ListValue slice( ListValue list, int from, int to )
{
int f = Math.max( from, 0 );
int t = Math.min( to, list.size() );
if ( f > t )
{
return EMPTY_LIST;
}
else
{
return new ListValue.ListSlice( list, f, t );
}
}

public static ListValue drop( ListValue list, int n ) public static ListValue drop( ListValue list, int n )
{ {
int start = Math.max( 0, Math.min( n, list.size() ) ); int start = Math.max( 0, Math.min( n, list.size() ) );
Expand Down
Expand Up @@ -27,7 +27,6 @@
import static org.neo4j.values.virtual.VirtualValues.EMPTY_LIST; import static org.neo4j.values.virtual.VirtualValues.EMPTY_LIST;
import static org.neo4j.values.virtual.VirtualValues.drop; import static org.neo4j.values.virtual.VirtualValues.drop;
import static org.neo4j.values.virtual.VirtualValues.list; import static org.neo4j.values.virtual.VirtualValues.list;
import static org.neo4j.values.virtual.VirtualValues.slice;
import static org.neo4j.values.virtual.VirtualValues.take; import static org.neo4j.values.virtual.VirtualValues.take;


public class ListSliceTest public class ListSliceTest
Expand All @@ -40,7 +39,7 @@ public void shouldSliceList()
longValue( 8L ), longValue( 9L ), longValue( 10L ), longValue( 11L ) ); longValue( 8L ), longValue( 9L ), longValue( 10L ), longValue( 11L ) );


// When // When
ListValue slice = slice( inner, 2, 4 ); ListValue slice = inner.slice(2, 4 );


// Then // Then
ListValue expected = list( longValue( 7L ), longValue( 8L ) ); ListValue expected = list( longValue( 7L ), longValue( 8L ) );
Expand All @@ -57,7 +56,7 @@ public void shouldReturnEmptyListIfEmptyRange()
longValue( 8L ), longValue( 9L ), longValue( 10L ), longValue( 11L ) ); longValue( 8L ), longValue( 9L ), longValue( 10L ), longValue( 11L ) );


// When // When
ListValue slice = slice( inner, 4, 2 ); ListValue slice = inner.slice( 4, 2 );


// Then // Then
assertEquals( slice, EMPTY_LIST ); assertEquals( slice, EMPTY_LIST );
Expand All @@ -71,7 +70,7 @@ public void shouldHandleExceedingRange()
longValue( 8L ), longValue( 9L ), longValue( 10L ), longValue( 11L ) ); longValue( 8L ), longValue( 9L ), longValue( 10L ), longValue( 11L ) );


// When // When
ListValue slice = slice( inner, 2, 400000 ); ListValue slice = inner.slice( 2, 400000 );


// Then // Then
ListValue expected = ListValue expected =
Expand All @@ -89,7 +88,7 @@ public void shouldHandleNegativeStart()
longValue( 8L ), longValue( 9L ), longValue( 10L ), longValue( 11L ) ); longValue( 8L ), longValue( 9L ), longValue( 10L ), longValue( 11L ) );


// When // When
ListValue slice = slice( inner, -2, 400000 ); ListValue slice = inner.slice( -2, 400000 );


// Then // Then
assertEquals( inner, slice ); assertEquals( inner, slice );
Expand Down
Expand Up @@ -225,7 +225,7 @@ public void shouldRecurseAndCoerce()
longValue( 4L ), longValue( 4L ),
longValue( 7L ), longValue( 7L ),
NO_VALUE ).dropNoValues(), NO_VALUE ).dropNoValues(),
VirtualValues.slice( list( -2L, 1L, 4L, 7L, 10L ), 1, 4 ), list( -2L, 1L, 4L, 7L, 10L ).slice( 1, 4 ),
VirtualValues.drop( list( -2L, 1L, 4L, 7L ), 1 ), VirtualValues.drop( list( -2L, 1L, 4L, 7L ), 1 ),
VirtualValues.take( list( 1L, 4L, 7L, 10L, 13L ), 3 ), VirtualValues.take( list( 1L, 4L, 7L, 10L, 13L ), 3 ),
VirtualValues.reverse( list( 7L, 4L, 1L ) ), VirtualValues.reverse( list( 7L, 4L, 1L ) ),
Expand All @@ -243,7 +243,7 @@ public void shouldRecurseAndCoerce()
longValue( 5L ), longValue( 5L ),
longValue( 7L ), longValue( 7L ),
NO_VALUE ).dropNoValues(), NO_VALUE ).dropNoValues(),
VirtualValues.slice( list( -2L, 1L, 5L, 8L, 11L ), 1, 4 ), list( -2L, 1L, 5L, 8L, 11L ).slice( 1, 4 ),
VirtualValues.drop( list( -2L, 6L, 9L, 12L ), 1 ), VirtualValues.drop( list( -2L, 6L, 9L, 12L ), 1 ),
VirtualValues.take( list( 7L, 10L, 13L, 10L, 13L ), 3 ), VirtualValues.take( list( 7L, 10L, 13L, 10L, 13L ), 3 ),
VirtualValues.reverse( list( 15L, 12L, 9L ) ), VirtualValues.reverse( list( 15L, 12L, 9L ) ),
Expand Down

0 comments on commit ecf4f6e

Please sign in to comment.