Skip to content

Commit 718f520

Browse files
committed
add cmp() for arrays, remove lexcmp
rename string.jl:depth to strdepth edit list of exports
1 parent aa029e3 commit 718f520

File tree

4 files changed

+27
-27
lines changed

4 files changed

+27
-27
lines changed

base/abstractarray.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -754,17 +754,19 @@ function isequal(A::AbstractArray, B::AbstractArray)
754754
return true
755755
end
756756

757-
function isless(A::AbstractArray, B::AbstractArray)
757+
function cmp(A::AbstractArray, B::AbstractArray)
758758
nA, nB = numel(A), numel(B)
759759
for i = 1:min(nA, nB)
760760
a, b = A[i], B[i]
761761
if !isequal(a, b)
762-
return isless(a, b)
762+
return isless(a, b) ? -1 : +1
763763
end
764764
end
765-
return nA < nB
765+
return cmp(nA, nB)
766766
end
767767

768+
isless(A::AbstractArray, B::AbstractArray) = cmp(A,B)<0
769+
768770
function (==)(A::AbstractArray, B::AbstractArray)
769771
if size(A) != size(B)
770772
return false

base/array.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,6 +1001,13 @@ for (f,scalarf) in ((:(.==),:(==)), (:.<, :<), (:.!=,:!=), (:.<=,:<=))
10011001
end
10021002
end
10031003

1004+
# use memcmp for cmp on byte arrays
1005+
function cmp(a::Array{Uint8,1}, b::Array{Uint8,1})
1006+
c = ccall(:memcmp, Int32, (Ptr{Uint8}, Ptr{Uint8}, Uint),
1007+
a, b, min(length(a),length(b)))
1008+
c < 0 ? -1 : c > 0 ? +1 : cmp(length(a),length(b))
1009+
end
1010+
10041011
## data movement ##
10051012

10061013
function slicedim(A::Array, d::Integer, i::Integer)

base/string.jl

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ ends_with(a::String, c::Char) = length(a) > 0 && a[thisind(a,end)] == c
278278

279279
# faster comparisons for byte strings
280280

281-
cmp(a::ByteString, b::ByteString) = lexcmp(a.data, b.data)
281+
cmp(a::ByteString, b::ByteString) = cmp(a.data, b.data)
282282
isequal(a::ByteString, b::ByteString) = length(a)==length(b) && cmp(a,b)==0
283283

284284
# TODO: fast begins_with and ends_with
@@ -402,17 +402,17 @@ type RopeString <: String
402402
length::Int
403403

404404
RopeString(h::RopeString, t::RopeString) =
405-
depth(h.tail) + depth(t) < depth(h.head) ?
405+
strdepth(h.tail) + strdepth(t) < strdepth(h.head) ?
406406
RopeString(h.head, RopeString(h.tail, t)) :
407407
new(h, t, max(h.depth,t.depth)+1, length(h)+length(t))
408408

409409
RopeString(h::RopeString, t::String) =
410-
depth(h.tail) < depth(h.head) ?
410+
strdepth(h.tail) < strdepth(h.head) ?
411411
RopeString(h.head, RopeString(h.tail, t)) :
412412
new(h, t, h.depth+1, length(h)+length(t))
413413

414414
RopeString(h::String, t::RopeString) =
415-
depth(t.head) < depth(t.tail) ?
415+
strdepth(t.head) < strdepth(t.tail) ?
416416
RopeString(RopeString(h, t.head), t.tail) :
417417
new(h, t, t.depth+1, length(h)+length(t))
418418

@@ -421,8 +421,8 @@ type RopeString <: String
421421
end
422422
RopeString(s::String) = RopeString(s,"")
423423

424-
depth(s::String) = 0
425-
depth(s::RopeString) = s.depth
424+
strdepth(s::String) = 0
425+
strdepth(s::RopeString) = s.depth
426426

427427
function next(s::RopeString, i::Int)
428428
if i <= length(s.head)
@@ -1147,14 +1147,6 @@ for conv in (:float, :float32, :float64,
11471147
@eval ($conv){S<:String}(a::AbstractArray{S}) = map($conv, a)
11481148
end
11491149

1150-
# lexicographically compare byte arrays (used by Latin-1 and UTF-8)
1151-
1152-
function lexcmp(a::Array{Uint8,1}, b::Array{Uint8,1})
1153-
c = ccall(:memcmp, Int32, (Ptr{Uint8}, Ptr{Uint8}, Uint),
1154-
a, b, min(length(a),length(b)))
1155-
c < 0 ? -1 : c > 0 ? +1 : cmp(length(a),length(b))
1156-
end
1157-
11581150
# find the index of the first occurrence of a value in a byte array
11591151

11601152
function memchr(a::Array{Uint8,1}, b::Integer, i::Integer)

base/sysimg.jl

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ export
4848
At_mul_B,At_mul_Bt,At_rdiv_B,At_rdiv_Bt,
4949
# Functions
5050
_c_free,abs,abs2,acos,acosd,acosh,acot,acotd,acoth,acsc,acscd,acsch,add,
51-
add_cmds,add_each,add_fd_handler,add_ports,add_weak_key,add_weak_value,
52-
addprocs_local,addprocs_sge,addprocs_ssh,alignment,all,allp,
51+
add_each,add_fd_handler,add_weak_key,add_weak_value,
52+
addprocs_local,addprocs_sge,addprocs_ssh,all,allp,
5353
amap,and!,angle,ans,any,anyp,append,append!,apropos,areduce,
5454
ascii,asec,asecd,asech,asin,asind,asinh,assert,assign,at_each,atan,atan2,
5555
atand,atanh,basename,begins_with,betarnd,bfft,bfftn,bin,binomial,bitmix,bits,bool,
@@ -64,12 +64,11 @@ export
6464
cov,cov_pearson,cov_spearman,cross,csc,cscd,csch,cstring,csvread,csvwrite,
6565
ctranspose,cumprod,cumsum,current_task,cwd,darray,dcell,dec,decile,deconv,
6666
defaultdist,degrees2radians,del,del_all,del_each,del_fd_handler,
67-
den,depth,deserialize,det,dfill,diag,diagm,diagmm,diagmm!,dict,diff,
67+
den,deserialize,det,dfill,diag,diagm,diagmm,diagmm!,dict,diff,
6868
dist,distdim,distribute,div,dlmread,dlmwrite,dlopen,dlsym,done,dones,dot,
6969
drand,drandn,dump,dup2,dzeros,each_col,each_col!,each_line,each_match,
7070
each_row,each_row!,each_search,each_vec,each_vec!,
71-
eatwspace,eatwspace_comment,
72-
edit,eig,elements,eltype,
71+
eatwspace,eatwspace_comment,edit,eig,elements,eltype,
7372
ends_with,enq_work,enqueue,enumerate,eof,eps,erf,erfc,errno,error,
7473
esc,escape_string,exec,exit,exp,exp2,expand,expm1,expr,exprnd,eye,
7574
factor,factorial,falses,fd,fdio,fetch,fft,fft2,fft3,fft_num_threads,
@@ -78,10 +77,10 @@ export
7877
find,find_in_path,findmax,findmin,findn,findn_nzs,finfer,first,
7978
first_utf8_byte,fld,flipdim,fliplr,flipsign,flipud,float,float32,
8079
float32_isvalid,float64,float64_isvalid,float64_valued,floor,flush,
81-
force,foreach,fork,fpart,fprintf,frexp,full,fullfile,function_loc,gamma,gc,
80+
force,fork,fpart,fprintf,frexp,full,fullfile,function_loc,gamma,gc,
8281
gc_disable,gc_enable,gcd,gcdx,gen_cartesian_map,gensym,get,getenv,
8382
gethostname,getipaddr,getmethods,getpid,gradient,
84-
grow,has,hasenv,hash,hcat,help,help_for,hex,hex2num,hist,histc,
83+
grow,has,hasenv,hash,hcat,help,hex,hex2num,hist,histc,
8584
htol,hton,hvcat,hypot,iceil,identity,ifft,ifft2,ifft3,ifftn,ifftshift,
8685
ifloor,ignorestatus,ilogb,imag,in,include_string,ind2chr,ind2sub,inf,
8786
insert,int,int128,int16,int2str,int32,int64,int8,integer,integer_partitions,
@@ -95,7 +94,7 @@ export
9594
istaskdone,istril,istriu,isvalid,iswalnum,iswalpha,iswascii,iswblank,
9695
iswcntrl,iswdigit,iswgraph,iswlower,iswprint,iswpunct,iswspace,iswupper,
9796
iswxdigit,itrunc,join,key,keys,kron,last,lc,lcfirst,lcm,ldexp,leading_ones,
98-
leading_zeros,length,less,lexcmp,lfact,lgamma,linreg,linspace,load,localize,
97+
leading_zeros,length,less,lfact,lgamma,linreg,linspace,load,localize,
9998
localize_copy,locate,log,log10,log1p,log2,logb,logspace,lowercase,lpad,ls,
10099
lstrip,ltoh,lu,lu!,mad,make_pipe,make_scheduled,map,map_to,map_to2,
101100
map_vectorized,mapreduce,match,matches,matmul2x2,matmul3x3,max,maxdim,
@@ -108,7 +107,7 @@ export
108107
other,out,output,owner,pairs,parse,parse_bin,parse_float,parse_hex,
109108
parse_input_line,parse_int,parse_oct,parseatom,partitions,pascal,
110109
peakflops,permute,pfor,pieceindex,pieceindexes,pipeline_error,pmap,
111-
pmap_static,pointer,pointer_to_array,pop,position,pow,power_by_squaring,
110+
pointer,pointer_to_array,pop,position,pow,power_by_squaring,
112111
powermod,preduce,prevfloat,prevind,print,print_escaped,print_joined,
113112
print_matrix,print_quoted,print_quoted_literal,print_shortest,
114113
print_unescaped,print_unescaped_chars,printf,println,process_exit_status,
@@ -138,7 +137,7 @@ export
138137
tril,triu,trues,trunc,truncate,tty_cols,tty_rows,typemax,typemin,uc,ucfirst,
139138
uint,uint128,uint16,uint32,uint64,uint8,
140139
unescape_chars,unescape_string,union,
141-
union!,unique_name,unsetenv,unshift,unsigned,uppercase,utf8,values,var,vcat,
140+
union!,unsetenv,unshift,unsigned,uppercase,utf8,values,var,vcat,
142141
vec,wait,wait_nohang,weighted_mean,which,whicht,whos,with_output_to_string,
143142
write,write_to,xcorr,xor!,yield,zero,zeros,zip, nextprod,
144143
prevprod, base, findfirst, qrp, sdd, require, ref_shape,

0 commit comments

Comments
 (0)