runningMax.h 842 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. void maxFilter(double *inpt,double *outp,int N,int b,int a){
  2. int k=1+a+b;
  3. struct deq *J=makeDeq(k);
  4. //Put afters
  5. for(int e=0;e<a+1;e++){
  6. for(;
  7. (J->right) && (inpt[e]>=inpt[*(J->right)]);
  8. popRightDeq(J)
  9. );
  10. pushRightDeq(J,e);
  11. }
  12. for(int e=0;e<N;e++){
  13. outp[e]=inpt[*J->left];
  14. //Update J:
  15. //Remove left elements older than b
  16. for(;
  17. (J->left) && (*(J->left)<=e-b);
  18. popLeftDeq(J)
  19. );
  20. //Remove elements smaller than cur+a
  21. if(e+a+1<N){
  22. for(;
  23. (J->right) && (inpt[*(J->right)]<=inpt[e+a+1]);
  24. popRightDeq(J)
  25. );
  26. //Add current
  27. pushRightDeq(J,e+a+1);
  28. }
  29. }
  30. }
  31. SEXP C_runningMax(SEXP X,SEXP B,SEXP A){
  32. int N=length(X);
  33. int a=INTEGER(A)[0];
  34. int b=INTEGER(B)[0];
  35. SEXP Ans=PROTECT(allocVector(REALSXP,N));
  36. double *x=REAL(X),*ans=REAL(Ans);
  37. maxFilter(x,ans,N,b,a);
  38. UNPROTECT(1);
  39. return(Ans);
  40. }