Maps the specified key to the specified value. @param key the key. @param value the value. @return the value of any previous mapping with the specified key or null if there was no mapping. @throws ClassCastException if the specified key cannot be co
(K key, V value)
| 1445 | * cannot handle {@code null} keys. |
| 1446 | */ |
| 1447 | @Override |
| 1448 | public V put(K key, V value) { |
| 1449 | if (root == null) { |
| 1450 | root = createNode(key, value); |
| 1451 | size = 1; |
| 1452 | modCount++; |
| 1453 | return null; |
| 1454 | } |
| 1455 | Comparable<K> object = comparator == null ? toComparable((K) key) : null; |
| 1456 | K keyK = (K) key; |
| 1457 | Node<K, V> node = root; |
| 1458 | Node<K, V> prevNode = null; |
| 1459 | int result = 0; |
| 1460 | while (node != null) { |
| 1461 | prevNode = node; |
| 1462 | K[] keys = node.keys; |
| 1463 | int left_idx = node.left_idx; |
| 1464 | result = cmp(object, keyK, keys[left_idx]); |
| 1465 | if (result < 0) { |
| 1466 | node = node.left; |
| 1467 | } else if (result == 0) { |
| 1468 | V res = node.values[left_idx]; |
| 1469 | node.values[left_idx] = value; |
| 1470 | return res; |
| 1471 | } else { |
| 1472 | int right_idx = node.right_idx; |
| 1473 | if (left_idx != right_idx) { |
| 1474 | result = cmp(object, keyK, keys[right_idx]); |
| 1475 | } |
| 1476 | if (result > 0) { |
| 1477 | node = node.right; |
| 1478 | } else if (result == 0) { |
| 1479 | V res = node.values[right_idx]; |
| 1480 | node.values[right_idx] = value; |
| 1481 | return res; |
| 1482 | } else { /*search in node*/ |
| 1483 | int low = left_idx + 1, mid = 0, high = right_idx - 1; |
| 1484 | while (low <= high) { |
| 1485 | mid = (low + high) >>> 1; |
| 1486 | result = cmp(object, keyK, keys[mid]); |
| 1487 | if (result > 0) { |
| 1488 | low = mid + 1; |
| 1489 | } else if (result == 0) { |
| 1490 | V res = node.values[mid]; |
| 1491 | node.values[mid] = value; |
| 1492 | return res; |
| 1493 | } else { |
| 1494 | high = mid - 1; |
| 1495 | } |
| 1496 | } |
| 1497 | result = low; |
| 1498 | break; |
| 1499 | } |
| 1500 | } |
| 1501 | } /* while */ |
| 1502 | /* |
| 1503 | if(node == null) { |
| 1504 | if(prevNode==null) { |
nothing calls this directly
no test coverage detected