/// <summary> /// Resizes the MultiDimension keeping values on the common 'surface'. /// Note that you can change any dimension index, but you cannot change the number of dimensions. /// </summary> public void Resize(params int[] newDimensions) { var olddimens = this.Dimensions; var oldvalues = this.values; if (this.Dimensions.Count == newDimensions.Length) { var aNotLastHasChanged = false; for (int i = 0; i < newDimensions.Length - 1; i++) { if (this.Dimensions[i] != newDimensions[i]) { aNotLastHasChanged = true; break; } } if (aNotLastHasChanged) { // Not only the last index has changed; any index can have changed: // Create a target instance as helper: var target = new MultiDimensional <TElement>(newDimensions); // Datermine common surface: var commonSurface = new int[newDimensions.Length]; for (int i = 0; i < newDimensions.Length; i++) { commonSurface[i] = Math.Min(olddimens[i], newDimensions[i]); } // Copy common surface values: foreach (var index in MultiCounter.Enumerate(commonSurface)) { target[index] = this[index]; } // Substitute current instance by target: this.Dimensions = newDimensions.ToList().AsReadOnly(); this.flatteningFactors = target.flatteningFactors; this.values = target.values; } else if (this.Dimensions[newDimensions.Length - 1] != newDimensions[newDimensions.Length - 1]) { // Only the last index changed. this.Initialize(newDimensions); Array.Copy(oldvalues, this.values, Math.Min(oldvalues.Length, this.values.Length)); } else { // All dimensions (last AND notlasts) are equal, nothing to do... } } else { // The number of dimensions has changed: throw new NotSupportedException("Cannot resize a MultiDimensional to a different number of dimensions."); } }
public MultiCounter Reversed() { var reversedAmplitudes = Amplitudes.Reverse().ToArray(); var reversedMultiCounter = new MultiCounter(reversedAmplitudes, this.RollingOver); reversedMultiCounter.values = this.values.Reverse().ToArray(); return(reversedMultiCounter); }