Ejemplo n.º 1
0
        public bool Add(ScoredObject <T> so)
        {
            if (so.Score < TopScore - Threshold)
            {
                return(false);
            }

            if (useNames)
            {
                if (names.Contains(so.Item.ToString()))
                {
                    return(false);
                }

                names.Add(so.Item.ToString());
            }

            scoredObjects.Add(so);
            if (so.Score > TopScore)
            {
                TopScore = so.Score;
                TopItem  = so.Item;
            }

            if (so.Score >= TopScore - Threshold)
            {
                so.Active = true;
            }
            else
            {
                so.Active = false;
            }
            return(true);
        }
Ejemplo n.º 2
0
        public bool Add(ScoredObject <T> so)
        {
            if (so.Score < TopScore - Threshold)
            {
                return(false);
            }

            if (items.Contains(so.Item))
            {
                return(false);
            }

            items.Add(so.Item);
            scores.Add(so.Score);
            activities.Add(true);

            if (useNames)
            {
                names.Add(so.Item.ToString());
            }

            if (so.Score > TopScore)
            {
                TopScore = so.Score;
                TopItem  = so.Item;
            }
            return(true);
        }
Ejemplo n.º 3
0
        public ScoredObject <T> Peek()
        {
            ScoredObject <T> frontItem = new ScoredObject <T>()
            {
                Score = scores[0], Item = items[0]
            };

            return(frontItem);
        }
Ejemplo n.º 4
0
 public void Add(ScoredObject <T> so)
 {
     items.Add(so.Item);
     scores.Add(so.Score);
     if (so.Score > TopScore)
     {
         TopScore = so.Score;
         TopItem  = so.Item;
     }
 }
Ejemplo n.º 5
0
        public ScoredObject <T> Dequeue()
        {
            // assumes pq is not empty; up to calling code
            int li = scores.Count - 1; // last index (before removal)
            ScoredObject <T> frontItem = new ScoredObject <T>()
            {
                Item = items[0], Score = scores[0]
            };                                                                                        // fetch the front

            scores[0] = scores[li];
            scores.RemoveAt(li);
            items[0] = items[li];
            items.RemoveAt(li);

            --li;       // last index (after removal)
            int pi = 0; // parent index. start at front of pq

            while (true)
            {
                int ci = pi * 2 + 1; // left child index of parent
                if (ci > li)
                {
                    break;                               // no children so done
                }
                int rc = ci + 1;                         // right child
                if (rc <= li && scores[rc] > scores[ci]) // if there is a rc (ci + 1), and it is smaller than left child, use the rc instead
                {
                    ci = rc;
                }
                if (scores[pi] >= scores[ci])
                {
                    break;                           // parent is smaller than (or equal to) smallest child so done
                }
                double tmpScore = scores[pi];
                T      tmpItem  = items[pi];
                scores[pi] = scores[ci];
                items[pi]  = items[ci];
                scores[ci] = tmpScore; // swap parent and child
                items[ci]  = tmpItem;
                pi         = ci;
            }
            return(frontItem);
        }
Ejemplo n.º 6
0
        public int CompareTo(ScoredObject <T> other)
        {
            if (other == null)
            {
                return(1);
            }

            if (Score < other.Score)
            {
                return(-1);
            }
            else if (Score > other.Score)
            {
                return(1);
            }
            else
            {
                return(0);
            }
        }