-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSubArray.cs
More file actions
72 lines (53 loc) · 1.42 KB
/
SubArray.cs
File metadata and controls
72 lines (53 loc) · 1.42 KB
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
namespace Menees.Diffs
{
#region Using Directives
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
#endregion
/// <summary>
/// Allows 1..M access for a selected portion of an int array.
/// </summary>
internal sealed class SubArray<T>
where T : IComparable<T>
{
#region Private Data Members
private readonly IList<T> data;
private readonly int length; // Stores the length of this subarray
private readonly int offset; // Stores the 0-based offset into this.arData where this subarray should start
#endregion
#region Constructors
public SubArray(IList<T> data)
{
this.data = data;
this.offset = 0;
this.length = this.data.Count;
}
public SubArray(SubArray<T> data, int offset, int length)
{
this.data = data.data;
// Subtract 1 here because offset will be 1-based
this.offset = data.offset + offset - 1;
this.length = length;
}
#endregion
#region Public Properties
public int Length => this.length;
public int Offset => this.offset;
public T this[int index] => this.data[this.offset + index - 1];
#endregion
#region Public Methods
public override string ToString()
{
const int SingleDataWidth = 3;
StringBuilder sb = new(SingleDataWidth * this.length);
for (int i = 0; i < this.length; i++)
{
sb.AppendFormat("{0} ", this.data[this.offset + i]);
}
return sb.ToString();
}
#endregion
}
}