MegaBites-AI/Windows-powershell
0372
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4using System.Collections;5 6using Dbg = System.Management.Automation.Diagnostics;7 8namespace System.Management.Automation.Remoting9{10 /// <summary>11 /// Provides an enumerator for iterating through a multi-dimensional array.12 /// This is needed to encode multi-dimensional arrays in remote host methods.13 /// </summary>14 internal class Indexer : IEnumerable, IEnumerator15 {16 /// <summary>17 /// Current.18 /// </summary>19 private readonly int[] _current;20 21 /// <summary>22 /// Lengths.23 /// </summary>24 private readonly int[] _lengths;25 26 /// <summary>27 /// Current.28 /// </summary>29 public object Current30 {31 get32 {33 return _current;34 }35 }36 37 /// <summary>38 /// Constructor for Indexer.39 /// </summary>40 internal Indexer(int[] lengths)41 {42 Dbg.Assert(lengths != null, "Expected lengths != null");43 _lengths = lengths;44 Dbg.Assert(CheckLengthsNonNegative(lengths), "Expected CheckLengthsNonNegative(lengths)");45 _current = new int[lengths.Length];46 }47 48 /// <summary>49 /// Check lengths non negative.50 /// </summary>51 [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]52 private static bool CheckLengthsNonNegative(int[] lengths)53 {54 for (int i = 0; i < lengths.Length; ++i)55 {56 if (lengths[i] < 0)57 {58 return false;59 }60 }61 62 return true;63 }64 65 /// <summary>66 /// Get enumerator.67 /// </summary>68 public IEnumerator GetEnumerator()69 {70 this.Reset();71 return this;72 }73 74 /// <summary>75 /// Reset.76 /// </summary>77 public void Reset()78 {79 for (int i = 0; i < _current.Length; ++i)80 {81 _current[i] = 0;82 }83 84 // Set last value to -1 so that MoveNext makes this 0,0,...,0.85 if (_current.Length > 0)86 {87 _current[_current.Length - 1] = -1;88 }89 }90 91 /// <summary>92 /// Move next.93 /// </summary>94 public bool MoveNext()95 {96 for (int i = _lengths.Length - 1; i >= 0; --i)97 {98 // See if we can increment this dimension.99 if (_current[i] < _lengths[i] - 1)100 {101 _current[i]++;102 return true;103 }104 105 // Otherwise reset i and try to increment the next one.106 _current[i] = 0;107 }108 109 return false;110 }111 }112}113 