Beispiel #1
0
        void CheckForSharp(AdjacencyListReader listReader)
        {
            var line = listReader.ReadNextLine();

            if (line != "#")
            {
                throw new ParserException("Parsing .txt file error.\n" + $"Wrong file format, no sharp in line: {listReader.CurrentLineIndex}. ");
            }
        }
Beispiel #2
0
 /// <summary>
 /// Creates adjacency list of graph using
 /// text file.
 /// </summary>
 /// <param name="filePath">File path.</param>
 /// <returns>Adjacency list.</returns>
 public int[][] ReadAdjacencyListFromFile(string filePath)
 {
     List <int>[] adjacencyList;
     using (var listReader = new AdjacencyListReader(filePath)) {
         SetAmoutOfVertex(listReader);
         CheckForSharp(listReader);
         adjacencyList = ReadAdjacencyListWith(listReader);
         CheckForSharp(listReader);
     }
     return(Proceed(adjacencyList));
 }
Beispiel #3
0
        void SetAmoutOfVertex(AdjacencyListReader listReader)
        {
            var line       = listReader.ReadNextLine();
            var readResult = int.TryParse(line, out int verdsNumber);

            if (readResult == false || verdsNumber < 1)
            {
                throw new ParserException("Parsing .txt file error.\n" + $"Invalid amount of verds, line: {listReader.CurrentLineIndex}.");
            }
            listReader.AmoutOfVertex = verdsNumber;
        }
Beispiel #4
0
        public int[][] ReadAdjacencyListFromFile(string filePath)
        {
            List <int>[] adjacencyList = null;

            using (var reader = new AdjacencyListReader(filePath)) {
                try {
                    reader.AmoutOfVertex = GetAmountOfVertices(filePath);
                    adjacencyList        = CreateAdjacencyList(reader);
                } catch {
                    throw new ParserException("Parsing .edg file error.\n" + $"Error in line {reader.CurrentLineIndex + 1}");
                }
            }
            return(Proceed(adjacencyList));
        }
Beispiel #5
0
        static List <int>[] CreateAdjacencyList(AdjacencyListReader reader)
        {
            var adjacencyList = new List <int> [reader.AmoutOfVertex];

            for (int i = 0; i < adjacencyList.Length; i++)
            {
                adjacencyList[i] = new List <int>();
            }
            var firstLine         = reader.ReadNextLine();
            var splittedFirstLine = firstLine.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
            var n = int.Parse(splittedFirstLine[0]);

            for (int i = 0; i < n; i++)
            {
                var line        = reader.ReadNextLine();
                var splitedLine = line.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                var u           = int.Parse(splitedLine[0]) - 100;
                var v           = int.Parse(splitedLine[1]) - 100;
                adjacencyList[u].Add(v);
            }
            return(adjacencyList);
        }
Beispiel #6
0
        List <int>[] ReadAdjacencyListWith(AdjacencyListReader listReader)
        {
            var adjacencyList = new List <int> [listReader.AmoutOfVertex];
            var separator     = "->";

            for (int i = 0; i < adjacencyList.Length; i++)
            {
                adjacencyList[i] = new List <int>();
                var line = listReader.ReadNextLine();

                if (line == null)
                {
                    throw new ParserException("Parsing .txt file error.\n" + $"Wrong file format, empty line {listReader.CurrentLineIndex}.");
                }
                else if (line == "#")
                {
                    throw new ParserException("Parsing .txt file error.\n" + $"Invalid amount of verds line {listReader.CurrentLineIndex}.");
                }

                var indexOfSeparator     = line.IndexOf(separator);
                var lastIndexOfSeparator = line.LastIndexOf(separator);

                if (indexOfSeparator != lastIndexOfSeparator)
                {
                    throw new ParserException("Parsing .txt file error.\n" + $"Wrong format, more than 1 arrow in line: {listReader.CurrentLineIndex}. ");
                }
                if (indexOfSeparator == 0)
                {
                    throw new ParserException("Parsing .txt file error.\n" + $"No verd's value in line: {listReader.CurrentLineIndex}. ");
                }
                else if (indexOfSeparator == -1)
                {
                    throw new ParserException("Parsing .txt file error.\n" + $"Wrong format, no arrow in line: {listReader.CurrentLineIndex}. ");
                }

                var splitedLine = line.Split(new string[] { separator }, StringSplitOptions.RemoveEmptyEntries);
                Array.ForEach(splitedLine, x => x.Trim());

                if (splitedLine.Length > 2)
                {
                    throw new ParserException("Parsing .txt file error.\n" + $"Wrong format, more than 1 arrow in line: {listReader.CurrentLineIndex}. ");
                }
                else if (splitedLine[0] == "")
                {
                    throw new ParserException("Parsing .txt file error.\n" + $"No verd's value in line: {listReader.CurrentLineIndex}. ");
                }

                var verd = ReadIntFromString(splitedLine[0], listReader.CurrentLineIndex);
                if (verd != i)
                {
                    throw new ParserException("Parsing .txt file error.\n" + $"Invalid verd's order, they should be ordered by ascending in line: {listReader.CurrentLineIndex}. ");
                }

                if (splitedLine.Length == 2)
                {
                    // Parsing line after arrow sign.
                    var secondLine = splitedLine[1];
                    if (secondLine != "")
                    {
                        var verdsLine = secondLine.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                        if (verdsLine.Length != 0)
                        {
                            for (int j = 0; j < verdsLine.Length; j++)
                            {
                                int v = ReadIntFromString(verdsLine[j], listReader.CurrentLineIndex);
                                if (v > adjacencyList.Length - 1)
                                {
                                    throw new ParserException("Parsing .txt file error.\n" + $"Invalid value of the verd in line: {listReader.CurrentLineIndex}.");
                                }
                                adjacencyList[i].Add(v);
                            }
                        }
                    }
                }
            }
            return(adjacencyList);
        }