⚡ change parameter list of
This commit is contained in:
parent
91b3074ce9
commit
6a6eab8db5
10
stl/sort.nas
10
stl/sort.nas
|
@ -5,7 +5,7 @@
|
|||
# only using < or > may cause infinite loop or the program may crash
|
||||
var sort=func(){
|
||||
srand(); # be aware! this causes global changes
|
||||
return func(vec,left,right,cmp=func(a,b){return a<=b;}){
|
||||
var quick_sort_core=func(vec,left,right,cmp){
|
||||
if(left>=right) return nil;
|
||||
var base=left+int(rand()*(right-left));
|
||||
(vec[left],vec[base])=(vec[base],vec[left]);
|
||||
|
@ -19,8 +19,12 @@ var sort=func(){
|
|||
vec[j]=vec[i];
|
||||
}
|
||||
vec[i]=tmp;
|
||||
sort(vec,left,i-1,cmp);
|
||||
sort(vec,i+1,right,cmp);
|
||||
quick_sort_core(vec,left,i-1,cmp);
|
||||
quick_sort_core(vec,i+1,right,cmp);
|
||||
return nil;
|
||||
}
|
||||
return func(vec,cmp=func(a,b){return a<=b;}){
|
||||
quick_sort_core(vec,0,size(vec)-1,cmp);
|
||||
return nil;
|
||||
}
|
||||
}();
|
|
@ -12,11 +12,11 @@ var fib=func(){
|
|||
}
|
||||
return;
|
||||
}
|
||||
# different coroutines don't share the same local scope
|
||||
var co=[coroutine.create(fib),coroutine.create(fib)];
|
||||
for(var i=0;i<45;i+=1){
|
||||
var res=[coroutine.resume(co[0]),coroutine.resume(co[1])];
|
||||
println('co[0]: ',res[0]==nil?nil:res[0][0],'\nco[1]: ',res[1]==nil?nil:res[1][0]);
|
||||
if(res[0]==nil or res[1]==nil or res[0][0]!=res[1][0])
|
||||
die("different coroutines don't share the same local scope");
|
||||
}
|
||||
|
||||
# test if coroutine can get upvalues
|
||||
|
@ -28,8 +28,11 @@ func(){
|
|||
x+=1;
|
||||
}
|
||||
});
|
||||
for(var i=0;i<16;i+=1)
|
||||
println(coroutine.resume(co));
|
||||
for(var i=0;i<16;i+=1){
|
||||
var res=coroutine.resume(co);
|
||||
if(res==nil or res[0]!=x or res[1]!=i)
|
||||
die("coroutine should have the ability to get upvalues");
|
||||
}
|
||||
}();
|
||||
|
||||
# pressure test
|
||||
|
@ -37,7 +40,7 @@ var productor=func(){
|
|||
for(var i=0;;i+=1)
|
||||
coroutine.yield(i);
|
||||
}
|
||||
var total=4000; # ms
|
||||
var total=1000; # ms
|
||||
var co=coroutine.create(productor);
|
||||
var tm=maketimestamp();
|
||||
|
||||
|
@ -48,7 +51,7 @@ var consumer=func(){
|
|||
for(var i=0;i<5;i+=1)
|
||||
coroutine.resume(co);
|
||||
var rate=(tm.elapsedMSec()+1)/total;
|
||||
print(bar.bar(rate)," ",rate*100,"% \r");
|
||||
print(bar.bar(rate)," ",rate*100,"% \r");
|
||||
}
|
||||
|
||||
tm.stamp();
|
||||
|
|
|
@ -1,22 +1,7 @@
|
|||
var sort=func(vec,left,right){
|
||||
if(left>=right) return;
|
||||
var (L,R,tmp)=(left,right,vec[left]);
|
||||
while(left<right){
|
||||
while(left<right and tmp<=vec[right])
|
||||
right-=1;
|
||||
while(left<right and tmp>=vec[left])
|
||||
left+=1;
|
||||
if(left!=right)
|
||||
(vec[left],vec[right])=(vec[right],vec[left]);
|
||||
}
|
||||
(vec[L],vec[left])=(vec[left],tmp);
|
||||
sort(vec,L,left-1);
|
||||
sort(vec,left+1,R);
|
||||
return;
|
||||
}
|
||||
var vec=[];
|
||||
rand(time(0));
|
||||
for(var i=0;i<1e4;i+=1)
|
||||
append(vec,int(rand()*1e5));
|
||||
sort(vec,0,size(vec)-1);
|
||||
import.stl.sort;
|
||||
var vec=[];
|
||||
rand(time(0));
|
||||
for(var i=0;i<1e4;i+=1)
|
||||
append(vec,int(rand()*1e5));
|
||||
sort(vec);
|
||||
println(vec);
|
|
@ -15,7 +15,7 @@ foreach(var f;argv){
|
|||
}
|
||||
|
||||
if(!all_exists){
|
||||
exit(-1);;
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
var to_lower=func(c){
|
||||
|
@ -45,5 +45,5 @@ for(var i=0;i<len;i+=1){
|
|||
}
|
||||
|
||||
var vec=keys(token);
|
||||
sort(vec,0,size(vec)-1,func(a,b){return cmp(a,b)<=0;});
|
||||
sort(vec,func(a,b){return cmp(a,b)<=0;});
|
||||
println(vec);
|
Loading…
Reference in New Issue