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 subset view of a Vector
 
 public class VectorView extends AbstractVector {
 
   private Vector vector;
 
   // the offset into the Vector
   private int offset;

  
For serialization purposes only
 
   public VectorView() {
     super(0);
   }
 
   public VectorView(Vector vectorint offsetint cardinality) {
     super(cardinality);
     this. = vector;
     this. = offset;
   }
 
   @Override
   protected Matrix matrixLike(int rowsint columns) {
     return ((AbstractVector).matrixLike(rowscolumns);
   }
 
   @Override
   public Vector clone() {
     VectorView r = (VectorViewsuper.clone();
     r.vector = .clone();
     r.offset = ;
     return r;
   }
 
   @Override
   public boolean isDense() {
     return .isDense();
   }
 
   @Override
   public boolean isSequentialAccess() {
     return .isSequentialAccess();
   }
 
   @Override
   public VectorView like() {
     return new VectorView(.like(), size());
   }
 
   @Override
   public double getQuick(int index) {
     return .getQuick( + index);
   }
 
   @Override
   public void setQuick(int indexdouble value) {
     .setQuick( + indexvalue);
   }
 
   @Override
   public int getNumNondefaultElements() {
     return size();
   }
 
   @Override
   public Vector viewPart(int offsetint length) {
     if (offset < 0) {
       throw new IndexException(offsetsize());
     }
     if (offset + length > size()) {
       throw new IndexException(offset + lengthsize());
     }
     return new VectorView(offset + this.length);
   }

  

Returns:
true if index is a valid index in the underlying Vector
 
   private boolean isInView(int index) {
     return index >=  && index <  + size();
  }
    return new NonZeroIterator();
  }
  public Iterator<Elementiterator() {
    return new AllIterator();
  }
  public final class NonZeroIterator extends AbstractIterator<Element> {
    private final Iterator<Elementit;
    private NonZeroIterator() {
       = .nonZeroes().iterator();
    }
    @Override
    protected Element computeNext() {
      while (.hasNext()) {
        Element el = .next();
        if (isInView(el.index()) && el.get() != 0) {
          Element decorated = .getElement(el.index());
          return new DecoratorElement(decorated);
        }
      }
      return endOfData();
    }
  }
  public final class AllIterator extends AbstractIterator<Element> {
    private final Iterator<Elementit;
    private AllIterator() {
       = .all().iterator();
    }
    @Override
    protected Element computeNext() {
      while (.hasNext()) {
        Element el = .next();
        if (isInView(el.index())) {
          Element decorated = .getElement(el.index());
          return new DecoratorElement(decorated);
        }
      }
      return endOfData(); // No element was found
    }
  }
  private final class DecoratorElement implements Element {
    private final Element decorated;
    private DecoratorElement(Element decorated) {
      this. = decorated;
    }
    @Override
    public double get() {
      return .get();
    }
    @Override
    public int index() {
      return .index() - ;
    }
    @Override
    public void set(double value) {
      .set(value);
    }
  }
  public double getLengthSquared() {
    double result = 0.0;
    int size = size();
    for (int i = 0; i < sizei++) {
      double value = getQuick(i);
      result += value * value;
    }
    return result;
  }
  public double getDistanceSquared(Vector v) {
    double result = 0.0;
    int size = size();
    for (int i = 0; i < sizei++) {
      double delta = getQuick(i) - v.getQuick(i);
      result += delta * delta;
    }
    return result;
  }
  public double getLookupCost() {
    return .getLookupCost();
  }
  public double getIteratorAdvanceCost() {
    // TODO: remove the 2x after fixing the Element iterator
    return 2 * .getIteratorAdvanceCost();
  }
  public boolean isAddConstantTime() {
    return .isAddConstantTime();
  }

  
Used internally by assign() to update multiple indices and values at once. Only really useful for sparse vectors (especially SequentialAccessSparseVector).

If someone ever adds a new type of sparse vectors, this method must merge (index, value) pairs into the vector.

Parameters:
updates a mapping of indices to values to merge in the vector.
  public void mergeUpdates(OrderedIntDoubleMapping updates) {
    for (int i = 0; i < updates.getNumMappings(); ++i) {
      updates.setIndexAt(iupdates.indexAt(i) + );
    }
    .mergeUpdates(updates);
  }
New to GrepCode? Check out our FAQ X