Exemplo n.º 1
0
    static public void First_fit(string Source, string Destination)
    {
        BaseOperations.Initlaize("AudiosInfo.txt", "readme.txt");
        BaseOperations.SortInDecreasing();
        string path = Destination;

        if (!Directory.Exists(path))
        {
            DirectoryInfo di = Directory.CreateDirectory(path);
        }
        int    k           = 1;
        string FileName    = path;
        string source      = Source;
        string destination = "";

        for (int i = 0; i < BaseOperations.num_of_rec; i++)
        {
            path        = Destination;
            source      = Source;
            destination = "";
            FileName    = path;
            int    directoryCount = System.IO.Directory.GetDirectories(path).Length;
            long   max            = 0;
            int    index          = 0;
            bool   test           = true;
            Folder Temp_folder    = new Folder();
            if (directoryCount == 0)
            {
                path += @"\f" + k;
                DirectoryInfo di = Directory.CreateDirectory(path);
                Temp_folder.hours       = BaseOperations.Audio_files[i].hours;
                Temp_folder.min         = BaseOperations.Audio_files[i].min;
                Temp_folder.sec         = BaseOperations.Audio_files[i].sec;
                Temp_folder.free_space  = BaseOperations.max_size;
                Temp_folder.free_space -= BaseOperations.Audio_files[i].total_in_sec;
                BaseOperations.Audio_Folders.Add(Temp_folder);
                source     += @"\" + (BaseOperations.Audio_files[i].index) + ".mp3";
                destination = path + @"\" + (BaseOperations.Audio_files[i].index) + ".mp3";
                System.IO.File.Move(source, destination);
                FileName += @"\f" + k + "_MetaData.txt";
                System.IO.FileStream F = new FileStream(FileName, FileMode.Create);
                StreamWriter         w = new StreamWriter(F);
                w.WriteLine("f" + k);
                w.WriteLine(BaseOperations.Audio_files[i].name + " " + BaseOperations.Audio_files[i].hours + ":" + BaseOperations.Audio_files[i].min + ":" + BaseOperations.Audio_files[i].sec);
                w.Close();
                k++;
            }
            else
            {
                for (int j = 0; j < directoryCount; j++)
                {
                    if (BaseOperations.Audio_Folders[j].free_space >= BaseOperations.Audio_files[i].total_in_sec)
                    {
                        BaseOperations.Audio_Folders[j].hours      += BaseOperations.Audio_files[i].hours;
                        BaseOperations.Audio_Folders[j].min        += BaseOperations.Audio_files[i].min;
                        BaseOperations.Audio_Folders[j].sec        += BaseOperations.Audio_files[i].sec;
                        BaseOperations.Audio_Folders[j].free_space -= BaseOperations.Audio_files[i].total_in_sec;
                        path       += @"\f" + (j + 1);
                        source     += @"\" + (BaseOperations.Audio_files[i].index) + ".mp3";
                        destination = path + @"\" + (BaseOperations.Audio_files[i].index) + ".mp3";
                        System.IO.File.Move(source, destination);
                        FileName += @"\f" + (j + 1) + "_MetaData.txt";
                        System.IO.FileStream F = new FileStream(FileName, FileMode.Append);
                        StreamWriter         w = new StreamWriter(F);
                        w.WriteLine(BaseOperations.Audio_files[i].name + " " + BaseOperations.Audio_files[i].hours + ":" + BaseOperations.Audio_files[i].min + ":" + BaseOperations.Audio_files[i].sec);
                        w.Close();
                        test = false;
                        break;
                    }
                }
                if (test)
                {
                    path        = Destination;
                    source      = Source;
                    destination = "";
                    FileName    = path;
                    path       += @"\f" + k;
                    DirectoryInfo di = Directory.CreateDirectory(path);
                    Temp_folder.hours       = BaseOperations.Audio_files[i].hours;
                    Temp_folder.min         = BaseOperations.Audio_files[i].min;
                    Temp_folder.sec         = BaseOperations.Audio_files[i].sec;
                    Temp_folder.free_space  = BaseOperations.max_size;
                    Temp_folder.free_space -= BaseOperations.Audio_files[i].total_in_sec;
                    BaseOperations.Audio_Folders.Add(Temp_folder);
                    source     += @"\" + (BaseOperations.Audio_files[i].index) + ".mp3";
                    destination = path + @"\" + (BaseOperations.Audio_files[i].index) + ".mp3";
                    System.IO.File.Move(source, destination);
                    FileName += @"\f" + k + "_MetaData" + ".txt";
                    System.IO.FileStream F = new FileStream(FileName, FileMode.Create);
                    StreamWriter         w = new StreamWriter(F);
                    w.WriteLine("f" + k);
                    w.WriteLine(BaseOperations.Audio_files[i].name + " " + BaseOperations.Audio_files[i].hours + ":" + BaseOperations.Audio_files[i].min + ":" + BaseOperations.Audio_files[i].sec);
                    w.Close();
                    k++;
                }
            }
        }
        int no = 0;

        FileName = Destination + @"\f" + (no + 1) + "_MetaData.txt";
        for (; no < k - 1; no++)
        {
            FileName = @"c:\Output\[1] FirstfitDecreasing\f" + (no + 1) + "_MetaData.txt";
            System.IO.FileStream F = new FileStream(FileName, FileMode.Append);
            StreamWriter         w = new StreamWriter(F);
            w.WriteLine(BaseOperations.Audio_Folders[no].hours + ":" + BaseOperations.Audio_Folders[no].min + ":" + BaseOperations.Audio_Folders[no].sec);
            w.Close();
        }
    }
Exemplo n.º 2
0
    /// <summary>
    /// The Complixty of Frist Fit is  O(N^2)
    /// </summary>

    static public void First_fit(string Source, string Destination)
    {
        /// Initlaize the Prameters
        BaseOperations.Initlaize("AudiosInfo.txt", "readme.txt");
        /// Sorting the Files
        BaseOperations.SortInDecreasing();  //O(NlogN)
        string path = Destination;

        // Adding the Frist file
        if (!Directory.Exists(path))
        {
            DirectoryInfo di = Directory.CreateDirectory(path);
        }
        int    k           = 1;
        string FileName    = path;
        string source      = Source;
        string destination = "";

        for (int i = 0; i < BaseOperations.num_of_rec; i++) //O(N)
        {
            path        = Destination;
            source      = Source;
            destination = "";
            FileName    = path;
            int    directoryCount = System.IO.Directory.GetDirectories(path).Length;
            long   max            = 0;
            int    index          = 0;
            bool   test           = true;
            Folder Temp_folder    = new Folder();
            if (directoryCount == 0)
            {
                /// Creating the Frist folder
                path += @"\f" + k;
                DirectoryInfo di = Directory.CreateDirectory(path);

                Temp_folder.hours       = BaseOperations.Audio_files[i].hours;
                Temp_folder.min         = BaseOperations.Audio_files[i].min;
                Temp_folder.sec         = BaseOperations.Audio_files[i].sec;
                Temp_folder.free_space  = BaseOperations.max_size;
                Temp_folder.free_space -= BaseOperations.Audio_files[i].total_in_sec;
                // Satr el Folder El 5ayaly
                BaseOperations.Audio_Folders.Add(Temp_folder);
                // Real Transfering
                source     += @"\" + (BaseOperations.Audio_files[i].index) + ".AMR";
                destination = path + @"\" + (BaseOperations.Audio_files[i].index) + ".AMR";
                // Copy mn El 5yaly to Real Folder
                System.IO.File.Copy(source, destination);
                /// Adding Data to Meta Data  Files
                FileName += @"\f" + k + "_MetaData.txt";
                System.IO.FileStream F = new FileStream(FileName, FileMode.Create);
                StreamWriter         w = new StreamWriter(F);
                w.WriteLine("f" + k);
                w.WriteLine(BaseOperations.Audio_files[i].name + " " + BaseOperations.Audio_files[i].hours + ":" + BaseOperations.Audio_files[i].min + ":" + BaseOperations.Audio_files[i].sec);
                w.Close();
                k++;
            }
            else
            {
                for (int j = 0; j < directoryCount; j++)  //O(N^2)
                {
                    if (BaseOperations.Audio_Folders[j].free_space >= BaseOperations.Audio_files[i].total_in_sec)
                    {
                        // Zy Ely Foo2
                        BaseOperations.Audio_Folders[j].hours      += BaseOperations.Audio_files[i].hours;
                        BaseOperations.Audio_Folders[j].min        += BaseOperations.Audio_files[i].min;
                        BaseOperations.Audio_Folders[j].sec        += BaseOperations.Audio_files[i].sec;
                        BaseOperations.Audio_Folders[j].free_space -= BaseOperations.Audio_files[i].total_in_sec;
                        path       += @"\f" + (j + 1);
                        source     += @"\" + (BaseOperations.Audio_files[i].index) + ".AMR";
                        destination = path + @"\" + (BaseOperations.Audio_files[i].index) + ".AMR";
                        System.IO.File.Copy(source, destination);
                        FileName += @"\f" + (j + 1) + "_MetaData.txt";
                        // Adding THe Remaning files in Folder
                        System.IO.FileStream F = new FileStream(FileName, FileMode.Append);
                        StreamWriter         w = new StreamWriter(F);
                        w.WriteLine(BaseOperations.Audio_files[i].name + " " + BaseOperations.Audio_files[i].hours + ":" + BaseOperations.Audio_files[i].min + ":" + BaseOperations.Audio_files[i].sec);
                        w.Close();
                        test = false;
                        break;
                    }
                }
                if (test)
                {
                    // Adding in A new Folder w Zy El Frist Condition
                    path        = Destination;
                    source      = Source;
                    destination = "";
                    FileName    = path;
                    path       += @"\f" + k;
                    DirectoryInfo di = Directory.CreateDirectory(path);
                    Temp_folder.hours       = BaseOperations.Audio_files[i].hours;
                    Temp_folder.min         = BaseOperations.Audio_files[i].min;
                    Temp_folder.sec         = BaseOperations.Audio_files[i].sec;
                    Temp_folder.free_space  = BaseOperations.max_size;
                    Temp_folder.free_space -= BaseOperations.Audio_files[i].total_in_sec;
                    BaseOperations.Audio_Folders.Add(Temp_folder);
                    source     += @"\" + (BaseOperations.Audio_files[i].index) + ".AMR";
                    destination = path + @"\" + (BaseOperations.Audio_files[i].index) + ".AMR";
                    System.IO.File.Copy(source, destination);
                    FileName += @"\f" + k + "_MetaData" + ".txt";
                    System.IO.FileStream F = new FileStream(FileName, FileMode.Create);
                    StreamWriter         w = new StreamWriter(F);
                    w.WriteLine("f" + k);
                    w.WriteLine(BaseOperations.Audio_files[i].name + " " + BaseOperations.Audio_files[i].hours + ":" + BaseOperations.Audio_files[i].min + ":" + BaseOperations.Audio_files[i].sec);
                    w.Close();
                    k++;
                }
            }
        }
        int no = 0;

        FileName = Destination + @"\f" + (no + 1) + "_MetaData.txt";
        for (; no < k - 1; no++) //O(N)
        {
            FileName = @"c:\Output\[1] FirstfitDecreasing\f" + (no + 1) + "_MetaData.txt";
            System.IO.FileStream F = new FileStream(FileName, FileMode.Append);
            StreamWriter         w = new StreamWriter(F);
            w.WriteLine(BaseOperations.Audio_Folders[no].hours + ":" + BaseOperations.Audio_Folders[no].min + ":" + BaseOperations.Audio_Folders[no].sec);
            w.Close();
        }
    }