@Override
 public boolean add(K k) {
   if (k == null) throw new NullPointerException();
   boolean retVal = !bitSet.get(k.getOrdinal());
   if (retVal) {
     bitSet.set(k.getOrdinal());
     size++;
   }
   return retVal;
 }
  @Override
  public boolean remove(Object o) {
    if (o == null) return false;
    K k = (K) o;
    boolean retVal = bitSet.get(k.getOrdinal());
    if (retVal) {
      bitSet.set(k.getOrdinal(), false);
      size--;
    }

    return retVal;
  }
예제 #3
0
 /**
  * toString methode: creates a String representation of the object
  *
  * @return the String representation
  * @author info.vancauwenberge.tostring plugin
  */
 public String toString() {
   StringBuffer buffer = new StringBuffer();
   ArrayList keys = new ArrayList(hashtable.keySet());
   Collections.sort(keys);
   for (K key : (List<K>) keys) {
     ArrayList<O> vals = get(key);
     buffer.append(key.toString()).append("(").append(vals.size()).append(")").append(":\n");
     for (O val : vals) {
       buffer.append("\t").append(val.toString()).append("\n");
     }
   }
   return buffer.toString();
 }
예제 #4
0
  public String getStratsAndStatesStringForKey(final int key) {
    if (keyStrings.get(key) == null) {
      StringBuilder b = new StringBuilder();
      for (int i = 0; i < stratifiers.size(); i++) {
        final K strat = stratifiers.get(i);
        final Object stratValue = stratifierValuesByKey.get(key).get(i);
        b.append(strat.toString()).append(":").append(stratValue.toString());
      }
      keyStrings.set(key, b.toString());
    }

    return keyStrings.get(key);
  }
예제 #5
0
  /**
   * Remaps the stratifications from one stratification set to another, combining the values in V
   * according to the combiner function.
   *
   * <p>stratifierToReplace defines a set of states S1, while newStratifier defines a new set S2.
   * remappedStates is a map from all of S1 into at least some of S2. This function creates a new,
   * fully initialized manager where all of the data in this new manager is derived from the
   * original data in this object combined according to the mapping remappedStates. When multiple
   * elements of S1 can map to the same value in S2, these are sequentially combined by the function
   * combiner. Suppose for example at states s1, s2, and s3 all map to N1. Eventually the value
   * associated with state N1 would be
   *
   * <p>value(N1) = combine(value(s1), combine(value(s2), value(s3))
   *
   * <p>in some order for s1, s2, and s3, which is not defined. Note that this function only
   * supports combining one stratification at a time, but in principle a loop over stratifications
   * and this function could do the multi-dimensional collapse.
   *
   * @param stratifierToReplace
   * @param newStratifier
   * @param combiner
   * @param remappedStates
   * @return
   */
  public StratificationManager<K, V> combineStrats(
      final K stratifierToReplace,
      final K newStratifier,
      final Combiner<V> combiner,
      final Map<Object, Object> remappedStates) {
    // make sure the mapping is reasonable
    if (!newStratifier.getAllStates().containsAll(remappedStates.values()))
      throw new ReviewedGATKException(
          "combineStrats: remapped states contains states not found in newStratifer state set");

    if (!remappedStates.keySet().containsAll(stratifierToReplace.getAllStates()))
      throw new ReviewedGATKException(
          "combineStrats: remapped states missing mapping for some states");

    // the new strats are the old ones with the single replacement
    final List<K> newStrats = new ArrayList<K>(getStratifiers());
    final int stratOffset = newStrats.indexOf(stratifierToReplace);
    if (stratOffset == -1)
      throw new ReviewedGATKException(
          "Could not find strat to replace "
              + stratifierToReplace
              + " in existing strats "
              + newStrats);
    newStrats.set(stratOffset, newStratifier);

    // create an empty but fully initialized new manager
    final StratificationManager<K, V> combined = new StratificationManager<K, V>(newStrats);

    // for each key, get its state, update it according to the map, and update the combined manager
    for (int key = 0; key < size(); key++) {
      // the new state is just the old one with the replacement
      final List<Object> newStates = new ArrayList<Object>(getStatesForKey(key));
      final Object oldState = newStates.get(stratOffset);
      final Object newState = remappedStates.get(oldState);
      newStates.set(stratOffset, newState);

      // look up the new key given the new state
      final int combinedKey = combined.getKey(newStates);
      if (combinedKey == -1)
        throw new ReviewedGATKException(
            "Couldn't find key for states: " + Utils.join(",", newStates));

      // combine the old value with whatever new value is in combined already
      final V combinedValue = combiner.combine(combined.get(combinedKey), get(key));

      // update the value associated with combined key
      combined.set(combinedKey, combinedValue);
    }

    return combined;
  }
예제 #6
0
    V put(K key, int hash, V value, boolean onlyIfAbsent) {
      lock();
      try {
        int c = count;
        HashEntry[] tab = table;
        int index = hash & (tab.length - 1);
        HashEntry<K, V> first = (HashEntry<K, V>) tab[index];

        for (HashEntry<K, V> e = first; e != null; e = (HashEntry<K, V>) e.next) {
          if (e.hash == hash && key.equals(e.key)) {
            V oldValue = e.value;
            if (!onlyIfAbsent) e.value = value;
            ++modCount;
            count = c; // write-volatile
            return oldValue;
          }
        }

        tab[index] = new HashEntry<K, V>(hash, key, value, first);
        ++modCount;
        ++c;
        count = c; // write-volatile
        if (c > threshold) setTable(rehash(tab));
        return null;
      } finally {
        unlock();
      }
    }
예제 #7
0
    boolean replace(K key, int hash, V oldValue, V newValue) {
      lock();
      try {
        int c = count;
        HashEntry[] tab = table;
        int index = hash & (tab.length - 1);
        HashEntry<K, V> first = (HashEntry<K, V>) tab[index];
        HashEntry<K, V> e = first;
        for (; ; ) {
          if (e == null) return false;
          if (e.hash == hash && key.equals(e.key)) break;
          e = e.next;
        }

        V v = e.value;
        if (v == null || !oldValue.equals(v)) return false;

        e.value = newValue;
        count = c; // write-volatile
        return true;

      } finally {
        unlock();
      }
    }
 synchronized boolean remove(long hash, K key) {
   int h = smallMap.startSearch(hash);
   boolean found = false;
   while (true) {
     int pos = smallMap.nextPos();
     if (pos < 0) {
       break;
     }
     bytes.storePositionAndSize(store, pos * smallEntrySize, smallEntrySize);
     K key2 = getKey();
     if (equals(key, key2)) {
       usedSet.clear(pos);
       smallMap.remove(h, pos);
       found = true;
       this.size--;
       break;
     }
   }
   K key2 = key instanceof CharSequence ? (K) key.toString() : key;
   DirectStore remove = map.remove(key2);
   if (remove == null) return found;
   offHeapUsed -= remove.size();
   remove.free();
   this.size--;
   return true;
 }
 public V put(K key, V value) {
   V oldValue = null;
   int index = Math.abs(key.hashCode()) % SIZE;
   if (buckets[index] == null) buckets[index] = new ArrayList<MapEntry<K, V>>();
   ArrayList<MapEntry<K, V>> bucket = buckets[index];
   MapEntry<K, V> pair = new MapEntry<K, V>(key, value);
   boolean found = false;
   ListIterator<MapEntry<K, V>> it = bucket.listIterator();
   // int probes=0;
   while (it.hasNext()) {
     //	probes++;
     MapEntry<K, V> iPair = it.next();
     //  if(!iPair.getKey().equals(key)) System.out.println("HashCode collision on put, have
     // key:"+key+" hascode:"+key.hashCode()+" found key:"+iPair.getKey()+" hashcode:"+
     // iPair.getKey().hashCode());
     if (iPair.getKey().equals(key)) {
       oldValue = iPair.getValue();
       it.set(pair); // Replace old with new
       found = true;
       break;
     }
   }
   // System.out.println("put() probes for key "+ key+" :"+probes);
   if (!found) buckets[index].add(pair);
   return oldValue;
 }
 synchronized V get(long hash, K key, V value) {
   smallMap.startSearch(hash);
   while (true) {
     int pos = smallMap.nextPos();
     if (pos < 0) {
       K key2 = key instanceof CharSequence ? (K) key.toString() : key;
       final DirectStore store = map.get(key2);
       if (store == null) return null;
       bytes.storePositionAndSize(store, 0, store.size());
       break;
     } else {
       bytes.storePositionAndSize(store, pos * smallEntrySize, smallEntrySize);
       K key2 = getKey();
       if (equals(key, key2)) break;
     }
   }
   if (bytesMarshallable) {
     try {
       V v = value == null ? (V) NativeBytes.UNSAFE.allocateInstance(vClass) : value;
       ((BytesMarshallable) v).readMarshallable(bytes);
       return v;
     } catch (InstantiationException e) {
       throw new AssertionError(e);
     }
   }
   return (V) bytes.readObject();
 }
예제 #11
0
 public int lastIndexOfSecond(K k, int index) {
   try {
     for (int i = index; i >= 0; i--) {
       if ((k == null ? get(i).second == null : k.equals(get(i).second))) return i;
     }
   } catch (final Exception e) {
   }
   return -1;
 }
예제 #12
0
 public synchronized boolean removeSecond(K k) {
   Quint<T, K, L, M, N> pair;
   for (final Iterator<Quint<T, K, L, M, N>> i = iterator(); i.hasNext(); ) {
     pair = i.next();
     if ((k == null ? pair.second == null : k.equals(pair.second))) {
       i.remove();
       return true;
     }
   }
   return false;
 }
예제 #13
0
  /**
   * Recursive construction helper for main constructor that fills into the complete tree of
   * StratNodes. This function returns the complete tree suitable for associating data with each
   * combinatino of keys. Note that the tree is not fully complete as the keys are not yet set for
   * each note (see assignStratifierValuesByKey)
   *
   * @param strats
   * @return
   */
  private StratNode<K> buildStratificationTree(final Queue<K> strats) {
    final K first = strats.poll();
    if (first == null) {
      // we are at a leaf
      return new StratNode<K>();
    } else {
      // we are in the middle of the tree
      final Collection<Object> states = first.getAllStates();

      if (states.isEmpty()) throw new ReviewedGATKException("State " + first + " is empty!");

      final LinkedHashMap<Object, StratNode<K>> subNodes =
          new LinkedHashMap<Object, StratNode<K>>(states.size());
      for (final Object state : states) {
        // have to copy because poll modifies the queue
        final Queue<K> copy = new LinkedList<K>(strats);
        subNodes.put(state, buildStratificationTree(copy));
      }
      return new StratNode<K>(first, subNodes);
    }
  }
  public int hashFunction(K dkey) {
    String hashstr = dkey.toString();
    int hashCode = 0;
    for (int i = 0; i < hashstr.length(); i++) {
      int code = (int) hashstr.charAt(i) * 32 ^ i;
      hashCode = hashCode + code;
    }

    hashCode = hashCode % size;
    // System.out.println(hashCode);
    return hashCode;
  }
    final long hash(K key) {
      if (isCharSequence) {
        h = Maths.hash((CharSequence) key);
      } else if (isLongHashable) {
        h = ((LongHashable) key).longHashCode();
      } else {
        h = (long) key.hashCode() << 31;
      }
      h += (h >>> 42) - (h >>> 21);
      h += (h >>> 14) - (h >>> 7);

      return h >>> segmentShift;
    }
 synchronized boolean containsKey(long hash, K key) {
   smallMap.startSearch(hash);
   while (true) {
     int pos = smallMap.nextPos();
     if (pos < 0) {
       K key2 = key instanceof CharSequence ? (K) key.toString() : key;
       return map.containsKey(key2);
     }
     bytes.storePositionAndSize(store, pos * smallEntrySize, smallEntrySize);
     K key2 = getKey();
     if (equals(key, key2)) {
       return true;
     }
   }
 }
 public V put(K key, V value) {
   V oldValue = null;
   int index = Math.abs(key.hashCode()) % SIZE;
   if (buckets[index] == null) buckets[index] = new LinkedList<>();
   LinkedList<MapEntry<K, V>> bucket = buckets[index];
   MapEntry<K, V> pair = new MapEntry<>(key, value);
   boolean found = false;
   ListIterator<MapEntry<K, V>> it = bucket.listIterator();
   while (it.hasNext()) {
     MapEntry<K, V> iPair = it.next();
     if (iPair.getKey().equals(key)) {
       oldValue = iPair.getValue();
       it.set(pair); // Replace old with new
       found = true;
       break;
     }
   }
   if (!found) buckets[index].add(pair);
   return oldValue;
 }
    synchronized void put(long hash, K key, V value, boolean ifPresent, boolean ifAbsent) {
      // search for the previous entry
      int h = smallMap.startSearch(hash);
      boolean foundSmall = false, foundLarge = false;
      while (true) {
        int pos = smallMap.nextPos();
        if (pos < 0) {
          K key2 = key instanceof CharSequence ? (K) key.toString() : key;
          final DirectStore store = map.get(key2);
          if (store == null) {
            if (ifPresent && !ifAbsent) return;
            break;
          }
          if (ifAbsent) return;
          bytes.storePositionAndSize(store, 0, store.size());
          foundLarge = true;
          break;
        } else {
          bytes.storePositionAndSize(store, pos * smallEntrySize, smallEntrySize);
          K key2 = getKey();
          if (equals(key, key2)) {
            if (ifAbsent && !ifPresent) return;
            foundSmall = true;
            break;
          }
        }
      }

      tmpBytes.clear();
      if (csKey)
        //noinspection ConstantConditions
        tmpBytes.writeUTFΔ((CharSequence) key);
      else tmpBytes.writeObject(key);
      long startOfValuePos = tmpBytes.position();
      if (bytesMarshallable) ((BytesMarshallable) value).writeMarshallable(tmpBytes);
      else tmpBytes.writeObject(value);
      long size = tmpBytes.position();
      if (size <= smallEntrySize) {
        if (foundSmall) {
          bytes.position(0);
          bytes.write(tmpBytes, 0, size);
          return;
        } else if (foundLarge) {
          remove(hash, key);
        }
        // look for a free spot.
        int position = h & (entriesPerSegment - 1);
        int free = usedSet.nextClearBit(position);
        if (free >= entriesPerSegment) free = usedSet.nextClearBit(0);
        if (free < entriesPerSegment) {
          bytes.storePositionAndSize(store, free * smallEntrySize, smallEntrySize);
          bytes.write(tmpBytes, 0, size);
          smallMap.put(h, free);
          usedSet.set(free);
          this.size++;
          return;
        }
      }
      if (foundSmall) {
        remove(hash, key);
      } else if (foundLarge) {
        // can it be reused.
        if (bytes.capacity() <= size || bytes.capacity() - size < (size >> 3)) {
          bytes.write(tmpBytes, startOfValuePos, size);
          return;
        }
        remove(hash, key);
      }
      size = size - startOfValuePos;
      DirectStore store = new DirectStore(bmf, size);
      bytes.storePositionAndSize(store, 0, size);
      bytes.write(tmpBytes, startOfValuePos, size);
      K key2 = key instanceof CharSequence ? (K) key.toString() : key;
      map.put(key2, store);
      offHeapUsed += size;
      this.size++;
    }
 boolean equals(K key, K key2) {
   return csKey ? equalsCS((CharSequence) key, (CharSequence) key2) : key.equals(key2);
 }
예제 #20
0
 public boolean containsSecond(K k) {
   for (final Iterator<Quint<T, K, L, M, N>> i = iterator(); i.hasNext(); ) {
     if ((k == null) ? i.next() == null : k.equals(i.next().second)) return true;
   }
   return false;
 }
예제 #21
0
 public int hashCode() {
   return ((key == null) ? 0 : key.hashCode()) ^ ((value == null) ? 0 : value.hashCode());
 }
  @Override
  public boolean contains(Object o) {
    K k = (K) o;

    return k != null && bitSet.get(k.getOrdinal());
  }
예제 #23
0
  /**
   * Gets registered object from CacheMap. The algorithm used to look up is <br>
   * 1. First check for exact match with clazz and context.<br>
   * 2. If didn't find, look for interfaces that clazz implements using the exact context.<br>
   * 3. If still didn't find, look for super class of clazz using the exact context. <br>
   * 4. If still didn't find, using the exact clazz with default context.<br>
   * 5. If still didn't find, return null.<br>
   * If found a match in step 1, 2, 3 or 4, it will return the registered object immediately.
   *
   * @param clazz the class which is used as the primary key.
   * @param context the context which is used as the secondary key. This parameter could be null in
   *     which case the default context is used.
   * @return registered object the object associated with the class and the context.
   */
  public T getRegisteredObject(Class<?> clazz, K context) {
    if (clazz == null) {
      return null;
    }

    Cache<K, T> cache = getCache(clazz);

    if (cache == null || !cache.containsKey(context)) {
      List<Class<?>> classesToSearch = new ArrayList<>();

      classesToSearch.add(clazz);
      if (TypeUtils.isPrimitive(clazz)) {
        classesToSearch.add(TypeUtils.convertPrimitiveToWrapperType(clazz));
      } else if (TypeUtils.isPrimitiveWrapper(clazz)) {
        classesToSearch.add(TypeUtils.convertWrapperToPrimitiveType(clazz));
      }

      // Direct super interfaces, recursively
      addAllInterfaces(classesToSearch, clazz);

      Class<?> superClass = clazz;
      // Direct super class, recursively
      while (!superClass.isInterface()) {
        superClass = superClass.getSuperclass();
        if (superClass != null) {
          classesToSearch.add(superClass);
          addAllInterfaces(classesToSearch, superClass);
        } else {
          break;
        }
      }

      if (!classesToSearch.contains(Object.class)) {
        classesToSearch.add(Object.class); // use Object as default fallback.
      }

      // search to match context first
      for (Class<?> c : classesToSearch) {
        Cache<K, T> cacheForClass = getCache(c);
        if (cacheForClass != null) {
          T object = cacheForClass.getObject(context);
          if (object != null) {
            return object;
          }
        }
      }

      // fall back to default context
      if (!_defaultContext.equals(context)) {
        for (Class<?> c : classesToSearch) {
          Cache<K, T> cacheForClass = getCache(c);
          if (cacheForClass != null) {
            T object = cacheForClass.getObject(_defaultContext);
            if (object != null) {
              return object;
            }
          }
        }
      }
    }

    if (cache != null) {
      T object = cache.getObject(context);
      if (object == null && !_defaultContext.equals(context)) {
        return getRegisteredObject(clazz, _defaultContext);
      }
      if (object != null) {
        return object;
      }
    }

    return null;
  }
 public V resolve(K key) {
   return resolve((Class<? extends K>) key.getClass());
 }
예제 #25
0
    // O: 1-{4,5,6,7} 0-{0,1,2,3}
    // P: 1-{2,3,6,7} 0-{0,1,4,5}
    // S: 1-{1,3,5,7} 0-{0,2,4,6}
    public int getPartition(K key, V value, int numReduceTasks) {

      String line = key.toString();

      if (line.startsWith("O")) // order
      {
        String keyIdS = line.substring(1, line.length() - 1);
        String placeId = line.substring(line.length() - 1);
        Integer keyId = 0;

        try {
          keyId = Integer.parseInt(keyIdS);
        } catch (Exception ex) {
          return 0;
        }
        if (keyId % 2 == 1) {
          // 4,5,6,7
          if (placeId.equalsIgnoreCase("A")) {
            return 4;
          } else if (placeId.equalsIgnoreCase("B")) {
            return 5;
          } else if (placeId.equalsIgnoreCase("C")) {
            return 6;
          } else if (placeId.equalsIgnoreCase("D")) {
            return 7;
          } else {
            return 0;
          }
        } else {
          // 0,1,2,3
          if (placeId.equalsIgnoreCase("A")) {
            return 0;
          } else if (placeId.equalsIgnoreCase("B")) {
            return 1;
          } else if (placeId.equalsIgnoreCase("C")) {
            return 2;
          } else if (placeId.equalsIgnoreCase("D")) {
            return 3;
          } else {
            return 0;
          }
        }
      } else if (line.startsWith("P")) // part
      {
        String keyIdS = line.substring(1, line.length() - 1);
        String placeId = line.substring(line.length() - 1);
        Integer keyId = 0;

        try {
          keyId = Integer.parseInt(keyIdS);
        } catch (Exception ex) {
          return 0;
        }

        if (keyId % 2 == 1) {
          // 2,3,6,7
          if (placeId.equalsIgnoreCase("A")) {
            return 2;
          } else if (placeId.equalsIgnoreCase("B")) {
            return 3;
          } else if (placeId.equalsIgnoreCase("C")) {
            return 6;
          } else if (placeId.equalsIgnoreCase("D")) {
            return 7;
          } else {
            return 0;
          }
        } else {
          // 0,1,4,5
          if (placeId.equalsIgnoreCase("A")) {
            return 0;
          } else if (placeId.equalsIgnoreCase("B")) {
            return 1;
          } else if (placeId.equalsIgnoreCase("C")) {
            return 4;
          } else if (placeId.equalsIgnoreCase("D")) {
            return 5;
          } else {
            return 0;
          }
        }
      } else if (line.startsWith("S")) // supplier
      {
        String keyIdS = line.substring(1, line.length() - 1);
        String placeId = line.substring(line.length() - 1);
        Integer keyId = 0;

        try {
          keyId = Integer.parseInt(keyIdS);
        } catch (Exception ex) {
          return 0;
        }

        if (keyId % 2 == 1) {
          // 1,3,5,7
          if (placeId.equalsIgnoreCase("A")) {
            return 1;
          } else if (placeId.equalsIgnoreCase("B")) {
            return 3;
          } else if (placeId.equalsIgnoreCase("C")) {
            return 5;
          } else if (placeId.equalsIgnoreCase("D")) {
            return 7;
          } else {
            return 0;
          }
        } else {
          // 0,2,4,6
          if (placeId.equalsIgnoreCase("A")) {
            return 0;
          } else if (placeId.equalsIgnoreCase("B")) {
            return 2;
          } else if (placeId.equalsIgnoreCase("C")) {
            return 4;
          } else if (placeId.equalsIgnoreCase("D")) {
            return 6;
          } else {
            return 0;
          }
        }
      } else if (line.startsWith("L")) // lineitem
      {
        String[] keyIdS = line.substring(1).split("[+]");

        Integer keyId0 = 0;
        Integer keyId1 = 0;
        Integer keyId2 = 0;

        try {
          keyId0 = Integer.parseInt(keyIdS[0].substring(1)) % 2; // Order
          keyId1 = Integer.parseInt(keyIdS[1].substring(1)) % 2; // Part
          keyId2 = Integer.parseInt(keyIdS[2].substring(1)) % 2; // Supplier
        } catch (Exception ex) {
          return 0;
        }
        return 4 * keyId0 + 2 * keyId1 + 1 * keyId2;

      } else {
        return 0;
      }
    }
예제 #26
0
파일: Sorter.java 프로젝트: gifford-lab/GEM
 public int compareTo(Sortable s) {
   return key.compareTo(s.key);
 }
예제 #27
0
파일: Sorter.java 프로젝트: gifford-lab/GEM
 public int hashCode() {
   int code = 17;
   code += key.hashCode();
   code *= 37;
   return code;
 }