public void add_hole(int start, int end, string name)
        {
            MEM_BLOCK hole = new MEM_BLOCK(start, end, name);

            if (end <= memorySize)
            {
                holes.Add(hole);
            }

            else // display error
            {
                MessageBox.Show("The hole size is larger than memory size");
            }
        }
        public void compact(int name_number)
        {
            compact_memory.Clear();

            for (int i = 0; i < active_memory.Count; i++)
            {
                if (!active_memory.ElementAt(i).isHole)
                {
                    compact_memory.Add(active_memory.ElementAt(i));
                }
            }


            if (compact_memory.Count != 0)
            {
                compact_memory.ElementAt(0).start = 0;
                compact_memory.ElementAt(0).end   = compact_memory.ElementAt(0).size;

                for (int i = 1; i <= compact_memory.Count - 1; i++)
                {
                    compact_memory.ElementAt(i).start = compact_memory.ElementAt(i - 1).end;
                    compact_memory.ElementAt(i).end   = compact_memory.ElementAt(i).start + compact_memory.ElementAt(i).size;
                }



                string    name = "hole_ " + name_number;
                MEM_BLOCK hole = new MEM_BLOCK(compact_memory.ElementAt(compact_memory.Count - 1).end, memorySize, name);
                hole.size = hole.end - hole.start;

                compact_memory.Add(hole);

                active_memory.Clear();
                active_memory.AddRange(compact_memory);
            }
        }
        public void add_process(string name, int size, string method)
        {
            if (method == "First Fit")
            {
                MEMORY.sortBy = "First Fit";
            }

            else if (method == "Best Fit")
            {
                MEMORY.sortBy = "Best Fit";
            }

            else if (method == "Worst Fit")
            {
                MEMORY.sortBy = "Worst Fit";
            }

            else
            {
                MessageBox.Show("Please select method first");
            }

            active_memory.Sort();

            for (int i = 0; i < active_memory.Count; i++)
            {
                if (active_memory.ElementAt(i).size > size && active_memory.ElementAt(i).isHole)
                {
                    // add the new process
                    int       start   = active_memory.ElementAt(i).start;
                    int       end     = start + size;
                    MEM_BLOCK process = new MEM_BLOCK(start, end, name);
                    process.isHole = false;



                    // update the new hole generated
                    active_memory.ElementAt(i).start = end;
                    active_memory.ElementAt(i).size  = active_memory.ElementAt(i).end - active_memory.ElementAt(i).start;

                    // add the process
                    active_memory.Add(process);

                    // sort the active_memory with respect to start address
                    active_memory.Sort((x, y) => x.start.CompareTo(y.start));


                    return;
                }


                else if (active_memory.ElementAt(i).size == size && active_memory.ElementAt(i).isHole)
                {
                    // add the new process
                    int       start   = active_memory.ElementAt(i).start;
                    int       end     = start + size;
                    MEM_BLOCK process = new MEM_BLOCK(start, end, name);
                    process.isHole = false;
                    active_memory.Add(process);

                    // delete the new hole generated
                    active_memory.RemoveAt(i);



                    // sort the active_memory with respect to start address
                    active_memory.Sort((x, y) => x.start.CompareTo(y.start));


                    return;
                }
            }

            MessageBox.Show("No avialable space for this process, please deallocate a process and try again");
        }
        public void fill_memory()
        {
            // sort holes with respect to start address
            holes.Sort((x, y) => x.start.CompareTo(y.start));


            for (int i = 0; i < holes.Count; i++)

            {
                int start = holes.ElementAt(i).start;
                int end   = holes.ElementAt(i).end;



                // for the input hole inside a hole or same hole twice corner cases :'D
                bool add = true;
                for (int j = 0; j < filtered_holes.Count; j++)
                {
                    if ((start >= holes.ElementAt(j).start&& end < holes.ElementAt(j).end) ||
                        start == holes.ElementAt(j).start&& end == holes.ElementAt(j).end)
                    {
                        add = false;
                        break;
                    }
                }



                // for the input hole inside a hole and the sum of the two is larger
                // than the old one :'D


                for (int j = 0; j < filtered_holes.Count; j++)
                {
                    if (start >= holes.ElementAt(j).start&& start < holes.ElementAt(j).end &&
                        end > holes.ElementAt(j).end)

                    {
                        add = false;
                        filtered_holes.ElementAt(j).end = end;
                        break;
                    }
                }


                if (add)
                {
                    filtered_holes.Add(holes.ElementAt(i));
                }
            }

            // if end of a hole = start of a new hole update them to one hole
            for (int i = 0; i < filtered_holes.Count - 1; i++)
            {
                // lw la2et 2 holes wra b3d
                if (filtered_holes.ElementAt(i).end == filtered_holes.ElementAt(i + 1).start)
                {
                    // update el end bta3 el 2ola w 5ale ysawy end bta3 l tnya
                    filtered_holes.ElementAt(i).end  = filtered_holes.ElementAt(i + 1).end;
                    filtered_holes.ElementAt(i).size = filtered_holes.ElementAt(i).end - filtered_holes.ElementAt(i).start;
                    filtered_holes.RemoveAt(i + 1);
                }
            }



            // start with hole and ends with hole case
            if (filtered_holes.ElementAt(0).start == 0 &&
                filtered_holes.ElementAt(filtered_holes.Count - 1).end == memorySize)
            {
                for (int i = 0; i < filtered_holes.Count - 1; i++)
                {
                    int       start   = filtered_holes.ElementAt(i).end;
                    int       end     = filtered_holes.ElementAt(i + 1).start;
                    string    name    = "process_ " + processes.Count.ToString();
                    MEM_BLOCK process = new MEM_BLOCK(start, end, name);
                    process.isHole = false;
                    processes.Add(process);
                }
            }

            // start with hole and ends with process case
            if (filtered_holes.ElementAt(0).start == 0 &&
                filtered_holes.ElementAt(filtered_holes.Count - 1).end != memorySize)
            {
                for (int i = 0; i < filtered_holes.Count - 1; i++)
                {
                    int       start   = filtered_holes.ElementAt(i).end;
                    int       end     = filtered_holes.ElementAt(i + 1).start;
                    string    name    = "process_ " + processes.Count.ToString();
                    MEM_BLOCK process = new MEM_BLOCK(start, end, name);
                    process.isHole = false;
                    processes.Add(process);
                }

                //last process
                string    lastName    = "process_ " + processes.Count.ToString();
                MEM_BLOCK lastProcess = new MEM_BLOCK(filtered_holes.ElementAt(filtered_holes.Count - 1).end, memorySize, lastName);
                lastProcess.isHole = false;
                processes.Add(lastProcess);
            }

            // start with process and ends with hole case
            if (filtered_holes.ElementAt(0).start != 0 &&
                filtered_holes.ElementAt(filtered_holes.Count - 1).end == memorySize)
            {
                for (int i = 0; i < filtered_holes.Count; i++)
                {
                    int start;
                    int end;

                    if (i == 0)
                    {
                        start = 0;
                        end   = filtered_holes.ElementAt(0).start;
                    }

                    else
                    {
                        start = filtered_holes.ElementAt(i - 1).end;
                        end   = filtered_holes.ElementAt(i).start;
                    }

                    string    name    = "process_ " + processes.Count.ToString();
                    MEM_BLOCK process = new MEM_BLOCK(start, end, name);
                    process.isHole = false;
                    processes.Add(process);
                }
            }



            // start with process and ends with process case
            if (filtered_holes.ElementAt(0).start != 0 &&
                filtered_holes.ElementAt(filtered_holes.Count - 1).end != memorySize)

            {
                for (int i = 0; i < filtered_holes.Count; i++)
                {
                    int start;
                    int end;

                    if (i == 0)
                    {
                        start = 0;
                        end   = filtered_holes.ElementAt(0).start;
                    }

                    else
                    {
                        start = filtered_holes.ElementAt(i - 1).end;
                        end   = filtered_holes.ElementAt(i).start;
                    }

                    string    name    = "process_ " + processes.Count.ToString();
                    MEM_BLOCK process = new MEM_BLOCK(start, end, name);
                    process.isHole = false;
                    processes.Add(process);
                }

                //last process
                string    lastName    = "process_ " + processes.Count.ToString();
                MEM_BLOCK lastProcess = new MEM_BLOCK(filtered_holes.ElementAt(filtered_holes.Count - 1).end, memorySize, lastName);
                lastProcess.isHole = false;
                processes.Add(lastProcess);
            }



            // putting the holes and processes in one active memory list
            active_memory.AddRange(processes);
            active_memory.AddRange(filtered_holes);

            // sort the active_memory with respect to start address
            active_memory.Sort((x, y) => x.start.CompareTo(y.start));
        }