Example #1
0
        /* func */
        /// <summary>
        /// 向当前的位置堆中添加最小值节点
        /// <para>如果超出容量,将丢弃最大值节点</para>
        /// </summary>
        /// <param name="position">位置</param>
        /// <param name="value">值</param>
        public virtual void AddMinItem(WeightedPoint weightedPoint)
        {
            if (weightedPoint is null)
            {
                throw new ArgumentNullException(nameof(weightedPoint));
            }

            LinkedListNode <WeightedPoint> node = PositionList.First;

            if (weightedPoint.Value >= Limit)
            {
                return;
            }
            else if (PositionList.Count == 0)
            {
                PositionList.AddFirst(weightedPoint);
                return;
            }
            else if (PositionList.Count >= Capacity &&
                     weightedPoint.Value > node.Value.Value)
            {
                return;
            }
            while (true)
            {
                if (weightedPoint.Value < node.Value.Value)
                {
                    if (node.Next is null)
                    {
                        PositionList.AddAfter(node, weightedPoint);
                        break;
                    }
                    else
                    {
                        node = node.Next;
                    }
                }
                else
                {
                    PositionList.AddBefore(node, weightedPoint);
                    break;
                }
            }
            if (PositionList.Count > Capacity)
            {
                PositionList.RemoveFirst();
            }
            Limit = weightedPoint.Value;
        }
Example #2
0
 internal WeightedPoint[] ToArray()
 {
     WeightedPoint[] copy = new WeightedPoint[PositionList.Count];
     PositionList.CopyTo(copy, 0);
     return(copy);
 }