From: Holden Glova Date: 2002-05-17T20:22:51+09:00 Subject: Re: Shifting array element & regex on array element -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Fri, 17 May 2002 22:45, Yohanes Santoso wrote: > In implementing a buffer gap mechanism, I was wondering if there is > any efficient way to shift elements in an array. > > Suppose that I have 80000 elements in an array. I would like to insert > a new element between the 1st and 2nd elements by making the 2nd > element be the 3rd and so on. > > In C, the way to do this is to do bcopy/memmove or > something like while (--1 >= 0) *--to = *--from; > > The way I am implementing right now is: > 80000.downto(2) {|i| array[i+1]=array[i]} > > 1. Is this as efficient as it gets? It looks fast enough so far. Perhaps ugly? But it works. irb(main):015:0> array = [1, 3, 4, 5] [1, 3, 4, 5] irb(main):023:0> array[0..0] + [2] + array[1..-1] [1, 2, 3, 4, 5] > The next future of the buffer is that it supports regex. Ruby's > standard Regex class can't operate over array, especially array with a > gap. > > 2. Anyone know of a Regex library that can operate over an array (either > Ruby's Array or C's array (unsigned char*) ) with a gap? > > TIA, > YS. I'm not sure what you mean here. - -- Signed, Holden Glova -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.0.6 (GNU/Linux) Comment: For info see http://www.gnupg.org iD8DBQE85Od2+mF116Lw2cQRAnXWAJ91fkiQbPNR+3jn0nygSCNfrQygyACeI/ge /gd2nx50+4SY0fH2J399+1g= =uQTZ -----END PGP SIGNATURE-----