gagolews / DataStructures

General data structures and algorithms for use with R
5 stars 1 forks source link

stack, queue: add as.list #1

Open gagolews opened 9 years ago

gagolews commented 9 years ago

It'd be lovely if we were able to convert a stack or a queue to an R list object

so we need

as.list.stack and as.list.queue

moreover, queue/list constructor should mark class of retval to queue/stack print.stack, print.queue and maybe format.stack and format.queue should be defined

bartoszukm commented 9 years ago

I wanted to create print.stack, and some draft is:

Function structure("str");

// [[Rcpp::export("print.stack")]]
void stack_print(SEXP stack) {
   XPtr< S > _stack = Rcpp::as< XPtr< S > > (stack);
   int n = (*_stack).size();
   std::deque<SEXP>::iterator it = (*_stack).begin();
   for (int i=0; i < n; ++i)
   {
      structure(*it++);
   }
}

The main problem is how to print a single element, which is very general SEXP. I've found http://stackoverflow.com/questions/23462640/print-part-of-dataframe-to-screen-from-rcpp which suggests using structure.

For this solution, for exemplary code

ms = stack_create() stack_push(ms, 1) stack_push(ms, 2) stack_push(ms, 3) stack_push(ms, "maciek") stack_push(ms, matrix(c(1,2,3,4),nrow = 2)) print.stack(ms)

I obtain a result:

num [1:2, 1:2] 1 2 3 4 chr "maciek" num 3 num 2 num 1

Does it a desirable output?

gagolews commented 9 years ago

You can also create a print.stack function in pure R - all it does it calls print(as.list(stack)), where as.list is a function that converts all the stack's elements into a list

bartoszukm commented 9 years ago

97367dc94fec35cdd6fff53ad9325624e5781298 also is related to this issue