Ejemplo n.º 1
0
  /**
   * Back-end to pick using BFS.
   *
   * @param limit maximum value of combinations
   * @param items object value table
   * @return solution
   */
  public static <T> Pack<T> breadthFirstSearch(Long limit, AbstractMap<T, Long> items) {
    if (Pack.needSearch(limit, items)) {
      return new Pack<T>(limit, new ArrayList<T>(items.keySet()));
    }

    ArrayList<Pack<T>> table = new ArrayList<Pack<T>>();
    table.add(new Pack<T>());

    for (Entry<T, Long> e : items.entrySet()) {
      ArrayList<Pack<T>> tmp = new ArrayList<Pack<T>>();
      for (Pack<T> p : table) {
        Long newSize = p.getScore() + e.getValue();
        if (newSize <= limit) {
          ArrayList<T> newDirs = new ArrayList<T>(p.getItems());
          newDirs.add(e.getKey());
          tmp.add(new Pack<T>(newSize, newDirs));
        }
      }
      table.addAll(table.size(), tmp);
    }

    Pack<T> max = new Pack<T>();
    for (Pack<T> p : table) {
      if (p.getScore() >= max.getScore()) {
        max = p;
      }
    }
    return max;
  }
Ejemplo n.º 2
0
 @SuppressWarnings("unchecked")
 @Override
 protected Object clone() throws CloneNotSupportedException {
   AbstractMap<K, V> result = (AbstractMap<K, V>) super.clone();
   result.keySet = null;
   result.valuesCollection = null;
   return result;
 }
Ejemplo n.º 3
0
 public BinarySearch(Long limit, AbstractMap<T, Long> items) {
   this.limit_ = limit;
   this.items_ = items;
   this.map_ = new ArrayList<T>(items.keySet());
   Collections.sort(
       this.map_,
       new Comparator<T>() {
         @Override
         public int compare(T l, T r) {
           return BinarySearch.this.items_.get(l).compareTo(BinarySearch.this.items_.get(r));
         }
       });
 }
  @Override
  public void sessionDestroyed(HttpSessionEvent sessionEvent) {

    HttpSession session = sessionEvent.getSession();
    String sessionID = session.getId();
    ApplicationContext applicationContext =
        WebApplicationContextUtils.getWebApplicationContext(session.getServletContext());
    AbstractMap<String, Future> futureHolder =
        (AbstractMap<String, Future>) applicationContext.getBean("futureHolder");
    Set<String> keySet = futureHolder.keySet();
    List<String> keysToRemove = new ArrayList<String>();

    for (String key : keySet) {
      if (key.endsWith("@" + sessionID)) {
        keysToRemove.add(key);
      }
    }

    for (String removeMe : keysToRemove) {
      futureHolder.remove(removeMe);
    }
  }
Ejemplo n.º 5
0
 /**
  * Returns a shallow copy of this <tt>AbstractMap</tt> instance: the keys and values themselves
  * are not cloned.
  *
  * @return a shallow copy of this map.
  */
 protected Object clone() throws CloneNotSupportedException {
   AbstractMap result = (AbstractMap) super.clone();
   result.keySet = null;
   result.values = null;
   return result;
 }
Ejemplo n.º 6
0
 /**
  * Back-end to pick using heuristic algorithm. The complexity is O(2^n).
  *
  * @param limit maximum value of combinations
  * @param items object value table
  * @return solution
  */
 public static <T> Pack<T> geneticAlgorithm(Long limit, AbstractMap<T, Long> items) {
   return (Pack.needSearch(limit, items))
       ? new GeneticAlgorithm<T>(limit, items).call()
       : new Pack<T>(limit, new ArrayList<T>(items.keySet()));
 }
Ejemplo n.º 7
0
 /**
  * Back-end to pick using DFS.
  *
  * @param limit maximum value of combinations
  * @param items object value table
  * @return solution
  */
 public static <T> Pack<T> depthFirstSearch(Long limit, AbstractMap<T, Long> items) {
   return (Pack.needSearch(limit, items))
       ? new DepthFirstSearch<T>(limit, items).call(0, new Pack<T>())
       : new Pack<T>(limit, new ArrayList<T>(items.keySet()));
 }
Ejemplo n.º 8
0
 public static <T> Pack<T> binarySearch(Long limit, AbstractMap<T, Long> items) {
   return (Pack.needSearch(limit, items))
       ? new BinarySearch<T>(limit, items).call()
       : new Pack<T>(limit, new ArrayList<T>(items.keySet()));
 }