Advertisement
Guest User

Untitled

a guest
Aug 24th, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. /**
  2. * Returns this map's entry for the given key, or {@code null} if the map
  3. * does not contain an entry for the key.
  4. *
  5. * @return this map's entry for the given key, or {@code null} if the map
  6. * does not contain an entry for the key
  7. * @throws ClassCastException if the specified key cannot be compared
  8. * with the keys currently in the map
  9. * @throws NullPointerException if the specified key is null
  10. * and this map uses natural ordering, or its comparator
  11. * does not permit null keys
  12. */
  13. final Entry<K,V> getEntry(Object key) {
  14. // Offload comparator-based version for sake of performance
  15. if (comparator != null)
  16. return getEntryUsingComparator(key);
  17. if (key == null)
  18. throw new NullPointerException();
  19. @SuppressWarnings("unchecked")
  20. Comparable<? super K> k = (Comparable<? super K>) key;
  21. Entry<K,V> p = root;
  22. while (p != null) {
  23. int cmp = k.compareTo(p.key);
  24. if (cmp < 0)
  25. p = p.left;
  26. else if (cmp > 0)
  27. p = p.right;
  28. else
  29. return p;
  30. }
  31. return null;
  32. }
  33.  
  34. /**
  35. * Gets the entry corresponding to the specified key; if no such entry
  36. * exists, returns the entry for the greatest key less than the specified
  37. * key; if no such entry exists, returns {@code null}.
  38. */
  39. final Entry<K,V> getFloorEntry(K key) {
  40. Entry<K,V> p = root;
  41. while (p != null) {
  42. int cmp = compare(key, p.key);
  43. if (cmp > 0) {
  44. if (p.right != null)
  45. p = p.right;
  46. else
  47. return p;
  48. } else if (cmp < 0) {
  49. if (p.left != null) {
  50. p = p.left;
  51. } else {
  52. Entry<K,V> parent = p.parent;
  53. Entry<K,V> ch = p;
  54. while (parent != null && ch == parent.left) {
  55. ch = parent;
  56. parent = parent.parent;
  57. }
  58. return parent;
  59. }
  60. } else
  61. return p;
  62.  
  63. }
  64. return null;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement