view topology/manager/ParentManager.cs @ 61:1a42115e51bb

fix
author KaitoMaeshiro <aosskaito@cr.ie.u-ryukyu.ac.jp>
date Fri, 28 Jan 2022 23:15:05 +0900
parents c4f8630b7822
children
line wrap: on
line source

using System;
using System.Collections.Generic;
using System.Linq;


namespace Christie_net.topology.manager
{
    public class ParentManager
    {
        private int maxChildren;
        private int position = 0;
        private LinkedList<Parent> list;
        public ParentManager(int hasChildren){
            list = new LinkedList<Parent>();
            maxChildren = hasChildren;
        }

        public String getMyParent() {
            checkChildNumber();
            return list.ElementAt(position).getName();
        }

        public int getMyNumber() {
            checkChildNumber();
            int num = list.ElementAt(position).children();
            list.ElementAt(position).increment();
            return num;
        }

        private void checkChildNumber() {
            for (;;next()) {
                if (list.ElementAt(position).children() < maxChildren)
                    break;
            }
        }

        public void register(String name) {
            Parent p = new Parent(name);
            list.AddLast(p);
        }

        public void next() {
            position++;
        }

        public void previous() {
            position--;
        }

        public void replaceAndRemove(String remove, String replace) {
            Parent removeNode = find(remove);
            //Remove(replace);
            removeNode.setName(replace);
        }

        public void remove(String remove) {
            Parent removeNode = find(remove);
            list.Remove(removeNode);
        }

        public Parent find(String name) {
            Boolean found = false;
            int i = 0;
            for (;i<list.Count;i++) {
                if (list.ElementAt(i).getName().Equals(name)) {
                    found = true;
                    break;
                }
            }
            if (found) {
                return list.ElementAt(i);
            } else {
                return null;
            }
        }

        public Parent getLastNode(){
            return list.ElementAt(0);
        }

        public void show() {
            int counter = 0;
            Console.WriteLine("| ");
            foreach (Parent p in list) {
                if (counter==position)
                    Console.WriteLine("P ");
                Console.WriteLine(p.getName()+" "+p.children()+" | ");
                counter++;
            }
        }

        public LinkedList<Parent> getList() {
            return list;
        }
    }
}