public SparseMultiIndexTable(string fileName, float defaultValue = 0.0f) { long lineNumber = 2; Data = new Dictionary <string, float>(); using (var reader = new CommentedStreamReader(fileName)) { var line = reader.ReadLine(); //get the header if (line != null) { var cells = line.Split(','); if (cells.Length < 2) { throw new IOException("A multi-index table requires at least two columns!"); } //Load header data, initialize this class. MappedIndices = new HashSet <int> [cells.Length - 1]; for (var i = 0; i < Dimensions; i++) { MappedIndices[i] = new HashSet <int>(); } IndexNames = new string[Dimensions]; for (var i = 0; i < Dimensions; i++) { IndexNames[i] = cells[i]; } DefaultValue = defaultValue; var indices = new int[Dimensions]; //This is getting constantly recycled, so there's no sense in using a malloc each time. //Read the actual data while (!reader.EndOfStream) { line = reader.ReadLine(); if (line == null) { return; } cells = line.Split(','); lineNumber++; if (cells.Length != (Dimensions + 1)) { throw new IOException("Error reading line " + lineNumber + ": The number of cells on this line needs to be " + (Dimensions + 1) + ", instead was " + cells.Length + " to match the correct size of this table."); } var value = Convert.ToSingle(cells[Dimensions]); //Get the last index // ReSharper disable once CompareOfFloatsByEqualityOperator if (value == DefaultValue) { continue; //Skip cells with this table's default value. } for (var i = 0; i < Dimensions; i++) { indices[i] = Convert.ToInt32(cells[i]); //Parse each int } for (var i = 0; i < Dimensions; i++) { MappedIndices[i].Add(indices[i]); // Store the mapped index to the HashSet. } var key = ConvertAddress(indices); Data[key] = value; } } } }
public SparseMultiIndexTable(string FileName, float DefaultValue = 0.0f) { long lineNumber = 2; this.Data = new Dictionary <string, float>(); using (var reader = new CommentedStreamReader(FileName)) { string line = reader.ReadLine(); //get the header string[] cells = line.Split(','); if (cells.Length < 2) { throw new IOException("A multi-index table requires at least two columns!"); } //Load header data, initialize this class. this.MappedIndices = new HashSet <int> [cells.Length - 1]; for (int i = 0; i < this.Dimensions; i++) { this.MappedIndices[i] = new HashSet <int>(); } this.IndexNames = new string[this.Dimensions]; for (int i = 0; i < this.Dimensions; i++) { this.IndexNames[i] = cells[i]; } this.DefaultValue = DefaultValue; int[] indices = new int[this.Dimensions]; //This is getting constantly recycled, so there's no sense in using a malloc each time. string key; //Read the actual data while (!reader.EndOfStream) { cells = reader.ReadLine().Split(','); lineNumber++; if (cells.Length != (this.Dimensions + 1)) { throw new IOException("Error reading line " + lineNumber + ": The number of cells on this line needs to be " + (this.Dimensions + 1) + ", instead was " + cells.Length + " to match the correct size of this table."); } float value = Convert.ToSingle(cells[this.Dimensions]); //Get the last index if (value == this.DefaultValue) { continue; //Skip cells with this table's default value. } for (int i = 0; i < this.Dimensions; i++) { indices[i] = Convert.ToInt32(cells[i]); //Parse each int } for (int i = 0; i < this.Dimensions; i++) { this.MappedIndices[i].Add(indices[i]); // Store the mapped index to the HashSet. } key = this.ConvertAddress(indices); this.Data[key] = value; } } }
public SparseMultiIndexTable(string FileName, float DefaultValue = 0.0f) { long lineNumber = 2; this.Data = new Dictionary<string, float>(); using ( var reader = new CommentedStreamReader( FileName ) ) { string line = reader.ReadLine(); //get the header string[] cells = line.Split( ',' ); if ( cells.Length < 2 ) { throw new IOException( "A multi-index table requires at least two columns!" ); } //Load header data, initialize this class. this.MappedIndices = new HashSet<int>[cells.Length - 1]; for ( int i = 0; i < this.Dimensions; i++ ) this.MappedIndices[i] = new HashSet<int>(); this.IndexNames = new string[this.Dimensions]; for ( int i = 0; i < this.Dimensions; i++ ) this.IndexNames[i] = cells[i]; this.DefaultValue = DefaultValue; int[] indices = new int[this.Dimensions]; //This is getting constantly recycled, so there's no sense in using a malloc each time. string key; //Read the actual data while ( !reader.EndOfStream ) { cells = reader.ReadLine().Split( ',' ); lineNumber++; if ( cells.Length != ( this.Dimensions + 1 ) ) { throw new IOException( "Error reading line " + lineNumber + ": The number of cells on this line needs to be " + ( this.Dimensions + 1 ) + ", instead was " + cells.Length + " to match the correct size of this table." ); } float value = Convert.ToSingle( cells[this.Dimensions] ); //Get the last index if ( value == this.DefaultValue ) continue; //Skip cells with this table's default value. for ( int i = 0; i < this.Dimensions; i++ ) indices[i] = Convert.ToInt32( cells[i] ); //Parse each int for ( int i = 0; i < this.Dimensions; i++ ) this.MappedIndices[i].Add( indices[i] ); // Store the mapped index to the HashSet. key = this.ConvertAddress( indices ); this.Data[key] = value; } } }