You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@mahout.apache.org by Laszlo Dosa <la...@fredhopper.com> on 2010/08/25 15:25:20 UTC

SequentialAccessSparseVector iterator() and iterateNonZero() odd behaviour

Hi,

 

I tried to iterate over the elements of a SequentialAccessSparseVector.

I run the following test and I observed that both iterators skip items:

 

                int [] index = new int[] { 0, 1, 2, 3, 4, 5 };

                int [] values = new int[] { 0, 1, 2, 3, 4, 5 };

                SequentialAccessSparseVector vector;

 

                @Before

                public void setUp() {

                               vector = new SequentialAccessSparseVector(6);

                               for(int i = 0; i < Math.min(index.length,
values.length); i++ ) {

                                               vector.set(index[i],
values[i]);

                               }

                }

Case A: 

                @Test

                public void testIteratorAll() {

                               

                               int elements = 0;

                               Iterator<Element> it = vector.iterator();

                               while (it.hasNext()) {

 
System.out.println(it.next().get());

                                               elements++;

                               }

                               

 
assertEquals((int)vector.get(Math.min(index.length,
values.length)-1),values[Math.min(index.length, values.length)-1]);

à                          assertEquals(Math.min(index.length,
values.length),elements);

                }

 

Case B: 

                @Test

                public void testIteratorNonNull() {

                               int elements = 0;

                               Iterator<Element> it =
vector.iterateNonZero();

                               while (it.hasNext()) {

 
System.out.println(it.next().get());

                                               elements++;

                                               

                               }

                               

 
assertEquals((int)vector.get(Math.min(index.length,
values.length)-1),values[Math.min(index.length, values.length)-1]);

è                        assertEquals(Math.min(index.length,
values.length),elements);  

                }

 

In both cases the second assertEquals fails, which indicates that not all
elements have been reached.

 

The result are as follows:

Case A: Skips the last item. 

0.0

1.0

2.0

3.0

4.0

 

Case B: Skips the first item.

1.0

2.0

3.0

4.0

5.0

 

Is this the expected behavior or do I misuse the library?

 

Regards,

Laszlo