aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2017-08-05 16:47:14 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2017-08-05 16:47:14 +0200
commitd9f0bc64898a207e5b15da443a92cc6c51ded419 (patch)
tree77128deaf268c2039897a31d1580bccbe3704c6f
parent6b3818fe1c4ae8f2009e8f0d25bfea3864a91748 (diff)
Add path_map::find_sub()
-rw-r--r--libbutl/path-map.hxx35
1 files changed, 32 insertions, 3 deletions
diff --git a/libbutl/path-map.hxx b/libbutl/path-map.hxx
index 7a966ab..5f282fb 100644
--- a/libbutl/path-map.hxx
+++ b/libbutl/path-map.hxx
@@ -109,12 +109,41 @@ namespace butl
// Note that the delimiter character is not used (is_delimiter() from
// path_traits is used instead).
//
+ template <typename P, typename T>
+ struct path_map_impl: prefix_map<P, T, P::traits::directory_separator>
+ {
+ using base = prefix_map<P, T, P::traits::directory_separator>;
+ using base::base;
+
+ using iterator = typename base::iterator;
+ using const_iterator = typename base::const_iterator;
+
+ // Find the most qualified entry of which this path is a sub-path.
+ //
+ iterator
+ find_sub (const P& p)
+ {
+ // Get the greatest less than, if any. We might still not be a sub. Note
+ // also that we still have to check the last element if upper_bound()
+ // returned end().
+ //
+ auto i (this->upper_bound (p));
+ return i == this->begin () || !p.sub ((--i)->first) ? this->end () : i;
+ }
+
+ const_iterator
+ find_sub (const P& p) const
+ {
+ auto i (this->upper_bound (p));
+ return i == this->begin () || !p.sub ((--i)->first) ? this->end () : i;
+ }
+ };
+
template <typename T>
- using path_map = prefix_map<path, T, path::traits::directory_separator>;
+ using path_map = path_map_impl<path, T>;
template <typename T>
- using dir_path_map =
- prefix_map<dir_path, T, dir_path::traits::directory_separator>;
+ using dir_path_map = path_map_impl<dir_path, T>;
}
#endif // LIBBUTL_PATH_MAP_HXX