gt:                 if (!iter.hasNext()) {
context: /*
   * Copyright (C) 2013 - 2015 fekir
   *
   * This file is part of jalgorithm.
   *
   * This program is free software: you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation, either version 3 of the License, or
   * (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
   */
  package fekir.algorithm;
  
  import java.util.*;
  
  /**
   * This class contains various methods for searching Iterables and Arrays inside
   * Iterables and Arrays.
   *
   * If assertions are turned on, checks against null pointers and indexes are
   * made beforehand, so an {@code AssertionError} will be thrown. If not a
   * {@code NullPointerException} or {@code IndexOutOfBoundsException} may be
   * thrown when the function is processing the data. Notice that assertion could
   * catch some null pointers that are otherwise not catched when comparing
   * elements using {@code .equals}, since it is not symmetric regarding the null
   * pointer.
   *
   * Warning: multiple iterator are created for searching an element. If this is
   * costly, consider copying the content of the iterable to an array/list and
   * then searching the element.
   *
   * @author fekir
   */
  @SuppressWarnings("FinalClass")
  public final class SearchEnd {
  
      /**
       *
       * @param <T>    class of the elements to compare
       * @param ignore ignored element used to determine the class instance
       *
       * @return 0 if elements are equals, not 0 if not equals ( it also
       *         check for null elements)
       */
      private static <T> Comparator<T> compareWithEquals(@SuppressWarnings("unused")
         final Class<T> ignore) {
          return new Comparator<T>() {
              @Override
              public int compare(final T o1, final T o2) {
                  return Objects.equals(o1,o2) ? 0 : 1;
              }
          };
      }
  
      /**
       * This function searches a subarray inside an array of int using equality
       * as comparison method
       *
       * @param content  array where to search
       * @param toSearch array to search
       *
       * @return index where the subarray is found, otherwise a negative value. If
       *         toSearch is empty the return value will be the first position.
       */
      public static int searchEnd(final int[] content, final int[] toSearch) {
          return searchEnd(content, 0, content.length, toSearch);
      }
  
      /**
       * This function searches a subarray inside an array of int using a
       * comparator
       *
       * @param content  array where to search
       * @param toSearch array to search
       * @param comp     comparator used to compare the single elements
       *
       * @return index where the subarray is found, otherwise a negative value. If
       *         toSearch is empty the return value will be the first position.
       */
      public static int searchEnd(final int[] content, final int[] toSearch, final Comparator<Integer> comp) {
          return searchEnd(content, 0, content.length, toSearch, comp);
      }
  
      /**
       * This function searches a subarray inside an array of int between begin
       * and end using a comparator
       *
       * @param content  array where to search
       * @param begin    index where the search begins
       * @param end      index where the search ends
       * @param toSearch array to search
       *
       * @return index where the subarray is found, otherwise a negative value. If
       *         toSearch is empty the return value will be the first position.
       */
      public static int searchEnd(final int[] content, final int begin, final int end, final int[] toSearch) {
          if (toSearch.length == 0) {
              return content.length;
          }
  
          if (end - begin < toSearch.length) {
              return -1;
          }
  
          int pos = end - toSearch.length;
          while (pos != begin) {
              int iter1 = pos;
              int iter2 = 0;
              while (content[iter1] == toSearch[iter2]) {
                  ++iter1;
                  ++iter2;
                  if (iter2 == toSearch.length) {
                      return pos;
                  }
                  if (iter1 == end) {
                      return -1;
                  }
              }
              --pos;
          }
          return -1;
      }
  
      /**
       * This function searches a subarray inside an array of int between begin
       * and end using a comparator
       *
       * @param content  array where to search
       * @param begin    index where the search begins
       * @param end      index where the search ends
       * @param toSearch array to search
       * @param comp     comparator used to compare the single elements
       *
       * @return index where the subarray is found, otherwise a negative value. If
       *         toSearch is empty the return value will be the first position.
       */
      public static int searchEnd(final int[] content, final int begin, final int end, final int[] toSearch, final Comparator<Integer> comp) {
          if (toSearch.length == 0) {
              return content.length;
          }
  
          if (end - begin < toSearch.length) {
              return -1;
          }
  
          int pos = end - toSearch.length;
          while (pos != begin) {
              int iter1 = pos;
              int iter2 = 0;
              while (comp.compare(content[iter1], toSearch[iter2]) == 0) {
                  ++iter1;
                  ++iter2;
                  if (iter2 == toSearch.length) {
                      return pos;
                  }
                  if (iter1 == end) {
                      return -1;
                  }
              }
              --pos;
          }
          return -1;
      }
  
      /**
       * This function searches a subarray inside an array of char usign equality
       * as comparison method
       *
       * @param content  array where to search
       * @param toSearch array to search
       *
       * @return index where the subarray is found, otherwise a negative value. If
       *         toSearch is empty the return value will be the first position.
       */
      public static int searchEnd(final char[] content, final char[] toSearch) {
          return searchEnd(content, 0, content.length, toSearch);
      }
  
      /**
       * This function searches a subarray inside an array of char between using a
       * comparator
       *
       * @param content  array where to search
       * @param toSearch array to search
       * @param comp     comparator used to compare the single elements
       *
       * @return index where the subarray is found, otherwise a negative value. If
       *         toSearch is empty the return value will be the first position.
       */
      public static int searchEnd(final char[] content, final char[] toSearch, final Comparator<Character> comp) {
          return searchEnd(content, 0, content.length, toSearch, comp);
      }
  
      /**
       * This function searches a subarray inside an array of char between begin
       * and end using equality as comparison method
       *
       * @param content  array where to search
       * @param begin    index where the search begins
       * @param end      index where the search ends
       * @param toSearch array to search
       *
       * @return index where the subarray is found, otherwise a negative value. If
       *         toSearch is empty the return value will be the first position.
       */
      public static int searchEnd(final char[] content, final int begin, final int end, final char[] toSearch) {
          if (toSearch.length == 0) {
              return content.length;
          }
  
          if (end - begin < toSearch.length) {
              return -1;
          }
  
          int pos = end - toSearch.length;
          while (pos != begin) {
              int iter1 = pos;
              int iter2 = 0;
              while (content[iter1] == toSearch[iter2]) {
                  ++iter1;
                  ++iter2;
                  if (iter2 == toSearch.length) {
                      return pos;
                  }
                  if (iter1 == end) {
                      return -1;
                  }
              }
              --pos;
          }
          return -1;
      }
  
      /**
       * This function searches a subarray inside an array of char between begin
       * and end using a comparator
       *
       * @param content  array where to search
       * @param begin    index where the search begins
       * @param end      index where the search ends
       * @param toSearch array to search
       * @param comp     comparator used to compare the single elements
       *
       * @return index where the subarray is found, otherwise a negative value. If
       *         toSearch is empty the return value will be the first position.
       */
      public static int searchEnd(final char[] content, final int begin, final int end, final char[] toSearch, final Comparator<Character> comp) {
          if (toSearch.length == 0) {
              return content.length;
          }
  
          if (end - begin < toSearch.length) {
              return -1;
          }
  
          int pos = end - toSearch.length;
          while (pos != begin) {
              int iter1 = pos;
              int iter2 = 0;
              while (comp.compare(content[iter1], toSearch[iter2]) == 0) {
                  ++iter1;
                  ++iter2;
                  if (iter2 == toSearch.length) {
                      return pos;
                  }
                  if (iter1 == end) {
                      return -1;
                  }
              }
              --pos;
          }
          return -1;
      }
  
      /**
       * This function searches a subarray inside an array of char usign equality
       * as comparison method
       *
       * @param content  array where to search
       * @param toSearch array to search
       *
       * @return index where the subarray is found, otherwise a negative value. If
       *         toSearch is empty the return value will be the first position.
       */
      public static int searchEnd(final CharSequence content, final char[] toSearch) {
          return searchEnd(content, 0, content.length(), toSearch);
      }
  
      /**
       * This function searches a subarray inside an array of char between using a
       * comparator
       *
       * @param content  array where to search
       * @param toSearch array to search
       * @param comp     comparator used to compare the single elements
       *
       * @return index where the subarray is found, otherwise a negative value. If
       *         toSearch is empty the return value will be the first position.
       */
      public static int searchEnd(final CharSequence content, final char[] toSearch, final Comparator<Character> comp) {
          return searchEnd(content, 0, content.length(), toSearch, comp);
      }
  
      /**
       * This function searches a subarray inside an array of char between begin
       * and end using equality as comparison method
       *
       * @param content  array where to search
       * @param begin    index where the search begins
       * @param end      index where the search ends
       * @param toSearch array to search
       *
       * @return index where the subarray is found, otherwise a negative value. If
       *         toSearch is empty the return value will be the first position.
       */
      public static int searchEnd(final CharSequence content, final int begin, final int end, final char[] toSearch) {
          if (toSearch.length == 0) {
              return content.length();
          }
  
          if (end - begin < toSearch.length) {
              return -1;
          }
  
          int pos = end - toSearch.length;
          while (pos != begin) {
              int iter1 = pos;
              int iter2 = 0;
              while (content.charAt(iter1) == toSearch[iter2]) {
                  ++iter1;
                  ++iter2;
                  if (iter2 == toSearch.length) {
                      return pos;
                  }
                  if (iter1 == end) {
                      return -1;
                  }
              }
              --pos;
          }
          return -1;
      }
  
      /**
       * This function searches a subarray inside an array of char between begin
       * and end using a comparator
       *
       * @param content  array where to search
       * @param begin    index where the search begins
       * @param end      index where the search ends
       * @param toSearch array to search
       * @param comp     comparator used to compare the single elements
       *
       * @return index where the subarray is found, otherwise a negative value. If
       *         toSearch is empty the return value will be the first position.
       */
      public static int searchEnd(final CharSequence content, final int begin, final int end, final char[] toSearch, final Comparator<Character> comp) {
          if (toSearch.length == 0) {
              return content.length();
          }
  
          if (end - begin < toSearch.length) {
              return -1;
          }
  
          int pos = end - toSearch.length;
          while (pos != begin) {
              int iter1 = pos;
              int iter2 = 0;
              while (comp.compare(content.charAt(iter1), toSearch[iter2]) == 0) {
                  ++iter1;
                  ++iter2;
                  if (iter2 == toSearch.length) {
                      return pos;
                  }
                  if (iter1 == end) {
                      return -1;
                  }
              }
              --pos;
          }
          return -1;
      }
  
      /**
       * This function searches a subarray inside an array of byte using equality
       * as comparison method
       *
       * @param content  array where to search
       * @param toSearch array to search
       *
       * @return index where the subarray is found, otherwise a negative value. If
       *         toSearch is empty the return value will be the first position.
       */
      public static int searchEnd(final byte[] content, final byte[] toSearch) {
          return searchEnd(content, 0, content.length, toSearch);
      }
  
      /**
       * This function searches a subarray inside an array of byte using a
       * comparator
       *
       * @param content  array where to search
       * @param toSearch array to search
       * @param comp     comparator used to compare the single elements
       *
       * @return index where the subarray is found, a negative value otherwise
       */
      public static int searchEnd(final byte[] content, final byte[] toSearch, final Comparator<Byte> comp) {
          return searchEnd(content, 0, content.length, toSearch, comp);
      }
  
      /**
       * This function searches a subarray inside an array of int between begin
       * and end using equality as comparison method
       *
       * @param content  array where to search
       * @param begin    index where the search begins
       * @param end      index where the search ends
       * @param toSearch array to search
       *
       * @return index where the subarray is found, otherwise a negative value. If
       *         toSearch is empty the return value will be the first position.
       */
      public static int searchEnd(final byte[] content, final int begin, final int end, final byte[] toSearch) {
          if (toSearch.length == 0) {
              return content.length;
          }
  
          if (end - begin < toSearch.length) {
              return -1;
          }
  
          int pos = end - toSearch.length;
          while (pos != begin) {
              int iter1 = pos;
              int iter2 = 0;
              while (content[iter1] == toSearch[iter2]) {
                  ++iter1;
                  ++iter2;
                  if (iter2 == toSearch.length) {
                      return pos;
                  }
                  if (iter1 == end) {
                      return -1;
                  }
              }
              --pos;
          }
          return -1;
      }
  
      /**
       * This function searches a subarray inside an array of byte between begin
       * and end using a comparator
       *
       * @param content  array where to search
       * @param begin    index where the search begins
       * @param end      index where the search ends
       * @param toSearch array to search
       * @param comp     comparator used to compare the single elements
       *
       * @return index where the subarray is found, otherwise a negative value. If
       *         toSearch is empty the return value will be the first position.
       */
      public static int searchEnd(final byte[] content, final int begin, final int end, final byte[] toSearch, final Comparator<Byte> comp) {
          if (toSearch.length == 0) {
              return content.length;
          }
  
          if (end - begin < toSearch.length) {
              return -1;
          }
  
          int pos = end - toSearch.length;
          while (pos != begin) {
              int iter1 = pos;
              int iter2 = 0;
              while (comp.compare(content[iter1], toSearch[iter2]) == 0) {
                  ++iter1;
                  ++iter2;
                  if (iter2 == toSearch.length) {
                      return pos;
                  }
                  if (iter1 == end) {
                      return -1;
                  }
              }
              --pos;
          }
          return -1;
      }
  
      /**
       * This function searches a subarray inside an array of short using equality
       * as comparison method
       *
       * @param content  array where to search
       * @param toSearch array to search
       *
       * @return index where the subarray is found, otherwise a negative value. If
       *         toSearch is empty the return value will be the first position.
       */
      public static int searchEnd(final short[] content, final short[] toSearch) {
          return searchEnd(content, 0, content.length, toSearch);
      }
  
      /**
       * This function searches a subarray inside an array of short using a
       * comparator
       *
       * @param content  array where to search
       * @param toSearch array to search
       * @param comp     comparator used to compare the single elements
       *
       * @return index where the subarray is found, a negative value otherwise
       */
      public static int searchEnd(final short[] content, final short[] toSearch, final Comparator<Short> comp) {
          return searchEnd(content, 0, content.length, toSearch, comp);
      }
  
      /**
       * This function searches a subarray inside an array of short between begin
       * and end using equality as comparison method
       *
       * @param content  array where to search
       * @param begin    index where the search begins
       * @param end      index where the search ends
       * @param toSearch array to search
       *
       * @return index where the subarray is found, otherwise a negative value. If
       *         toSearch is empty the return value will be the first position.
       */
      public static int searchEnd(final short[] content, final int begin, final int end, final short[] toSearch) {
          if (toSearch.length == 0) {
              return content.length;
          }
  
          if (end - begin < toSearch.length) {
              return -1;
          }
  
          int pos = end - toSearch.length;
          while (pos != begin) {
              int iter1 = pos;
              int iter2 = 0;
              while (content[iter1] == toSearch[iter2]) {
                  ++iter1;
                  ++iter2;
                  if (iter2 == toSearch.length) {
                      return pos;
                  }
                  if (iter1 == end) {
                      return -1;
                  }
              }
              --pos;
          }
          return -1;
      }
  
      /**
       * This function searches a subarray inside an array of short between begin
       * and end using a comparator
       *
       * @param content  array where to search
       * @param begin    index where the search begins
       * @param end      index where the search ends
       * @param toSearch array to search
       * @param comp     comparator used to compare the single elements
       *
       * @return index where the subarray is found, otherwise a negative value. If
       *         toSearch is empty the return value will be the first position.
       */
      public static int searchEnd(final short[] content, final int begin, final int end, final short[] toSearch, final Comparator<Short> comp) {
          if (toSearch.length == 0) {
              return content.length;
          }
  
          if (end - begin < toSearch.length) {
              return -1;
          }
  
          int pos = end - toSearch.length;
          while (pos != begin) {
              int iter1 = pos;
              int iter2 = 0;
              while (comp.compare(content[iter1], toSearch[iter2]) == 0) {
                  ++iter1;
                  ++iter2;
                  if (iter2 == toSearch.length) {
                      return pos;
                  }
                  if (iter1 == end) {
                      return -1;
                  }
              }
              --pos;
          }
          return -1;
      }
  
      /**
       * This function searches a subarray inside an array of long using equality
       * as comparison mehod
       *
       * @param content  array where to search
       * @param toSearch array to search
       *
       * @return index where the subarray is found, otherwise a negative value. If
       *         toSearch is empty the return value will be the first position.
       */
      public static int searchEnd(final long[] content, final long[] toSearch) {
          return searchEnd(content, 0, content.length, toSearch);
      }
  
      /**
       * This function searches a subarray inside an array of long using a
       * comparator
       *
       * @param content  array where to search
       * @param toSearch array to search
       * @param comp     comparator used to compare the single elements
       *
       * @return index where the subarray is found, a negative value otherwise
       */
      public static int searchEnd(final long[] content, final long[] toSearch, final Comparator<Long> comp) {
          return searchEnd(content, 0, content.length, toSearch, comp);
      }
  
      /**
       * This function searches a subarray inside an array of long between begin
       * and end using equality as comparison method
       *
       * @param content  array where to search
       * @param begin    index where the search begins
       * @param end      index where the search ends
       * @param toSearch array to search
       *
       * @return index where the subarray is found, otherwise a negative value. If
       *         toSearch is empty the return value will be the first position.
       */
      public static int searchEnd(final long[] content, final int begin, final int end, final long[] toSearch) {
          if (toSearch.length == 0) {
              return content.length;
          }
  
          if (end - begin < toSearch.length) {
              return -1;
          }
  
          int pos = end - toSearch.length;
          while (pos != begin) {
              int iter1 = pos;
              int iter2 = 0;
              while (content[iter1] == toSearch[iter2]) {
                  ++iter1;
                  ++iter2;
                  if (iter2 == toSearch.length) {
                      return pos;
                  }
                  if (iter1 == end) {
                      return -1;
                  }
              }
              --pos;
          }
          return -1;
      }
  
      /**
       * This function searches a subarray inside an array of long between begin
       * and end using a comparator
       *
       * @param content  array where to search
       * @param begin    index where the search begins
       * @param end      index where the search ends
       * @param toSearch array to search
       * @param comp     comparator used to compare the single elements
       *
       * @return index where the subarray is found, otherwise a negative value. If
       *         toSearch is empty the return value will be the first position.
       */
      public static int searchEnd(final long[] content, final int begin, final int end, final long[] toSearch, final Comparator<Long> comp) {
          if (toSearch.length == 0) {
              return content.length;
          }
  
          if (end - begin < toSearch.length) {
              return -1;
          }
  
          int pos = end - toSearch.length;
          while (pos != begin) {
              int iter1 = pos;
              int iter2 = 0;
              while (comp.compare(content[iter1], toSearch[iter2]) == 0) {
                  ++iter1;
                  ++iter2;
                  if (iter2 == toSearch.length) {
                      return pos;
                  }
                  if (iter1 == end) {
                      return -1;
                  }
              }
              --pos;
          }
          return -1;
      }
  
      /**
       * This function searches a subarray inside an array of float using equality
       * as comparison method
       *
       * @param content  array where to search
       * @param toSearch array to search
       *
       * @return index where the subarray is found, otherwise a negative value. If
       *         toSearch is empty the return value will be the first position.
       */
      public static int searchEnd(final float[] content, final float[] toSearch) {
          return searchEnd(content, 0, content.length, toSearch);
      }
  
      /**
       * This function searches a subarray inside an array of float using equality
       * as comparison method
       *
       * @param content  array where to search
       * @param toSearch array to search
       * @param comp     comparator used to compare the single elements
       *
       * @return index where the subarray is found, otherwise a negative value. If
       *         toSearch is empty the return value will be the first position.
       */
      public static int searchEnd(final float[] content, final float[] toSearch, final Comparator<Float> comp) {
          return searchEnd(content, 0, content.length, toSearch, comp);
      }
  
      /**
       * This function searches a subarray inside an array of float between begin
       * and end using equality as comparator
       *
       * @param content  array where to search
       * @param begin    index where the search begins
       * @param end      index where the search ends
       * @param toSearch array to search
       *
       * @return index where the subarray is found, otherwise a negative value. If
       *         toSearch is empty the return value will be the first position.
       */
      public static int searchEnd(final float[] content, final int begin, final int end, final float[] toSearch) {
          if (toSearch.length == 0) {
              return content.length;
          }
  
          if (end - begin < toSearch.length) {
              return -1;
          }
  
          int pos = end - toSearch.length;
          while (pos != begin) {
              int iter1 = pos;
              int iter2 = 0;
              while (content[iter1] == toSearch[iter2]) {
                  ++iter1;
                  ++iter2;
                  if (iter2 == toSearch.length) {
                      return pos;
                  }
                  if (iter1 == end) {
                      return -1;
                  }
              }
              --pos;
          }
          return -1;
      }
  
      /**
       * This function searches a subarray inside an array of float between begin
       * and end using a comparator
       *
       * @param content  array where to search
       * @param begin    index where the search begins
       * @param end      index where the search ends
       * @param toSearch array to search
       * @param comp     comparator used to compare the single elements
       *
       * @return index where the subarray is found, otherwise a negative value. If
       *         toSearch is empty the return value will be the first position.
       */
      public static int searchEnd(final float[] content, final int begin, final int end, final float[] toSearch, final Comparator<Float> comp) {
          if (toSearch.length == 0) {
              return content.length;
          }
  
          if (end - begin < toSearch.length) {
              return -1;
          }
  
          int pos = end - toSearch.length;
          while (pos != begin) {
              int iter1 = pos;
              int iter2 = 0;
              while (comp.compare(content[iter1], toSearch[iter2]) == 0) {
                  ++iter1;
                  ++iter2;
                  if (iter2 == toSearch.length) {
                      return pos;
                  }
                  if (iter1 == end) {
                      return -1;
                  }
              }
              --pos;
          }
          return -1;
      }
  
      /**
       * This function searches a subarray inside an array of double using
       * equality as comparison method
       *
       * @param content  array where to search
       * @param toSearch array to search
       *
       * @return index where the subarray is found, otherwise a negative value. If
       *         toSearch is empty the return value will be the first position.
       */
      public static int searchEnd(final double[] content, final double[] toSearch) {
          return searchEnd(content, 0, content.length, toSearch);
      }
  
      /**
       * This function searches a subarray inside an array of double between begin
       * and end using a comparator
       *
       * @param content  array where to search
       * @param toSearch array to search
       * @param comp     comparator used to compare the single elements
       *
       * @return index where the subarray is found, otherwise a negative value. If
       *         toSearch is empty the return value will be the first position.
       */
      public static int searchEnd(final double[] content, final double[] toSearch, final Comparator<Double> comp) {
          return searchEnd(content, 0, content.length, toSearch, comp);
      }
  
      /**
       * This function searches a subarray inside an array of double between begin
       * and end using equality as comparison method
       *
       * @param content  array where to search
       * @param begin    index where the search begins
       * @param end      index where the search ends
       * @param toSearch array to search
       *
       * @return index where the subarray is found, otherwise a negative value. If
       *         toSearch is empty the return value will be the first position.
       */
      public static int searchEnd(final double[] content, final int begin, final int end, final double[] toSearch) {
          if (toSearch.length == 0) {
              return content.length;
          }
  
          if (end - begin < toSearch.length) {
              return -1;
          }
  
          int pos = end - toSearch.length;
          while (pos != begin) {
              int iter1 = pos;
              int iter2 = 0;
              while (content[iter1] == toSearch[iter2]) {
                  ++iter1;
                  ++iter2;
                  if (iter2 == toSearch.length) {
                      return pos;
                  }
                  if (iter1 == end) {
                      return -1;
                  }
              }
              --pos;
          }
          return -1;
      }
  
      /**
       * This function searches a subarray inside an array of double between begin
       * and end using a comparator
       *
       * @param content  array where to search
       * @param begin    index where the search begins
       * @param end      index where the search ends
       * @param toSearch array to search
       * @param comp     comparator used to compare the single elements
       *
       * @return index where the subarray is found, otherwise a negative value. If
       *         toSearch is empty the return value will be the first position.
       */
      public static int searchEnd(final double[] content, final int begin, final int end, final double[] toSearch, final Comparator<Double> comp) {
          if (toSearch.length == 0) {
              return content.length;
          }
  
          if (end - begin < toSearch.length) {
              return -1;
          }
  
          int pos = end - toSearch.length;
          while (pos != begin) {
              int iter1 = pos;
              int iter2 = 0;
              while (comp.compare(content[iter1], toSearch[iter2]) == 0) {
                  ++iter1;
                  ++iter2;
                  if (iter2 == toSearch.length) {
                      return pos;
                  }
                  if (iter1 == end) {
                      return -1;
                  }
              }
              --pos;
          }
          return -1;
      }
  
      /**
       * This function searches a subarray inside an array of boolean using
       * equality as comparison method
       *
       * @param content  array where to search
       * @param toSearch array to search
       *
       * @return index where the subarray is found, otherwise a negative value. If
       *         toSearch is empty the return value will be the first position.
       */
      public static int searchEnd(final boolean[] content, final boolean[] toSearch) {
          return searchEnd(content, 0, content.length, toSearch);
      }
  
      /**
       * This function searches a subarray inside an array of boolean using a
       * comparator
       *
       * @param content  array where to search
       * @param toSearch array to search
       * @param comp     comparator used to compare the single elements
       *
       * @return index where the subarray is found, otherwise a negative value. If
       *         toSearch is empty the return value will be the first position.
       */
      public static int searchEnd(final boolean[] content, final boolean[] toSearch, final Comparator<Boolean> comp) {
          return searchEnd(content, 0, content.length, toSearch, comp);
      }
  
      /**
       * This function searches a subarray inside an array of boolean between
       * begin and end using equality as comparison method
       *
       * @param content  array where to search
       * @param begin    index where the search begins
       * @param end      index where the search ends
       * @param toSearch array to search
       *
       * @return index where the subarray is found, otherwise a negative value. If
       *         toSearch is empty the return value will be the first position.
       */
      public static int searchEnd(final boolean[] content, final int begin, final int end, final boolean[] toSearch) {
          if (toSearch.length == 0) {
              return content.length;
          }
  
          if (end - begin < toSearch.length) {
              return -1;
          }
  
          int pos = end - toSearch.length;
          while (pos != begin) {
              int iter1 = pos;
              int iter2 = 0;
              while (content[iter1] == toSearch[iter2]) {
                  ++iter1;
                  ++iter2;
                  if (iter2 == toSearch.length) {
                      return pos;
                  }
                  if (iter1 == end) {
                      return -1;
                  }
              }
              --pos;
          }
          return -1;
      }
  
      /**
       * This function searches a subarray inside an array of boolean between
       * begin and end using a comparator
       *
       * @param content  array where to search
       * @param begin    index where the search begins
       * @param end      index where the search ends
       * @param toSearch array to search
       * @param comp     comparator used to compare the single elements
       *
       * @return index where the subarray is found, otherwise a negative value. If
       *         toSearch is empty the return value will be the first position.
       */
      public static int searchEnd(final boolean[] content, final int begin, final int end, final boolean[] toSearch, final Comparator<Boolean> comp) {
          if (toSearch.length == 0) {
              return content.length;
          }
  
          if (end - begin < toSearch.length) {
              return -1;
          }
  
          int pos = end - toSearch.length;
          while (pos != begin) {
              int iter1 = pos;
              int iter2 = 0;
              while (comp.compare(content[iter1], toSearch[iter2]) == 0) {
                  ++iter1;
                  ++iter2;
                  if (iter2 == toSearch.length) {
                      return pos;
                  }
                  if (iter1 == end) {
                      return -1;
                  }
              }
              --pos;
          }
          return -1;
      }
  
      /**
       * This function searches a subarray inside an array of Object of type T
       * using {@code .equals} as comparison method
       *
       * @param <T>      the class of the objects in the array
       * @param content  array where to search
       * @param toSearch array to search
       *
       * @return index where the subarray is found, otherwise a negative value. If
       *         toSearch is empty the return value will be the first position.
       */
      public static <T> int searchEnd(final T[] content, final T[] toSearch) {
          return searchEnd(content, 0, content.length, toSearch);
      }
  
      /**
       * This function searches a subarray inside an array of objects of type T
       * using a comparator
       *
       * @param <T>      the class of the objects in the array
       * @param content  array where to search
       * @param toSearch array to search
       * @param comp     comparator used to compare the single elements
       *
       * @return index where the subarray is found, otherwise a negative value. If
       *         toSearch is empty the return value will be the first position.
       */
      public static <T> int searchEnd(final T[] content, final T[] toSearch, final Comparator<T> comp) {
          return searchEnd(content, 0, content.length, toSearch, comp);
      }
  
      /**
       * This function searches a subarray inside an array of objects of type T
       * between begin and end using {@code .equals} as comparison method
       *
       * @param <T>      the class of the object in the array
       * @param content  array where to search
       * @param begin    index where the search begins
       * @param end      index where the search ends
       * @param toSearch array to search
       *
       * @return index where the subarray is found, otherwise a negative value. If
       *         toSearch is empty the return value will be the first position.
       */
      public static <T> int searchEnd(final T[] content, final int begin, final int end, final T[] toSearch) {
          if (toSearch.length == 0) {
              return content.length;
          }
  
          if (end - begin < toSearch.length) {
              return -1;
          }
  
          int pos = end - toSearch.length;
          while (pos != begin) {
              int iter1 = pos;
              int iter2 = 0;
              while (Objects.equals(content[iter1], toSearch[iter2])) {
                  ++iter1;
                  ++iter2;
                  if (iter2 == toSearch.length) {
                      return pos;
                  }
                  if (iter1 == end) {
                      return -1;
                  }
              }
              --pos;
          }
          return -1;
      }
  
      /**
       * This function searches a subarray inside an array of objects of type T
       * between begin and end using a comparator
       *
       * @param <T>      the class of the object in the array
       * @param content  array where to search
       * @param begin    index where the search begins
       * @param end      index where the search ends
       * @param toSearch array to search
       * @param comp     comparator used to compare the single elements
       *
       * @return index where the subarray is found, otherwise a negative value. If
       *         toSearch is empty the return value will be the first position.
       */
      public static <T> int searchEnd(final T[] content, final int begin, final int end, final T[] toSearch, final Comparator<T> comp) {
          if (toSearch.length == 0) {
              return content.length;
          }
  
          if (end - begin < toSearch.length) {
              return -1;
          }
  
          int pos = end - toSearch.length;
          while (pos != begin) {
              int iter1 = pos;
              int iter2 = 0;
              while (comp.compare(content[iter1], toSearch[iter2]) == 0) {
                  ++iter1;
                  ++iter2;
                  if (iter2 == toSearch.length) {
                      return pos;
                  }
                  if (iter1 == end) {
                      return -1;
                  }
              }
              --pos;
          }
          return -1;
      }
  
      /**
       * This function searches a iterable inside an array of objects of type T
       * using {@code .equals} as comparison method
       *
       * @param <T>      the class of the object in the array
       * @param content  array where to search
       * @param toSearch array to search
       *
       * @return index where the iterable is found, otherwise a negative value. If
       *         toSearch is empty the return value will be the first position.
       */
      public static <T> int searchEnd(final T[] content, final Iterable<T> toSearch) {
          return searchEnd(content, 0, content.length, toSearch);
      }
  
      /**
       * This function searches a iterable inside an array of objects of type T
       * using a comparator
       *
       * @param <T>      the class of the object in the array
       * @param content  array where to search
       * @param toSearch array to search
       * @param comp     comparator used to compare the single elements
       *
       * @return index where the iterable is found, otherwise a negative value. If
       *         toSearch is empty the return value will be the first position.
       */
      public static <T> int searchEnd(final T[] content, final Iterable<T> toSearch, final Comparator<T> comp) {
          return searchEnd(content, 0, content.length, toSearch, comp);
      }
  
      /**
       * This function searches a iterable inside an array of objects of type T
       * between begin and end using {@code .equals} as comparison method
       *
       * @param <T>      the class of the object in the array
       * @param content  array where to search
       * @param begin    index where the search begins
       * @param end      index where the search ends
       * @param toSearch array to search
       *
       * @return index where the iterable is found, otherwise a negative value. If
       *         toSearch is empty the return value will be the first position.
       */
      public static <T> int searchEnd(final T[] content, final int begin, final int end, final Iterable<T> toSearch) {
          if (!toSearch.iterator().hasNext()) {
              return 0;
          }
  
          if (toSearch instanceof Collection<?>) {
              if (end - begin < ((Collection<?>) toSearch).size()) {
                  return -1;
              }
          }
  
          int pos = end;
          while (pos != begin) {
              final Iterator<T> iter = toSearch.iterator();
              int iter1 = pos;
              while (Objects.equals(content[iter1], iter.next())) {
                  ++iter1;
  