private bool ModifyHeap()

in csharp/src/Containers/HeapWithIndices.cs [399:450]


		private bool ModifyHeap(int key, TValue newValue)
		{
			if (key >= keysPosition.Length || key < 0 || keysPosition[key] < 0) return false;
			int index = keysPosition[key];
			HeapEntry value = new HeapEntry(elements[keysPosition[key]].Key, newValue);
			int current = index;
			while (current < count)
			{
				int left = (current << 1) + 1;
				int right = (current << 1) + 2;
				int swapIndex = current;
				if (left >= count) swapIndex = current;
				else
				if (right >= count)
				{
					if (comparer.Compare(elements[left].Value, value.Value) < 0) swapIndex = left;
				}
				else
				{
					if (comparer.Compare(elements[left].Value, elements[right].Value) < 0)
					{
						if (comparer.Compare(elements[left].Value, value.Value) < 0) swapIndex = left;
					}
					else if (comparer.Compare(elements[right].Value, value.Value) < 0) swapIndex = right;
				}
				if (swapIndex == current) break;
				elements[current] = elements[swapIndex];
				keysPosition[elements[current].Key] = current;
				current = swapIndex;
			}
			elements[current] = value;
			keysPosition[elements[current].Key] = current;
			current = index;
			value = elements[current];
			while (current != 0)
			{
				int swapIndex = (current - 1) >> 1;
				if (comparer.Compare(value.Value, elements[swapIndex].Value) < 0)
				{
					elements[current] = elements[swapIndex];
					keysPosition[elements[current].Key] = current;
					current = swapIndex;
				}
				else break;
			}
			if (current != index)
			{
				elements[current] = value;
				keysPosition[elements[current].Key] = current;
			}
			return true;
		}