/*
 * get_unique_perms: find unique permutations of a combination vector
 * which may have non-distinct entries.  For example, if _c = [1 5 5]
 * and _perms = [1 2 3; 1 3 2; 2 3 1; 2 1 3; 3 1 2; 3 2 1] then the
 * result should be r = [1 3 4 0 0 0] and the three unique permutations
 * are _c(_perms(_r(i),:)) i=1,2,3, i.e. [1 5 5], [5 1 5], and [5 5 1]
 */
vector get_unique_perms( vector _c, matrix _perms)
{
  int np = rows(_perms);
  vector ci, r = zeros(1,np);
  int nr, i, j, unique;

  r(1) = 1;	/* start with first permutation */
  nr = 1;

  for( i = 2; i <= np; ++i) {	/* check all of the other permutations */

    unique = 1;

    ci = _c(_perms(i,:));

    for( j = 1; j <= nr; ++j)
      if( all( _c(_perms(r(j),:)) == ci)) {	/* matched previous permutation */
        unique = 0;
	break;
      }

    if( unique) {	/* found another unique permutation */
      ++nr;
      r(nr) = i;
    }
  }

  return r;
}
