www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - D array, frequently used operations.

reply "Andrew Fedoniouk" <news terrainformatica.com> writes:
Attached file is my attempt to implement standard set of array
operations:

pop
    - remove last element and return it
ins
    - insert element into array
insr
    - insert array into array
cut
    - remove element and return it
cutr
    - remove range and return its content
remove
    - just remove range
index
    -  find index of the element in array

provided template allows to use these fiunctions as methods of array types:

char[] s = "world";
s.ins("hello ",0);
char c = s.pop();

assert( s == "hello worl" && c == 'd' );

Any comments will be gladly appreciated.
E.g. I would like to be able to mixin these definitions
for any arbitrary T[] but no luck so far - see commented
section at the bottom of the file.

Would be also extremely nice if ~ operations will allow
to do something like this:

char c = 'a';
char[] s = "abc";
s = s[0..1] ~ c ~ s[1..s.length];

Now it does not compile. Seems like a bug in DMD.

Andrew Fedoniouk.
http://terrainformatica.com
Mar 20 2005
parent reply "Andrew Fedoniouk" <news terrainformatica.com> writes:
oops, attachment is here 


begin 666 darray.d






M=68[('T-" T*("  ("\O("YL96YG=&  <')O<&5R='D-"B  ("!U:6YT(&QE
M;F=T:" I('L <F5T=7)N('5S960[('T-"B  ("!V;VED(&QE;F=T:"AU:6YT



M("  ("  =6EN="!N97=?86QL;V-A=&5D(#T 8G5F+FQE;F=T:"HR.PT*"2  
M("  (&EF("AN(#X ;F5W7V%L;&]C871E9"D ;F5W7V%L;&]C871E9" ](&X[

M("  ("!U<V5D(#T

M*3L 8G5F6W1O<%T /2!V.PT*("  ('T-" T*("  ('9O:60 <'5S:"A46UT 

M:"AT;W  *R!V82YL96YG=& I.PT*"2  ("!B=69;=&]P+BYT;W  *R!V82YL
M96YG=&A=(#T



M=7-H*'8I.R!R971U<FX[('T-"B  ("  (&QE;F=T:"AU<V5D*S$I.PT*("  
M("  ;65M;6]V92 F8G5F6V%T*S%=+"9B=69;871=+"AU<V5D+6%T*2I4+G-I


M/2!U<V5D*2![('!U<V H=F$I.R!R971U<FX[('T-"B  ("  (&QE;F=T:"AU
M<V5D("L =F$N;&5N9W1H*3L-"B  ("  (&UE;6UO=F4H)F)U9EMA="MV82YL

M=69;870N+F%T*W9A+FQE;F=T:%T /2!V85LP+BYV82YL96YG=&A=.PT*("  
M('T-" T*("  (&)O;VP 96UP='DH*2![(')E='5R;B!U<V5D(#T


M"2  ("!A<W-E<G0H:2 \('5S960I.PT*"2  ("!R971U<FX 8G5F6VE=.PT*
M("  ('T-" T*("  (%0 ;W!);F1E>$%S<VEG;BA4('8L('5I;G0 :2D +R\ 
M;W9E<FQO861S(&%;:5T /2!V.PT*("  ('L-" D ("  87-S97)T*&D /"!U


M(2!D;VXG="!F;W)G970 =&\ 9'5P('1H92!R97-U;'0-"B  (" O+R  ("  
M(&EF('EO=2!N965D('1O('-T;W)E(&ET('-O;65W:&5R90T*("  ("\O("  
M("  ;7EH87-H6R)S<W,B72 ](&%;72YD=7 [( T*("  ('L-"B  ("  (')E

M:6YT(&PL('5I;G0 :"D)+R\ ;W9E<FQO861S(&%;:2 N+B!J70T*("  ('L-
M"B  ("  (&%S<V5R="AL(#P]('5S960I.PT*("  ("  87-S97)T*&  /#T 

M(&%R<F%Y(&]P0V%T07-S:6=N*%0 =BD >R!P=7-H*'8I.R!R971U<FX =&AI
M<SL ?2 O+R!O=F5R;&]A9', 87)R87D ?CT 5" -"B  ("!A<G)A>2!O<$-A
M=$%S<VEG;BA46UT =F$I('L <'5S:"AV82D[(')E='5R;B!T:&ES.R!]("\O

M('8I('L <'5S:"AV*3L <F5T=7)N('1H:7,[('T +R\ ;W9E<FQO861S(&%R
M<F%Y(#T
M<V H=F$I.R!R971U<FX =&AI<SL ?2 O+R!O=F5R;&]A9', 87)R87D /2!A


M?B!A<G)A>2 -"B  ("!A<G)A>2!O<$-A=%]R*%1;72!V82D >R!I;G-E<G0H
M,"QV82D[(')E='5R;B!T:&ES.R!]("\O(&]V97)L;V%D<R!A<G)A>2 ](%1;

`
end
Mar 20 2005
parent "Andrew Fedoniouk" <news terrainformatica.com> writes:
Wrong one :)

Here is what I mean in fact 


begin 666 xarray.d

M;W(Z($%N9')E=R!&961O;FEO=6L 0"!T97)R86EN9F]R;6%T:6-A+F-O;0T*

M(&5L96UE;G0 86YD(')E='5R;B!I= T*("!4('!O<"AI;F]U="!46UT 96QE

M9W1H("T ,3L-"B  ("  ("  87-S97)T*&P /CT ,"D[( T*("  ("  ("!4
M('0 /2!E;&5M96YT<UML73L-"B  ("  ("  96QE;65N=',N;&5N9W1H(#T 
M;#L-
M(&5L96UE;G0 :6YT;R!A<G)A>0T*("!V;VED(&EN<RAI;F]U="!46UT 96QE

M;G0 ;" ](&5L96UE;G1S+FQE;F=T:#L-"B  ("  ("  :68H870 /CT ;"D 
M>R!E;&5M96YT<R!^/2!V.R!R971U<FX[('T-"B  ("  ("  +R\ 9F]R('-O

M(&5L96UE;G1S(#T 96QE;65N='-;,"XN871=('X =B!^(&5L96UE;G1S6V%T
M+BYE;&5M96YT<RYL96YG=&A=.PT*("  ("  (" O+R!S;R!W92!U<V4 0R!W

M("!M96UM;W9E*"9E;&5M96YT<UMA="LQ72PF96QE;65N='-;871=+"AL+6%T


M(&EN<W(H:6YO=70 5%M=(&5L96UE;G1S+"!46UT =F$L('5I;G0 870 /2 P
M*0T*("  ("  >PT*("  ("  ("!U:6YT(&P /2!E;&5M96YT<RYL96YG=& [

M;CL ?0T*("  ("  ("!E;&5M96YT<RYL96YG=&  /2!L("L =F$N;&5N9W1H
M.PT*("  ("  ("!M96UM;W9E*"9E;&5M96YT<UMA=" K('9A+FQE;F=T:%TL
M)F5L96UE;G1S6V%T72PH;"UA="DJ5"YS:7IE;V8I.PT*("  ("  ("!E;&5M

M(')E;6]V92!E;&5M96YT(&%N9"!R971U<FX :70-"B  5"!C=70H:6YO=70 
M5%M=(&5L96UE;G1S+"!U:6YT(&%T*0T*("  ("  >PT*("  ("  ("!U:6YT
M(&P /2!E;&5M96YT<RYL96YG=&  +2 Q.PT*("  ("  ("!I9BAA=" ^/2!L
M*2!R971U<FX <&]P*&5L96UE;G1S*3L-"B  ("  ("  5"!T(#T 96QE;65N
M='-;871=.R -"B  ("  ("  ;65M;6]V92 F96QE;65N='-;871=+"9E;&5M
M96YT<UMA="LQ72PH;"UA="DJ5"YS:7IE;V8I.PT*("  ("  ("!E;&5M96YT
M<RYL96YG=&  /2!L.PT*("  ("  ("!R971U<FX =#L-"B  ("  ('T-" T*
M(" O+R!R96UO=F4 <F%N9V4 86YD(')E='5R;B!I=', 8V]N=&5N= T*("!4
M6UT 8W5T<BAI;F]U="!46UT 96QE;65N=',L('5I;G0 870L('5I;G0 96YD
M*0T*("  ("  >PT*("  ("  ("  (&%S<V5R="AA=" \/2!E;F0I.PT*("  
M("  ("  (&EF*&%T(#T
M5%M=('0 /2!E;&5M96YT<UMA="XN96YD72YD=7 [( T*("  ("  ("  ('5I

M("  :68H870 /"!L*2![(&5L96UE;G1S+FQE;F=T:" ](&%T.R!R971U<FX 
M=#L ?0T*("  ("  ("  (&UE;6UO=F4H)F5L96UE;G1S6V%T72PF96QE;65N
M='-;96YD72PH;"UA="DJ5"YS:7IE;V8I.PT*("  ("  ("  (&5L96UE;G1S


M72!E;&5M96YT<RP :6YT(&%T+"!I;G0 96YD*0T*("  ("  >PT*("  ("  
M("  (&%S<V5R="AA=" \/2!E;F0I.PT*("  ("  ("  (&EF*&%T(#T](&5N
M9"D <F5T=7)N.PT*("  ("  ("  ('5I;G0 ;" ](&5L96UE;G1S+FQE;F=T

M96YT<RYL96YG=&  /2!A=#L
M92 F96QE;65N='-;871=+"9E;&5M96YT<UME;F1=+"AL+6%T*2I4+G-I>F5O

M" T*(" O+R!F:6YD(&EN9&5X(&]F('1H92!E;&5M96YT(&EN(&%R<F%Y+" M
M,2!I9B!N;W0 9F]U;F0-"B  :6YT(&EN9&5X*&EN(%1;72!E;&5M96YT<RQ4

M+2 Q.PT*("  ("  (&9O<B [(&D /CT ,#L +2UI*0T*("  ("  ("  (&EF
M*"!E;&5M96YT<UMI72 ]/3T =" I(&)R96%K.PT*("  ("  (')E='5R;B!I


M;&EA<R!X87)R87E4(2AS:&]R="DN<&]P("  ('!O<#L-"F%L:6%S('AA<G)A
M>50A*'5S:&]R="DN<&]P("  <&]P.PT*86QI87, >&%R<F%Y5"$H:6YT*2YP

M<#L-"F%L:6%S('AA<G)A>50A*&QO;F<I+G!O<"  ("  <&]P.PT*86QI87, 

M;&]A="DN<&]P("  ('!O<#L-"F%L:6%S('AA<G)A>50A*&1O=6)L92DN<&]P

M;&EA<R!X87)R87E4(2AI9FQO870I+G!O<"  ('!O<#L-"F%L:6%S('AA<G)A
M>50A*&ED;W5B;&4I+G!O<"  <&]P.PT*86QI87, >&%R<F%Y5"$H:7)E86PI

M<#L-"F%L:6%S('AA<G)A>50A*&-D;W5B;&4I+G!O<"  <&]P.PT*86QI87, 

M:&%R*2YP;W  ("  ('!O<#L-"F%L:6%S('AA<G)A>50A*'=C:&%R*2YP;W  

M"F%L:6%S('AA<G)A>50A*&)Y=&4I+FEN<R  ("  :6YS.PT*86QI87, >&%R

M="DN:6YS("  (&EN<SL-"F%L:6%S('AA<G)A>50A*'5S:&]R="DN:6YS("  

M<R!X87)R87E4(2AU:6YT*2YI;G, ("  (&EN<SL-"F%L:6%S('AA<G)A>50A
M*&QO;F<I+FEN<R  ("  :6YS.PT*86QI87, >&%R<F%Y5"$H=6QO;F<I+FEN

M"F%L:6%S('AA<G)A>50A*&1O=6)L92DN:6YS("  :6YS.PT*86QI87, >&%R

M870I+FEN<R  (&EN<SL-"F%L:6%S('AA<G)A>50A*&ED;W5B;&4I+FEN<R  

M<R!X87)R87E4(2AC9FQO870I+FEN<R  (&EN<SL-"F%L:6%S('AA<G)A>50A
M*&-D;W5B;&4I+FEN<R  :6YS.PT*86QI87, >&%R<F%Y5"$H8W)E86PI+FEN

M"F%L:6%S('AA<G)A>50A*'=C:&%R*2YI;G, ("  :6YS.PT*86QI87, >&%R


M<B  ("!I;G-R.PT*86QI87, >&%R<F%Y5"$H<VAO<G0I+FEN<W( ("  :6YS

M<R!X87)R87E4(2AI;G0I+FEN<W( ("  ("!I;G-R.PT*86QI87, >&%R<F%Y
M5"$H=6EN="DN:6YS<B  ("  :6YS<CL-"F%L:6%S('AA<G)A>50A*&QO;F<I

M("!I;G-R.PT*86QI87, >&%R<F%Y5"$H9FQO870I+FEN<W( ("  :6YS<CL-

M87)R87E4(2AR96%L*2YI;G-R("  ("!I;G-R.PT*86QI87, >&%R<F%Y5"$H
M:69L;V%T*2YI;G-R("  :6YS<CL-"F%L:6%S('AA<G)A>50A*&ED;W5B;&4I

M;G-R.PT*86QI87, >&%R<F%Y5"$H8V9L;V%T*2YI;G-R("  :6YS<CL-"F%L

M87E4(2AC<F5A;"DN:6YS<B  ("!I;G-R.PT*86QI87, >&%R<F%Y5"$H8VAA
M<BDN:6YS<B  ("  :6YS<CL-"F%L:6%S('AA<G)A>50A*'=C:&%R*2YI;G-R


M('AA<G)A>50A*'5B>71E*2YC=70 ("  8W5T.PT*86QI87, >&%R<F%Y5"$H

M="  (&-U=#L-"F%L:6%S('AA<G)A>50A*&EN="DN8W5T("  ("  8W5T.PT*

M87E4(2AL;VYG*2YC=70 ("  (&-U=#L-"F%L:6%S('AA<G)A>50A*'5L;VYG
M*2YC=70 ("  8W5T.PT*86QI87, >&%R<F%Y5"$H9FQO870I+F-U="  ("!C

M('AA<G)A>50A*')E86PI+F-U="  ("  8W5T.PT*86QI87, >&%R<F%Y5"$H

M=70 (&-U=#L-"F%L:6%S('AA<G)A>50A*&ER96%L*2YC=70 ("  8W5T.PT*

M87E4(2AC9&]U8FQE*2YC=70 (&-U=#L-"F%L:6%S('AA<G)A>50A*&-R96%L
M*2YC=70 ("  8W5T.PT*86QI87, >&%R<F%Y5"$H8VAA<BDN8W5T("  ("!C


M(2AB>71E*2YC=71R("  ("!C=71R.PT*86QI87, >&%R<F%Y5"$H=6)Y=&4I
M+F-U='( ("  8W5T<CL-"F%L:6%S('AA<G)A>50A*'-H;W)T*2YC=71R("  

M86QI87, >&%R<F%Y5"$H:6YT*2YC=71R("  ("  8W5T<CL-"F%L:6%S('AA

M;VYG*2YC=71R("  ("!C=71R.PT*86QI87, >&%R<F%Y5"$H=6QO;F<I+F-U
M='( ("  8W5T<CL-"F%L:6%S('AA<G)A>50A*&9L;V%T*2YC=71R("  (&-U

M87, >&%R<F%Y5"$H<F5A;"DN8W5T<B  ("  8W5T<CL-"F%L:6%S('AA<G)A

M8FQE*2YC=71R("!C=71R.PT*86QI87, >&%R<F%Y5"$H:7)E86PI+F-U='( 
M("  8W5T<CL-"F%L:6%S('AA<G)A>50A*&-F;&]A="DN8W5T<B  (&-U='([

M>&%R<F%Y5"$H8W)E86PI+F-U='( ("  8W5T<CL-"F%L:6%S('AA<G)A>50A

M8W5T<B  ("!C=71R.PT*86QI87, >&%R<F%Y5"$H9&-H87(I+F-U='( ("  
M8W5T<CL-" T*86QI87, >&%R<F%Y5"$H8GET92DN<F5M;W9E("  ("!R96UO

M"F%L:6%S('AA<G)A>50A*'-H;W)T*2YR96UO=F4 ("  <F5M;W9E.PT*86QI

M87)R87E4(2AI;G0I+G)E;6]V92  ("  (')E;6]V93L-"F%L:6%S('AA<G)A
M>50A*'5I;G0I+G)E;6]V92  ("  <F5M;W9E.PT*86QI87, >&%R<F%Y5"$H

M9RDN<F5M;W9E("  (')E;6]V93L-"F%L:6%S('AA<G)A>50A*&9L;V%T*2YR
M96UO=F4 ("  <F5M;W9E.PT*86QI87, >&%R<F%Y5"$H9&]U8FQE*2YR96UO

M(')E;6]V93L-"F%L:6%S('AA<G)A>50A*&EF;&]A="DN<F5M;W9E("  <F5M
M;W9E.PT*86QI87, >&%R<F%Y5"$H:61O=6)L92DN<F5M;W9E("!R96UO=F4[

M:6%S('AA<G)A>50A*&-F;&]A="DN<F5M;W9E("  <F5M;W9E.PT*86QI87, 

M87E4(2AC<F5A;"DN<F5M;W9E("  (')E;6]V93L-"F%L:6%S('AA<G)A>50A
M*&-H87(I+G)E;6]V92  ("  <F5M;W9E.PT*86QI87, >&%R<F%Y5"$H=V-H

M<F5M;W9E("  (')E;6]V93L-" T*86QI87, >&%R<F%Y5"$H8GET92DN:6YD
M97  ("  (&EN9&5X.PT*86QI87, >&%R<F%Y5"$H=6)Y=&4I+FEN9&5X("  
M(&EN9&5X.PT*86QI87, >&%R<F%Y5"$H<VAO<G0I+FEN9&5X("  (&EN9&5X
M.PT*86QI87, >&%R<F%Y5"$H=7-H;W)T*2YI;F1E>"  (&EN9&5X.PT*86QI
M87, >&%R<F%Y5"$H:6YT*2YI;F1E>"  ("  (&EN9&5X.PT*86QI87, >&%R
M<F%Y5"$H=6EN="DN:6YD97  ("  (&EN9&5X.PT*86QI87, >&%R<F%Y5"$H
M;&]N9RDN:6YD97  ("  (&EN9&5X.PT*86QI87, >&%R<F%Y5"$H=6QO;F<I
M+FEN9&5X("  (&EN9&5X.PT*86QI87, >&%R<F%Y5"$H9FQO870I+FEN9&5X
M("  (&EN9&5X.PT*86QI87, >&%R<F%Y5"$H9&]U8FQE*2YI;F1E>"  (&EN
M9&5X.PT*86QI87, >&%R<F%Y5"$H<F5A;"DN:6YD97  ("  (&EN9&5X.PT*
M86QI87, >&%R<F%Y5"$H:69L;V%T*2YI;F1E>"  (&EN9&5X.PT*86QI87, 
M>&%R<F%Y5"$H:61O=6)L92DN:6YD97  (&EN9&5X.PT*86QI87, >&%R<F%Y
M5"$H:7)E86PI+FEN9&5X("  (&EN9&5X.PT*86QI87, >&%R<F%Y5"$H8V9L
M;V%T*2YI;F1E>"  (&EN9&5X.PT*86QI87, >&%R<F%Y5"$H8V1O=6)L92DN
M:6YD97  (&EN9&5X.PT*86QI87, >&%R<F%Y5"$H8W)E86PI+FEN9&5X("  
M(&EN9&5X.PT*86QI87, >&%R<F%Y5"$H8VAA<BDN:6YD97  ("  (&EN9&5X
M.PT*86QI87, >&%R<F%Y5"$H=V-H87(I+FEN9&5X("  (&EN9&5X.PT*86QI


M:6%S('AA<G)A>50A*%0I+G!O<"  ("  <&]P.PT*("!A;&EA<R!X87)R87E4
M(2A4*2YI;G, ("  (&EN<SL-"B  86QI87, >&%R<F%Y5"$H5"DN:6YS<B  
M("!I;G-R.PT*("!A;&EA<R!X87)R87E4(2A4*2YC=70 ("  (&-U=#L-"B  
M86QI87, >&%R<F%Y5"$H5"DN8W5T<B  ("!C=71R.PT*("!A;&EA<R!X87)R
M87E4(2A4*2YR96UO=F4 (')E;6]V93L-"B  86QI87, >&%R<F%Y5"$H5"DN
M:6YD97  ("!I;F1E>#L-"GT-"FUI>&EN('AA(2AI;G0I.PT*;6EX:6X >&$A
M*&-H87(I.PT*;6EX:6X >&$A*'=C:&%R*3L-"BXN+BX-"F)U="!$340N97AE
?(')E<&]R=', ;F%M92!C;VYF;&EC=',-" T**B\-" ``
`
end
Mar 20 2005