// Path: src/main/java/net/tradelib/functors/Percentile.java
// public class Percentile {
//    private int length;
//    private int count;
//    private double [] buffer;
//    private double [] sorted;
//    private boolean isSorted;
//    
//    public Percentile(int length) {
//       this.length = length;
//       this.count = 0;
//       this.buffer = new double [length];
//       this.sorted = new double [length];
//       this.isSorted = false;
//    }
//    
//    public int getLength() { return length; }
//    public int getCount() { return count; }
//    
//    public void add(double value) {
//       buffer[count % length] = value;
//       ++count;
//       isSorted = false;
//    }
//    
//    public double last(double percentile) {
//       if(count < length) return Double.NaN;
//       
//       if(!isSorted) {
//          System.arraycopy(buffer, 0, sorted, 0, length);
//          Arrays.sort(sorted);
//          isSorted = true;
//       }
//       
//       double n = (length - 1.0)*percentile + 1.0;
//       if(n == 1d) {
//          return sorted[0];
//       } else if(n == length) {
//          return sorted[length - 1];
//       } else {
//          int k = (int)n;
//          double d = n - k;
//          return sorted[k - 1] + d * (sorted[k] - sorted[k - 1]);
//       }
//    }
// }


import static org.junit.Assert.*;
import net.tradelib.functors.Percentile;
import org.junit.Test;
// Copyright 2015 by Ivan Popivanov
//
// Licensed 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 net.tradelib.functors;



public class PercentileTest {

   @Test
   public void test() {
      Double [] inputs = { 204.97, 205.45, 202.74, 200.14, 201.99, 199.45, 201.92, 204.84, 204.06, 206.12,
                           205.55, 204.63, 206.81, 206.93, 208.92, 209.78, 210.11, 210.13, 209.98, 211.24 };
      Double [] expected = { null, null, null, null, null, null, null, null, null, null, 205.21, 205.145,
                             205.195, 205.835, 206.465, 206.87, 207.925, 209.35, 209.88, 210.045 };

// Path: src/main/java/net/tradelib/functors/Percentile.java
// public class Percentile {
//    private int length;
//    private int count;
//    private double [] buffer;
//    private double [] sorted;
//    private boolean isSorted;
//    
//    public Percentile(int length) {
//       this.length = length;
//       this.count = 0;
//       this.buffer = new double [length];
//       this.sorted = new double [length];
//       this.isSorted = false;
//    }
//    
//    public int getLength() { return length; }
//    public int getCount() { return count; }
//    
//    public void add(double value) {
//       buffer[count % length] = value;
//       ++count;
//       isSorted = false;
//    }
//    
//    public double last(double percentile) {
//       if(count < length) return Double.NaN;
//       
//       if(!isSorted) {
//          System.arraycopy(buffer, 0, sorted, 0, length);
//          Arrays.sort(sorted);
//          isSorted = true;
//       }
//       
//       double n = (length - 1.0)*percentile + 1.0;
//       if(n == 1d) {
//          return sorted[0];
//       } else if(n == length) {
//          return sorted[length - 1];
//       } else {
//          int k = (int)n;
//          double d = n - k;
//          return sorted[k - 1] + d * (sorted[k] - sorted[k - 1]);
//       }
//    }
// }


// Path: src/test/java/net/tradelib/functors/PercentileTest.java
import static org.junit.Assert.*;
import net.tradelib.functors.Percentile;
import org.junit.Test;

// Copyright 2015 by Ivan Popivanov
//
// Licensed 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 net.tradelib.functors;



public class PercentileTest {

   @Test
   public void test() {
      Double [] inputs = { 204.97, 205.45, 202.74, 200.14, 201.99, 199.45, 201.92, 204.84, 204.06, 206.12,
                           205.55, 204.63, 206.81, 206.93, 208.92, 209.78, 210.11, 210.13, 209.98, 211.24 };
      Double [] expected = { null, null, null, null, null, null, null, null, null, null, 205.21, 205.145,
                             205.195, 205.835, 206.465, 206.87, 207.925, 209.35, 209.88, 210.045 };

      Percentile percentile = new Percentile(11);