Macaulay2 Engine
Loading...
Searching...
No Matches
comb.hpp
Go to the documentation of this file.
1// Copyright 1997 by Michael E. Stillman
2#ifndef _comb_hh_
3#define _comb_hh_
4
29
30#include <cassert>
31#include <vector>
32#include <iosfwd>
33
34// Class: Subsets
35// Manipulate p-subsets of 0..n-1, for fixed n, but possibly several p.
36//
37// Encoding and decoding is done as follows:
38// Consider e.g. 3-subsets:
39// These are
40// 0 = 0 1 2
41// 1 = 0 1 3
42// 2 = 0 2 3
43// 3 = 1 2 3
44// 4 = 0 1 4
45// 5 = 0 2 4
46// 6 = 0 3 4
47// 7 = 1 2 4
48// 8 = 1 3 4
49// 9 = 2 3 4
50// ... (if there are more than 5 elements in the set).
51// Notice that the encoded value does not depend on the number of elements
52// [a0, a1, ..., a(p-1)] becomes
53// the integer binomial(a0,1) + binomial(a1,2) + binomial(a2,3) + ...
54// example: 10 = 0 1 5. This is binomial(0,1) + binomial(1,2) + binomial(5,3) =
55// 10
56// example: 9 = 2 3 4. 9 == 2 + binomial(3,2) + binomial(4,3) = 2 + 3 + 4
57
58typedef std::vector<size_t> Subset;
59
74{
75 public:
76 // Create an object which allows to encode and decode
77 // q-subsets of a (size n) set, for all q <= p.
78 // Throws an exception if the result will not fit into a size_t.
79 Subsets(size_t n, size_t p);
80
81 ~Subsets();
82
83 bool isValid(const Subset &a);
84
85 size_t encode(const Subset &a); // an assertion failure if a.size() is > p,
86
87 // encode the subset obtained by removing the
88 // element of a at index 'index',
89 size_t encodeBoundary(size_t index, const Subset &a);
90
91 void decode(size_t val, Subset &result);
92
93 static void show(std::ostream &o, const Subset &a);
94
95 // Take a p-subset s of 0..n-1 and change it to be the next one in the above
96 // mentioned order
97 static bool increment(size_t n, Subset &s);
98
99 static bool increment(size_t n, size_t subset_size, size_t *subset);
100
101 static bool isValid(size_t nElements, size_t subsetSize, const size_t *a);
102 static bool isValid(size_t nElements, size_t subsetSize, const int *a);
103
104 // Places the sorted value of [s0..s(p-1),t0..t(q-1)]
105 // (where p=size of s, q = size of t)
106 // into 'result', and returns the sign of the permutation
107 // required. 0 is returned if s and t have a common
108 // element, in which case, the value in 'result'
109 // is undefined.
110 // Note: The size of result is not changed, and it needs to be
111 // s.size() + t.size().
112 static int concatenateSubsets(const Subset &s,
113 const Subset &t,
114 Subset &result);
115
116 private:
117 size_t binom(size_t n, size_t p)
118 {
119 assert(n <= mNumElements);
120 assert(p <= mMaxSubsetSize);
121 return mTable[p][n];
122 }
123
124 size_t **mTable;
125 size_t mNumElements; // number of elements in the set: the elements are
126 // 0..mNumElements-1
127 size_t mMaxSubsetSize; // This table can only handle subsets up to this size.
128};
129#endif
130
131// Local Variables:
132// compile-command: "make -C $M2BUILDDIR/Macaulay2/e "
133// indent-tabs-mode: nil
134// End:
static bool increment(size_t n, Subset &s)
Definition comb.cpp:124
~Subsets()
Definition comb.cpp:35
size_t mNumElements
Definition comb.hpp:125
bool isValid(const Subset &a)
Definition comb.cpp:41
static int concatenateSubsets(const Subset &s, const Subset &t, Subset &result)
Definition comb.cpp:163
Subsets(size_t n, size_t p)
Definition comb.cpp:20
size_t encode(const Subset &a)
Definition comb.cpp:66
void decode(size_t val, Subset &result)
Definition comb.cpp:96
size_t binom(size_t n, size_t p)
Definition comb.hpp:117
size_t ** mTable
Definition comb.hpp:124
size_t encodeBoundary(size_t index, const Subset &a)
Definition comb.cpp:80
static void show(std::ostream &o, const Subset &a)
Definition comb.cpp:203
size_t mMaxSubsetSize
Definition comb.hpp:127
std::vector< size_t > Subset
Definition comb.hpp:58
int p
void size_t s
Definition m2-mem.cpp:271
VALGRIND_MAKE_MEM_DEFINED & result(result)