a580e12000-09-27Fredrik Hübinette (Hubbe) #pike __REAL_VERSION__
a20af62000-09-26Fredrik Hübinette (Hubbe) 
d3ffb22002-03-20Martin Nilsson #pragma strict_types
088e2e1998-02-12Mirar (Pontus Hagland) constant diff = __builtin.diff; constant diff_longest_sequence = __builtin.diff_longest_sequence; constant diff_compare_table = __builtin.diff_compare_table;
5bb99c1998-02-12Henrik Grubbström (Grubba) constant longest_ordered_sequence = __builtin.longest_ordered_sequence;
a7759e1998-11-17Henrik Grubbström (Grubba) constant interleave_array = __builtin.interleave_array;
088e2e1998-02-12Mirar (Pontus Hagland)  constant sort = __builtin.sort;
f7aff61998-04-14Henrik Wallin constant everynth = __builtin.everynth; constant splice = __builtin.splice; constant transpose = __builtin.transpose;
652d922000-04-19David Hedbor constant uniq = __builtin.uniq_array;
55683e2000-08-28Per Hedbor 
5a36971999-07-27Mirar (Pontus Hagland) constant filter=predef::filter; constant map=predef::map;
55683e2000-08-28Per Hedbor constant permute = __builtin.permute;
3665092000-12-15Martin Nilsson constant enumerate = predef::enumerate;
c2a4061997-02-06Fredrik Hübinette (Hubbe) 
5d66082000-12-13Henrik Grubbström (Grubba) //! @[reduce()] sends the first two elements in @[arr] to @[fun], //! then the result and the next element in @[arr] to @[fun] and
edcae22001-07-30Johan Sundström //! so on. Then it returns the result. The function will return
5d66082000-12-13Henrik Grubbström (Grubba) //! @[zero] if @[arr] is the empty array. If @[arr] has //! only one element, that element will be returned. //! //! @seealso
56cd002001-10-28Martin Nilsson //! @[rreduce()]
5d66082000-12-13Henrik Grubbström (Grubba) //!
be192f1999-07-26Marcus Comstedt mixed reduce(function fun, array arr, mixed|void zero)
6625111999-07-25Marcus Comstedt { if(sizeof(arr)) zero = arr[0]; for(int i=1; i<sizeof(arr); i++)
0662a92000-07-12Henrik Grubbström (Grubba)  zero = ([function(mixed,mixed:mixed)]fun)(zero, arr[i]);
6625111999-07-25Marcus Comstedt  return zero; }
5d66082000-12-13Henrik Grubbström (Grubba) //! @[rreduce()] sends the last two elements in @[arr] to @[fun], //! then the third last element in @[arr] and the result to @[fun] and
edcae22001-07-30Johan Sundström //! so on. Then it returns the result. The function will return
5d66082000-12-13Henrik Grubbström (Grubba) //! @[zero] if @[arr] is the empty array. If @[arr] has //! only one element, that element will be returned. //! //! @seealso
56cd002001-10-28Martin Nilsson //! @[reduce()]
5d66082000-12-13Henrik Grubbström (Grubba) //!
be192f1999-07-26Marcus Comstedt mixed rreduce(function fun, array arr, mixed|void zero)
6625111999-07-25Marcus Comstedt { if(sizeof(arr)) zero = arr[-1]; for(int i=sizeof(arr)-2; i>=0; --i)
0662a92000-07-12Henrik Grubbström (Grubba)  zero = ([function(mixed,mixed:mixed)]fun)(arr[i], zero);
6625111999-07-25Marcus Comstedt  return zero; }
5d66082000-12-13Henrik Grubbström (Grubba) //! @[shuffle()] gives back the same elements, but in random order.
0a96ae2002-08-03Martin Nilsson //! The array is modified destructively.
5d66082000-12-13Henrik Grubbström (Grubba) //! //! @seealso
56cd002001-10-28Martin Nilsson //! @[permute()]
5d66082000-12-13Henrik Grubbström (Grubba) //!
b77d8d1998-02-28Henrik Grubbström (Grubba) array shuffle(array arr) { int i = sizeof(arr); while(i) {
d88ed32002-08-26Martin Nilsson  int j = random(i--);
b77d8d1998-02-28Henrik Grubbström (Grubba)  if (j != i) { mixed tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; } } return(arr); }
5d66082000-12-13Henrik Grubbström (Grubba) //! @[search_array()] works like @[map()], only it returns the index //! of the first call that returnes true instead. //! //! If no call returns true, -1 is returned. //! //! @seealso
56cd002001-10-28Martin Nilsson //! @[sum()], @[map()]
5d66082000-12-13Henrik Grubbström (Grubba) //!
a9d4a22002-03-02Martin Nilsson int search_array(array arr, string|function|int fun, mixed ... args)
c2a4061997-02-06Fredrik Hübinette (Hubbe) { int e; if(stringp(fun)) { for(e=0;e<sizeof(arr);e++)
0662a92000-07-12Henrik Grubbström (Grubba)  if(([function(mixed...:mixed)]([array(object)]arr)[e][fun])(@args))
c2a4061997-02-06Fredrik Hübinette (Hubbe)  return e; return -1; } else if(functionp(fun)) { for(e=0;e<sizeof(arr);e++)
0662a92000-07-12Henrik Grubbström (Grubba)  if(([function(mixed,mixed...:mixed)]fun)(arr[e],@args))
c2a4061997-02-06Fredrik Hübinette (Hubbe)  return e; return -1; } else if(intp(fun)) { for(e=0;e<sizeof(arr);e++)
0662a92000-07-12Henrik Grubbström (Grubba)  if(([array(function(mixed...:mixed))]arr)[e](@args))
c2a4061997-02-06Fredrik Hübinette (Hubbe)  return e; return -1; }
a9d4a22002-03-02Martin Nilsson  error("Bad argument 2 to search_array().\n");
c2a4061997-02-06Fredrik Hübinette (Hubbe) }
d3ffb22002-03-20Martin Nilsson //! Applies the function @[sum] columnwise on the elements in the //! provided arrays. E.g. @tt{sum_array(`+,a,b,c)@} does the same //! as @tt{`+(a[*],b[*],c[*])@}. array sum_arrays(function(mixed ...:mixed) sum, array ... args)
c2a4061997-02-06Fredrik Hübinette (Hubbe) {
d3ffb22002-03-20Martin Nilsson  array ret = allocate(sizeof(args[0])); for(int e=0; e<sizeof(args[0]); e++) ret[e] = sum( @column(args, e) );
c2a4061997-02-06Fredrik Hübinette (Hubbe)  return ret; }
5d66082000-12-13Henrik Grubbström (Grubba) //! This function sorts an array after a compare-function @[cmp] //! which takes two arguments and should return @tt{1@} if the first argument //! is larger then the second. //! //! The remaining arguments @[args] will be sent as 3rd, 4th etc. argument //! to @[cmp]. //! //! If @[cmp] is omitted, @[`>()] is used instead. //! //! @seealso
edcae22001-07-30Johan Sundström //! @[map()], @[sort()], @[`>()], @[dwim_sort_func], @[lyskom_sort_func]
5d66082000-12-13Henrik Grubbström (Grubba) //! array sort_array(array foo, function|void cmp, mixed ... args)
c2a4061997-02-06Fredrik Hübinette (Hubbe) { array bar,tmp; int len,start; int length; int foop, fooend, barp, barend; if(!cmp || cmp==`>) { foo+=({}); sort(foo); return foo; } if(cmp == `<) { foo+=({}); sort(foo); return reverse(foo); } length=sizeof(foo); foo+=({}); bar=allocate(length); for(len=1;len<length;len*=2) { start=0; while(start+len < length) { foop=start; barp=start+len; fooend=barp; barend=barp+len; if(barend > length) barend=length;
edcae22001-07-30Johan Sundström 
c2a4061997-02-06Fredrik Hübinette (Hubbe)  while(1) {
0662a92000-07-12Henrik Grubbström (Grubba)  if(([function(mixed,mixed,mixed...:int)]cmp)(foo[foop],foo[barp],@args) <= 0)
c2a4061997-02-06Fredrik Hübinette (Hubbe)  { bar[start++]=foo[foop++]; if(foop == fooend) { while(barp < barend) bar[start++]=foo[barp++]; break; } }else{ bar[start++]=foo[barp++]; if(barp == barend) { while(foop < fooend) bar[start++]=foo[foop++]; break; } } } } while(start < length) bar[start]=foo[start++]; tmp=foo; foo=bar; bar=tmp; } return foo; }
0dcb7f1998-01-31Fredrik Hübinette (Hubbe) array columns(array x, array ind) { array ret=allocate(sizeof(ind)); for(int e=0;e<sizeof(ind);e++) ret[e]=column(x,ind[e]); return ret; }
905bb11998-01-31Fredrik Hübinette (Hubbe) 
0662a92000-07-12Henrik Grubbström (Grubba) array transpose_old(array(array|string) x)
905bb11998-01-31Fredrik Hübinette (Hubbe) {
8ad6f81998-02-28Mirar (Pontus Hagland)  if (!sizeof(x)) return x;
0662a92000-07-12Henrik Grubbström (Grubba)  array ret=allocate(sizeof([array|string]x[0])); for(int e=0;e<sizeof([array|string]x[0]);e++) ret[e]=column(x,e);
8ad6f81998-02-28Mirar (Pontus Hagland)  return ret;
088e2e1998-02-12Mirar (Pontus Hagland) }
b98e4a1998-02-15Mirar (Pontus Hagland) 
04a1a81998-11-30Martin Stjernholm // diff3, complement to diff array(array(array)) diff3 (array a, array b, array c) { // This does not necessarily produce the optimal sequence between // all three arrays. A diff_longest_sequence() that takes any number // of arrays would be nice. array(int) seq_ab = diff_longest_sequence (a, b); array(int) seq_bc = diff_longest_sequence (b, c); array(int) seq_ca = diff_longest_sequence (c, a);
f5c8ba1998-12-02Martin Stjernholm  array(int) aeq = allocate (sizeof (a) + 1); array(int) beq = allocate (sizeof (b) + 1); array(int) ceq = allocate (sizeof (c) + 1); aeq[sizeof (a)] = beq[sizeof (b)] = ceq[sizeof (c)] = 7;
04a1a81998-11-30Martin Stjernholm  for (int i = 0, j = 0; j < sizeof (seq_ab); i++)
f5c8ba1998-12-02Martin Stjernholm  if (a[i] == b[seq_ab[j]]) aeq[i] |= 2, beq[seq_ab[j]] |= 1, j++;
04a1a81998-11-30Martin Stjernholm  for (int i = 0, j = 0; j < sizeof (seq_bc); i++)
f5c8ba1998-12-02Martin Stjernholm  if (b[i] == c[seq_bc[j]]) beq[i] |= 2, ceq[seq_bc[j]] |= 1, j++;
04a1a81998-11-30Martin Stjernholm  for (int i = 0, j = 0; j < sizeof (seq_ca); i++)
f5c8ba1998-12-02Martin Stjernholm  if (c[i] == a[seq_ca[j]]) ceq[i] |= 2, aeq[seq_ca[j]] |= 1, j++;
04a1a81998-11-30Martin Stjernholm 
54e28f1999-05-31Martin Stjernholm  //werror ("%O\n", ({aeq, beq, ceq}));
04a1a81998-11-30Martin Stjernholm  array(array) ares = ({}), bres = ({}), cres = ({}); int ai = 0, bi = 0, ci = 0;
f5c8ba1998-12-02Martin Stjernholm  int prevodd = -2;
04a1a81998-11-30Martin Stjernholm 
f17d2e1998-12-04Martin Stjernholm  while (!(aeq[ai] & beq[bi] & ceq[ci] & 4)) {
54e28f1999-05-31Martin Stjernholm  //werror ("aeq[%d]=%d beq[%d]=%d ceq[%d]=%d prevodd=%d\n", // ai, aeq[ai], bi, beq[bi], ci, ceq[ci], prevodd);
f5c8ba1998-12-02Martin Stjernholm  array empty = ({}), apart = empty, bpart = empty, cpart = empty;
54e28f1999-05-31Martin Stjernholm  int side = aeq[ai] & beq[bi] & ceq[ci]; if ((<1, 2>)[side]) { // Got cyclically interlocking equivalences. Have to break one // of them. Prefer the shortest. int which, merge, inv_side = side ^ 3, i, oi; array(int) eq, oeq; array arr, oarr; int atest = side == 1 ? ceq[ci] != 3 : beq[bi] != 3; int btest = side == 1 ? aeq[ai] != 3 : ceq[ci] != 3; int ctest = side == 1 ? beq[bi] != 3 : aeq[ai] != 3; for (i = 0;; i++) { int abreak = atest && aeq[ai] != aeq[ai + i]; int bbreak = btest && beq[bi] != beq[bi + i]; int cbreak = ctest && ceq[ci] != ceq[ci + i]; if (abreak + bbreak + cbreak > 1) { // More than one shortest sequence. Avoid breaking one that // could give an all-three match later. if (side == 1) { if (!atest) cbreak = 0; if (!btest) abreak = 0; if (!ctest) bbreak = 0; } else { if (!atest) bbreak = 0; if (!btest) cbreak = 0; if (!ctest) abreak = 0; } // Prefer breaking one that can be joined with the previous // diff part. switch (prevodd) { case 0: if (abreak) bbreak = cbreak = 0; break; case 1: if (bbreak) cbreak = abreak = 0; break; case 2: if (cbreak) abreak = bbreak = 0; break; } } if (abreak) { which = 0, merge = (<0, -1>)[prevodd]; i = ai, eq = aeq, arr = a; if (inv_side == 1) oi = bi, oeq = beq, oarr = b; else oi = ci, oeq = ceq, oarr = c; break; } if (bbreak) { which = 1, merge = (<1, -1>)[prevodd]; i = bi, eq = beq, arr = b; if (inv_side == 1) oi = ci, oeq = ceq, oarr = c; else oi = ai, oeq = aeq, oarr = a; break; } if (cbreak) { which = 2, merge = (<2, -1>)[prevodd]; i = ci, eq = ceq, arr = c; if (inv_side == 1) oi = ai, oeq = aeq, oarr = a; else oi = bi, oeq = beq, oarr = b; break; } } //werror (" which=%d merge=%d inv_side=%d i=%d oi=%d\n", // which, merge, inv_side, i, oi); int s = i, mask = eq[i]; do { eq[i++] &= inv_side; while (!(oeq[oi] & inv_side)) oi++; oeq[oi] &= side; } while (eq[i] == mask); if (merge && !eq[s]) { array part = ({}); do part += ({arr[s++]}); while (!eq[s]); switch (which) { case 0: ai = s; ares[-1] += part; break; case 1: bi = s; bres[-1] += part; break; case 2: ci = s; cres[-1] += part; break; } } } //werror ("aeq[%d]=%d beq[%d]=%d ceq[%d]=%d prevodd=%d\n", // ai, aeq[ai], bi, beq[bi], ci, ceq[ci], prevodd);
04a1a81998-11-30Martin Stjernholm 
f5c8ba1998-12-02Martin Stjernholm  if (aeq[ai] == 2 && beq[bi] == 1) { // a and b are equal. do apart += ({a[ai++]}), bi++; while (aeq[ai] == 2 && beq[bi] == 1); bpart = apart; while (!ceq[ci]) cpart += ({c[ci++]}); prevodd = 2; } else if (beq[bi] == 2 && ceq[ci] == 1) { // b and c are equal. do bpart += ({b[bi++]}), ci++; while (beq[bi] == 2 && ceq[ci] == 1); cpart = bpart; while (!aeq[ai]) apart += ({a[ai++]}); prevodd = 0; } else if (ceq[ci] == 2 && aeq[ai] == 1) { // c and a are equal. do cpart += ({c[ci++]}), ai++; while (ceq[ci] == 2 && aeq[ai] == 1); apart = cpart; while (!beq[bi]) bpart += ({b[bi++]}); prevodd = 1;
04a1a81998-11-30Martin Stjernholm  }
54e28f1999-05-31Martin Stjernholm  else if ((<1*2*3, 3*3*3>)[aeq[ai] * beq[bi] * ceq[ci]]) { // All are equal. // Got to match both when all three are 3 and when they are 1, 2 // and 3 in that order modulo rotation (might get such sequences // after breaking the cyclic equivalences above). do apart += ({a[ai++]}), bi++, ci++; while ((<0333, 0123, 0312, 0231>)[aeq[ai] << 6 | beq[bi] << 3 | ceq[ci]]);
f5c8ba1998-12-02Martin Stjernholm  cpart = bpart = apart; prevodd = -1; }
54e28f1999-05-31Martin Stjernholm 
f5c8ba1998-12-02Martin Stjernholm  else { // Haven't got any equivalences in this block. Avoid adjacent // complementary blocks (e.g. ({({"foo"}),({}),({})}) next to // ({({}),({"bar"}),({"bar"})})). Besides that, leave the // odd-one-out sequence empty in a block where two are equal.
54e28f1999-05-31Martin Stjernholm  switch (prevodd) { case 0: apart = ares[-1], ares[-1] = ({}); break; case 1: bpart = bres[-1], bres[-1] = ({}); break; case 2: cpart = cres[-1], cres[-1] = ({}); break;
f5c8ba1998-12-02Martin Stjernholm  }
54e28f1999-05-31Martin Stjernholm  prevodd = -1; while (!aeq[ai]) apart += ({a[ai++]}); while (!beq[bi]) bpart += ({b[bi++]}); while (!ceq[ci]) cpart += ({c[ci++]});
f5c8ba1998-12-02Martin Stjernholm  }
34e2b31998-12-01Martin Stjernholm 
54e28f1999-05-31Martin Stjernholm  //werror ("%O\n", ({apart, bpart, cpart}));
f5c8ba1998-12-02Martin Stjernholm  ares += ({apart}), bres += ({bpart}), cres += ({cpart});
04a1a81998-11-30Martin Stjernholm  } return ({ares, bres, cres}); }
b98e4a1998-02-15Mirar (Pontus Hagland) // diff3, complement to diff (alpha stage)
4755b72000-02-18Henrik Grubbström (Grubba) array(array(array)) diff3_old(array mid,array left,array right)
b98e4a1998-02-15Mirar (Pontus Hagland) {
4755b72000-02-18Henrik Grubbström (Grubba)  array(array) lmid,ldst; array(array) rmid,rdst;
b98e4a1998-02-15Mirar (Pontus Hagland) 
d774421998-02-16Mirar (Pontus Hagland)  [lmid,ldst]=diff(mid,left); [rmid,rdst]=diff(mid,right);
b98e4a1998-02-15Mirar (Pontus Hagland) 
04a1a81998-11-30Martin Stjernholm  int l=0,r=0,n;
4755b72000-02-18Henrik Grubbström (Grubba)  array(array(array)) res=({});
b98e4a1998-02-15Mirar (Pontus Hagland)  int lpos=0,rpos=0; array eq=({});
189dab1998-02-16Mirar (Pontus Hagland)  int x;
b98e4a1998-02-15Mirar (Pontus Hagland) 
723b281998-02-19Mirar (Pontus Hagland)  for (n=0; ;)
b98e4a1998-02-15Mirar (Pontus Hagland)  {
723b281998-02-19Mirar (Pontus Hagland)  while (l<sizeof(lmid) && lpos>=sizeof(lmid[l]))
b98e4a1998-02-15Mirar (Pontus Hagland)  {
189dab1998-02-16Mirar (Pontus Hagland)  if (sizeof(ldst[l])>lpos) res+=({({({}),ldst[l][lpos..],({})})});
b98e4a1998-02-15Mirar (Pontus Hagland)  l++;
189dab1998-02-16Mirar (Pontus Hagland)  lpos=0;
b98e4a1998-02-15Mirar (Pontus Hagland)  }
edcae22001-07-30Johan Sundström  while (r<sizeof(rmid) && rpos>=sizeof(rmid[r]))
b98e4a1998-02-15Mirar (Pontus Hagland)  {
189dab1998-02-16Mirar (Pontus Hagland)  if (sizeof(rdst[r])>rpos) res+=({({({}),({}),rdst[r][rpos..]})}); r++; rpos=0;
b98e4a1998-02-15Mirar (Pontus Hagland)  }
723b281998-02-19Mirar (Pontus Hagland)  if (n==sizeof(mid)) break;
189dab1998-02-16Mirar (Pontus Hagland)  x=min(sizeof(lmid[l])-lpos,sizeof(rmid[r])-rpos);
b98e4a1998-02-15Mirar (Pontus Hagland) 
189dab1998-02-16Mirar (Pontus Hagland)  if (lmid[l]==ldst[l] && rmid[r]==rdst[r])
b98e4a1998-02-15Mirar (Pontus Hagland)  {
189dab1998-02-16Mirar (Pontus Hagland)  eq=lmid[l][lpos..lpos+x-1]; res+=({({eq,eq,eq})}); } else if (lmid[l]==ldst[l]) { eq=lmid[l][lpos..lpos+x-1]; res+=({({eq,eq,rdst[r][rpos..rpos+x-1]})}); } else if (rmid[r]==rdst[r]) { eq=rmid[r][rpos..rpos+x-1]; res+=({({eq,ldst[l][lpos..lpos+x-1],eq})}); }
edcae22001-07-30Johan Sundström  else
189dab1998-02-16Mirar (Pontus Hagland)  { res+=({({lmid[l][lpos..lpos+x-1], ldst[l][lpos..lpos+x-1], rdst[r][rpos..rpos+x-1]})});
b98e4a1998-02-15Mirar (Pontus Hagland)  }
723b281998-02-19Mirar (Pontus Hagland) // werror(sprintf("-> %-5{%O%} %-5{%O%} %-5{%O%}" // " x=%d l=%d:%d r=%d:%d \n",@res[-1],x,l,lpos,r,rpos));
edcae22001-07-30Johan Sundström 
189dab1998-02-16Mirar (Pontus Hagland)  rpos+=x; lpos+=x; n+=x;
b98e4a1998-02-15Mirar (Pontus Hagland)  }
edcae22001-07-30Johan Sundström 
d774421998-02-16Mirar (Pontus Hagland)  return transpose(res);
b98e4a1998-02-15Mirar (Pontus Hagland) }
78762b1999-06-01Mirar (Pontus Hagland) 
edcae22001-07-30Johan Sundström //! Sort without respect to number formatting (most notably leading //! zeroes).
df8c5b2001-04-25Henrik Grubbström (Grubba) int dwim_sort_func(string a, string b)
78762b1999-06-01Mirar (Pontus Hagland) {
df8c5b2001-04-25Henrik Grubbström (Grubba)  if (a==b) return 0;
ff04e22001-07-15Per Hedbor  array aa=({}), bb=({}); int state, oi;
edcae22001-07-30Johan Sundström 
ff04e22001-07-15Per Hedbor  for( int i = 0; i<sizeof(a); i++ ) if( (<'0','1','2','3','4','5','6','7','8','9'>)[a[i]] != state ) { state = !state;
226d6d2002-01-31Henrik Grubbström (Grubba)  if( state ) aa += ({ a[oi..i-1] }); else aa += ({ (int)a[oi..i-1] });
ff04e22001-07-15Per Hedbor  oi = i; } if( state ) aa += ({ (int)a[oi..] }); else aa += ({ a[oi..] }); oi = state = 0;
edcae22001-07-30Johan Sundström 
ff04e22001-07-15Per Hedbor  for( int i = 0; i<sizeof(b); i++ ) if( (<'0','1','2','3','4','5','6','7','8','9'>)[b[i]] != state ) { state = !state;
226d6d2002-01-31Henrik Grubbström (Grubba)  if( state ) bb += ({ b[oi..i-1] }); else bb += ({ (int)b[oi..i-1] });
ff04e22001-07-15Per Hedbor  oi = i; } if( state ) bb += ({ (int)b[oi..] }); else bb += ({ b[oi..] });
edcae22001-07-30Johan Sundström 
ff04e22001-07-15Per Hedbor  for( int i = 0; i<sizeof( aa ); i++ ) { if( i >= sizeof( bb ) ) return 1; // a is definately bigger.
df8c5b2001-04-25Henrik Grubbström (Grubba) 
ff04e22001-07-15Per Hedbor  if( aa[i] < bb[i] ) return -1; if( aa[i] > bb[i] ) return 1; }
226d6d2002-01-31Henrik Grubbström (Grubba)  // Either equal, or bb is longer. return -(sizeof(aa)<sizeof(bb));
78762b1999-06-01Mirar (Pontus Hagland) }
7473581999-06-01Mirar (Pontus Hagland) 
ff04e22001-07-15Per Hedbor //! Sort comparison function that does not care about case, nor about //! the contents of any parts of the string enclosed with '()' //! //! Example: "Foo (bar)" is given the same weight as "foo (really!)"
7473581999-06-01Mirar (Pontus Hagland) int lyskom_sort_func(string a,string b) { string a0=a,b0=b;
d228d51999-07-25Marcus Comstedt  a=replace(lower_case(a),"][\\}{|"/1,"åäöåäö"/1); b=replace(lower_case(b),"][\\}{|"/1,"åäöåäö"/1);
edcae22001-07-30Johan Sundström 
7473581999-06-01Mirar (Pontus Hagland)  while (sscanf(a0=a,"%*[ \t](%*[^)])%*[ \t]%s",a)==4 && a0!=a); while (sscanf(b0=b,"%*[ \t](%*[^)])%*[ \t]%s",b)==4 && b0!=b); a0=b0=""; sscanf(a,"%[^ \t]%*[ \t](%*[^)])%*[ \t]%s",a,a0); sscanf(b,"%[^ \t]%*[ \t](%*[^)])%*[ \t]%s",b,b0); if (a>b) return 1; if (a<b) return 0; if (a0==b0) return 0; return lyskom_sort_func(a0,b0); }
13d6ac2000-05-03Fredrik Hübinette (Hubbe) 
df8c5b2001-04-25Henrik Grubbström (Grubba) //! Flatten a multi-dimensional array to a one-dimensional array.
13d6ac2000-05-03Fredrik Hübinette (Hubbe) array flatten(array a) { array ret=({});
0662a92000-07-12Henrik Grubbström (Grubba)  foreach(a, mixed b) ret+=arrayp(b)?flatten([array]b):({b});
13d6ac2000-05-03Fredrik Hübinette (Hubbe)  return ret; }
a703e72001-04-25Mirar (Pontus Hagland) 
ff04e22001-07-15Per Hedbor //! Sum the elements of an array using `+
a703e72001-04-25Mirar (Pontus Hagland) mixed sum(array a) { // 1000 is a safe stack limit if (sizeof(a)<1000) return `+(@a); else
df8c5b2001-04-25Henrik Grubbström (Grubba)  {
a703e72001-04-25Mirar (Pontus Hagland)  mixed mem=`+(@a[..999]); int j=1000; array v; while (sizeof(v=a[j..j+999])) mem=`+(mem,@v),j+=1000; return mem; } }
2297022001-05-19Mirar (Pontus Hagland) 
edcae22001-07-30Johan Sundström //! Perform the same action as the Unix uniq command on an array, //! that is, fold consecutive occurrences of the same element into //! a single element of the result array: //!
ff04e22001-07-15Per Hedbor //! aabbbcaababb -> abcabab. //! //! See also the @[uniq] function.
2297022001-05-19Mirar (Pontus Hagland) array uniq2(array a) { array res; mixed last; if (!sizeof(a)) return ({}); res=({last=a[0]}); foreach (a,mixed v) if (v!=last) last=v,res+=({v}); return res; }
11e3692001-06-06Mirar (Pontus Hagland) 
2ad6992001-07-30Johan Sundström //! Make an array of the argument, if it isn't already. A zero_type //! argument gives the empty array. This is useful when something is //! either an array or a basic datatype, for instance in headers from //! the MIME module or Protocols.HTTP.Server. //! @param x //! Result depends of the argument type: //! @dl //! @item arrayp(x) //! arrayify(x) => x //! @item zero_type(x) //! arrayify(x) => ({}) //! @item otherwise //! arrayify(x) => ({ x }) //! @enddl array arrayify(void|array|mixed x)
11e3692001-06-06Mirar (Pontus Hagland) {
2ad6992001-07-30Johan Sundström  if(zero_type(x)) return ({});
d3ffb22002-03-20Martin Nilsson  if(arrayp(x)) return [array]x;
2ad6992001-07-30Johan Sundström  return ({ x });
11e3692001-06-06Mirar (Pontus Hagland) }
b519292001-08-28Honza Petrous  //! Sort with care of numerical sort for OID values: //! "1.2.1" before "1.11.1" int oid_sort_func(string a0,string b0) { string a2="",b2=""; int a1, b1; sscanf(a0,"%d.%s",a1,a2); sscanf(b0,"%d.%s",b1,b2); if (a1>b1) return 1; if (a1<b1) return 0; if (a2==b2) return 0; return oid_sort_func(a2,b2); }
3bbf9a2002-03-18Johan Sundström 
d3ffb22002-03-20Martin Nilsson static array(array(array)) low_greedy_diff(array(array) d1, array(array) d2)
3bbf9a2002-03-18Johan Sundström {
28aaf92002-03-18Henrik Grubbström (Grubba)  array r1, r2, x, y, yb, b, c;
3bbf9a2002-03-18Johan Sundström  r1 = r2 = ({}); int at, last, seen; while(-1 != (at = search(d1, ({}), last))) { last = at + 1; if(at < 2) continue; b = d2[at-1]; yb = d2[at];
0275742002-03-18Johan Sundström out:if(sizeof(yb) > sizeof(b))
3bbf9a2002-03-18Johan Sundström  {
0275742002-03-18Johan Sundström  int i = sizeof(b), j = sizeof(yb); while(i) if(b[--i] != yb[--j]) break out; // past five lines implement an if(has_suffix(yb, b))
3bbf9a2002-03-18Johan Sundström  x = d2[at-2]; y = yb[..sizeof(yb)-sizeof(b)-1];
0275742002-03-18Johan Sundström  if(at+1 <= sizeof(d1))
3bbf9a2002-03-18Johan Sundström  { c = d2[at+1];
28aaf92002-03-18Henrik Grubbström (Grubba)  array bc = b+c; r1 += d1[seen..at-2] + ({ bc }); r2 += d2[seen..at-3] + ({ x+b+y }) + ({ bc });
3bbf9a2002-03-18Johan Sundström  } else {
28aaf92002-03-18Henrik Grubbström (Grubba)  // At last chunk. There is no C.
3bbf9a2002-03-18Johan Sundström  r1 += d1[seen..at-2] + ({ b }); r2 += d2[seen..at-3] + ({ x+b+y }) + ({ b }); } seen = at + 5; } }
28aaf92002-03-18Henrik Grubbström (Grubba)  if(!seen) return ({ d1, d2 }); // No change.
d3ffb22002-03-20Martin Nilsson  return ({ [array(array)]r1 + d1[seen..], [array(array)]r2 + d2[seen..] });
3bbf9a2002-03-18Johan Sundström }
28aaf92002-03-18Henrik Grubbström (Grubba)  //! Like @[Array.diff], but tries to generate bigger continuous chunks of the //! differences, instead of maximizing the number of difference chunks. More
4509862002-03-18Johan Sundström //! specifically, @[greedy_diff] optimizes the cases where @[Array.diff] returns
28aaf92002-03-18Henrik Grubbström (Grubba) //! @code{({ ..., A, Z, B, ({}), C, ... })@} //! @code{({ ..., A, X, B, Y+B, C, ... })@} //! into the somewhat shorter diff arrays //! @code{({ ..., A, Z, B+C, ... })@} //! @code{({ ..., A, X+B+Y, B+C, ... })@}
d3ffb22002-03-20Martin Nilsson array(array(array)) greedy_diff(array from, array to)
28aaf92002-03-18Henrik Grubbström (Grubba) {
d3ffb22002-03-20Martin Nilsson  array(array) d1, d2;
28aaf92002-03-18Henrik Grubbström (Grubba)  [d1, d2] = Array.diff(from, to);
4509862002-03-18Johan Sundström  [d2, d1] = low_greedy_diff(d2, d1); return low_greedy_diff(d1, d2);
28aaf92002-03-18Henrik Grubbström (Grubba) }
9eb9b22002-04-17Martin Nilsson 
95c68e2002-04-18Johan Sundström //! @decl int count(array|mapping|multiset haystack, mixed needle) //! @decl mapping(mixed:int) count(array|mapping|multiset haystack)
c805a62002-04-18Johan Sundström //! Returns the number of occurrences of @[needle] in @[haystack]. //! If the optional @[needle] argument is omitted, @[count] instead //! works similar to the unix command @tt{sort|uniq -c@}, returning //! a mapping with the number of occurrences of each element in
95c68e2002-04-18Johan Sundström //! @[haystack]. For array or mapping @[haystack]s, it's the values //! that are counted, for multisets the indices, as you'd expect.
c805a62002-04-18Johan Sundström //! @seealso //! @[String.count], @[search], @[has_value]
95c68e2002-04-18Johan Sundström int|mapping(mixed:int) count(array|mapping|multiset haystack, mixed|void needle)
c805a62002-04-18Johan Sundström { if(zero_type(needle)) {
debf1b2002-05-15Henrik Grubbström (Grubba)  mapping(mixed:int) res = ([]);
95c68e2002-04-18Johan Sundström  if(mappingp(haystack))
debf1b2002-05-15Henrik Grubbström (Grubba)  haystack = values([mapping]haystack); foreach((array)haystack, mixed what)
c805a62002-04-18Johan Sundström  res[what]++; return res; }
95c68e2002-04-18Johan Sundström  return sizeof(filter(haystack, `==, needle));
c805a62002-04-18Johan Sundström }
02b5312002-09-16Johan Schön  //! Find the longest common prefix from an array of arrays. //! @seealso //! @[String.common_prefix] array common_prefix(array(array) arrs) { if(!sizeof(arrs)) return 0; array arrs0 = arrs[0]; int n, i; catch { for(n = 0; n < sizeof(arrs0); n++) for(i = 1; i < sizeof(arrs); i++) if(!equal(arrs[i][n],arrs0[n])) return arrs0[0..n-1]; }; return arrs0[0..n-1]; }