1
#include <Rcpp.h>
2
using namespace Rcpp;
3

4
// [[Rcpp::export]]
5 2
std::vector<std::vector<int> > listingIdeals(std::vector<std::vector<int> > P, int nElem, int nIdeals) {
6 2
  std::vector<std::vector<int> > Ideals(nIdeals);
7 2
  std::vector< std::vector<int> > S(nElem+1);
8 2
  std::vector<int> S2;
9 2
  std::vector<std::vector<int> > Pwork=P;
10 2
  std::vector<std::vector<int> > Ptmp=Pwork;
11 2
  std::vector<int> I(nElem) ; 
12

13 2
  int ifound=0;
14
  /*Full Ideal*/
15 2
  std::iota (std::begin(I), std::end(I), 0);
16 2
  Ideals[0]=I;
17
  
18
  /* S(i)=max(P)*/  
19 2
  int i=0;
20 2
  for(int k=nElem-1; k>=0; --k){
21 2
    if(P[k].size()==0){
22 2
      S[0].push_back(k);  
23
    }
24
  }
25 2
  std::list<int> Code;
26 2
  while(i>=0){
27 2
    Rcpp::checkUserInterrupt();
28 2
    if(S[i].size()!=0){
29 2
      int x = S[i][0];
30 2
      Code.push_back(x);
31

32 2
      std::vector<int>::iterator position = std::find(I.begin(), I.end(), x);
33 2
      I.erase(position);
34 2
      S[i].erase(S[i].begin());
35 2
      ifound+=1;
36

37 2
      std::sort(I.begin(), I.end());
38 2
      Ideals.at(ifound)=I;
39

40
      /*Remove x from P*/
41 2
      Ptmp=Pwork;
42 2
      for(int k=0;k<nElem;k++){
43 2
        std::vector<int>::iterator position = std::find(Ptmp[k].begin(), Ptmp[k].end(), x);
44 2
        if (position != Ptmp[k].end())
45 2
          Ptmp[k].erase(position);
46
      }
47
      
48 2
      S2.clear();
49 2
      for(int k=nElem-1; k>=0; --k){
50 2
        if((Ptmp[k].size()==0) & (Pwork[k].size()!=0)){
51 2
          S2.push_back(k);  
52
        }
53
      }
54 2
      Pwork=Ptmp;
55 2
      S[i+1].clear();
56 2
      std::merge(S[i].begin(), S[i].end(), S2.begin(), S2.end(), std::back_inserter(S[i+1]), std::greater<int>());
57 2
      i+=1;
58
    }
59
    else{
60 2
      if(Code.size()!=0){
61 2
        int x=Code.back();
62 2
        Code.pop_back();
63 2
        I.push_back(x);
64
        
65
        /*Insert x from P*/
66 2
        for(int k=0;k<nElem;k++){
67 2
          if(P[k].size()>0){
68 2
            std::vector<int>::iterator position = std::find(P[k].begin(), P[k].end(), x);
69 2
            if (position != P[k].end())
70 2
              Pwork[k].insert(Pwork[k].begin() + (position - P[k].begin()),x);
71
          }
72
        }
73
      }
74 2
      i-=1;
75
      
76
    }
77
  }
78 2
  return Ideals;
79
}
80

81

Read our documentation on viewing source code .

Loading