[osflash] Q:Converting C routines to AS3
Ralf Bokelberg
ralf.bokelberg at gmail.com
Wed Jan 2 11:36:47 PST 2008
In the past the native sorting method of Array was rather fast.
I always assumed, that it is implemented as quicksort anyways.
Should be standard these days, unless you have special requirements.
Cheers
Ralf.
On Jan 2, 2008 3:40 PM, jake varghese <osflash at flvorful.com> wrote:
> Hi Moveup,
>
>
> AS3 already has a Match function for Strings. Check it out at
> http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/String.html#match()
>
> There is also a method for sorting an Array at
> http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/Array.html#sort()
> but i dont know if it is a quicksort or one of the many other algorithms.
>
> You can prototype your own quicksort for array with the following code:
>
> Array.prototype.quicksort = function(Number:left, Number:right):Array {
> if (right > left){
> pIndex = left
> newPindex = self.partition(left, right, pIndex)
> self.quicksort(list, left, newPindex-1)
> self.quicksort(list, newPindex+1, right)
> }
> };
> Array.prototype.swap = function(Number:left, Number:r):Array{
> tmp = self[l]
> self[l] = self[r]
> self[r] = tmp
> }
> Array.prototype.partition= function(Number:left, Number:right,
> Number:pindex):Array {
> pvalue = self[pindex]
> self.swap(pindex, right)
> sindex = left
> for (i = left; i <= right - 1; i++){
> if self[i] <= pvalue{
> self.swap(sindex, i)
> sindex = sindex + 1
> }
> }
>
> You should be able to use the sort like:
> var Array:arr = new Array("a", "b", "c");
> arr.quicksort(0, arr, size - 1)
>
>
> You might want to double check that. I didnt have time to test it.
>
>
> Thanks,
>
>
> Jake Varghese
> CEO / Lead Developer
> Flvorful
> www.flvorful.com
> office: 877-821-8022 x701
> cell: 512-289-3916
>
>
>
> moveup at mac.com wrote:
> > There are several classic C routines that I would love to convert for use in As3.
> > However, not being a C programmer, I am stumbling right now.
> >
> > Can anyone offer some help with the following 2 routines???
> > The first is a method from a classic Regex matching routine and the second is a recursive quicksort algorithm
> >
> >
> > /* match: search for regexp anywhere in the text*/
> > int match(char *regexp, char *text)
> > {
> > if (regexp[0] =='^')
> > return matchhere(regexp+1, text);
> > do { /* must look even if string is empty */
> > if (matchhere(regexp, text))
> > return 1;
> > } while (*text++ != '\0');
> > return 0;
> > }
> >
> >
> >
> >
> > void quicksort(int L, int u)
> > if (L>=u) return;
> > swap(L, radint(L, u));
> > m=L;
> > for (i=L+1;i<=u;i++)
> > if (x[i] < x[L])
> > swap(++m, i);
> > swap(L,m);
> > quicksort(L, m-1);
> > quicksort(m+1, u);
> > }
> >
> >
> >
> >
> >
> >
> > [f] www.flickr.com/photos/bitstream
> > [c] 416.668.0034
> > [w] www.bitstream.ca
> > --------------------------------------------
> > "...all improvisation is life in search of a style."
> > - Bruce Mau,'LifeStyle'
> >
> > _______________________________________________
> > osflash mailing list
> > osflash at osflash.org
> > http://osflash.org/mailman/listinfo/osflash_osflash.org
> >
> >
>
> _______________________________________________
> osflash mailing list
> osflash at osflash.org
> http://osflash.org/mailman/listinfo/osflash_osflash.org
>
--
Ralf Bokelberg <ralf.bokelberg at gmail.com>
Flex & Flash Consultant based in Cologne/Germany
More information about the osflash
mailing list