billbar...@apache.org a écrit :
> Author: billbarker
> Date: Fri Dec 11 06:38:20 2009
> New Revision: 889516
> 
> URL: http://svn.apache.org/viewvc?rev=889516&view=rev
> Log:
> Initial fixes to the iterators.

Thanks a lot for your help, Bill.

Luc

> 
> This fixes the default sparseIterator problem where a RealVector only 
> contains zero elements, and add a sparseIterator to the OpenMapRealVector.
> 
> Still TODO:
> 1) add unit tests
> 2) remove *mapTo and *mapToSelf from OpenMapRealVector
> 3) fix javadocs
> 4) remove support for non-zero default values in OpenMapRealVector 
> 
> Modified:
>     
> commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/AbstractRealVector.java
>     
> commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/OpenMapRealVector.java
> 
> Modified: 
> commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/AbstractRealVector.java
> URL: 
> http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/AbstractRealVector.java?rev=889516&r1=889515&r2=889516&view=diff
> ==============================================================================
> --- 
> commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/AbstractRealVector.java
>  (original)
> +++ 
> commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/AbstractRealVector.java
>  Fri Dec 11 06:38:20 2009
> @@ -769,9 +769,15 @@
>              if (current.getValue() == 0) {
>                  advance(current);
>              }
> -            next = new EntryImpl();
> -            next.setIndex(current.getIndex());
> -            advance(next);
> +            if(current.getIndex() >= 0){
> +                // There is at least one non-zero entry
> +                next = new EntryImpl();
> +                next.setIndex(current.getIndex());
> +                advance(next);
> +            } else {
> +                // The vector consists of only zero entries, so deny having 
> a next
> +                current = null;
> +            }
>          }
>  
>          /** Advance an entry up to the next non null one.
> 
> Modified: 
> commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/OpenMapRealVector.java
> URL: 
> http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/OpenMapRealVector.java?rev=889516&r1=889515&r2=889516&view=diff
> ==============================================================================
> --- 
> commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/OpenMapRealVector.java
>  (original)
> +++ 
> commons/proper/math/trunk/src/main/java/org/apache/commons/math/linear/OpenMapRealVector.java
>  Fri Dec 11 06:38:20 2009
> @@ -1250,4 +1250,70 @@
>          return (double)entries.size()/(double)getDimension();
>      }
>  
> +    /** @{InheritDoc} */
> +    public java.util.Iterator<Entry> sparseIterator() {
> +        return new OpenMapSparseIterator();
> +    }
> +    
> +    /**
> +     *  Implementation of <code>Entry</code> optimized for OpenMap.
> +     * <p>This implementation does not allow arbitrary calls to 
> <code>setIndex</code>
> +     * since the order that entries are returned is undefined.
> +     */
> +    protected class OpenMapEntry extends Entry {
> +        private final Iterator iter;
> +
> +        protected OpenMapEntry(Iterator iter) {
> +            this.iter = iter;
> +        }
> +        /** {...@inheritdoc} */
> +        @Override
> +        public double getValue() {
> +            return iter.value();
> +        }
> +
> +        /** {...@inheritdoc} */
> +        @Override
> +        public void setValue(double value) {
> +            entries.put(iter.key(), value);
> +        }
> +        
> +        /** {...@inheritdoc} */
> +        @Override
> +        public int getIndex() {
> +            return iter.key();
> +        }
> +    }
> +    
> +    /**
> +     *  Iterator class to do iteration over just the non-zero elements.
> +     *  <p>This implementation is fail-fast, so cannot be used to modify any 
> zero element. 
> +     *
> +     */
> +    
> +    protected class OpenMapSparseIterator implements 
> java.util.Iterator<Entry> {
> +        private final Iterator iter;
> +        private final Entry current;
> +        
> +        protected OpenMapSparseIterator() {
> +            iter = entries.iterator();
> +            current = new OpenMapEntry(iter);
> +        }
> +
> +        /** {...@inheritdoc} */
> +        public boolean hasNext() {
> +            return iter.hasNext();
> +        }
> +
> +        /** {...@inheritdoc} */
> +        public Entry next() {
> +            iter.advance();
> +            return current;
> +        }
> +
> +        public void remove() {
> +            throw new UnsupportedOperationException("Not supported");
> +       }
> +        
> +    }
>  }
> 
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@commons.apache.org
For additional commands, e-mail: dev-h...@commons.apache.org

Reply via email to