Skip to content

Commit 13d9f64

Browse files
committed
Improve perms.cc (bug #67423)
* libinterp/corefcn/perms.cc: Determine the unique elements and the number of permutations in one step instead of two separate steps.
1 parent 2803556 commit 13d9f64

File tree

1 file changed

+7
-19
lines changed

1 file changed

+7
-19
lines changed

libinterp/corefcn/perms.cc

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,11 @@ GetPerms (const Array<T>& ar_in, bool uniq_v = false)
7474
// In practice, and because n must be very small, mutual comparison is
7575
// typically faster and consumes less memory.
7676

77+
// Number of unique permutations is n! / (n_el1! * n_el2! * ...)
78+
// where n_elX is the number of myvidx elements with value X.
7779
for (octave_idx_type i = 0; i < m - 1; i++)
7880
{
81+
octave_idx_type count = 1;
7982
for (octave_idx_type j = i + 1; j < m; j++)
8083
{
8184
bool isequal;
@@ -86,30 +89,15 @@ GetPerms (const Array<T>& ar_in, bool uniq_v = false)
8689
isequal = (Ar[i] == Ar[j]);
8790

8891
if (myvidx[j] > myvidx[i] && isequal)
92+
{
8993
myvidx[j] = myvidx[i]; // not yet processed...
94+
++count;
95+
}
9096
}
97+
nr /= Factorial (count);
9198
}
9299

93100
// At this point, myvidx serves as a unique id of the elements.
94-
// Two elements having the same myvidx are equal.
95-
96-
// Number of unique permutations is n! / (n_el1! * n_el2! * ...)
97-
// where n_elX is the number of myvidx elements with value X.
98-
// There can be no more than m different ids.
99-
octave_idx_type cumulative = 0;
100-
for (octave_idx_type i = 0; i < m; i++) // each possible id
101-
{
102-
octave_idx_type count = 0;
103-
for (octave_idx_type j = i; j < m; j++) // range for this id
104-
if (myvidx[j] == i)
105-
++count;
106-
107-
nr /= Factorial (count);
108-
109-
cumulative += count;
110-
if (cumulative == m) // all elements accounted for, break early
111-
break;
112-
}
113101
}
114102

115103
// Sort vector indices for inverse lexicographic order later.

0 commit comments

Comments
 (0)