diff options
author | Jörg Frings-Fürst <debian@jff-webhosting.net> | 2014-07-23 09:06:59 +0200 |
---|---|---|
committer | Jörg Frings-Fürst <debian@jff-webhosting.net> | 2014-07-23 09:06:59 +0200 |
commit | 4ea2cc3bd4a7d9b1c54a9d33e6a1cf82e7c8c21d (patch) | |
tree | d2e54377d14d604356c86862a326f64ae64dadd6 /src/tags/HierarchicalTagIndex.vala |
Imported Upstream version 0.18.1upstream/0.18.1
Diffstat (limited to 'src/tags/HierarchicalTagIndex.vala')
-rw-r--r-- | src/tags/HierarchicalTagIndex.vala | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/src/tags/HierarchicalTagIndex.vala b/src/tags/HierarchicalTagIndex.vala new file mode 100644 index 0000000..58b5f89 --- /dev/null +++ b/src/tags/HierarchicalTagIndex.vala @@ -0,0 +1,90 @@ +/* Copyright 2011-2014 Yorba Foundation + * + * This software is licensed under the GNU LGPL (version 2.1 or later). + * See the COPYING file in this distribution. + */ + +public class HierarchicalTagIndex { + private Gee.Map<string, Gee.Collection<string>> tag_table; + private Gee.SortedSet<string> known_paths; + + public HierarchicalTagIndex( ) { + this.tag_table = new Gee.HashMap<string, Gee.ArrayList<string>>(); + this.known_paths = new Gee.TreeSet<string>(); + } + + public static HierarchicalTagIndex from_paths(Gee.Collection<string> client_paths) { + Gee.Collection<string> paths = client_paths.read_only_view; + + HierarchicalTagIndex result = new HierarchicalTagIndex(); + + foreach (string path in paths) { + if (path.has_prefix(Tag.PATH_SEPARATOR_STRING)) { + Gee.Collection<string> components = + HierarchicalTagUtilities.enumerate_path_components(path); + + foreach (string component in components) + result.add_path(component, path); + } else { + result.add_path(path, path); + } + } + + return result; + } + + public static HierarchicalTagIndex get_global_index() { + return HierarchicalTagIndex.from_paths(Tag.global.get_all_names()); + } + + public void add_path(string tag, string path) { + if (!tag_table.has_key(tag)) { + tag_table.set(tag, new Gee.ArrayList<string>()); + } + + tag_table.get(tag).add(path); + known_paths.add(path); + } + + public Gee.Collection<string> get_all_paths() { + return known_paths.read_only_view; + } + + public bool is_tag_in_index(string tag) { + return tag_table.has_key(tag); + } + + public Gee.Collection<string> get_all_tags() { + return tag_table.keys; + } + + public bool is_path_known(string path) { + return known_paths.contains(path); + } + + public string get_path_for_name(string name) { + if (!is_tag_in_index(name)) + return name; + + Gee.Collection<string> paths = tag_table.get(name); + foreach (string path in paths) { + Gee.List<string> components = HierarchicalTagUtilities.enumerate_path_components(path); + if (components.get(components.size - 1) == name) { + return path; + } + } + + assert_not_reached(); + } + + public string[] get_paths_for_names_array(string[] names) { + string[] result = new string[0]; + + foreach (string name in names) + result += get_path_for_name(name); + + return result; + } + +} + |