public static IEnumerable <T> GetReindexed <T>(this IIndexTranslator source, IReadOnlyList <T> beforeReindexing) { for (var dstIndex = 0; dstIndex < beforeReindexing.Count; ++dstIndex) { yield return(beforeReindexing[source.GetSrcIndex(dstIndex)]); } }
public static IEnumerable <KeyValuePair <int, int> > GetDestToSrcIndexPairs(this IIndexTranslator source) { var count = source.ItemsCount; for (var dstIndex = 0; dstIndex < count; dstIndex++) { yield return(new KeyValuePair <int, int>(source.GetSrcIndex(dstIndex), dstIndex)); } }
/// <summary>Applies the reindexing specified by the given translator to the given list.</summary> /// <seealso cref="GetReindexed{T}"/> /// <remarks>The complexity of this operation is O(N)</remarks> /// <typeparam name="T">The type of an item.</typeparam> /// <param name="source">The index translation source.</param> /// <param name="list">The list to adjust items order.</param> public static void ApplyReindexing <T>(this IIndexTranslator source, IList <T> list) { if (list.Count <= 0) { return; } var count = source.ItemsCount; var marked = new BitArray(list.Count); var markedCount = 0; for (var idx = 0; idx < count && markedCount < count; idx++) { var dstIndex = source.GetDestIndex(idx); if (dstIndex == idx || marked[dstIndex]) { // no index change or already copied continue; } var srcValue = list[idx]; while (!marked[dstIndex]) { ++markedCount; marked[dstIndex] = true; var tmp = list[dstIndex]; list[dstIndex] = srcValue; srcValue = tmp; dstIndex = source.GetSrcIndex(dstIndex); } } }
/// <summary> /// Initializes a new instance of the <see cref="SymbolToIndexConverter{TSymbol}" /> class copying data from the other map corresponding to the items sequence before re-indexing. /// It maps the symbols to the new index sequence respecting the specified result of re-indexing operation. /// </summary> /// <param name="prev">The map before re-indexing.</param> /// <param name="reindexingResult">The re-indexing result.</param> public SymbolToIndexConverter(ISymbolToIndexReadOnlyConverter <TSymbol> prev, IIndexTranslator reindexingResult) : this(reindexingResult.ItemsCount, prev.Comparer) { var count = reindexingResult.ItemsCount; for (var dstIndex = 0; dstIndex < count; dstIndex++) { AddSymbol(prev.GetSymbolAt(reindexingResult.GetSrcIndex(dstIndex))); } }
/// <summary> /// Initializes a new instance of the <see cref="ReindexedVertexAdjacencyFactoryWrapper{TEdgeData}" /> class. /// </summary> /// <param name="translator">The index translator used to convert old vertices indexes to new ones.</param> /// <param name="wrapped">The wrapped factory.</param> public ReindexedVertexAdjacencyFactoryWrapper(IIndexTranslator translator, IVertexAdjacencyFactory <TEdgeData> wrapped) { _translator = translator; _wrapped = wrapped; }
public static ReindexedDataResult <TData> ToReindexedDataResult <TData>(this IIndexTranslator source, TData sequence) { return(new ReindexedDataResult <TData>(source, sequence)); }
/// <summary> /// Initializes a new instance of the <see cref="SymbolToIndexConverter{TSymbol}" /> class copying data from the other map /// corresponding to the items sequence before re-indexing. /// It maps the symbols to the new index sequence respecting the specified result of re-indexing operation. /// </summary> /// <param name="prev">The map before re-indexing.</param> /// <param name="reindexingResult">The re-indexing result.</param> public SymbolToIndexReadOnlyConverter(ISymbolToIndexReadOnlyConverter <TSymbol> prev, IIndexTranslator reindexingResult) { var count = reindexingResult.ItemsCount; _symbolToIndexMap = new Dictionary <TSymbol, int>(count, prev.Comparer); _indexToSymbolMap = new TSymbol[count]; for (var dstIndex = 0; dstIndex < count; dstIndex++) { var srcIndex = reindexingResult.GetSrcIndex(dstIndex); var symbol = prev.GetSymbolAt(srcIndex); _symbolToIndexMap.Add(symbol, dstIndex); _indexToSymbolMap[dstIndex] = symbol; } }