Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 
 package org.apache.mahout.math;
 
 
Implements vector that only stores non-zero doubles
 
 public class RandomAccessSparseVector extends AbstractVector {
 
   private static final int INITIAL_CAPACITY = 11;
 
   private OpenIntDoubleHashMap values;

  
For serialization purposes only.
 
   public RandomAccessSparseVector() {
     super(0);
   }
 
   public RandomAccessSparseVector(int cardinality) {
     this(cardinality, Math.min(cardinality)); // arbitrary estimate of 'sparseness'
   }
 
   public RandomAccessSparseVector(int cardinalityint initialCapacity) {
     super(cardinality);
      = new OpenIntDoubleHashMap(initialCapacity);
   }
 
   public RandomAccessSparseVector(Vector other) {
     this(other.size(), other.getNumNondefaultElements());
     for (Element e : other.nonZeroes()) {
       .put(e.index(), e.get());
     }
   }
 
   private RandomAccessSparseVector(int cardinalityOpenIntDoubleHashMap values) {
     super(cardinality);
     this. = values;
   }
 
   public RandomAccessSparseVector(RandomAccessSparseVector otherboolean shallowCopy) {
     super(other.size());
      = shallowCopy ? other.values : (OpenIntDoubleHashMap)other.values.clone();
   }
 
   @Override
   protected Matrix matrixLike(int rowsint columns) {
     return new SparseRowMatrix(rowscolumns);
   }
 
   @Override
   public RandomAccessSparseVector clone() {
   }
 
   @Override
   public String toString() {
     StringBuilder result = new StringBuilder();
     result.append('{');
     Iterator<Elementit = iterateNonZero();
     boolean first = true;
     while (it.hasNext()) {
       if (first) {
         first = false;
       } else {
         result.append(',');
       }
       Element e = it.next();
       result.append(e.index());
       result.append(':');
       result.append(e.get());
     }
     result.append('}');
     return result.toString();
   }
 
   @Override
   public Vector assign(Vector other) {
     if (size() != other.size()) {
      throw new CardinalityException(size(), other.size());
    }
    .clear();
    for (Element e : other.nonZeroes()) {
      setQuick(e.index(), e.get());
    }
    return this;
  }
  public void mergeUpdates(OrderedIntDoubleMapping updates) {
    for (int i = 0; i < updates.getNumMappings(); ++i) {
      .put(updates.getIndices()[i], updates.getValues()[i]);
    }
  }

  

Returns:
false
  public boolean isDense() {
    return false;
  }

  

Returns:
false
  public boolean isSequentialAccess() {
    return false;
  }
  public double getQuick(int index) {
    return .get(index);
  }
  public void setQuick(int indexdouble value) {
    if (value == 0.0) {
      .removeKey(index);
    } else {
      .put(indexvalue);
    }
  }
  public void incrementQuick(int indexdouble increment) {
    .adjustOrPutValue(indexincrementincrement);
  }
    return new RandomAccessSparseVector(size(), .size());
  }
  public int getNumNondefaultElements() {
    return .size();
  }
  public double getLookupCost() {
    return 1;
  }
  public double getIteratorAdvanceCost() {
  }

  
This is "sort of" constant, but really it might resize the array.
  public boolean isAddConstantTime() {
    return true;
  }
  /*
  @Override
  public Element getElement(int index) {
    // TODO: this should return a MapElement so as to avoid hashing for both getQuick and setQuick.
    return super.getElement(index);
  }
   */

  
NOTE: this implementation reuses the Vector.Element instance for each call of next(). If you need to preserve the instance, you need to make a copy of it

Returns:
an java.util.Iterator over the Elements.
See also:
AbstractVector.getElement(int)
    return new NonDefaultIterator();
  }
  public Iterator<Elementiterator() {
    return new AllIterator();
  }
  private final class NonDefaultIterator implements Iterator<Element> {
    private final class NonDefaultElement implements Element {
      @Override
      public double get() {
        return .get();
      }
      @Override
      public int index() {
        return .index();
      }
      @Override
      public void set(double value) {
        invalidateCachedLength();
        .set(value);
      }
    }
    private MapElement mapElement;
    private final NonDefaultElement element = new NonDefaultElement();
    private final Iterator<MapElementiterator;
    private NonDefaultIterator() {
      this. = .iterator();
    }
    @Override
    public boolean hasNext() {
      return .hasNext();
    }
    @Override
    public Element next() {
       = .next(); // This will throw an exception at the end of enumeration.
      return ;
    }
    @Override
    public void remove() {
      throw new UnsupportedOperationException();
    }
  }
  private final class AllIterator implements Iterator<Element> {
    private final RandomAccessElement element = new RandomAccessElement();
    private AllIterator() {
      . = -1;
    }
    @Override
    public boolean hasNext() {
      return . + 1 < size();
    }
    @Override
    public Element next() {
      if (!hasNext()) {
        throw new NoSuchElementException();
      }
      .++;
      return ;
    }
    @Override
    public void remove() {
      throw new UnsupportedOperationException();
    }
  }
  private final class RandomAccessElement implements Element {
    int index;
    @Override
    public double get() {
      return .get();
    }
    @Override
    public int index() {
      return ;
    }
    @Override
    public void set(double value) {
      if (value == 0.0) {
        .removeKey();
      } else {
        .put(value);
      }
    }
  }
New to GrepCode? Check out our FAQ X