Commit graph

2 commits

Author SHA1 Message Date
William McPherson aa8b40dce6 Shell: Cache PATH for faster tab completion
This patch reduces the O(n) tab completion to something like O(log(n)).
The cache is just a sorted vector of strings and we binary search it to
get a string matching our input, and then check the surrounding strings
to see if we need to remove any characters. Also we no longer stat each
file every time.

Also added an #include in BinarySearch since it was using size_t. Oops.

If `export` is called, we recache. Need to implement the `hash` builtin
for when an executable has been added to a directory in PATH.
2019-12-05 17:09:22 +01:00
William McPherson d7177212fd AK: Add a BinarySearch template implementation
binary_search takes a haystack, a size, a needle and a compare function.
The compare function should return negative if a < b, positive if a > b
and 0 if a == b. The "sane default" compare function is integral_compare
which implements this with subtraction a - b.
binary_search returns a pointer to a matching element, NOT necessarily
the FIRST matching element. It returns a nullptr if the element was not
found.

This patch includes tests for binary_search.
2019-12-02 15:30:45 +01:00