MCPcopy Create free account
hub / github.com/codenameone/CodenameOne / putImpl

Method putImpl

Ports/CLDC11/src/java/util/TreeMap.java:4435–4647  ·  view source on GitHub ↗
(K key, V value)

Source from the content-addressed store, hash-verified

4433 }
4434
4435 private V putImpl(K key, V value) {
4436 if (root == null) {
4437 root = createNode(key, value);
4438 size = 1;
4439 modCount++;
4440 return null;
4441 }
4442 java.lang.Comparable<K> object = comparator == null ? toComparable((K) key)
4443 : null;
4444 K keyK = (K) key;
4445 Node<K, V> node = root;
4446 Node<K, V> prevNode = null;
4447 int result = 0;
4448 while (node != null) {
4449 prevNode = node;
4450 K[] keys = node.keys;
4451 int left_idx = node.left_idx;
4452 result = cmp(object, keyK, keys[left_idx]);
4453 if (result < 0) {
4454 node = node.left;
4455 } else if (result == 0) {
4456 V res = node.values[left_idx];
4457 node.values[left_idx] = value;
4458 return res;
4459 } else {
4460 int right_idx = node.right_idx;
4461 if (left_idx != right_idx) {
4462 result = cmp(object, keyK, keys[right_idx]);
4463 }
4464 if (result > 0) {
4465 node = node.right;
4466 } else if (result == 0) {
4467 V res = node.values[right_idx];
4468 node.values[right_idx] = value;
4469 return res;
4470 } else { /* search in node */
4471 int low = left_idx + 1, mid = 0, high = right_idx - 1;
4472 while (low <= high) {
4473 mid = (low + high) >>> 1;
4474 result = cmp(object, keyK, keys[mid]);
4475 if (result > 0) {
4476 low = mid + 1;
4477 } else if (result == 0) {
4478 V res = node.values[mid];
4479 node.values[mid] = value;
4480 return res;
4481 } else {
4482 high = mid - 1;
4483 }
4484 }
4485 result = low;
4486 break;
4487 }
4488 }
4489 } /* while */
4490 /*
4491 * if(node == null) { if(prevNode==null) { - case of empty Tree } else {
4492 * result < 0 - prevNode.left==null - attach here result > 0 -

Callers 2

putMethod · 0.95
putAllMethod · 0.95

Calls 9

createNodeMethod · 0.95
toComparableMethod · 0.95
cmpMethod · 0.95
appendFromLeftMethod · 0.95
appendFromRightMethod · 0.95
attachToLeftMethod · 0.95
attachToRightMethod · 0.95
balanceMethod · 0.95
arraycopyMethod · 0.95

Tested by

no test coverage detected