File size: 2,946 Bytes
8c763fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.Collections;

using Dbg = System.Management.Automation.Diagnostics;

namespace System.Management.Automation.Remoting
{
    /// <summary>
    /// Provides an enumerator for iterating through a multi-dimensional array.
    /// This is needed to encode multi-dimensional arrays in remote host methods.
    /// </summary>
    internal class Indexer : IEnumerable, IEnumerator
    {
        /// <summary>
        /// Current.
        /// </summary>
        private readonly int[] _current;

        /// <summary>
        /// Lengths.
        /// </summary>
        private readonly int[] _lengths;

        /// <summary>
        /// Current.
        /// </summary>
        public object Current
        {
            get
            {
                return _current;
            }
        }

        /// <summary>
        /// Constructor for Indexer.
        /// </summary>
        internal Indexer(int[] lengths)
        {
            Dbg.Assert(lengths != null, "Expected lengths != null");
            _lengths = lengths;
            Dbg.Assert(CheckLengthsNonNegative(lengths), "Expected CheckLengthsNonNegative(lengths)");
            _current = new int[lengths.Length];
        }

        /// <summary>
        /// Check lengths non negative.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
        private static bool CheckLengthsNonNegative(int[] lengths)
        {
            for (int i = 0; i < lengths.Length; ++i)
            {
                if (lengths[i] < 0)
                {
                    return false;
                }
            }

            return true;
        }

        /// <summary>
        /// Get enumerator.
        /// </summary>
        public IEnumerator GetEnumerator()
        {
            this.Reset();
            return this;
        }

        /// <summary>
        /// Reset.
        /// </summary>
        public void Reset()
        {
            for (int i = 0; i < _current.Length; ++i)
            {
                _current[i] = 0;
            }

            // Set last value to -1 so that MoveNext makes this 0,0,...,0.
            if (_current.Length > 0)
            {
                _current[_current.Length - 1] = -1;
            }
        }

        /// <summary>
        /// Move next.
        /// </summary>
        public bool MoveNext()
        {
            for (int i = _lengths.Length - 1; i >= 0; --i)
            {
                // See if we can increment this dimension.
                if (_current[i] < _lengths[i] - 1)
                {
                    _current[i]++;
                    return true;
                }

                // Otherwise reset i and try to increment the next one.
                _current[i] = 0;
            }

            return false;
        }
    }
}