Save This Page
Home » mahout-collections-1.0-src » org.apache.mahout.math.buffer » [javadoc | source]
    1   /*
    2   Copyright 1999 CERN - European Organization for Nuclear Research.
    3   Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose 
    4   is hereby granted without fee, provided that the above copyright notice appear in all copies and 
    5   that both that copyright notice and this permission notice appear in supporting documentation. 
    6   CERN makes no representations about the suitability of this software for any purpose. 
    7   It is provided "as is" without expressed or implied warranty.
    8   */
    9   package org.apache.mahout.math.buffer;
   10   
   11   import org.apache.mahout.math.PersistentObject;
   12   import org.apache.mahout.math.list.DoubleArrayList;
   13   
   14   /** @deprecated until unit tests are in place.  Until this time, this class/interface is unsupported. */
   15   @Deprecated
   16   public class DoubleBuffer2D extends PersistentObject implements DoubleBuffer2DConsumer {
   17   
   18     private final DoubleBuffer2DConsumer target;
   19     private final double[] xElements;
   20     private final double[] yElements;
   21   
   22     // vars cached for speed
   23     private final DoubleArrayList xList;
   24     private final DoubleArrayList yList;
   25     private final int capacity;
   26     private int size;
   27   
   28     /**
   29      * Constructs and returns a new buffer with the given target.
   30      *
   31      * @param target   the target to flush to.
   32      * @param capacity the number of points the buffer shall be capable of holding before overflowing and flushing to the
   33      *                 target.
   34      */
   35     public DoubleBuffer2D(DoubleBuffer2DConsumer target, int capacity) {
   36       this.target = target;
   37       this.capacity = capacity;
   38       this.xElements = new double[capacity];
   39       this.yElements = new double[capacity];
   40       this.xList = new DoubleArrayList(xElements);
   41       this.yList = new DoubleArrayList(yElements);
   42       this.size = 0;
   43     }
   44   
   45     /**
   46      * Adds the specified point (x,y) to the receiver.
   47      *
   48      * @param x the x-coordinate of the point to add.
   49      * @param y the y-coordinate of the point to add.
   50      */
   51     public void add(double x, double y) {
   52       if (this.size == this.capacity) {
   53         flush();
   54       }
   55       this.xElements[this.size] = x;
   56       this.yElements[this.size++] = y;
   57     }
   58   
   59     /**
   60      * Adds all specified points (x,y) to the receiver.
   61      *
   62      * @param x the x-coordinates of the points to add.
   63      * @param y the y-coordinates of the points to add.
   64      */
   65     @Override
   66     public void addAllOf(DoubleArrayList x, DoubleArrayList y) {
   67       int listSize = x.size();
   68       if (this.size + listSize >= this.capacity) {
   69         flush();
   70       }
   71       this.target.addAllOf(x, y);
   72     }
   73   
   74     /** Sets the receiver's size to zero. In other words, forgets about any internally buffered elements. */
   75     public void clear() {
   76       this.size = 0;
   77     }
   78   
   79     /** Adds all internally buffered points to the receiver's target, then resets the current buffer size to zero. */
   80     public void flush() {
   81       if (this.size > 0) {
   82         xList.setSize(this.size);
   83         yList.setSize(this.size);
   84         this.target.addAllOf(xList, yList);
   85         this.size = 0;
   86       }
   87     }
   88   }

Save This Page
Home » mahout-collections-1.0-src » org.apache.mahout.math.buffer » [javadoc | source]