Exemple #1
0
        /// <summary>
        /// Writes each internal array of the provided sparse matrix to a different file. All these filenames have a common
        /// prefix, extracted from <paramref name="pathBase"/>, and a suffix corresponding to the purpose of each array.
        /// </summary>
        /// <param name="matrix">The sparse matrix to write.</param>
        /// <param name="pathBase">An absolute path. This filename will not be used directly. Instead one file per internal array
        ///     of <paramref name="matrix"/> will be used. Each file will be suffixed with the name of that array and then the
        ///     extension specified in <paramref name="pathBase"/>.</param>
        /// <param name="writeArrayLengthFirst">If true, the first line of each file will contain the length of the corresponding
        ///     array. If false, there will be only one line per file, which will contain the array.</param>
        public void WriteToMultipleFiles(ISparseMatrix matrix, string pathBase, bool writeArrayLengthFirst = true) // TODO: this should be a different writer
        {
            string       path         = Path.GetDirectoryName(pathBase);
            string       nameOnly     = Path.GetFileNameWithoutExtension(pathBase);
            string       ext          = Path.GetExtension(pathBase);
            SparseFormat sparseFormat = matrix.GetSparseFormat();

            // Values array
            string suffix     = "-" + sparseFormat.RawValuesTitle.ToLower();
            string valuesPath = path + "\\" + nameOnly + suffix + ext; // Not too sure about the \\

            using (var writer = new StreamWriter(valuesPath))
            {
#if DEBUG
                writer.AutoFlush = true; // To look at intermediate output at certain breakpoints
#endif
                WriteArray(sparseFormat.RawValuesArray, writer, writeArrayLengthFirst);
            }

            // Indexing arrays
            foreach (var nameArrayPair in sparseFormat.RawIndexArrays)
            {
                suffix = "-" + nameArrayPair.Key.ToLower();
                string indexerPath = path + "\\" + nameOnly + suffix + ext; // Not too sure about the \\
                using (var writer = new StreamWriter(indexerPath))
                {
#if DEBUG
                    writer.AutoFlush = true; // To look at intermediate output at certain breakpoints
#endif
                    WriteArray(nameArrayPair.Value, writer, writeArrayLengthFirst);
                }
            }
        }
Exemple #2
0
        private void WriteToStream(ISparseMatrix matrix, StreamWriter writer)
        {
            SparseFormat sparseFormat = matrix.GetSparseFormat();

            writer.Write(sparseFormat.RawValuesTitle + ": ");
            if (titlesOnOtherLines)
            {
                writer.WriteLine();
            }
            WriteArray(sparseFormat.RawValuesArray, writer, false);

            foreach (var nameArrayPair in sparseFormat.RawIndexArrays)
            {
                if (lineBetweenArrays)
                {
                    writer.WriteLine();
                }
                writer.WriteLine(); // otherwise everything would be on the same line
                writer.Write(nameArrayPair.Key + ": ");
                if (titlesOnOtherLines)
                {
                    writer.WriteLine();
                }
                WriteArray(nameArrayPair.Value, writer, false);
            }
        }