コード例 #1
0
        public void LoadFromBinary(string fileName)
        {
            Console.WriteLine("Loading data base from binary file: {0} ...", fileName);
            try
            {
                TimeInterval loadTimeInterval = new TimeInterval();
                using (FileStream fr = new FileStream(fileName, FileMode.Open))
                {
                    BinaryReader br = new BinaryReader(fr);
                    songNames.Clear();

                    int header = br.ReadInt32();
                    if (header != BINARY_HEADER)
                    {
                        throw new InvalidDataException("Invalid binary index file!");
                    }

                    int nameCount = br.ReadInt32();
                    for (int i = 0; i < nameCount; i++)
                    {
                        string name = br.ReadString();
                        songNames.Add(name);
                        if (CheckDuplicate)
                            songNameSet.Add(name);
                    }

                    hashMap.Clear();
                    int hashCount = br.ReadInt32();
                    for (int i = 0; i < hashCount; i++)
                    {
                        long key = br.ReadInt64();
                        int count = br.ReadInt32();

                        List<DataPoint> pointList = new List<DataPoint>(count);
                        for (int j = 0; j < count; j++)
                        {
                            short time = br.ReadInt16();
                            short songID = br.ReadInt16();
                            DataPoint dataPoint = new DataPoint(time, songID);
                            pointList.Add(dataPoint);
                        }

                        hashMap.Add(key, pointList);
                    }
                }
                Console.WriteLine("Done! {0}s", loadTimeInterval.GetDurationInSecond());
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
            }
        }
コード例 #2
0
        public void Load(string fileName)
        {
            Console.WriteLine("Loading data base from file: {0} ...", fileName);
            try
            {
                TimeInterval loadTimeInterval = new TimeInterval();
                using (StreamReader sr = new StreamReader(fileName))
                {
                    songNames.Clear();
                    int nameCount = int.Parse(sr.ReadLine());
                    for (int i = 0; i < nameCount; i++)
                    {
                        string name = sr.ReadLine();
                        songNames.Add(name);
                        if (CheckDuplicate)
                            songNameSet.Add(name);
                    }

                    hashMap.Clear();
                    int hashCount = int.Parse(sr.ReadLine());
                    for (int i = 0; i < hashCount; i++)
                    {
                        string record = sr.ReadLine();
                        string[] items = record.Split('\t');

                        long hash = long.Parse(items[0]);
                        List<DataPoint> pointList = new List<DataPoint>();
                        for (int j = 1; j < items.Length; j++)
                        {
                            string[] values = items[j].Split(',');
                            short time = short.Parse(values[0]);
                            short songID = short.Parse(values[1]);
                            DataPoint dataPoint = new DataPoint(time, songID);
                            pointList.Add(dataPoint);
                        }

                        hashMap.Add(hash, pointList);
                    }
                }
                Console.WriteLine("Done! {0}s", loadTimeInterval.GetDurationInSecond());
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }