Skip to content

Commit

Permalink
Break out sorted int set logic to own class
Browse files Browse the repository at this point in the history
  • Loading branch information
thobe committed Mar 24, 2017
1 parent 892a248 commit 334ecd1
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 80 deletions.
@@ -0,0 +1,93 @@
/*
* 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.collection.primitive;

import java.util.Arrays;

public class PrimitiveSortedArraySet
{
/**
* Merges two sets of integers represented as sorted arrays.
*
* @param lhs
* a set of integers, represented as a sorted array.
* @param rhs
* a set of integers, represented as a sorted array.
* @return a set of integers, represented as a sorted array.
*/
public static int[] mergeSortedSet( int[] lhs, int[] rhs )
{
if ( lhs.length < rhs.length )
{
return mergeSortedSet( rhs, lhs );
}
int[] merged = null;
int m = 0;
for ( int l = 0, r = 0; l < lhs.length && r < rhs.length; )
{
while ( l < lhs.length && lhs[l] < rhs[r] )
{
if ( merged != null )
{
merged[m++] = lhs[l];
}
l++;
}
if ( l == lhs.length )
{
if ( merged == null )
{
merged = Arrays.copyOf( lhs, lhs.length + rhs.length - r );
m = l;
}
System.arraycopy( rhs, r, merged, m, rhs.length - r );
m += rhs.length - r;
break;
}
if ( lhs[l] > rhs[r] )
{
if ( merged == null )
{
merged = Arrays.copyOf( lhs, lhs.length + rhs.length - r );
m = l;
}
merged[m++] = rhs[r++];
}
else // i.e. ( lhs[l] == rhs[r] )
{
if ( merged != null )
{
merged[m++] = lhs[l];
}
l++;
r++;
}
}
if ( merged == null )
{
return lhs;
}
if ( merged.length < m )
{
merged = Arrays.copyOf( merged, m );
}
return merged;
}
}
Expand Up @@ -5,19 +5,19 @@
* This file is part of Neo4j. * This file is part of Neo4j.
* *
* Neo4j is free software: you can redistribute it and/or modify * Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as * it under the terms of the GNU General Public License as published by
* published by the Free Software Foundation, either version 3 of the * the Free Software Foundation, either version 3 of the License, or
* License, or (at your option) any later version. * (at your option) any later version.
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU Affero General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package org.neo4j.kernel.impl.enterprise; package org.neo4j.collection.primitive;


import java.util.Arrays; import java.util.Arrays;


Expand All @@ -27,10 +27,10 @@


import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertSame; import static org.junit.Assert.assertSame;
import static org.neo4j.kernel.impl.enterprise.PropertyExistenceEnforcer.merge; import static org.neo4j.collection.primitive.PrimitiveSortedArraySet.mergeSortedSet;


@RunWith( Parameterized.class ) @RunWith( Parameterized.class )
public class PropertyExistenceEnforcerMergeTest public class PrimitiveSortedArraySetTest
{ {
@Parameterized.Parameters( name = "{0}" ) @Parameterized.Parameters( name = "{0}" )
public static Iterable<Object[]> parameters() public static Iterable<Object[]> parameters()
Expand All @@ -53,7 +53,7 @@ public static Iterable<Object[]> parameters()
private final int[] rhs; private final int[] rhs;
private final int[] expected; private final int[] expected;


public PropertyExistenceEnforcerMergeTest( Input input ) public PrimitiveSortedArraySetTest( Input input )
{ {
this.lhs = input.lhs; this.lhs = input.lhs;
this.rhs = input.rhs; this.rhs = input.rhs;
Expand All @@ -63,7 +63,7 @@ public PropertyExistenceEnforcerMergeTest( Input input )
@Test @Test
public void testMerge() throws Exception public void testMerge() throws Exception
{ {
int[] actual = merge( lhs, rhs ); int[] actual = mergeSortedSet( lhs, rhs );
if ( lhs == expected || rhs == expected ) if ( lhs == expected || rhs == expected )
{ {
assertSame( expected, actual ); assertSame( expected, actual );
Expand Down
Expand Up @@ -50,6 +50,7 @@


import static java.lang.String.format; import static java.lang.String.format;
import static java.util.Collections.emptyList; import static java.util.Collections.emptyList;
import static org.neo4j.collection.primitive.PrimitiveSortedArraySet.mergeSortedSet;
import static org.neo4j.kernel.api.exceptions.schema.ConstraintValidationException.Phase.VALIDATION; import static org.neo4j.kernel.api.exceptions.schema.ConstraintValidationException.Phase.VALIDATION;


class PropertyExistenceEnforcer class PropertyExistenceEnforcer
Expand Down Expand Up @@ -84,7 +85,7 @@ private static void update( PrimitiveIntObjectMap<int[]> map, int key, int[] val
int[] current = map.get( key ); int[] current = map.get( key );
if ( current != null ) if ( current != null )
{ {
values = merge( current, values ); values = mergeSortedSet( current, values );
} }
map.put( key, values ); map.put( key, values );
} }
Expand Down Expand Up @@ -373,74 +374,6 @@ private void failRelationship( long id, int relationshipType, int propertyKey )
relationshipType, propertyKey ) ); relationshipType, propertyKey ) );
} }


/**
* Merges two sets of integers represented as sorted arrays.
*
* @param lhs
* a set of integers, represented as a sorted array.
* @param rhs
* a set of integers, represented as a sorted array.
* @return a set of integers, represented as a sorted array.
*/
static int[] merge( int[] lhs, int[] rhs )
{
if ( lhs.length < rhs.length )
{
return merge( rhs, lhs );
}
int[] merged = null;
int m = 0;
for ( int l = 0, r = 0; l < lhs.length && r < rhs.length; )
{
while ( l < lhs.length && lhs[l] < rhs[r] )
{
if ( merged != null )
{
merged[m++] = lhs[l];
}
l++;
}
if ( l == lhs.length )
{
if ( merged == null )
{
merged = Arrays.copyOf( lhs, lhs.length + rhs.length - r );
m = l;
}
System.arraycopy( rhs, r, merged, m, rhs.length - r );
m += rhs.length - r;
break;
}
if ( lhs[l] > rhs[r] )
{
if ( merged == null )
{
merged = Arrays.copyOf( lhs, lhs.length + rhs.length - r );
m = l;
}
merged[m++] = rhs[r++];
}
else // i.e. ( lhs[l] == rhs[r] )
{
if ( merged != null )
{
merged[m++] = lhs[l];
}
l++;
r++;
}
}
if ( merged == null )
{
return lhs;
}
if ( merged.length < m )
{
merged = Arrays.copyOf( merged, m );
}
return merged;
}

private boolean contains( int[] list, int value ) private boolean contains( int[] list, int value )
{ {
for ( int x : list ) for ( int x : list )
Expand Down

0 comments on commit 334ecd1

Please sign in to comment.