1. New version of my General Functions Package
- Posted by rforno at tutopia.com Apr 13, 2002
- 392 views
Today I sent a new version of my General Function Package to Rob. This is a much improved version. Some speedups have been implemented, an Index (on suggestion by Antoine Tammer) has been added, many new routines have been provided. Most interesting are: 1) A small library of string functions, that a few days ago was requested in this list. 2) A set of new sorting algorithms, with performances exceeding that of Shell sort provided with Euphoria for more than 500 elements, approximately. But their main strength lies in being really much faster for data that is already nearly sorted, either in ascending or descending order, even in chunks. Following, I am including the Index, that is also in the package. ------------------------------- INDEX ----------------------------------------- -- REVERSE --Reverse(sequence s) does the same as the reverse function provided with -- Euphoria, but is slightly faster in most cases. -- FLOOR, CEIL, ROUND --Ceil(object a) is similar to floor, but rounds up. --Round(object a) rounds either down or up, according to each element of 'a' -- being below or above the middle point between integers. --RoundCents(object a, integer cents) allows for rounding with a specified -- precision. --FloorTowardsZero(object a) is similar to floor, but for negative numbers it -- rounds towards zero. --CeilTowardsInfinity(object a) is similar to Ceil, but it rounds towards -- minus infinity for negative numbers. -- SIGN, ABS --Sign(object a) returns -1, 0 or 1 for each element of 'a' according to it -- being negative, zero or positive. --Abs(object a) returns the absolute value of each element of 'a'. -- SUM, PRODUCT, MAX, MIN, AND, OR, AND_BITS, OR_BITS --Sum(object a), Product(object a), Max(object a), Min(object(a), -- And(object a), Or(object a), And_bits(object a), and Or_bits(object a) -- apply the respective function successively to all elements of object 'a', -- so that Sum obtains the summatory, and so on. Xor and Xor_bits are not -- provided because they are not defined for a single element. -- RESIDUE --Residue(object a, object d) is similar to remainder, but gives different -- results for negative operands, and allows zero divisors. -- STRING ROUTINES --A number of string/sequence manipulation routines are provided. --Rotate(sequence s, integer n) puts the n elements in front of s at the end. --Take(sequence s, integer n) returns the n first elements of s. --Drop(sequence s, integer n) drops the n first elements of s. --Replicate(object r, object s) replicates selected elements of s as -- specified by r. --ReplicateNR(sequence r, sequence s) does something similar (see the -- comments in the routine). --Expand(sequence s, sequence a) inserts zero elements in 'a' as specified -- by s. --Index(sequence s, object r) returns the elements of s as selected by -- the elements of r used as indexes. For example, it simulates things -- like s[{7, 6, 1, 9, 6}]. --type SimpleSequence(object o) returns true if and only if o is a sequence -- composed only by atoms. --type String(sequence s) returns true if all the elements of s are integers -- between 0 and 255. --TrimHead(sequence s), TrimTail(sequence s), and TrimBoth(sequence s) -- return their argument with whitespace removed either at the beginning, -- the end, or both ends, respectively. --Substring(sequence s, integer start, integer len), -- Remove(sequence s, integer start, integer len), -- Insert(sequence s, sequence t, integer start), -- Overlap(sequence s, sequence t, integer start) do to strings/sequences -- what their names imply, allowing for negative values of 'start', -- removing more elements than exist, etc., without ever crashing. Oh, well... -- MISCELLANEOUS --Ravel(object a) returns 'a' as a simple sequence. --IndexGenerator(integer n) returns {1, 2, 3, ..., n). --IndexOf(sequence base, sequence look) returns a sequence containing the -- indexes of elements of 'look' in 'base'. This is similar to find, but it -- operates on the elements of 'look' and is faster than a loop of find's. --CountEquals(sequence base, sequence look) returns a sequence containing -- the number of times each element of 'look' is in 'base'. --Membership(sequence base, sequence look) returns a sequence containing a 1 -- or a 0 for each element of look, according if it is or not in 'base'. --Unique(sequence a) returns the elements of 'a' that are different from -- each other, in the form of a sorted sequence. --ReshapeAs(object s, object f) returns s reshaped as the structure of f. --MaxDepth(object o) returns the maximum depth of 'o'. Depth is defined -- to be 0 for atoms, 1 for simple sequences, 2 for sequences composed by -- simple sequences and atoms, etc. --Structure(object o) returns the structure of o, that is, a 0 for each -- element of o. --RangeFind(object x, sequence s, integer pos1, integer pos2) simulates -- find(x, s[pos1..pos2] + pos1 - 1. In some cases, may be faster than -- using find. From a post by Chris Bensler. --MultipleMatch(sequence s, sequence t) is similar to match, but it -- returns all matches, not only the first one. --MultipleFind(object o, sequence t) is similar to find, but it returns -- all findings, not only the first one. -- MATHEMATICAL --Log10(object a) returns the logarithms base 10 of elements of a. --Mcd2(atom a, atom r), Mcd(sequence s), Mcm2(atom a, atom b), -- Mcm(sequence s) return the Maximun Common Divisor or the Minimum Common -- Multiple of either 2 atoms or a sequence. -- PrimeFactors(atom a) returns a list of prime factors of 'a'. -- Divisors(atom a) returns an ordered list of the divisors of 'a'. -- ENCODING AND DECODING --BaseEvaluation(object a, integer base), -- AnyBase1(atom a, integer len, integer base), -- AnyBase2(atom a, integer len, integer base), -- AnyBase3(atom a, integer len, integer base), -- Hexa(atom a, integer len), Octal(atom a, integer len), -- Encode(object c, atom a), Decode(object c, sequence z) -- are routines dealing with encoding and decoding numbers in several -- numbering base systems. -- STATISTICS RELATED --Permutation(sequence set, integer size, integer which) returns permutation -- numbered 'which' with length 'size' from set 'set'. -- Deal(integer upto, integer howmany) returns 'howmany' non-repetitive -- integers from 1..upto. -- DealSorted(integer upto, integer howmany) returns 'howmany' sorted -- non-repetitive integers from 1..upto. -- Scramble(sequence s) returns s scrambled, that is, randomly ordered. -- SORTING --GradeUp(sequence s, integer n), GradeDown(sequence s, integer n), -- GradeUpNWay(sequence s, integer n), GradeDownNWay(sequence s, integer n), -- SortUp(sequence s), SortUpNWay(sequence s) are all sorting routines -- using different algorithms. Grade... can be used either as data or index -- sorts, while Sort... can be used only as data sorts. They share the -- property of being faster than the standard Shell sort provided with -- Euphoria for more than 500 elements, approximately. But their main -- advantage lies in being much faster for data that is already nearly -- sorted, either in ascending or descending order, even in chunks of -- ascending and descending order. ----------------- END OF INDEX --------------------