Beispiel #1
0
        public int[] allEle(int j)
        {
            hole tmp = head;
            int  s   = size();

            int[] arr = new int[s];
            if (j == 0)
            {
                for (int i = 0; i < s; i++)
                {
                    arr[i] = tmp.get_start();
                    tmp    = tmp.get_next();
                }
            }
            else if (j == 1)
            {
                for (int i = 0; i < s; i++)
                {
                    arr[i] = tmp.get_end();
                    tmp    = tmp.get_next();
                }
            }
            else if (j == 2)
            {
                for (int i = 0; i < s; i++)
                {
                    arr[i] = tmp.get_size();
                    tmp    = tmp.get_next();
                }
            }


            return(arr);
        }
Beispiel #2
0
 public hole(int num, int startAt, int size)
 {
     mNext    = null;
     mHoleNum = num;
     mStart   = startAt;
     mSize    = size;
     mEnd     = startAt + size - 1;
 }
Beispiel #3
0
        public hole findByEnd(int i)
        {
            hole tmp = head;

            while (tmp.get_end() != i)
            {
                tmp = tmp.get_next();
            }
            return(tmp);
        }
Beispiel #4
0
        public hole findBySize(int i)
        {
            hole tmp = head;

            while (tmp.get_size() != i)
            {
                tmp = tmp.get_next();
            }
            return(tmp);
        }
Beispiel #5
0
        public void printHoles()
        {
            mMem.sort(0);
            hole holes = mMem.get_hHole();
            int  size  = mMem.size();

            for (int i = 0; i < size; i++)
            {
                int start = holes.get_start();
                int end   = holes.get_end();
                listBox1.Items.Add(mTxt1Hole + start + "    to    " + end);
                holes = holes.get_next();
            }
        }
Beispiel #6
0
        public void Add(int num, int start, int size)
        {
            hole nw = new hole(num, start, size);

            if (isEmpty())
            {
                head = nw;
            }
            else
            {
                hole tmp = last();
                tmp.set_next(nw);
            }
        }
Beispiel #7
0
        public hole last()
        {
            if (isEmpty())
            {
                return(null);
            }
            hole tmp = head;

            while (tmp.get_next() != null)
            {
                tmp = tmp.get_next();
            }
            return(tmp);
        }
Beispiel #8
0
        public int holeSize()
        {
            if (isEmpty())
            {
                return(0);
            }
            hole tmp = head;
            int  i   = 0;

            while (tmp != null)
            {
                i   = i + tmp.get_size();
                tmp = tmp.get_next();
            }
            return(i);
        }
Beispiel #9
0
        public int size()
        {
            if (isEmpty())
            {
                return(0);
            }
            int  i   = 1;
            hole tmp = head.get_next();

            while (tmp != null)
            {
                i++;
                tmp = tmp.get_next();
            }
            return(i);
        }
Beispiel #10
0
        public void swap(hole tmp, hole tmp2)
        {
            int tmpHN    = tmp2.get_holeNum();
            int tmpStart = tmp2.get_start();
            int tmpSize  = tmp2.get_size();
            int tmpEnd   = tmp2.get_end();

            tmp2.set_start(tmp.get_start());
            tmp2.set_size(tmp.get_size());
            tmp2.set_holeNum(tmp.get_holeNum());
            tmp2.set_end(tmp.get_end());
            tmp.set_start(tmpStart);
            tmp.set_size(tmpSize);
            tmp.set_holeNum(tmpHN);
            tmp.set_end(tmpEnd);
        }
Beispiel #11
0
        public hole findByHoleNum(int i)
        {
            if (isEmpty())
            {
                return(null);
            }
            hole tmp = head;

            while (tmp.get_holeNum() != i && tmp.get_next() != null)
            {
                tmp = tmp.get_next();
            }
            if (tmp.get_holeNum() == i)
            {
                return(tmp);
            }
            else
            {
                return(null);
            }
        }
Beispiel #12
0
        public void remove(hole h)
        {
            hole tmp = head;

            if (size() == 1)
            {
                head = null;
                return;
            }
            else if (tmp == h)
            {
                head = tmp.get_next();
                return;
            }
            else
            {
                while (tmp.get_next() != h && tmp.get_next() != null)
                {
                    tmp = tmp.get_next();
                }
                hole tmp2 = tmp.get_next();
                tmp.set_next(tmp2.get_next());
            }
        }
Beispiel #13
0
        private void button2_Click(object sender, EventArgs e)
        {
            bool ok = true;

            disHole();
            listBox1.Items.Clear();
            listBox2.Items.Clear();
            string proName = textBox1.Text;
            int    proSize = Convert.ToInt32(numericUpDown3.Value);

            if (comboBox1.Text == "First Fit")
            {
                int[] arr     = mMem.allEle(2);
                int   holeNum = mMem.size();
                for (int i = 0; i < holeNum; i++)
                {
                    if (arr[i] >= proSize)
                    {
                        mNumProcess++;
                        hole tmp      = mMem.findBySize(arr[i]);
                        int  proStart = tmp.get_start();
                        int  proEnd   = proStart + proSize - 1;
                        mPro.Add(proName, proStart, proSize, tmp.get_holeNum());
                        int tmpSize = tmp.get_size();
                        int newSize = tmpSize - proSize;
                        if (newSize != 0)
                        {
                            tmp.set_start(proStart + proSize);
                            tmp.set_size(tmpSize - proSize);
                        }
                        else
                        {
                            mMem.remove(tmp);
                            mHoleNum--;
                        }
                        ok = false;
                        break;
                    }
                    else
                    {
                        ok = true;
                    }
                }
            }
            else if (comboBox1.Text == "Best Fit")
            {
                mMem.sort(2);
                int[] arr     = mMem.allEle(2);
                int   holeNum = mMem.size();
                for (int i = 0; i < holeNum; i++)
                {
                    if (arr[i] >= proSize)
                    {
                        mNumProcess++;
                        hole tmp      = mMem.findBySize(arr[i]);
                        int  proStart = tmp.get_start();
                        int  proEnd   = proStart + proSize - 1;
                        mPro.Add(proName, proStart, proSize, tmp.get_holeNum());
                        int tmpSize = tmp.get_size();
                        int newSize = tmpSize - proSize;
                        if (newSize != 0)
                        {
                            tmp.set_start(proStart + proSize);
                            tmp.set_size(tmpSize - proSize);
                        }
                        else
                        {
                            mMem.remove(tmp);
                            mHoleNum--;
                        }
                        ok = false;
                        break;
                    }
                    else
                    {
                        ok = true;
                    }
                }
            }
            else if (comboBox1.Text == "Worst Fit")
            {
                mMem.sort(2);
                int[] arr     = mMem.allEle(2);
                int   holeNum = mMem.size();
                if (holeNum == 0)
                {
                    ;
                }
                else if (arr[holeNum - 1] >= proSize)

                {
                    mNumProcess++;
                    hole tmp      = mMem.findBySize(arr[holeNum - 1]);
                    int  proStart = tmp.get_start();
                    int  proEnd   = proStart + proSize - 1;
                    mPro.Add(proName, proStart, proSize, tmp.get_holeNum());
                    int tmpSize = tmp.get_size();
                    int newSize = tmpSize - proSize;
                    if (newSize != 0)
                    {
                        tmp.set_start(proStart + proSize);
                        tmp.set_size(tmpSize - proSize);
                    }
                    else
                    {
                        mMem.remove(tmp);
                        mHoleNum--;
                    }
                    ok = false;
                }
                else
                {
                    ok = true;
                }
            }
            if (ok)
            {
                mErrorMsg = "Thers is no enough memory";
                MessageBox.Show(mErrorMsg);
            }
            textBox1.Text = "p" + mNumProcess;

            printPro();
        }
Beispiel #14
0
        public void removeByStart(int i)
        {
            hole tmp = findByStart(i);

            remove(tmp);
        }
Beispiel #15
0
 public HolesLinkedList()
 {
     head = null;
 }
Beispiel #16
0
        private void listBox2_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            int    ind          = listBox2.IndexFromPoint(e.X, e.Y);
            string tmp          = listBox2.Items[ind].ToString();
            int    indexOfSpace = tmp.IndexOf(" ", 0);
            string str          = tmp.Substring(0, indexOfSpace);

            listBox1.Items.Clear();
            listBox2.Items.Clear();
            process p        = mPro.findByName(str);
            int     holeNum  = p.get_holeNum();
            hole    h        = mMem.findByHoleNum(holeNum);
            int     proEnd   = p.get_end();
            int     proStart = p.get_start();
            int     proSize  = p.get_size();

            mMem.sort(0);
            int[] startArr = mMem.allEle(0);
            int[] endArr   = mMem.allEle(1);
            int   limit    = mMem.size();
            bool  ok       = true;

            for (int i = 0; i < limit; i++)
            {
                if (proEnd <= endArr[i] && proStart >= startArr[i])
                {
                    mErrorMsg = "already hole";
                    MessageBox.Show(mErrorMsg);
                    ok = false;
                    break;
                }
                else if ((i < limit - 1) &&
                         (proEnd + 1 == startArr[i + 1] && proStart - 1 == endArr[i]))
                {
                    hole tmpH  = mMem.findByStart(startArr[i]);
                    hole tmpH2 = mMem.findByStart(startArr[i + 1]);
                    tmpH.set_end(tmpH2.get_end());
                    tmpH.set_size(proSize + tmpH.get_size() + tmpH2.get_size());
                    mMem.removeByStart(startArr[i + 1]);
                    ok = false;
                    break;
                }
                else if (proEnd + 1 == startArr[i] || (proEnd >= startArr[i] && proEnd <= endArr[i]))
                {
                    hole tmpH = mMem.findByStart(startArr[i]);
                    tmpH.set_start(proStart);
                    tmpH.set_size(endArr[i] - proStart + 1);
                    ok = false;
                    break;
                }
                else if (proStart - 1 == endArr[i] || (proStart <= endArr[i] && proStart >= startArr[i]))
                {
                    hole tmpH = mMem.findByEnd(endArr[i]);
                    tmpH.set_end(proEnd);
                    tmpH.set_size(proEnd - startArr[i] + 1);
                    ok = false;
                    break;
                }
            }
            if (ok)
            {
                mMem.Add(mHoleNum, proStart, proSize);
                enProcess();
                mHoleNum++;
            }

            mPro.remove(p);
            if (mPro.size() == 0)
            {
                enHole();
            }
            printPro();
        }
Beispiel #17
0
        public void sort(int sel)
        {
            hole tmp = head;
            int  n = size();
            int  x, y;

            if (sel == 0)
            {
                for (x = 0; x < n; x++)
                {
                    hole tmp2 = tmp.get_next();
                    for (y = 0; y < n - x - 1; y++)
                    {
                        int c  = tmp.get_start();
                        int nn = tmp2.get_start();
                        if (c > nn)
                        {
                            swap(tmp, tmp2);
                        }

                        tmp2 = tmp2.get_next();
                    }
                    tmp = tmp.get_next();
                }
            }
            else if (sel == 1)
            {
                for (x = 0; x < n; x++)
                {
                    hole tmp2 = tmp.get_next();
                    for (y = 0; y < n - x - 1; y++)
                    {
                        int c  = tmp.get_end();
                        int nn = tmp2.get_end();
                        if (c > nn)
                        {
                            swap(tmp, tmp2);
                        }

                        tmp2 = tmp2.get_next();
                    }
                    tmp = tmp.get_next();
                }
            }
            else if (sel == 2)
            {
                for (x = 0; x < n; x++)
                {
                    hole tmp2 = tmp.get_next();
                    for (y = 0; y < n - x - 1; y++)
                    {
                        int c  = tmp.get_size();
                        int nn = tmp2.get_size();
                        if (c > nn)
                        {
                            swap(tmp, tmp2);
                        }

                        tmp2 = tmp2.get_next();
                    }
                    tmp = tmp.get_next();
                }
            }
        }
Beispiel #18
0
        private void button1_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            listBox2.Items.Clear();
            int start = Convert.ToInt32(numericUpDown1.Value);
            int size  = Convert.ToInt32(numericUpDown2.Value);
            int end   = start + size - 1;

            mMem.sort(0);
            int[] startArr = mMem.allEle(0);
            int[] endArr   = mMem.allEle(1);
            int   limit    = mMem.size();
            bool  ok       = true;

            for (int i = 0; i < limit; i++)
            {
                if (end <= endArr[i] && start >= startArr[i])
                {
                    mErrorMsg = "already hole";
                    MessageBox.Show(mErrorMsg);
                    ok = false;
                    break;
                }
                else if ((i < limit - 1) &&
                         (end + 1 == startArr[i + 1] && start - 1 == endArr[i]))
                {
                    hole tmpH  = mMem.findByStart(startArr[i]);
                    hole tmpH2 = mMem.findByStart(startArr[i + 1]);
                    tmpH.set_end(tmpH2.get_end());
                    tmpH.set_size(size + tmpH.get_size() + tmpH2.get_size());
                    mMem.removeByStart(startArr[i + 1]);
                    ok = false;
                    break;
                }
                else if (end + 1 == startArr[i] || (end >= startArr[i] && end <= endArr[i]))
                {
                    hole tmp = mMem.findByStart(startArr[i]);
                    tmp.set_start(start);
                    tmp.set_size(endArr[i] - start + 1);
                    ok = false;
                    break;
                }
                else if (start - 1 == endArr[i] || (start <= endArr[i] && start >= startArr[i]))
                {
                    hole tmp = mMem.findByEnd(endArr[i]);
                    for (int j = i + 1; j < limit; j++)
                    {
                        if (end >= endArr[j])
                        {
                            hole tmp2 = mMem.findByEnd(endArr[j]);
                            mMem.remove(tmp2);
                        }
                        else if (end >= startArr[j])
                        {
                            end = endArr[j];
                            hole tmp2 = mMem.findByStart(startArr[j]);
                            mMem.remove(tmp2);
                        }
                        else
                        {
                            break;
                        }
                    }
                    tmp.set_end(end);
                    tmp.set_size(end - startArr[i] + 1);
                    ok = false;
                    break;
                }
            }
            if (ok)
            {
                mMem.Add(mHoleNum, start, size);
                enProcess();
                mHoleNum++;
            }
            printHoles();
        }
Beispiel #19
0
 public void set_next(hole p)
 {
     mNext = p;
 }