Commit Graph

4 Commits

Author SHA1 Message Date
Zoltan Somogyi
6b9afbdd9f Significantly improve psqueue.m.
library/psqueue.m:
    Fix a bug: the adjust predicate was documented to fail if asked to adjust
    the priority of a nonexistent key, but it succeeded anyway.

    Fix a bug: the function that computed the size of a psqueue did not count
    the priority/key pair in the winner structure.

    Make the at_most predicate significantly more efficient, by eliminating
    an expensive conversion to the min view. Most of the contents of the min
    view was not needed. In Haskell (the language used in the paper that
    this module is derived from), computing stuff that won't be looked at
    is cheap due to lazyness; in Mercury, it can be expensive.

    Make the at_most predicate significantly more efficient in another respect
    as well, by using cords to avoid re-re-re-appending the same elements.

    Make most other predicates more efficient by eliminating the redundant
    checking for empty queues. For example, when you convert a nonempty queue
    to the tournament view, you shouldn't need to check whether the result
    is a tournament with no players; it will not be. This change required
    two main changes in data structures. The first is to separate out
    the concepts of psqueues that may be empty from those which may not.
    The second is updating the tournament view to eliminate the possibility
    of no players, and handling empty psqueues *without* converting them
    to the tournament view.

    Document the meanings of the module's data structures, both original
    and updated, including their invariants (at least, the invariants
    I can see).

    Delete the type for min views, since after the performance fix to at_most,
    it is no longer needed.

    Delete the type for tree views, since it is isomorphic to the actual
    data structure, and conversions to it just waste time.

    Delete the t_ prefix from the names of types.

    Change the documentation of predicates to use full sentences, not just
    sentence fragments.

    Do not include the predicates intended only for unit tests in the
    publicly documented part of the interface.

    Use the P, K order of type variables CONSISTENTLY. Change the order
    of fields representing priority/key pairs in structures likewise.

    Use consistent naming schemes for variables: PSQx for psqueues, LTreex
    for ltrees, etc. Use xPrime instead of x0 for variables bound in the
    conditions of if-then-elses. Use Maybe as a prefix on the names of
    variables of maybe types.

    Avoid the use of generic variable names such as "Res"; use names that
    reflect the value being returned instead.

    Avoid the use of numeric suffixes on variables when these do NOT denote
    progression over time; use A and B, or L and R, suffixes instead.

    Give some function symbols and predicates more meaningful names.

    Internal operations do not need to be available as both functions
    and predicates; pick whichever seems more appropriate, and remove
    the other. Remove some other unused functions, such as construct_leaf.

    Factor out some common code, e.g. for updating minimums and maximums.

    Remove redundant "is det" declarations from functions.

    Fix the type specializations. Specializing a predicate is useless
    unless its caller either always calls it with values of the specialized
    type, or is itself specialized the same way. This module needs the latter,
    so add type specializations to all predicates between the exported
    predicates and the primitives that can directly benefit from the
    specializations.

tests/hard_coded/psqueue_test.{m,exp}:
    Make this test case significantly harder. The old version did not
    pick up the two bugs referred to above, but the new version does.
2014-12-12 17:42:39 +11:00
Paul Bone
4700b73596 Update psqueue.m's interface to match other library modules.
library/psqueue.m:
    Make the psqueue interface more consistent with other standard library
    modules.

tests/hard_coded/psqueue_test.exp:
tests/hard_coded/psqueue_test.m:
    Update tests.
2014-12-07 23:15:28 +11:00
Paul Bone
8df5533ed5 Update psqueue.m code style.
library/psqueue.m:
    Update coding style.

    In min_view/1 there's two variable names are incorrect (they should be
    swapped) this doesn't change the program but may confuse people reading
    the code.

tests/hard_coded/psqueue_test.exp:
tests/hard_coded/psqueue_test.m:
    Also improve the coding style in the test code.
    Add lables to all of the test outputs.
2014-12-07 23:14:35 +11:00
Matthias Güdemann
02c6c4f155 Add priority search queue ADT
This implements a priority search queue ADT. This is a kind of blend
between priority queues and search trees; in contrast to a priority
queue, it also allows changing priorities of keys, looking up keys and
deleting keys.

This is an implementation based on the ICFP 2001 paper "A Simple
Implementation Technique for Priority Search Queues" by Ralf Hinze.
http://www.mercurylang.org/list-archives/users/2014-October/007804.html

Commit message paraphrased from Matthias' e-mail.
http://www.mercurylang.org/list-archives/reviews/2014-October/017414.html

library/psqueue.m:
    Add new module.

library/MODULES_DOC:
library/library.m:
    Include new module.

tests/hard_coded/Mmakefile:
tests/hard_coded/psqueue_test.exp:
tests/hard_coded/psqueue_test.m:
    Add test for psqueue.m
2014-12-07 21:54:07 +11:00