View file File name : slshfun-8.html Content :<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> <HTML> <HEAD> <META NAME="GENERATOR" CONTENT="LinuxDoc-Tools 0.9.69"> <TITLE> SLSH Library Reference (version 2.3.0): List Functions and List-based Data Structures</TITLE> <LINK HREF="slshfun-9.html" REL=next> <LINK HREF="slshfun-7.html" REL=previous> <LINK HREF="slshfun.html#toc8" REL=contents> </HEAD> <BODY> <A HREF="slshfun-9.html">Next</A> <A HREF="slshfun-7.html">Previous</A> <A HREF="slshfun.html#toc8">Contents</A> <HR> <H2><A NAME="s8">8.</A> <A HREF="slshfun.html#toc8">List Functions and List-based Data Structures</A></H2> <P>The file <CODE>arrayfuns.sl</CODE> includes functions for manipulating lists. It also includes some list-based data structures such as heaps.</P> <H2><A NAME="list_sort"></A> <A NAME="ss8.1">8.1</A> <A HREF="slshfun.html#toc8.1"><B>list_sort</B></A> </H2> <P> <DL> <DT><B> Synopsis </B><DD> <P>Sort a list of item</P> <DT><B> Usage </B><DD> <P> <BLOCKQUOTE><CODE> <PRE> Int_Type indices = list_sort (List_Type list); % Form 1 list_sort (List_Type list; inplace); % Form 2 </PRE> </CODE></BLOCKQUOTE> </P> <DT><B> Description </B><DD> <P>The <CODE>list_sort</CODE> may be used to sort a list. By default, it returns a permutation index array for the desired sort. If the <CODE>inplace</CODE> qualifier is given, the sort will be in-place and no index array will be returned.</P> <DT><B> Qualifiers </B><DD> <P>The following qualifiers may be used to modify the behavior of the function:</P> <P> <BLOCKQUOTE><CODE> <PRE> dir=1 </PRE> </CODE></BLOCKQUOTE> If the value of the <CODE>dir</CODE> qualifier is non-negative, the sort will take place in an increasing order. If negative, the list will be sorted in decreasing order.</P> <P> <BLOCKQUOTE><CODE> <PRE> cmp=&cmpfunc </PRE> </CODE></BLOCKQUOTE> The <CODE>cmp</CODE> qualifier specifies the function used to comparison operation for two elements of an array. Its value must be a reference to a function that accepts two arguments representing the objects to be compared. It must return a positive integer, 0, or a negative integer if the value of the first argument is greater than, equal to, or less than the value of the second, respectively.</P> <P> <BLOCKQUOTE><CODE> <PRE> inplace[=1] </PRE> </CODE></BLOCKQUOTE> The inplace qualifier may be used to indicate if the list is to be sorted in place. If the qualifier is present with no-value or a non-zero value, the list will be sorted in place and no index array will be returned. If not present, or present with a value of 0, the list will not be modified, and an index array will be returned.</P> <DT><B> Example </B><DD> <P> <BLOCKQUOTE><CODE> <PRE> list = {1, 9, 3, 7}; i = list_sort (list); sorted_list = list[i]; </PRE> </CODE></BLOCKQUOTE> The above example creates a second list (<CODE>sorted_list</CODE>) by using the permutation index array to rearrange the objects in the unsorted list.</P> <P> <BLOCKQUOTE><CODE> <PRE> list = {1, 9, 3, 7}; list_sort (list; inplace); </PRE> </CODE></BLOCKQUOTE> In this example, the list is sorted in-place.</P> <P>Consider a list of strings that are to be sorted in increasing order of the number of bytes used by each string. For this, a custom sort function is required: <BLOCKQUOTE><CODE> <PRE> private define cmpfunc (a, b) { return strbytelen(a) - strbytelen(b); } list = {"Here", "is", "a", "list", "of", "strings"}; list_sort (list; inplace); </PRE> </CODE></BLOCKQUOTE> </P> <DT><B> Notes </B><DD> <P>Keep in mind that a list can contain a heterogeneous collection of object where there is no predefined comparison operation. For example, there may be no natural way to compare an integer to a string. For a heterogeneous list, a comparison function must be provided.</P> <DT><B> See Also </B><DD> <P><CODE>array_sort, rearrange</CODE></P> </DL> </P> <H2><A NAME="heap_new"></A> <A NAME="ss8.2">8.2</A> <A HREF="slshfun.html#toc8.2"><B>heap_new</B></A> </H2> <P> <DL> <DT><B> Synopsis </B><DD> <P>Instantiate a heap data object</P> <DT><B> Usage </B><DD> <P><CODE>Struct_Type h = new_heap (List_Type list)</CODE></P> <DT><B> Description </B><DD> <P>The <CODE>new_heap</CODE> function takes a <CODE>List_Type</CODE> object and rearranges its elements to form a heap. A list rearranged to form a heap has the property that <CODE>list[i] >= list[2*i+j]</CODE>, where j=1 or 2, and i is the list index (from 0).</P> <P>Upon return, the elements of the list will be rearranged to have the heap property and a new container object (the heap) will be returned. It may be manipulated using the method calls described below.</P> <DT><B> Qualifiers </B><DD> <P> <BLOCKQUOTE><CODE> <PRE> dir=-1 </PRE> </CODE></BLOCKQUOTE> The <CODE>dir</CODE> qualifier may be used to specify the direction of the heap. The default (dir=-1) specifies a top-down ordering. A bottom-up ordering corresponds to dir=1.</P> <P> <BLOCKQUOTE><CODE> <PRE> cmp=&cmpfunc </PRE> </CODE></BLOCKQUOTE> The <CODE>cmp</CODE> may be used to specify the function to be used when comparing elements of the list. Its value must be a reference to a function that takes two parameters and returns a positive integer if the value of the first parameter is greater than the second, 0 if they are equal, or a negative integer if the first is less than the second. For example, if the list consists of structures with fields called <CODE>lastname</CODE> and <CODE>firstname</CODE> that are to be used for ordering, then the desired function would look something like: <BLOCKQUOTE><CODE> <PRE> define cmpfunc (a, b) { variable c = strcmp (a.lastname, b.lastname); if (c == 0) c = strcmp (a.firstname, b.firstname); return c; } </PRE> </CODE></BLOCKQUOTE> </P> <DT><B> Methods </B><DD> <P><CODE></CODE></P> <DT><B> length() </B><DD> <P>Get the length to the heap</P> <DT><B> add(obj) </B><DD> <P>Add a new object to the heap</P> <DT><B> remove() </B><DD> <P>Remove the largest (for top-down) or smallest (for bottom-up) item from the heap and return its value.</P> <DT><B> peek() </B><DD> <P>Get the largest (top-down) or smallest(bottom-up) item from the heap, but do not remove it.</P> <P>Note that attempting to peek at or remove an item from an empty list will result in an <CODE>IndexError</CODE> exception.</P> <DT><B> Example </B><DD> <P>Suppose that merging two objects requires a time equal to the combined length of the objects. For example, if the length of first is A and that of the second is B, then the time to merge the two will be A + B. What is the minimum amount of time it takes to merge a list of N objects with lengths <CODE>{L_k}, k=0...N-1</CODE>? The answer to this is very simple if a bottom-up heap is used. The idea is to remove the two smallest objects from the heap, combine them and then add the result back to the heap. Repeat this process until the heap is empty. In the following, <CODE>list</CODE> is a list of the lengths of the objects to be merged. <BLOCKQUOTE><CODE> <PRE> define compute_merge_time (list) { variable h = heap_new (list; dir=1); % bottom-up heap variable a, b, c, t; a = h.remove (); t = 0; while (h.length()) { b = h.remove (); c = a + b; t += c; h.add (c); a = h.remove (); } return t; } </PRE> </CODE></BLOCKQUOTE> </P> <DT><B> Notes </B><DD> <P>Generally speaking, a list will require the specification of a comparison function unless the list consists solely of elements that may be compared using the <CODE>></CODE>, <CODE><</CODE>, and <CODE>==</CODE> operators.</P> <DT><B> See Also </B><DD> <P><CODE>list_sort, list_new</CODE></P> </DL> </P> <HR> <A HREF="slshfun-9.html">Next</A> <A HREF="slshfun-7.html">Previous</A> <A HREF="slshfun.html#toc8">Contents</A> </BODY> </HTML>