Exemplo n.º 1
0
 public BinaryCell(BinaryCell <T> leftCell, BinaryCell <T> rightCell, int index, T value)
 {
     LeftCell  = leftCell;
     RightCell = rightCell;
     Index     = index;
     Value     = value;
 }
Exemplo n.º 2
0
    public override void Clear()
    {
        if (Count <= 0)
        {
            return;
        }

        BinaryCell <PriorityPair <T> > cell = startCell.RightCell;

        while (cell != finalCell)
        {
            BinaryCell <PriorityPair <T> > nextCell = cell.RightCell;

            cell.LeftCell  = null;
            cell.RightCell = null;
            cell.Index     = 0;
            cell.Value     = null;

            cell = nextCell;
        }

        startCell.RightCell = finalCell;
        finalCell.LeftCell  = startCell;
        Count = 0;
    }
Exemplo n.º 3
0
    public override PriorityPair <T> Remove(PriorityPair <T> data)
    {
        if (Count <= 0 || !Contains(data))
        {
            return(null);
        }

        BinaryCell <PriorityPair <T> > cell = finalCell.LeftCell;

        while (cell.Value != data)
        {
            cell.Index--;
            cell = cell.LeftCell;
        }

        BinaryCell <PriorityPair <T> > leftCell  = cell.LeftCell;
        BinaryCell <PriorityPair <T> > rightCell = cell.RightCell;

        leftCell.RightCell = rightCell;
        rightCell.LeftCell = leftCell;

        cell.RightCell = null;
        cell.LeftCell  = null;
        cell.Index     = 0;

        Count--;

        return(cell.Value);
    }
Exemplo n.º 4
0
    public PriorityList()
    {
        startCell = new BinaryCell <PriorityPair <T> >();
        finalCell = new BinaryCell <PriorityPair <T> >();

        startCell.LeftCell  = null;
        startCell.RightCell = finalCell;

        finalCell.LeftCell  = startCell;
        finalCell.RightCell = null;
    }
Exemplo n.º 5
0
    public override List <PriorityPair <T> > ToList()
    {
        List <PriorityPair <T> >       returnList = new List <PriorityPair <T> >();
        BinaryCell <PriorityPair <T> > cell       = startCell.RightCell;

        while (cell != finalCell)
        {
            returnList.Add(cell.Value);
            cell = cell.RightCell;
        }

        return(returnList);
    }
Exemplo n.º 6
0
    public override PriorityPair <T> Find(int index)
    {
        if (Count <= 0 || index >= Count || index < 0)
        {
            return(null);
        }

        BinaryCell <PriorityPair <T> > cell = finalCell.LeftCell;

        while (cell.Index != index)
        {
            cell = cell.LeftCell;
        }

        return(cell.Value);
    }
Exemplo n.º 7
0
    public override void Add(PriorityPair <T> data)
    {
        if (Count == 0)
        {
            BinaryCell <PriorityPair <T> > newCell = new BinaryCell <PriorityPair <T> >(Count, data);

            finalCell.LeftCell  = newCell;
            startCell.RightCell = newCell;

            newCell.LeftCell  = startCell;
            newCell.RightCell = finalCell;
        }
        else
        {
            BinaryCell <PriorityPair <T> > newCell    = new BinaryCell <PriorityPair <T> >(Count, data);
            BinaryCell <PriorityPair <T> > graterCell = finalCell.LeftCell;

            while (!(graterCell.Value.CompareTo(newCell.Value) > 0))
            {
                graterCell.Index++;
                graterCell = graterCell.LeftCell;

                if (graterCell == startCell)
                {
                    break;
                }
            }

            BinaryCell <PriorityPair <T> > smallerCell = graterCell.RightCell;

            graterCell.RightCell = newCell;
            smallerCell.LeftCell = newCell;

            newCell.RightCell = smallerCell;
            newCell.LeftCell  = graterCell;

            if (smallerCell != finalCell)
            {
                newCell.Index = smallerCell.Index - 1;
            }
        }

        Count++;
    }
Exemplo n.º 8
0
    public override string ToString()
    {
        string listString = "Values: [";

        BinaryCell <PriorityPair <T> > cell = startCell.RightCell;

        while (cell.RightCell != null)
        {
            listString += " (" + cell.Index + ")";
            listString += " Key: \"" + cell.Value.Key + "\"";
            listString += " Priority: " + cell.Value.Priority + ";";

            cell = cell.RightCell;
        }

        listString += " ]";

        return(listString);
    }
Exemplo n.º 9
0
    public override bool Contains(PriorityPair <T> data)
    {
        if (Count <= 0)
        {
            return(false);
        }

        BinaryCell <PriorityPair <T> > cell = finalCell.LeftCell;

        while (cell.Value != data)
        {
            cell = cell.LeftCell;

            if (cell == startCell)
            {
                return(false);
            }
        }

        return(cell.Value == data);
    }
Exemplo n.º 10
0
    public override int IndexOf(PriorityPair <T> data)
    {
        if (Count <= 0)
        {
            return(-1);
        }

        BinaryCell <PriorityPair <T> > cell = finalCell.LeftCell;

        while (cell.Value != data)
        {
            cell = cell.LeftCell;

            if (cell == startCell)
            {
                return(-1);
            }
        }

        return(cell.Index);
    }
Exemplo n.º 11
0
    public bool ContainsKey(T data)
    {
        if (Count <= 0)
        {
            return(false);
        }

        BinaryCell <PriorityPair <T> > cell = finalCell.LeftCell;

        while (!cell.Value.Key.Equals(data))
        {
            cell = cell.LeftCell;

            if (cell == startCell)
            {
                return(false);
            }
        }

        return(cell.Value.Key.Equals(data));
    }
Exemplo n.º 12
0
    public PriorityPair <T> Remove(T key)
    {
        if (Count <= 0)
        {
            return(null);
        }

        BinaryCell <PriorityPair <T> > cell = finalCell.LeftCell;

        while (!cell.Value.Key.Equals(key))
        {
            cell = cell.LeftCell;

            if (cell == startCell)
            {
                return(null);
            }
        }

        for (BinaryCell <PriorityPair <T> > decrementCell = finalCell;
             decrementCell != cell; decrementCell = decrementCell.LeftCell)
        {
            decrementCell.Index--;
        }

        BinaryCell <PriorityPair <T> > leftCell  = cell.LeftCell;
        BinaryCell <PriorityPair <T> > rightCell = cell.RightCell;

        leftCell.RightCell = rightCell;
        rightCell.LeftCell = leftCell;

        cell.RightCell = null;
        cell.LeftCell  = null;
        cell.Index     = -1;

        Count--;

        return(cell.Value);
    }