Inserts a key-value pair in a subtree

@param mixed             $key   The key
@param mixed             $value The value
@param RedBlackNode|null $node  The subtree root

@return RedBlackNode
483414
protected function nodeSet($key, $value, ?RedBlackNode $node): RedBlackNode
    {
        if ($node === null) {
            return new RedBlackNode($key, $value, 1, RedBlackNode::RED);
        }

        $comp = $this->comparator->compare($key, $node->key());

        if ($comp < 0) {
            $node->setLeft($this->nodeSet($key, $value, $node->left()));
        } elseif ($comp > 0) {
            $node->setRight($this->nodeSet($key, $value, $node->right()));
        } else {
            $node->setValue($value);
        }

        return $this->balanceOnInsert($node);
    }
456191
def insert(key, value)
      node = self

      # Traverse the tree to find the right node to add or replace the value.
      while node do
        # All nodes that we find on the way that are full will be split into
        # two half-full nodes.
        if node.keys.size >= @tree.order
          node = node.split_node
        end

        # Once we have reached a leaf node we can insert or replace the value.
        if node.is_leaf
          return node.insert_element(key, value)
        else
          # Descend into the right child node to add the value to.
          node = node.children[node.search_key_index(key)]
        end
      end

      PEROBS.log.fatal 'Could not find proper node to add to'
    end
736216
@Override
    public V put(String key, V value) {
        // Keys can not be null
        if (key == null) {
            throw new NullPointerException("Key can not be null");
        }

        // Empty keys are stored in the root
        if (key.equals("")) {
            PatriciaNode<V> node = new PatriciaNode<>(key, value, -1);
            node.setValue(value);
            root.setRight(node);
            entries++;
            return value;
        }

        // Find nearest node
        PatriciaNode<V> nearest = findNearestNode(key);

        // Key already exist, replace value and return
        if (key.equals(nearest.getKey())) {
            nearest.setValue(value);
            return value;
        }

        // Find differing bit and create new node to insert
        int bit = findFirstDifferingBit(key, nearest.getKey());
        PatriciaNode<V> node = new PatriciaNode<>(key, value, bit);

        // Insert new node
        insertNode(node);

        entries++;

        return value;
    }
290360
public function withParameter(string $key, $value): self
    {
        if ($this->lastOperation === Node::class) {
            $element = $this->elements->last();
        } else {
            $element = $this->elements->dropEnd(1)->last();
        }

        $element = $element->withParameter($key, $value);
        $path = new self;
        $path->lastOperation = $this->lastOperation;

        if ($this->lastOperation === Node::class) {
            $path->elements = $this
                ->elements
                ->dropEnd(1)
                ->add($element);
        } else {
            $path->elements = $this
                ->elements
                ->dropEnd(2)
                ->add($element)
                ->add($this->elements->last());
        }

        return $path;
    }
624783
protected function deserializeValue(Node $node, $key, $value) {
    if (!is_array($value)) {
      // force set value to avoid exceptions in this stage
      $node->setValue($key, $value, true);
    }
    else {
      $role = $key;
      if ($this->isMultiValued($node, $role)) {
        // deserialize children
        foreach($value as $childData) {
          $this->deserializeNode($childData, $node, $role);
        }
      }
      else {
        $this->deserializeNode($value, $node, $role);
      }
    }
  }
1216974
public final V put(K key, V value) {
	BinTreeNode<K,V> prev = null;
	BinTreeNode<K,V> node = root;
	int key_hash_code = key.hashCode();

	while(node != null) {
	    prev = node;
	    if(key_hash_code < node.keyHashCode) {
		node = node.left;
	    }
	    else {
		if((key_hash_code > node.keyHashCode) || !node.key.equals(key)) {
		    node = node.right;
		}
		else {
		    cachedHashCode -= node.hashCode();
		    V temp = node.value;
		    node.value = value;
		    cachedHashCode += node.hashCode();
		    return temp;
		}
	    }
	}

	size++;

	BinTreeNode<K,V> new_node = new BinTreeNode<K,V>(key, value);
	cachedHashCode += new_node.hashCode(); // invalidate the cached hash code

	if(prev == null) {
	    root = new_node;
	    return null;
	}

	if(key_hash_code < prev.keyHashCode) {
	    prev.left = new_node;
	}
	else {
	    prev.right = new_node;
	}

	return null;
    }
111151
public function add($key, $value, $expiration = null)
    {

        return $this->set($key, $value, $expiration);
    }
940279
public function createNode($parentKey, $key, $label, $value, $count, $selected)
    {
        /** @var $parentNode Node|null */
        $parentNode = isset($this->nodesByKey[$parentKey]) ? $this->nodesByKey[$parentKey] : null;
        $node = new Node($this, $parentNode, $key, $label, $value, $count, $selected);
        $this->nodesByKey[$key] = $node;

        if ($parentNode === null) {
            $this->addChildNode($node);
        } else {
            $parentNode->addChildNode($node);
        }

        $this->allNodes->add($node);
    }
1254966
public function createRoot(Node $node)
    {
        if($node instanceof NodeWrapper)
        {
            throw new \InvalidArgumentException('Can\'t create a root node from a NodeWrapper node');
        }

        $node->setLeftValue(1);
        $node->setRightValue(2);

        if($this->getConfiguration()->hasManyRoots())
        {
            $rootValue = $node->getId();
            if($rootValue === null)
            {
                // Set a temporary value in case wrapped node requires root value to be set
                $node->setRootValue(0);
                $this->getEntityManager()->persist($node);
                $this->getEntityManager()->flush();
                $rootValue = $node->getId();
            }

            if($rootValue === null)
            {
                // @codeCoverageIgnoreStart
                throw new \RuntimeException('Node must have an identifier available via getId()');
                // @codeCoverageIgnoreEnd
            }

            $node->setRootValue($rootValue);
        }


        $this->getEntityManager()->persist($node);
        $this->getEntityManager()->flush();

        return $this->wrapNode($node);
    }
1007219
public static function create($key, $value) {
    $node = new ArrayPairNode();
    $node->addChild($key, 'key');
    $node->addChild(Token::space());
    $node->addChild(Token::doubleArrow());
    $node->addChild(Token::space());
    $node->addChild($value, 'value');
    return $node;
  }
593995
public static function key($key, $value = null)
  {
    if ($value == null) return $_SESSION[$key];

    $_SESSION[$key] = $value;
  }
989400
public function set($key, $value = null)
    {
        return self::setValue($this->data, $key, $value);
    }
146365
public function attachPayload($key, $value)
    {
        if (is_null($this->payload)) {
            $this->payload = [];
        }

        $this->payload[$key] = $value;

        return $this;
    }
1259513
protected function appendValue($key, $value, $expiration = 0)
    {
        if ($this->redis->exists($key)) {
            $this->redis->append($key, $value);

            return $this->redis->expire($key, $expiration);
        }

        return $this->redis->setex($key, $expiration, $value);
    }
27274
public function replaceValue($key, $value): bool {
        $replaced = false;
        $node = $this->getHead();
        while ($node !== null) {
            if (Comparator::equals($node->getKey(), $key)) {
                $node->setValue($value);
                $replaced = true;
            }
            $node = $node->getNext();
        }
        return $replaced;
    }
1176826
public function offsetSet($key, $value)
    {
        if ($this->root) {
            $this->root = $this->root->insert($key, $value, $this->comparator);
        } else {
            $this->root = TreeNode::create($key, $value);
        }
    }
554670
protected function nodeMax(RedBlackNode $node): RedBlackNode
    {
        if ($node->right() === null) {
            return $node;
        }

        return $this->nodeMax($node->right());
    }