Skip to content

Commit 9dd19cd

Browse files
authored
Add header file to median.cpp
1 parent 7f1e74a commit 9dd19cd

2 files changed

Lines changed: 138 additions & 108 deletions

File tree

src/median.cpp

Lines changed: 2 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -24,119 +24,13 @@ SOFTWARE.
2424

2525

2626
#include <Rcpp.h>
27-
#include "insertSort.h"
28-
#include "lsd.h"
27+
#include "median.h"
2928
using namespace Rcpp;
3029

3130

32-
/* DONE
33-
* more effecient where the size of data is greater then 1e4
34-
*
35-
*
36-
*/
37-
int p_int_odd (unsigned *l, unsigned *r, int k){
38-
39-
if(r-l<128){
40-
unsigned tmp[r-l];
41-
unsigned *it=tmp;
42-
for(unsigned *i=l;i!=r;i++) *it++=*i;
43-
sort_insert((int*)tmp,r-l);
44-
return tmp[k];
45-
46-
47-
}
48-
if(r-l<0x10000){
49-
unsigned tmp[r-l];
50-
unsigned *it=tmp;
51-
for(unsigned *i=l;i!=r;i++) *it++=*i;
52-
olsd(tmp,tmp+(r-l));
53-
return tmp[k];
54-
55-
56-
}
57-
58-
59-
int count[0x10000]={0};
60-
for(unsigned *i=l;i!=r;i++) count[(*i-INT_MIN) >>16]++;
61-
unsigned f=0;int pos=k+1;
62-
while(count[f]<pos) pos-=count[f++];
63-
64-
for(int i=0;i<0x10000;i++)count[i]=0; // reset count to 0
65-
for(unsigned *i=l;i!=r;i++)
66-
if((*i-INT_MIN)>>16==f)count[(*i-INT_MIN)& 0xffff]++;
67-
unsigned b=0;
68-
while(count[b]<pos) pos-=count[b++];
69-
return ((f<<16)+b)+INT_MIN;
70-
71-
72-
}
7331

74-
double p_int_pair (unsigned *l, unsigned *r, int k){
75-
if(r-l<128){
76-
unsigned tmp[r-l];
77-
unsigned *it=tmp;
78-
for(unsigned *i=l;i!=r;i++) *it++=*i;
79-
sort_insert((int*)tmp,r-l);
80-
return ((double)tmp[k]+(double)tmp[k-1])/(double) 2;
81-
82-
83-
}
84-
if(r-l<0x10000){
85-
unsigned tmp[r-l];
86-
unsigned *it=tmp;
87-
for(unsigned *i=l;i!=r;i++) *it++=*i;
88-
olsd(tmp,tmp+(r-l));
89-
return ((double)tmp[k]+(double)tmp[k-1])/(double) 2;
90-
91-
92-
}
93-
94-
int count[0x10000]={0};
95-
for(unsigned *i=l;i!=r;i++) count[(*i-INT_MIN) >>16]++;
96-
unsigned f=0;int pos=k;
97-
while(count[f]<pos) pos-=count[f++];
98-
unsigned f1=0; int pos1=k+1;
99-
while(count[f1]<pos1) pos1-=count[f1++];
100-
101-
if(f==f1){
102-
103-
for(int i=0;i<0x10000;i++)count[i]=0; // reset count to 0
104-
for(unsigned *i=l;i!=r;i++)
105-
if((*i-INT_MIN)>>16==f)count[(*i-INT_MIN)& 0xffff]++;
106-
107-
unsigned b=0;
108-
while(count[b]<pos) pos-=count[b++];
109-
unsigned b1=0;
110-
while(count[b1]<pos1) pos1-=count[b1++];
111-
112-
int v1=(f<<16)+b+INT_MIN;
113-
int v2=(f<<16)+b1+INT_MIN;
114-
115-
return ((double)v1+(double)v2)/(double)2; // double convertion is needed to avoid overflow
116-
}
117-
118-
for(int i=0;i<0x10000;i++)count[i]=0; // reset count to 0
119-
for(unsigned *i=l;i!=r;i++)
120-
if((*i-INT_MIN)>>16==f)count[(*i-INT_MIN)& 0xffff]++;
121-
122-
unsigned b=0;
123-
while(count[b]<pos) pos-=count[b++];
124-
125-
for(int i=0;i<0x10000;i++)count[i]=0; // reset count to 0
126-
for(unsigned *i=l;i!=r;i++)
127-
if((*i-INT_MIN)>>16==f1)count[(*i-INT_MIN)& 0xffff]++;
128-
129-
130-
unsigned b1=0;
131-
while(count[b1]<pos1) pos1-=count[b1++];
132-
int v1=(f<<16)+b+INT_MIN;
133-
int v2=(f1<<16)+b1+INT_MIN;
134-
return ((double)v1+(double)v2)/(double)2;
135-
136-
137-
}
13832

139-
// [[Rcpp::export]]
33+
// [[Rcpp::export]]
14034
double imedian (SEXP x){
14135
unsigned int *a=(unsigned int *)INTEGER(x);
14236
int N=LENGTH(x);

src/median.h

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/*
2+
MIT License
3+
4+
Copyright (c) [2019] [Med Ezzeddine Macherki]
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.
23+
*/
24+
25+
26+
#include "insertSort.h"
27+
#include "lsd.h"
28+
29+
30+
/* DONE
31+
* more effecient where the size of data is greater then 1e4
32+
*
33+
*
34+
*/
35+
int p_int_odd (unsigned *l, unsigned *r, int k){
36+
37+
if(r-l<128){
38+
unsigned tmp[r-l];
39+
unsigned *it=tmp;
40+
for(unsigned *i=l;i!=r;i++) *it++=*i;
41+
sort_insert((int*)tmp,r-l);
42+
return tmp[k];
43+
44+
45+
}
46+
if(r-l<0x10000){
47+
unsigned tmp[r-l];
48+
unsigned *it=tmp;
49+
for(unsigned *i=l;i!=r;i++) *it++=*i;
50+
olsd(tmp,tmp+(r-l));
51+
return tmp[k];
52+
53+
54+
}
55+
56+
57+
int count[0x10000]={0};
58+
for(unsigned *i=l;i!=r;i++) count[(*i-INT_MIN) >>16]++;
59+
unsigned f=0;int pos=k+1;
60+
while(count[f]<pos) pos-=count[f++];
61+
62+
for(int i=0;i<0x10000;i++)count[i]=0; // reset count to 0
63+
for(unsigned *i=l;i!=r;i++)
64+
if((*i-INT_MIN)>>16==f)count[(*i-INT_MIN)& 0xffff]++;
65+
unsigned b=0;
66+
while(count[b]<pos) pos-=count[b++];
67+
return ((f<<16)+b)+INT_MIN;
68+
69+
70+
}
71+
72+
double p_int_pair (unsigned *l, unsigned *r, int k){
73+
if(r-l<128){
74+
unsigned tmp[r-l];
75+
unsigned *it=tmp;
76+
for(unsigned *i=l;i!=r;i++) *it++=*i;
77+
sort_insert((int*)tmp,r-l);
78+
return ((double)tmp[k]+(double)tmp[k-1])/(double) 2;
79+
80+
81+
}
82+
if(r-l<0x10000){
83+
unsigned tmp[r-l];
84+
unsigned *it=tmp;
85+
for(unsigned *i=l;i!=r;i++) *it++=*i;
86+
olsd(tmp,tmp+(r-l));
87+
return ((double)tmp[k]+(double)tmp[k-1])/(double) 2;
88+
89+
90+
}
91+
92+
int count[0x10000]={0};
93+
for(unsigned *i=l;i!=r;i++) count[(*i-INT_MIN) >>16]++;
94+
unsigned f=0;int pos=k;
95+
while(count[f]<pos) pos-=count[f++];
96+
unsigned f1=0; int pos1=k+1;
97+
while(count[f1]<pos1) pos1-=count[f1++];
98+
99+
if(f==f1){
100+
101+
for(int i=0;i<0x10000;i++)count[i]=0; // reset count to 0
102+
for(unsigned *i=l;i!=r;i++)
103+
if((*i-INT_MIN)>>16==f)count[(*i-INT_MIN)& 0xffff]++;
104+
105+
unsigned b=0;
106+
while(count[b]<pos) pos-=count[b++];
107+
unsigned b1=0;
108+
while(count[b1]<pos1) pos1-=count[b1++];
109+
110+
int v1=(f<<16)+b+INT_MIN;
111+
int v2=(f<<16)+b1+INT_MIN;
112+
113+
return ((double)v1+(double)v2)/(double)2; // double convertion is needed to avoid overflow
114+
}
115+
116+
for(int i=0;i<0x10000;i++)count[i]=0; // reset count to 0
117+
for(unsigned *i=l;i!=r;i++)
118+
if((*i-INT_MIN)>>16==f)count[(*i-INT_MIN)& 0xffff]++;
119+
120+
unsigned b=0;
121+
while(count[b]<pos) pos-=count[b++];
122+
123+
for(int i=0;i<0x10000;i++)count[i]=0; // reset count to 0
124+
for(unsigned *i=l;i!=r;i++)
125+
if((*i-INT_MIN)>>16==f1)count[(*i-INT_MIN)& 0xffff]++;
126+
127+
128+
unsigned b1=0;
129+
while(count[b1]<pos1) pos1-=count[b1++];
130+
int v1=(f<<16)+b+INT_MIN;
131+
int v2=(f1<<16)+b1+INT_MIN;
132+
return ((double)v1+(double)v2)/(double)2;
133+
134+
135+
}
136+

0 commit comments

Comments
 (0)