Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce primitive long list and use it in a places where List<Long> was used before.
- Loading branch information
1 parent
8c92dda
commit 77514cc
Showing
7 changed files
with
323 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
...primitive-collections/src/main/java/org/neo4j/collection/primitive/PrimitiveLongList.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
/* | ||
* 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; | ||
|
||
/** | ||
* List implementation that holds primitive longs in array that grows on demand. | ||
*/ | ||
public class PrimitiveLongList implements PrimitiveLongCollection | ||
{ | ||
private static final int DEFAULT_SIZE = 8; | ||
|
||
private long[] elements = PrimitiveLongCollections.EMPTY_LONG_ARRAY; | ||
private int size; | ||
|
||
PrimitiveLongList() | ||
{ | ||
this( DEFAULT_SIZE ); | ||
} | ||
|
||
PrimitiveLongList( int size ) | ||
{ | ||
elements = new long[size]; | ||
} | ||
|
||
public void add( long element ) | ||
{ | ||
if ( elements.length == size ) | ||
{ | ||
ensureCapacity(); | ||
} | ||
elements[size++] = element; | ||
} | ||
|
||
public long get( int position ) | ||
{ | ||
if ( position >= size ) | ||
{ | ||
throw new IndexOutOfBoundsException( "Requested element: " + position + ", list size: " + size ); | ||
} | ||
return elements[position]; | ||
} | ||
|
||
@Override | ||
public boolean isEmpty() | ||
{ | ||
return size == 0; | ||
} | ||
|
||
@Override | ||
public void clear() | ||
{ | ||
size = 0; | ||
elements = PrimitiveLongCollections.EMPTY_LONG_ARRAY; | ||
} | ||
|
||
@Override | ||
public int size() | ||
{ | ||
return size; | ||
} | ||
|
||
@Override | ||
public void close() | ||
{ | ||
clear(); | ||
} | ||
|
||
@Override | ||
public PrimitiveLongIterator iterator() | ||
{ | ||
return new PrimitiveLongListIterator(); | ||
} | ||
|
||
@Override | ||
public <E extends Exception> void visitKeys( PrimitiveLongVisitor<E> visitor ) throws E | ||
{ | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
public long[] toArray() | ||
{ | ||
return Arrays.copyOf( elements, size ); | ||
} | ||
|
||
private void ensureCapacity() | ||
{ | ||
int newCapacity = elements.length << 1; | ||
if ( newCapacity < 0 ) | ||
{ | ||
throw new IllegalStateException( "Fail to increase list capacity." ); | ||
} | ||
elements = Arrays.copyOf( elements, newCapacity ); | ||
} | ||
|
||
private class PrimitiveLongListIterator implements PrimitiveLongIterator | ||
{ | ||
int cursor; | ||
|
||
@Override | ||
public boolean hasNext() | ||
{ | ||
return cursor < size; | ||
} | ||
|
||
@Override | ||
public long next() | ||
{ | ||
return elements[cursor++]; | ||
} | ||
} | ||
} |
156 changes: 156 additions & 0 deletions
156
...itive-collections/src/test/java/org/neo4j/collection/primitive/PrimitiveLongListTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
/* | ||
* 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 org.junit.Test; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
public class PrimitiveLongListTest | ||
{ | ||
|
||
@Test | ||
public void newListIsEmpty() | ||
{ | ||
assertTrue( new PrimitiveLongList().isEmpty() ); | ||
assertTrue( new PrimitiveLongList( 12 ).isEmpty() ); | ||
} | ||
|
||
@Test | ||
public void newListHasZeroSize() | ||
{ | ||
assertEquals( 0, new PrimitiveLongList().size() ); | ||
assertEquals( 0, new PrimitiveLongList( 12 ).size() ); | ||
} | ||
|
||
@Test | ||
public void addingElementsChangeSize() | ||
{ | ||
PrimitiveLongList longList = new PrimitiveLongList(); | ||
longList.add( 1L ); | ||
|
||
assertFalse( longList.isEmpty() ); | ||
assertEquals( 1, longList.size() ); | ||
|
||
longList.add( 2L ); | ||
assertFalse( longList.isEmpty() ); | ||
assertEquals( 2, longList.size() ); | ||
|
||
longList.add( 3L ); | ||
|
||
assertFalse( longList.isEmpty() ); | ||
assertEquals( 3, longList.size() ); | ||
} | ||
|
||
@Test | ||
public void accessAddedElements() | ||
{ | ||
PrimitiveLongList longList = new PrimitiveLongList(); | ||
for ( long i = 1; i < 6L; i++ ) | ||
{ | ||
longList.add( i ); | ||
} | ||
|
||
assertEquals( 5L, longList.get( 4 ) ); | ||
assertEquals( 1L, longList.get( 0 ) ); | ||
} | ||
|
||
@Test( expected = IndexOutOfBoundsException.class ) | ||
public void throwExceptionOnAccessingNonExistentElement() | ||
{ | ||
PrimitiveLongList longList = new PrimitiveLongList(); | ||
longList.get( 0 ); | ||
} | ||
|
||
@Test | ||
public void iterateOverListElements() | ||
{ | ||
PrimitiveLongList longList = new PrimitiveLongList(); | ||
for ( long i = 0; i < 10L; i++ ) | ||
{ | ||
longList.add( i ); | ||
} | ||
|
||
int iteratorElements = 0; | ||
long value = 0; | ||
PrimitiveLongIterator iterator = longList.iterator(); | ||
while ( iterator.hasNext() ) | ||
{ | ||
iteratorElements++; | ||
assertEquals( value++, iterator.next() ); | ||
} | ||
|
||
assertEquals( iteratorElements, longList.size() ); | ||
} | ||
|
||
@Test | ||
public void clearResetListSize() | ||
{ | ||
PrimitiveLongList longList = new PrimitiveLongList(); | ||
long size = 10; | ||
for ( long i = 0; i < 10L; i++ ) | ||
{ | ||
longList.add( i ); | ||
} | ||
assertEquals( size, longList.size() ); | ||
|
||
longList.clear(); | ||
|
||
assertEquals( 0, longList.size() ); | ||
assertTrue( longList.isEmpty() ); | ||
} | ||
|
||
@Test | ||
public void transformListToArray() | ||
{ | ||
PrimitiveLongList longList = new PrimitiveLongList(); | ||
long size = 24L; | ||
for ( long i = 0; i < size; i++ ) | ||
{ | ||
longList.add( i ); | ||
} | ||
|
||
long[] longs = longList.toArray(); | ||
assertEquals( size, longs.length ); | ||
for ( int i = 0; i < longs.length; i++ ) | ||
{ | ||
assertEquals( i, longs[i] ); | ||
} | ||
} | ||
|
||
@Test | ||
public void holdLotsOfElements() | ||
{ | ||
PrimitiveLongList longList = new PrimitiveLongList(); | ||
long size = 13077L; | ||
for ( long i = 0; i < size; i++ ) | ||
{ | ||
longList.add( i ); | ||
} | ||
|
||
assertEquals( size, longList.size() ); | ||
for ( int i = 0; i < size; i++ ) | ||
{ | ||
assertEquals( i, longList.get( i ) ); | ||
} | ||
} | ||
} |
Oops, something went wrong.