Exemplo n.º 1
0
 /// <summary>
 /// Faz a leitura do csv.
 /// </summary>
 /// <param name="tabularItem">O item tabular.</param>
 /// <param name="reader">O leitor.</param>
 /// <exception cref="ArgumentNullException">Se algum dos argumentos for nulo.</exception>
 public void Read(ITabularItem tabularItem, TextReader reader)
 {
     if (reader == null)
     {
         throw new ArgumentNullException("reader");
     }
     else if (tabularItem == null)
     {
         throw new ArgumentNullException("tabularItem");
     }
     else
     {
         this.ReadCsv(tabularItem, reader);
     }
 }
Exemplo n.º 2
0
 /// <summary>
 /// Escreve o item tabular no formato csv.
 /// </summary>
 /// <param name="stream">O controlo da cadeia de escrita do csv.</param>
 /// <param name="tabularItem">O item tabular.</param>
 /// <param name="encoding">A codificação de escrita.</param>
 /// <exception cref="ArgumentNullException">Se o leitor ou ou item forem nulos.</exception>
 public void Write(Stream stream, ITabularItem tabularItem, Encoding encoding)
 {
     if (stream == null)
     {
         throw new ArgumentNullException("stream");
     }
     else if (tabularItem == null)
     {
         throw new ArgumentNullException("tabularItem");
     }
     else
     {
         var writer = new StreamWriter(stream, encoding);
         this.Write(writer, tabularItem);
     }
 }
Exemplo n.º 3
0
        /// <summary>
        /// Escreve o csv para um contentor de texto.
        /// </summary>
        /// <param name="writer">O contentor.</param>
        /// <param name="tabularItem">O item tabular.</param>
        /// <exception cref="ArgumentNullException">Se algum dos argumentos for nulo.</exception>
        public void Write(TextWriter writer, ITabularItem tabularItem)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }
            else if (tabularItem == null)
            {
                throw new ArgumentNullException("tabularItem");
            }
            else
            {
                var lineEnumerator = tabularItem.GetEnumerator();
                if (lineEnumerator.MoveNext())
                {
                    var currentLine      = lineEnumerator.Current;
                    var columnEnumerator = currentLine.GetEnumerator();
                    if (columnEnumerator.MoveNext())
                    {
                        writer.Write(columnEnumerator.Current.GetCellValue <object>());
                        while (columnEnumerator.MoveNext())
                        {
                            writer.Write(this.columnSeparatorChar);
                            writer.Write(columnEnumerator.Current.GetCellValue <object>());
                        }
                    }

                    while (lineEnumerator.MoveNext())
                    {
                        currentLine = lineEnumerator.Current;
                        writer.Write(this.lineSeparatorChar);
                        columnEnumerator = currentLine.GetEnumerator();
                        if (columnEnumerator.MoveNext())
                        {
                            writer.Write(columnEnumerator.Current.GetCellValue <object>());
                            while (columnEnumerator.MoveNext())
                            {
                                writer.Write(this.columnSeparatorChar);
                                writer.Write(columnEnumerator.Current.GetCellValue <object>());
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Realiza a leitura de um csv providenciando a codificação dos carácteres.
        /// </summary>
        /// <param name="tabularItem">O item tabular a ser lido.</param>
        /// <param name="stream">O leitor de informação.</param>
        /// <param name="encoding">A codificação.</param>
        /// <exception cref="ArgumentNullException">Se o leitor de informação for nulo.</exception>
        public void Read(ITabularItem tabularItem, Stream stream, Encoding encoding)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }
            else
            {
                var innerEncoding = encoding;
                if (encoding == null)
                {
                    innerEncoding = Encoding.ASCII;
                }

                var textReader = new StreamReader(stream, innerEncoding);
                this.Read(tabularItem, textReader);
            }
        }
        /// <summary>
        /// Determina o número de elementos na matriz.
        /// </summary>
        /// <param name="matrix">A matriz.</param>
        /// <returns>O número de elementos na matriz e -1 caso exista alguma linha vazia.</returns>
        private int CountVertices(ITabularItem matrix)
        {
            var result = 0;

            foreach (var line in matrix)
            {
                if (line.Count == 0)
                {
                    return(-1);
                }
                else
                {
                    result += line.Count;
                }
            }

            return(result);
        }
Exemplo n.º 6
0
 /// <summary>
 /// Instancia um novo objecto do tipo <see cref="TabularListCell"/>.
 /// </summary>
 /// <param name="rowNumber">O número da linha.</param>
 /// <param name="columnNumber">O número da coluna.</param>
 /// <param name="parent">A linha à qual pertence a célula.</param>
 public TabularListCell(int rowNumber, int columnNumber, ITabularItem parent)
     : base(rowNumber, columnNumber)
 {
     this.parent = parent;
 }
Exemplo n.º 7
0
 /// <summary>
 /// Adiciona um conjunto de elementos a um item tabular.
 /// </summary>
 /// <param name="addObj">O item tabular.</param>
 /// <param name="elements">O conjunto de elementos.</param>
 public void Add(
     ITabularItem addObj,
     IEnumerable <ElementType> elements)
 {
     addObj.Add(elements);
 }
Exemplo n.º 8
0
        /// <summary>
        /// Completa a leitura do csv tendo em conta os leitores individuais de dados.
        /// </summary>
        /// <param name="result">O resultado da leitura.</param>
        /// <param name="reader">O leitor de texto.</param>
        /// <exception cref="UtilitiesDataException">Se a leitura falhar.</exception>
        private void ReadCsv(
            ITabularItem result,
            TextReader reader)
        {
            var elements      = new List <object>();
            var currentItem   = string.Empty;
            var currentLine   = 0;
            var currentColumn = 0;
            var readed        = reader.Read();

            if (readed != -1)
            {
                var readedChar = (char)readed;
                if (readedChar == this.lineSeparatorChar)
                {
                    ++currentLine;
                    var element = this.fieldReader.Invoke(currentLine, currentColumn, currentItem);
                    elements.Add(element);
                    result.Add(elements);
                    currentItem = string.Empty;
                    elements.Clear();
                }
                else if (readedChar == this.columnSeparatorChar)
                {
                    var element = this.fieldReader.Invoke(currentLine, currentColumn, currentItem);
                    elements.Add(element);
                    ++currentColumn;
                    currentItem = string.Empty;
                }
                else if (readedChar == this.delimiter)
                {
                    currentItem += readedChar;
                    currentItem += this.GetBetweenDelimiters(reader);
                }
                else
                {
                    currentItem += readedChar;
                }

                readed = reader.Read();
                while (readed != -1)
                {
                    readedChar = (char)readed;
                    if (readedChar == this.lineSeparatorChar)
                    {
                        ++currentLine;
                        var element = this.fieldReader.Invoke(currentLine, currentColumn, currentItem);
                        elements.Add(element);
                        result.Add(elements);
                        currentItem = string.Empty;
                        elements.Clear();
                    }
                    else if (readedChar == this.columnSeparatorChar)
                    {
                        var element = this.fieldReader.Invoke(currentLine, currentColumn, currentItem);
                        elements.Add(element);
                        ++currentColumn;
                        currentItem = string.Empty;
                    }
                    else if (readedChar == this.delimiter)
                    {
                        currentItem += readedChar;
                        currentItem += this.GetBetweenDelimiters(reader);
                    }
                    else
                    {
                        currentItem += readedChar;
                    }

                    readed = reader.Read();
                }

                var outerElement = this.fieldReader.Invoke(currentLine, currentColumn, currentItem);
                elements.Add(outerElement);
                currentItem = string.Empty;
                result.Add(elements);
            }
        }
Exemplo n.º 9
0
 /// <summary>
 /// Escreve o item tabular no formato csv.
 /// </summary>
 /// <param name="stream">O controlo da cadeia de escrita do csv.</param>
 /// <param name="tabularItem">O item tabular.</param>
 public void Write(Stream stream, ITabularItem tabularItem)
 {
     this.Write(stream, tabularItem, Encoding.ASCII);
 }
Exemplo n.º 10
0
 /// <summary>
 /// Realiza a leitura de um csv.
 /// </summary>
 /// <param name="tabularItem">O item tabular a ser lido.</param>
 /// <param name="stream">O leitor de informação.</param>
 public void Read(ITabularItem tabularItem, Stream stream)
 {
     this.Read(tabularItem, stream, Encoding.ASCII);
 }
Exemplo n.º 11
0
 /// <summary>
 /// Instancia um novo objecto do tipo <see cref="TabularListRow"/>.
 /// </summary>
 /// <param name="rowNumber">O número da linha.</param>
 /// <param name="parent">A tabela que contém a linha.</param>
 public TabularListRow(int rowNumber, ITabularItem parent)
     : base(rowNumber)
 {
     this.parent = parent;
 }
        /// <summary>
        /// Constrói o tuplo ordenado que corresponde à t-decomposição de n de acordo com a matriz
        /// de custos especificada.
        /// </summary>
        /// <param name="n">O número a ser decomposto.</param>
        /// <param name="matrix">A matriz dos custos.</param>
        /// <returns>A decomposição caso exista e nulo caso contrário.</returns>
        /// <exception cref="ArgumentNullException">Se a matriz for nula.</exception>
        public IntMinWeightTdecompResult <CostType> Run(
            int n,
            ITabularItem matrix)
        {
            if (matrix == null)
            {
                throw new ArgumentNullException("matrix");
            }
            else if (n < matrix.Count)
            {
                // Para existir solução, o número a ser decomposto terá de ser superior ao número de elementos
                // na decomposição.
                return(null);
            }
            else
            {
                var countVertices = this.CountVertices(matrix);
                if (countVertices < 0 || countVertices < n)
                {
                    // Não é possível encontrar solução quando n é superior ao número de elementos na matriz
                    // nem quando existe alguma linha vazia.
                    return(null);
                }
                else
                {
                    var p = matrix.Count;
                    var verticesInfoTable = new List <List <VertexCostPair> >();
                    if (0 < p)
                    {
                        var verticesInfoLine  = new List <VertexCostPair>();
                        var offSets           = new int[p - 1];
                        var precomputedNumber = n - p + 1;
                        var currentCosts      = matrix[0];
                        var generalOffset     = n - countVertices - 1;
                        offSets[0] = Math.Max(generalOffset + currentCosts.Count, 0);
                        for (int j = offSets[0]; j < currentCosts.Count && j < precomputedNumber; ++j)
                        {
                            var currentCost = currentCosts[j];
                            verticesInfoLine.Add(new VertexCostPair()
                            {
                                Cost   = currentCosts[j].GetCellValue <CostType>(),
                                Vertex = 0
                            });
                        }

                        verticesInfoTable.Add(verticesInfoLine);

                        for (int i = 1; i < p - 1; ++i)
                        {
                            var previousInfoLine = verticesInfoLine;
                            verticesInfoLine   = new List <VertexCostPair>();
                            currentCosts       = matrix[i];;
                            offSets[i]         = Math.Max(generalOffset + currentCosts.Count, 0);
                            precomputedNumber -= offSets[i];

                            var count             = 0;
                            var previousLineValue = previousInfoLine[0];
                            for (int k = offSets[i]; k < currentCosts.Count && count < precomputedNumber; ++k)
                            {
                                verticesInfoLine.Add(new VertexCostPair()
                                {
                                    Cost = this.ring.Add(
                                        previousLineValue.Cost,
                                        currentCosts[k].GetCellValue <CostType>()),
                                    Vertex = 0
                                });

                                ++count;
                            }

                            var lastIndex = count - 1 + offSets[i];
                            var lastCost  = currentCosts[lastIndex].GetCellValue <CostType>();
                            for (int j = 1; j < previousInfoLine.Count && count < precomputedNumber; ++j)
                            {
                                verticesInfoLine.Add(new VertexCostPair()
                                {
                                    Cost   = this.ring.Add(lastCost, previousInfoLine[j].Cost),
                                    Vertex = j
                                });

                                ++count;
                            }

                            for (int j = 1; j < previousInfoLine.Count && j < precomputedNumber; ++j)
                            {
                                var maxValue = precomputedNumber - j;
                                for (int k = offSets[i]; k < currentCosts.Count - 1 && k < maxValue; ++k)
                                {
                                    var currentPreviousValue = previousInfoLine[j].Cost;
                                    var tempCost             = this.ring.Add(
                                        currentPreviousValue,
                                        currentCosts[k].GetCellValue <CostType>());
                                    var currentVertexLine = verticesInfoLine[k + j - offSets[i]];
                                    if (this.comparer.Compare(tempCost, currentVertexLine.Cost) < 0)
                                    {
                                        currentVertexLine.Cost   = tempCost;
                                        currentVertexLine.Vertex = j;
                                    }
                                }
                            }

                            verticesInfoTable.Add(verticesInfoLine);
                        }

                        // p iguala o número total de componentes do custo
                        currentCosts = matrix[p - 1];
                        var lastOffSet = Math.Max(generalOffset + currentCosts.Count, 0);
                        precomputedNumber -= lastOffSet;
                        var vertex = verticesInfoLine.Count;
                        var diff   = precomputedNumber - vertex;
                        --vertex;
                        var lastValue = new VertexCostPair()
                        {
                            Cost = this.ring.Add(
                                verticesInfoLine[vertex].Cost,
                                currentCosts[diff].GetCellValue <CostType>()),
                            Vertex = vertex
                        };

                        --vertex;
                        for (int i = diff + 1; i < currentCosts.Count; ++i)
                        {
                            var currentValue = this.ring.Add(
                                verticesInfoLine[vertex].Cost,
                                currentCosts[i].GetCellValue <CostType>());
                            if (this.comparer.Compare(currentValue, lastValue.Cost) < 0)
                            {
                                lastValue.Cost   = currentValue;
                                lastValue.Vertex = vertex;
                            }

                            --vertex;
                        }

                        return(this.GetSolution(n, p, offSets, lastValue, verticesInfoTable));
                    }
                }
            }

            return(null);
        }