Exemplo n.º 1
0
 /** Note the same entry instance is returned each time this method is called. */
 public Entry next() {
   if (!hasNext) throw new NoSuchElementException();
   if (!valid) throw new GdxRuntimeException("#iterator() cannot be used nested.");
   int[] keyTable = map.keyTable;
   if (nextIndex == INDEX_ZERO) {
     entry.key = 0;
     entry.value = map.zeroValue;
   } else {
     entry.key = keyTable[nextIndex];
     entry.value = map.valueTable[nextIndex];
   }
   currentIndex = nextIndex;
   findNextIndex();
   return entry;
 }
 /**
  * Reads the given number of entries from the input stream, returning the corresponding tree.
  *
  * @param s the input stream.
  * @param n the (positive) number of entries to read.
  * @param pred the entry containing the key that preceeds the first key in the tree.
  * @param succ the entry containing the key that follows the last key in the tree.
  */
 @SuppressWarnings("unchecked")
 private Entry<K> readTree(
     final java.io.ObjectInputStream s, final int n, final Entry<K> pred, final Entry<K> succ)
     throws java.io.IOException, ClassNotFoundException {
   if (n == 1) {
     final Entry<K> top = new Entry<K>((K) s.readObject());
     top.pred(pred);
     top.succ(succ);
     top.black(true);
     return top;
   }
   if (n == 2) {
     /* We handle separately this case so that recursion will
      *always* be on nonempty subtrees. */
     final Entry<K> top = new Entry<K>((K) s.readObject());
     top.black(true);
     top.right(new Entry<K>((K) s.readObject()));
     top.right.pred(top);
     top.pred(pred);
     top.right.succ(succ);
     return top;
   }
   // The right subtree is the largest one.
   final int rightN = n / 2, leftN = n - rightN - 1;
   final Entry<K> top = new Entry<K>();
   top.left(readTree(s, leftN, pred, top));
   top.key = (K) s.readObject();
   top.black(true);
   top.right(readTree(s, rightN, top, succ));
   if (n + 2 == ((n + 2) & -(n + 2)))
     top.right.black(false); // Quick test for determining whether n + 2 is a power of 2.
   return top;
 }
Exemplo n.º 3
0
  /** Expunges stale entries from the table. */
  private void expungeStaleEntries() {
    Entry<K, V> e;
    while ((e = (Entry<K, V>) queue.poll()) != null) {
      int h = e.hash;
      int i = indexFor(h, table.length);

      Entry<K, V> prev = table[i];
      Entry<K, V> p = prev;
      while (p != null) {
        Entry<K, V> next = p.next;
        if (p == e) {
          if (prev == e) table[i] = next;
          else {
            prev.next = next;
            if (prev == next) {
              throw new RuntimeException("circular");
            }
          }
          e.next = null; // Help GC
          e.key = null; //  "   "
          size--;
          break;
        }
        prev = p;
        p = next;
      }
    }
  }
Exemplo n.º 4
0
  private Node insert(Node h, Key key, Value value, int ht) {
    int j;
    Entry t = new Entry(key, value, null);

    // external node
    if (ht == 0) {
      for (j = 0; j < h.m; j++) {
        if (less(key, h.children[j].key)) break;
      }
    }

    // internal node
    else {
      for (j = 0; j < h.m; j++) {
        if ((j + 1 == h.m) || less(key, h.children[j + 1].key)) {
          Node u = insert(h.children[j++].next, key, value, ht - 1);
          if (u == null) return null;
          t.key = u.children[0].key;
          t.next = u;
          break;
        }
      }
    }

    for (int i = h.m; i > j; i--) h.children[i] = h.children[i - 1];
    h.children[j] = t;
    h.m++;
    if (h.m < M) return null;
    else return split(h);
  }
 /**
  * Add an entry to the cache. The entry may or may not exist in the cache yet. This method will
  * usually mark unknown entries as cold and known entries as hot.
  *
  * @param key the key (may not be null)
  * @param hash the hash
  * @param value the value (may not be null)
  * @param memory the memory used for the given entry
  * @return the old value, or null if there was no resident entry
  */
 synchronized V put(long key, int hash, V value, int memory) {
   if (value == null) {
     throw DataUtils.newIllegalArgumentException("The value may not be null");
   }
   V old;
   Entry<V> e = find(key, hash);
   if (e == null) {
     old = null;
   } else {
     old = e.value;
     remove(key, hash);
   }
   e = new Entry<V>();
   e.key = key;
   e.value = value;
   e.memory = memory;
   int index = hash & mask;
   e.mapNext = entries[index];
   entries[index] = e;
   usedMemory += memory;
   if (usedMemory > maxMemory && mapSize > 0) {
     // an old entry needs to be removed
     evict(e);
   }
   mapSize++;
   // added entries are always added to the stack
   addToStack(e);
   return old;
 }
 private static <V> Entry<V> copy(Entry<V> old) {
   Entry<V> e = new Entry<V>();
   e.key = old.key;
   e.value = old.value;
   e.memory = old.memory;
   e.topMove = old.topMove;
   return e;
 }
Exemplo n.º 7
0
 /** Note the same entry instance is returned each time this method is called. */
 public Entry<K> next() {
   if (!hasNext) throw new NoSuchElementException();
   K[] keyTable = map.keyTable;
   entry.key = keyTable[nextIndex];
   entry.value = map.valueTable[nextIndex];
   currentIndex = nextIndex;
   findNextIndex();
   return entry;
 }
 private Entry<E> makeEntry(E key) {
   Entry<E> entry = new Entry<E>();
   entry.index = size();
   entry.key = key;
   entry.priority = Double.NEGATIVE_INFINITY;
   indexToEntry.add(entry);
   keyToEntry.put(key, entry);
   return entry;
 }
Exemplo n.º 9
0
 /** Note the same entry instance is returned each time this method is called. */
 public Entry<K> next() {
   if (!hasNext) throw new NoSuchElementException();
   if (!valid) throw new GdxRuntimeException("#iterator() cannot be used nested.");
   K[] keyTable = map.keyTable;
   entry.key = keyTable[nextIndex];
   entry.value = map.valueTable[nextIndex];
   currentIndex = nextIndex;
   findNextIndex();
   return entry;
 }
Exemplo n.º 10
0
 @SuppressWarnings("unchecked")
 public Entry<K> clone() {
   Entry<K> c;
   try {
     c = (Entry<K>) super.clone();
   } catch (CloneNotSupportedException cantHappen) {
     throw new InternalError();
   }
   c.key = key;
   c.info = info;
   return c;
 }
Exemplo n.º 11
0
  /**
   * Express a BinaryTreeNode as a String.
   *
   * @return a String representing the BinaryTreeNode.
   */
  public String toString() {
    String s = "";

    if (leftChild != null) {
      s = "(" + leftChild.toString() + ")";
    }
    s = s + entry.key().toString() + entry.value();
    if (rightChild != null) {
      s = s + "(" + rightChild.toString() + ")";
    }
    return s;
  }
Exemplo n.º 12
0
  public Map<String, Object> extractJson(String value) {
    if (isNullOrEmpty(value)) {
      return Collections.emptyMap();
    }

    final Map<String, Object> json;
    try {
      json = mapper.readValue(value, new TypeReference<Map<String, Object>>() {});
    } catch (IOException e) {
      return Collections.emptyMap();
    }

    final Map<String, Object> results = new HashMap<>(json.size());
    for (Map.Entry<String, Object> mapEntry : json.entrySet()) {
      for (Entry entry : parseValue(mapEntry.getKey(), mapEntry.getValue())) {
        results.put(entry.key(), entry.value());
      }
    }

    return results;
  }
Exemplo n.º 13
0
 /** Transfers all entries from src to dest tables */
 private void transfer(Entry[] src, Entry[] dest) {
   for (int j = 0; j < src.length; ++j) {
     Entry<K, V> e = src[j];
     src[j] = null;
     while (e != null) {
       Entry<K, V> next = e.next;
       Object key = e.key;
       if (key == null) {
         e.next = null; // Help GC
         e.key = null; //  "   "
         size--;
       } else {
         int i = indexFor(e.hash, dest.length);
         e.next = dest[i];
         if (e == e.next) {
           throw new RuntimeException("circular");
         }
         dest[i] = e;
       }
       e = next;
     }
   }
 }
Exemplo n.º 14
0
 public LuaValue key() {
   return entry.key();
 }
 @Override
 public void putAll(List<Entry<K, V>> entries) {
   for (Entry<K, V> entry : entries) put(entry.key(), entry.value());
 }