Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Address Subsets to-do about clearer members names #1048

Merged
merged 1 commit into from
Nov 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 21 additions & 22 deletions MoreLinq/Subsets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,12 @@ sealed class SubsetEnumerator : IEnumerator<IList<T>>
readonly T[] _subset; // the current subset to return
readonly int[] _indices; // indices into the original set

// TODO: It would be desirable to give these index members clearer names
bool _continue; // termination indicator, set when all subsets have been produced

int _m; // previous swap index (upper index)
int _m2; // current swap index (lower index)
int _k; // size of the subset being produced
int _n; // size of the original set (sequence)
int _z; // count of items excluded from the subset
int _prevSwapIndex; // previous swap index (upper index)
int _currSwapIndex; // current swap index (lower index)
int _subsetSize; // size of the subset being produced
int _setSize; // size of the original set (sequence)

public SubsetEnumerator(IList<T> set, int subsetSize)
{
Expand All @@ -161,11 +159,10 @@ public SubsetEnumerator(IList<T> set, int subsetSize)

public void Reset()
{
_m = _subset.Length;
_m2 = -1;
_k = _subset.Length;
_n = _set.Count;
_z = _n - _k + 1;
_prevSwapIndex = _subset.Length;
_currSwapIndex = -1;
_subsetSize = _subset.Length;
_setSize = _set.Count;
_continue = true;
}

Expand All @@ -178,35 +175,37 @@ public bool MoveNext()
if (!_continue)
return false;

if (_m2 == -1)
if (_currSwapIndex == -1)
{
_m2 = 0;
_m = _k;
_currSwapIndex = 0;
_prevSwapIndex = _subsetSize;
}
else
{
if (_m2 < _n - _m)
if (_currSwapIndex < _setSize - _prevSwapIndex)
{
_m = 0;
_prevSwapIndex = 0;
}
_m++;
_m2 = _indices[_k - _m];
_prevSwapIndex++;
_currSwapIndex = _indices[_subsetSize - _prevSwapIndex];
}

for (var j = 1; j <= _m; j++)
_indices[_k + j - _m - 1] = _m2 + j;
for (var j = 1; j <= _prevSwapIndex; j++)
_indices[_subsetSize + j - _prevSwapIndex - 1] = _currSwapIndex + j;

ExtractSubset();

_continue = _indices.Length > 0 && _indices[0] != _z;
// ........................................ count of items excluded from the subset
_continue = _indices is [var i, ..] && i != _setSize - _subsetSize + 1;

return true;
}

void IDisposable.Dispose() { }

void ExtractSubset()
{
for (var i = 0; i < _k; i++)
for (var i = 0; i < _subsetSize; i++)
_subset[i] = _set[_indices[i] - 1];
}
}
Expand Down