<CFSCRIPT>
        /**
         * int arrayFind(Array, item, ["textnocase"|"numeric"]);
         *
         * Searches an array for a specific item
         * and returns the positon of that item
         * or 0 if that item is not found.
         * uses the binary search algorithm
         */
        function arrayFind(collection, item){
                //what kind of array
                var type = "textnocase";                
                if(arrayLen(arguments) gt 2 AND arguments[3]) {
                        type = arguments[3];
                }
                //need a sorted array for this algorithm
                arraySort(collection,type);
                floor = 1;
                currentPos = arrayLen(collection);
                roof = currentPos;
                
                while(roof-floor gt 1){
                        //split the currentPos in half
                        currentPos = ceiling((roof - floor)/2 + floor);
                                        
                        switch(compareNoCase(collection[currentPos],item)){
                                case 1:
                                        roof = currentPos;
                                        break;
                                //equal
                                case 0:
                                        return currentPos;                             
 
                                        break;
                                case -1:
                                        floor = currentPos;
                                        break; 
                        }
                
                        if(currentPos eq 1 OR currentPos eq arrayLen(collection)){
                                break;
                        }
                }
                return 0;
        }
</CFSCRIPT>

Rob

Certified Organic
"When you put things in quotes, people think someone actually said it."
http://treebeard.sourceforge.net
http://ruinworld.sourceforge.net
Scientia Est Potentia
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Archives: http://www.houseoffusion.com/cf_lists/index.cfm?forumid=4
Subscription: 
http://www.houseoffusion.com/cf_lists/index.cfm?method=subscribe&forumid=4
FAQ: http://www.thenetprofits.co.uk/coldfusion/faq
Get the mailserver that powers this list at http://www.coolfusion.com

Reply via email to