[eb793a] | 1 | #include <Singular/libsingular.h> |
---|
| 2 | |
---|
| 3 | #include <vector> |
---|
| 4 | |
---|
| 5 | void subset(std::vector<int> &arr, int size, int left, int index, std::vector<int> &l, std::vector<std::vector<int> > &L) |
---|
| 6 | { |
---|
| 7 | if(left==0) |
---|
| 8 | { |
---|
| 9 | L.push_back(l); |
---|
| 10 | return; |
---|
| 11 | } |
---|
| 12 | |
---|
| 13 | for(int i=index; i<size;i++) |
---|
| 14 | { |
---|
| 15 | l.push_back(arr[i]); |
---|
| 16 | subset(arr,size,left-1,i+1,l,L); |
---|
| 17 | l.pop_back(); |
---|
| 18 | } |
---|
| 19 | } |
---|
| 20 | |
---|
| 21 | BOOLEAN subsets(leftv res, leftv args) |
---|
| 22 | { |
---|
| 23 | leftv u = args; |
---|
| 24 | if ((u!=NULL) && (u->Typ()==INT_CMD)) |
---|
| 25 | { |
---|
| 26 | leftv v = u->next; |
---|
| 27 | if ((v!=NULL) && (v->Typ()==INT_CMD) && (v->next==NULL)) |
---|
| 28 | { |
---|
| 29 | int n = (int)(long) u->Data(); |
---|
| 30 | int k = (int)(long) v->Data(); |
---|
| 31 | std::vector<int> array(n); |
---|
| 32 | for (int i=0; i<n; i++) |
---|
| 33 | array[i]=i+1; |
---|
| 34 | std::vector<int> ltemp; |
---|
| 35 | std::vector<std::vector<int> > lt; |
---|
| 36 | subset(array,n,k,0,ltemp,lt); |
---|
| 37 | |
---|
| 38 | lists Lt = (lists) omAllocBin(slists_bin); |
---|
| 39 | Lt->Init(lt.size()); |
---|
| 40 | for (int i=0; i<lt.size(); i++) |
---|
| 41 | { |
---|
| 42 | std::vector<int> lti = lt[i]; |
---|
| 43 | lists Lti = (lists) omAllocBin(slists_bin); |
---|
| 44 | Lti->Init(k); |
---|
| 45 | for(int j=0; j<lti.size(); j++) |
---|
| 46 | { |
---|
| 47 | Lti->m[j].rtyp = INT_CMD; |
---|
| 48 | Lti->m[j].data = (void*)(long)lti[j]; |
---|
| 49 | } |
---|
| 50 | Lt->m[i].rtyp = LIST_CMD; |
---|
| 51 | Lt->m[i].data = (void*) Lti; |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | res->rtyp = LIST_CMD; |
---|
| 55 | res->data = (void*) Lt; |
---|
| 56 | return FALSE; |
---|
| 57 | } |
---|
| 58 | } |
---|
| 59 | WerrorS("subsets: unexpected parameter"); |
---|
| 60 | return TRUE; |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | //------------------------------------------------------------------------ |
---|
| 64 | // initialisation of the module |
---|
| 65 | extern "C" int SI_MOD_INIT(customstd)(SModulFunctions* p) |
---|
| 66 | { |
---|
| 67 | p->iiAddCproc("general.lib","subsets",FALSE,subsets); |
---|
| 68 | return (MAX_TOK); |
---|
| 69 | } |
---|