ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Array-Heap/README
(Generate patch)

Comparing Array-Heap/README (file contents):
Revision 1.2 by root, Sun Jul 26 05:25:18 2009 UTC vs.
Revision 1.6 by root, Tue Jul 14 23:45:10 2015 UTC

1NAME 1NAME
2 Array::Heap - treat perl arrays as heaps (priority queues) 2 Array::Heap - treat perl arrays as binary heaps/priority queues
3 3
4SYNOPSIS 4SYNOPSIS
5 use Array::Heap; 5 use Array::Heap;
6 6
7DESCRIPTION 7DESCRIPTION
9 want to search for /Heap/ and /Priority/ to find many. They implement 9 want to search for /Heap/ and /Priority/ to find many. They implement
10 more or less fancy datastructures that might well be what you are 10 more or less fancy datastructures that might well be what you are
11 looking for. 11 looking for.
12 12
13 This module takes a different approach: It exports functions (i.e. no 13 This module takes a different approach: It exports functions (i.e. no
14 object orientation) that are loosely modeled after the C++ STL's heap 14 object orientation) that are loosely modeled after the C++ STL's binary
15 functions. They all take an array as argument, just like perl's built-in 15 heap functions. They all take an array as argument, just like perl's
16 functions "push", "pop" etc. 16 built-in functions "push", "pop" etc.
17 17
18 The implementation itself is in C for maximum speed. 18 The implementation itself is in C for maximum speed.
19 19
20FUNCTIONS 20FUNCTIONS
21 All of the following functions are being exported by default. 21 All of the following functions are being exported by default.
23 make_heap @heap (\@) 23 make_heap @heap (\@)
24 Reorders the elements in the array so they form a heap, with the 24 Reorders the elements in the array so they form a heap, with the
25 lowest value "on top" of the heap (corresponding to the first array 25 lowest value "on top" of the heap (corresponding to the first array
26 element). 26 element).
27 27
28 make_heap_idx @heap (\@)
29 Just like "make_heap", but updates the index (see INDEXED
30 OPERATIONS).
31
28 make_heap_lex @heap (\@) 32 make_heap_lex @heap (\@)
29 Just like "make_heap", but in string comparison order instead of 33 Just like "make_heap", but in string comparison order instead of
30 numerical comparison order. 34 numerical comparison order.
31 35
32 make_heap_cmp { compare } @heap (&\@) 36 make_heap_cmp { compare } @heap (&\@)
33 Just like "make_heap", but takes a custom comparison function. 37 Just like "make_heap", but takes a custom comparison function.
34 38
35 push_heap @heap, $element, ... (\@@) 39 push_heap @heap, $element, ... (\@@)
36 Adds the given element(s) to the heap. 40 Adds the given element(s) to the heap.
37 41
42 push_heap_idx @heap, $element, ... (\@@)
43 Just like "push_heap", but updates the index (see INDEXED
44 OPERATIONS).
45
38 push_heap_lex @heap, $element, ... (\@@) 46 push_heap_lex @heap, $element, ... (\@@)
39 Just like "push_heap", but in string comparison order instead of 47 Just like "push_heap", but in string comparison order instead of
40 numerical comparison order. 48 numerical comparison order.
41 49
42 push_heap_cmp { compare } @heap, $element, ... (&\@@) 50 push_heap_cmp { compare } @heap, $element, ... (&\@@)
43 Just like "push_heap", but takes a custom comparison function. 51 Just like "push_heap", but takes a custom comparison function.
44 52
45 pop_heap @heap (\@) 53 pop_heap @heap (\@)
46 Removes the topmost (lowest) heap element and repairs the heap. 54 Removes the topmost (lowest) heap element and repairs the heap.
55
56 pop_heap_idx @heap (\@)
57 Just like "pop_heap", but updates the index (see INDEXED
58 OPERATIONS).
47 59
48 pop_heap_lex @heap (\@) 60 pop_heap_lex @heap (\@)
49 Just like "pop_heap", but in string comparison order instead of 61 Just like "pop_heap", but in string comparison order instead of
50 numerical comparison order. 62 numerical comparison order.
51 63
54 66
55 splice_heap @heap, $index (\@$) 67 splice_heap @heap, $index (\@$)
56 Similar to "pop_heap", but removes and returns the element at index 68 Similar to "pop_heap", but removes and returns the element at index
57 $index. 69 $index.
58 70
71 splice_heap_idx @heap, $index (\@$)
72 Just like "splice_heap", but updates the index (see INDEXED
73 OPERATIONS).
74
59 splice_heap_lex @heap, $index (\@$) 75 splice_heap_lex @heap, $index (\@$)
60 Just like "splice_heap", but in string comparison order instead of 76 Just like "splice_heap", but in string comparison order instead of
61 numerical comparison order. 77 numerical comparison order.
62 78
63 splice_heap_cmp { compare } @heap, $index (&\@$) 79 splice_heap_cmp { compare } @heap, $index (&\@$)
65 81
66 adjust_heap @heap, $index (\@$) 82 adjust_heap @heap, $index (\@$)
67 Assuming you have only changed the element at index $index, repair 83 Assuming you have only changed the element at index $index, repair
68 the heap again. Can be used to remove elements, replace elements, 84 the heap again. Can be used to remove elements, replace elements,
69 adjust the priority of elements and more. 85 adjust the priority of elements and more.
86
87 adjust_heap_idx @heap, $index (\@$)
88 Just like "adjust_heap", but updates the index (see INDEXED
89 OPERATIONS).
70 90
71 adjust_heap_lex @heap, $index (\@$) 91 adjust_heap_lex @heap, $index (\@$)
72 Just like "adjust_heap", but in string comparison order instead of 92 Just like "adjust_heap", but in string comparison order instead of
73 numerical comparison order. 93 numerical comparison order.
74 94
93 will sort according to the first element of the arrays, i.e. "1,2,3". 113 will sort according to the first element of the arrays, i.e. "1,2,3".
94 114
95 The custom comparison functions work similar to how "sort" works: $a and 115 The custom comparison functions work similar to how "sort" works: $a and
96 $b are set to the elements to be compared, and the result should be 116 $b are set to the elements to be compared, and the result should be
97 greater than zero then $a is greater than $b, 0 otherwise. This means 117 greater than zero then $a is greater than $b, 0 otherwise. This means
98 that you cna use the same function as for sorting the array, but you 118 that you can use the same function as for sorting the array, but you
99 could also use a simpler function that just does "$a > $b". 119 could also use a simpler function that just does "$a > $b".
100 120
101 The first example above corresponds to this comparison "function": 121 The first example above corresponds to this comparison "function":
102 122
103 { $a <=> $b } 123 { $a <=> $b }
106 126
107 { $a->[0] <=> $b->[0] } 127 { $a->[0] <=> $b->[0] }
108 128
109 Unlike "sort", the default sort is numerical and it is not possible to 129 Unlike "sort", the default sort is numerical and it is not possible to
110 use normal subroutines. 130 use normal subroutines.
131
132 INDEXED OPERATIONS
133 The functions whose names end in "_idx" also "update the index". That
134 means that all elements must be array refs, with the first element being
135 the heap value, and the second value being the array index:
136
137 [$value, $index, ...]
138
139 This allows you to quickly locate an element in the array when all you
140 have is the array reference.
111 141
112BUGS 142BUGS
113 * Numerical comparison is always done using floatingpoint, which 143 * Numerical comparison is always done using floatingpoint, which
114 usually has less precision than a 64 bit integer that perl might use 144 usually has less precision than a 64 bit integer that perl might use
115 for integers internally, resulting in precision loss on the built-in 145 for integers internally, resulting in precision loss on the built-in
120 150
121 * This module can leak memory (or worse) when your comparison function 151 * This module can leak memory (or worse) when your comparison function
122 exits unexpectedly (e.g. "last") or throws an exception, so do not 152 exits unexpectedly (e.g. "last") or throws an exception, so do not
123 do that. 153 do that.
124 154
125AUTHOR 155SEE ALSO
156 This module has a rather low-level interface. If it seems daunting, you
157 should have a look at Array::Heap::ModifiablePriorityQueue, which is
158 based on this module but provides more and higher-level operations with
159 an object-oriented API which makes it harder to make mistakes.
160
161 A slightly less flexible (only numeric weights), but also slightly
162 faster variant of that module can be found as
163 Array::Heap::PriorityQueue::Numeric on CPAN.
164
165AUTHOR AND CONTACT INFORMATION
126 Marc Lehmann <schmorp@schmorp.de> 166 Marc Lehmann <schmorp@schmorp.de>
127 http://home.schmorp.de/ 167 http://software.schmorp.de/pkg/Array-Heap
128 168

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines