mahout-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Laszlo Dosa" <laszlo.d...@fredhopper.com>
Subject SequentialAccessSparseVector iterator() and iterateNonZero() odd behaviour
Date Wed, 25 Aug 2010 13:25:20 GMT
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


Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message