All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.chen0040.gp.utils.CollectionUtils Maven / Gradle / Ivy

There is a newer version: 1.0.14
Show newest version
package com.github.chen0040.gp.utils;


import com.github.chen0040.data.utils.TupleTwo;
import com.github.chen0040.gp.services.RandEngine;
import com.github.chen0040.gp.services.SimpleRandEngine;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by xschen on 4/5/2017.
 */
public class CollectionUtils {
   public static  void shuffle(List a, RandEngine randEngine) {
      for(int i=0; i < a.size(); ++i) {
         int j = randEngine.nextInt(i+1);
         exchange(a, i, j);
      }
   }

   public static  void shuffle(List a) {
      RandEngine randEngine = new SimpleRandEngine();
      shuffle(a, randEngine);
   }

   public static  void exchange(List a, int i, int j) {
      T temp = a.get(i);
      a.set(i, a.get(j));
      a.set(j, temp);
   }


   public static > boolean isBetterThan(T a, T b) {
      return a.compareTo(b) < 0;
   }


   public static  TupleTwo,List> split(List data, double p) {
      int split_index = (int)(data.size() * p);
      List data1 = new ArrayList<>();
      List data2 = new ArrayList<>();
      for(int i=0; i < split_index; ++i){
         data1.add(data.get(i));
      }
      for(int i=split_index; i < data.size(); ++i) {
         data2.add(data.get(i));
      }
      return new TupleTwo<>(data1, data2);
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy