//Similar code above but doesn't pad out a variable's line until it needs to /// <summary> /// Create a DenseStructMatrix from a sequences of RowKeyColKeyValue triples. /// </summary> /// <param name="tripleEnumerable">The squences of RowKeyColKeyValue triples</param> public void GetInstanceFromSparseInternal(IEnumerable <RowKeyColKeyValue <string, string, TValue> > tripleEnumerable) { ColSerialNumbers = new SerialNumbers <string>(); RowKeyToStoreList = new Dictionary <string, List <TStore> >(); foreach (var triple in tripleEnumerable) { string rowKey = triple.RowKey; List <TStore> storeList; if (!RowKeyToStoreList.TryGetValue(rowKey, out storeList)) { storeList = Enumerable.Repeat(StoreMissingValue, ColSerialNumbers.Count).ToList(); RowKeyToStoreList[rowKey] = storeList; } string colKey = triple.ColKey; TValue value = triple.Value; TStore store = ValueToStore(value); Helper.CheckCondition(!store.Equals(StoreMissingValue), () => string.Format(CultureInfo.InvariantCulture, Properties.Resource.ErrorConvertingValGaveMissingValue, value, StoreMissingValue)); int colIndex = ColSerialNumbers.GetNewOrOld(colKey); if (colIndex < storeList.Count) { Helper.CheckCondition(storeList[colIndex].Equals(StoreMissingValue), () => string.Format(CultureInfo.InvariantCulture, "Each pair of keys, i,e,.<{0},{1}>, should only be seen once", rowKey, colKey)); storeList[colIndex] = store; } else { while (colIndex > storeList.Count) { storeList.Add(StoreMissingValue); } // REVIEW: is this a bogus check? Only a failure of storeList.Add could make this not be true Helper.CheckCondition(colIndex == storeList.Count, () => Properties.Resource.RealAssert); storeList.Add(store); } } foreach (var storeList in RowKeyToStoreList.Values) { storeList.TrimExcess(); } _rowKeys = RowKeyToStoreList.Keys.ToList(); _indexOfRowKey = RowKeys.Select((key, index) => new { key, index }).ToDictionary(keyAndIndex => keyAndIndex.key, keyAndIndex => keyAndIndex.index); }
//Similar code above but doesn't pad out a variable's line until it needs to /// <summary> /// Create a DenseStructMatrix from a sequences of RowKeyColKeyValue triples. /// </summary> /// <param name="tripleEnumerable">The squences of RowKeyColKeyValue triples</param> public void GetInstanceFromSparseInternal(IEnumerable <RowKeyColKeyValue <string, string, TValue> > tripleEnumerable) { ColSerialNumbers = new SerialNumbers <string>(); RowKeyToStoreList = new Dictionary <string, List <TStore> >(); foreach (var triple in tripleEnumerable) { string rowKey = triple.RowKey; List <TStore> storeList; if (!RowKeyToStoreList.TryGetValue(rowKey, out storeList)) { storeList = Enumerable.Repeat(StoreMissingValue, ColSerialNumbers.Count).ToList(); RowKeyToStoreList[rowKey] = storeList; } string colKey = triple.ColKey; TValue value = triple.Value; TStore store = ValueToStore(value); Helper.CheckCondition(!store.Equals(StoreMissingValue), "Vals cannot covert to '" + StoreMissingValue.ToString() + "'"); int colIndex = ColSerialNumbers.GetNewOrOld(colKey); if (colIndex < storeList.Count) { storeList[colIndex] = store; } else { while (colIndex > storeList.Count) { storeList.Add(StoreMissingValue); } Helper.CheckCondition(colIndex == storeList.Count, "real assert"); storeList.Add(store); } } foreach (var storeList in RowKeyToStoreList.Values) { storeList.TrimExcess(); } _rowKeys = RowKeyToStoreList.Keys.ToList(); _indexOfRowKey = RowKeys.Select((key, index) => new { key, index }).ToDictionary(keyAndIndex => keyAndIndex.key, keyAndIndex => keyAndIndex.index); }
internal void GetInstanceFromSparseInternal(string inputSparsePattern) { ColSerialNumbers = new SerialNumbers <string>(); RowKeyToStoreList = new Dictionary <string, List <TStore> >(); //!!!05/18/2009 optimize could be made faster with plinq using an index read to keep the order of the rowKeys (aka vars) foreach (List <string[]> linesWithSameRowKey in SparseGroupedByVar(inputSparsePattern, /*zeroIsOK*/ false, "File {0}")) { Debug.Assert(linesWithSameRowKey.Count > 0); // real assert string rowKey = linesWithSameRowKey[0][0]; List <TStore> storeList = Enumerable.Repeat(StoreMissingValue, ColSerialNumbers.Count).ToList(); RowKeyToStoreList.Add(rowKey, storeList); foreach (string[] rowKeyColKeyVal in linesWithSameRowKey) { string colKey = rowKeyColKeyVal[1]; string valAsString = rowKeyColKeyVal[2]; TStore store = SparseValToStore(valAsString); Helper.CheckCondition(!store.Equals(StoreMissingValue), () => string.Format(CultureInfo.InvariantCulture, Properties.Resource.ErrorConvertingValGaveMissingValue, valAsString, StoreMissingValue)); //OK to use Equals because TScore can't be null int colIndex = ColSerialNumbers.GetNewOrOld(colKey); if (colIndex < storeList.Count) { Helper.CheckCondition(storeList[colIndex].Equals(StoreMissingValue), () => string.Format(CultureInfo.InvariantCulture, "Each pair of keys, i,e,.<{0},{1}>, should only be seen once", rowKey, colKey)); storeList[colIndex] = store; } else { Helper.CheckCondition(colIndex == storeList.Count, () => Properties.Resource.ErrorInputDataShouldBeGroupedByVar); storeList.Add(store); } } storeList.TrimExcess(); } _rowKeys = RowKeyToStoreList.Keys.ToList(); _indexOfRowKey = RowKeys.Select((key, index) => new { key, index }).ToDictionary(keyAndIndex => keyAndIndex.key, keyAndIndex => keyAndIndex.index); }
internal void GetInstanceFromSparseInternal(string inputSparsePattern) { ColSerialNumbers = new SerialNumbers <string>(); RowKeyToStoreList = new Dictionary <string, List <TStore> >(); //!!!05/18/2009 optimize could be made faster with plinq using an index read to keep the order of the rowKeys (aka vars) foreach (List <string[]> linesWithSameRowKey in SparseGroupedByVar(inputSparsePattern, /*zeroIsOK*/ false, "File {0}")) { Debug.Assert(linesWithSameRowKey.Count > 0); // real assert string rowKey = linesWithSameRowKey[0][0]; List <TStore> storeList = Enumerable.Repeat(StoreMissingValue, ColSerialNumbers.Count).ToList(); RowKeyToStoreList.Add(rowKey, storeList); foreach (string[] rowKeyColKeyVal in linesWithSameRowKey) { string colKey = rowKeyColKeyVal[1]; string valAsString = rowKeyColKeyVal[2]; TStore store = SparseValToStore(valAsString); Helper.CheckCondition(!store.Equals(StoreMissingValue), "Vals cannot covert to '" + StoreMissingValue.ToString() + "'"); //OK to use Equals because TScore can't be null int colIndex = ColSerialNumbers.GetNewOrOld(colKey); if (colIndex < storeList.Count) { storeList[colIndex] = store; } else { Helper.CheckCondition(colIndex == storeList.Count, "the input data should be grouped by var"); storeList.Add(store); } } storeList.TrimExcess(); } _rowKeys = RowKeyToStoreList.Keys.ToList(); _indexOfRowKey = RowKeys.Select((key, index) => new { key, index }).ToDictionary(keyAndIndex => keyAndIndex.key, keyAndIndex => keyAndIndex.index); }