Example #1
0
        public readrecords(FileStream fs, ifitAlgorithms algo)
        {
            StreamReader sr = new StreamReader(fs);

            this.algo = algo;
            while (sr.Peek() != -1)
            {
                string line = sr.ReadLine();
                if (line == "")
                {
                    continue;
                }

                record r = Get_recordFromFile(line);

                if (line[0] == 'A' || line[0] == 'a')
                {
                    if (!algo.reclaimSpace(r, ls))
                    {
                        ls.Add(r);
                    }
                }
                else
                {
                    deleteRecordFromList(r);
                }
            }

            sr.Close();
        }
Example #2
0
        public void deleteRecordFromList(record r)
        {
            for (int i = 0; i < ls.Count; i++)//search for record in the list
            {
                if (r.name.Equals(ls[i].name))
                {
                    algo.addtoavail_list(ls[i]);

                    string s = '*' + ls[i].name;
                    ls[i].name = s;

                    break;
                }
            }
        }
Example #3
0
        public record Get_recordFromFile(String s)
        {
            int    cnt  = 0;
            string name = "";
            string size = "";

            int ptr1 = s.Length, ptr2 = s.Length;

            for (int i = 0; i < s.Length; i++)
            {
                if (s[i] == ',' && cnt == 0)
                {
                    ptr1 = i;
                    cnt++;
                }
                else if (s[i] == ',' && cnt != 0)
                {
                    ptr2 = i;
                }
            }

            for (int i = ptr1 + 1; i < s.Length && s[i] != ','; i++)
            {
                if (s[i] != ' ')
                {
                    name += s[i];
                }
            }

            for (int i = ptr2 + 1; i < s.Length && s[i] != ','; i++)
            {
                if (s[i] != ' ')
                {
                    size += s[i];
                }
            }

            if (size == "")
            {
                size = "0";
            }

            record r = new record(name);

            r.size = int.Parse(size);

            return(r);
        }
Example #4
0
        public bool reclaimSpace(record r, List <record> ls)
        {
            foreach (var availRecord in availList)
            {
                if (r.size <= availRecord.size)
                {
                    for (int j = 0; j < ls.Count; j++)
                    {
                        if (availRecord.name == ls[j].name)//remove from the list
                        {
                            ls[j] = r;
                            break;
                        }
                    }
                    availList.Remove(availRecord);
                    fragmentationSize -= r.size;
                    return(true);
                }
            }

            return(false);
        }
Example #5
0
        public bool reclaimSpace(record r, List <record> ls)
        {
            for (int i = availList.Count - 1; i >= 0; i--)
            {
                if (r.size <= availList[i].size)
                {
                    for (int j = 0; j < ls.Count; j++)
                    {
                        if (availList[i].name == ls[j].name)//remove from the list
                        {
                            ls[j] = r;
                            break;
                        }
                    }
                    availList.RemoveAt(i);
                    fragmentationSize -= r.size;
                    return(true);
                }
            }

            return(false);
        }
Example #6
0
 public void addtoavail_list(record r)
 {
     fragmentationSize += r.size;
     availList.Add(r);
 }