Level 2: Identical to level 1 only faster. The array with sorted keys
and hash lookup table are only recreated when a new key is added,
a value is changed, or the sort routine is changed.
That's not necessarely faster. It depends on how often you are inserting
and how often you are asking for the keys. What you could do is some
form of hybrid:
- Create an array @sorted, and a variable $clean, initially set to 0.
- If FIRSTKEY is called, check the value of $clean. If it's true,
@sorted contains the sorted keys. If false, fill @sorted with
the sorted keys of the hash, and set $clean to 1.
- If you insert a key in the hash, or change the sort order, or
delete something from the hash, set $clean to 0.
- This might cause some bizarre results if you modify a hash while
iterating over it, but that may happen with normal hashes too.
Deleting a key doesn't require a rebuild because the element is deleted
from the array and the lookup hash.
Are you aware that deleting an element from an array can take time
linear in the size of the array?
Abigail
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|