Removes the mapping with the specified key from this map. @param key the key of the mapping to remove. @return the value of the removed mapping or null if no mapping for the specified key was found. @throws ClassCastException if the specified key cannot be com
(Object key)
| 4814 | * cannot handle {@code null} keys. |
| 4815 | */ |
| 4816 | @SuppressWarnings("unchecked") |
| 4817 | @Override |
| 4818 | public V remove(Object key) { |
| 4819 | java.lang.Comparable<K> object = comparator == null ? toComparable((K) key) : null; |
| 4820 | if (size == 0) { |
| 4821 | return null; |
| 4822 | } |
| 4823 | K keyK = (K) key; |
| 4824 | Node<K, V> node = root; |
| 4825 | while (node != null) { |
| 4826 | K[] keys = node.keys; |
| 4827 | int left_idx = node.left_idx; |
| 4828 | int result = cmp(object, keyK, keys[left_idx]); |
| 4829 | if (result < 0) { |
| 4830 | node = node.left; |
| 4831 | } else if (result == 0) { |
| 4832 | V value = node.values[left_idx]; |
| 4833 | removeLeftmost(node); |
| 4834 | return value; |
| 4835 | } else { |
| 4836 | int right_idx = node.right_idx; |
| 4837 | if (left_idx != right_idx) { |
| 4838 | result = cmp(object, keyK, keys[right_idx]); |
| 4839 | } |
| 4840 | if (result > 0) { |
| 4841 | node = node.right; |
| 4842 | } else if (result == 0) { |
| 4843 | V value = node.values[right_idx]; |
| 4844 | removeRightmost(node); |
| 4845 | return value; |
| 4846 | } else { /*search in node*/ |
| 4847 | int low = left_idx + 1, mid = 0, high = right_idx - 1; |
| 4848 | while (low <= high) { |
| 4849 | mid = (low + high) >>> 1; |
| 4850 | result = cmp(object, keyK, keys[mid]); |
| 4851 | if (result > 0) { |
| 4852 | low = mid + 1; |
| 4853 | } else if (result == 0) { |
| 4854 | V value = node.values[mid]; |
| 4855 | removeMiddleElement(node, mid); |
| 4856 | return value; |
| 4857 | } else { |
| 4858 | high = mid - 1; |
| 4859 | } |
| 4860 | } |
| 4861 | return null; |
| 4862 | } |
| 4863 | } |
| 4864 | } |
| 4865 | return null; |
| 4866 | } |
| 4867 | |
| 4868 | K removeLeftmost(Node<K, V> node) { |
| 4869 | int index = node.left_idx; |
no test coverage detected