Skip to content
Tcl Source Code View Ticket
Login

View Ticket

lsearch fuller functionality

Closed / Rejected689652ffffffffffffffffffffffffffffffffffCreated 24 years agoUpdated 20 years ago
Descriptionplain2003-02-19 23:06:10
Recent versions of Tcl introduced the '-sorted', 
and '-all' flags to lsearch (with associated flags '-
ascii', '-dictionary', 'integer', etc).

However, many combinations are disallowed, and 
some of the implementation could be made more 
efficient.

For example 'lsearch -sorted -all' doesn't do a 
binary search, claiming (in the code) "note that 
there is no point in being smart when -all was 
specified; in that case, we have to look at all items 
anyway".  But that isn't true, one could do a forward 
and a backward binary search and then return the 
remaining range (or do a forward binary search and 
then a forward negated binary search from that 
point).

Another problem is that one can't use any form of 
pattern with '-sorted', which would be very useful. 
For example these useful examples are illegal:

lsearch -sorted -glob -- "aaaa aabb aabc aacc 
abcc" "aab*"

would (if the list was much longer, like a 
dictionary) return 1, and:

lsearch -sorted -glob -all -- "aaaa aabb aabc aacc 
abcc" "aab*"

would return "1 2" (and with '-inline' this would be 
a nice way of efficiently returning just the 
relevant 'lrange').

Finally, I point out that several examples at the 
bottom of the lsearch man page use a '-data' 
option which is not in Tcl at all!
vincentdarleyplain2003-02-20 16:01:01
Logged In: YES 
user_id=32170

One more comment, 'lsearch' always searches from the 
front of the list.  Why not '-last' (or -reverse) flag?
dkfplain2003-02-20 22:46:45
Logged In: YES 
user_id=79902

Jeff got rid of that doc bug last August. :^)
vincentdarleyplain2003-02-27 19:47:28
Logged In: YES 
user_id=32170

Comments from tcl-core:

On Thu, 27 Feb 2003, Michael Schlenker wrote:
> Looks also reasonable, but I note, that your "illegal" -
sorted + pattern
> examples do work...

Indeed, now I see they do, but they *ignore* the '-
sorted' flag.  This
contradicts what is in the man page, where '-sorted' is 
explicitly
mutually exclusive with -glob and -regexp.  I think an 
error should be
thrown for illegal flag combinations.

The man page really ought to specify what happens in 
this case, and also
in the case where the list isn't sorted but '-sorted' is 
given (even if
only to say the behaviour is undefined).

Currently using '-all' tends to hide all these problems 
because the
implementation with -all is brute force (I doesn't use 
the fact that
-sorted could still provide a benefit).
dkf Closed ticket [689652ffff]. [History diff]2003-02-27 23:31:53
dkfplain2003-02-27 23:31:54
Logged In: YES 
user_id=79902

You *can't* mix glob matching and specialised sorting
handling.  This is because you can only tell whether a glob
pattern matches or not, and not whether there cannot be a
match before the given entry because of that.  Consider the
glob pattern *[abyz]?[cdx]* - how could you even begin to
combine that with an ordering criterion?!

Similar problems with -regexp, of course.

With -all, you have a similar thing in that it is really
designed for use with -glob and -regexp.  Is the case of an
exact search of sorted non-unique list all that common?

Maybe could do with a new -prefix option; prefix matching
*would* be compatible with -sorted.  Backwards searching
would be another nice feature.  Please open a new FRQ or
submit a TIP if you want to take them further.
dkf Ticket [689652ffff]: 1 change [History diff]2007-01-14 22:36:21
Log in (anonymous is fine) to add a comment.