From 8609b9a6a74c2349d29bf4ef11b8cb5502b9501b Mon Sep 17 00:00:00 2001 From: Bryan Schumaker Date: Sun, 6 Nov 2011 10:43:35 -0500 Subject: [PATCH] libsaria: Use a linked list of library paths I was using a map, but the map required duplicating the library path everywhere. Except for removing a path, I don't ever need to look up a specific path by name, so why bother optimizing this case? Iterating over a linked list should be more efficient (and easier to comprehend) --- libsaria/library/file.cpp | 16 +++++----- libsaria/library/library.cpp | 58 ++++++++++++++++++++---------------- libsaria/library/library.h | 3 +- libsaria/library/path.cpp | 20 +++++++------ 4 files changed, 52 insertions(+), 45 deletions(-) diff --git a/libsaria/library/file.cpp b/libsaria/library/file.cpp index 259f6ccb..5e537a64 100644 --- a/libsaria/library/file.cpp +++ b/libsaria/library/file.cpp @@ -23,7 +23,7 @@ namespace libsaria { unsigned int size; string dir; - map *path_map = get_library_map(); + list *path_list = get_library_paths(); InFile in("library.lib"); if (!in.good()) @@ -32,21 +32,19 @@ namespace libsaria for (unsigned int i = 0; i < size; i++) { dir = in.read_str(); - path_map->insert( - pair(dir, LibraryPath(in, dir)) - ); + path_list->push_back(LibraryPath(in, dir)); } } void library::save() { OutFile out("library.lib"); - map *path_map = get_library_map(); - map::iterator it; + list *path_list = get_library_paths(); + list::iterator it; - out.write_lui(path_map->size(), true); - for (it = path_map->begin(); it != path_map->end(); it++) - it->second.save(out); + out.write_lui(path_list->size(), true); + for (it = path_list->begin(); it != path_list->end(); it++) + it->save(out); } }; /* namespace: libsaria */ diff --git a/libsaria/library/library.cpp b/libsaria/library/library.cpp index 354469e8..46ac9c01 100644 --- a/libsaria/library/library.cpp +++ b/libsaria/library/library.cpp @@ -1,5 +1,5 @@ -#include +#include #include using namespace std; @@ -9,19 +9,20 @@ using namespace std; #include #include "library.h" -static map path_map; +static list path_list; -map *get_library_map() +list *get_library_paths() { - return &path_map; + return &path_list; } LibraryPath *get_library_path(string dir) { - map::iterator it; - it = path_map.find(dir); - if (it != path_map.end()) - return &it->second; + list::iterator it; + for (it = path_list.begin(); it != path_list.end(); it++) { + if (it->get_path() == dir) + return &(*it); + } return NULL; } @@ -35,6 +36,11 @@ void LibraryPath::for_each(libsaria::SourceModel *model) } } +string LibraryPath::get_path() +{ + return path; +} + void LibraryPath::get_info(void (*info_func)(struct libsaria::library::PathInfo &)) { struct libsaria::library::PathInfo info; @@ -70,30 +76,30 @@ namespace libsaria void library::for_each(SourceModel *model) { - map::iterator it; - for (it = path_map.begin(); it != path_map.end(); it++) - it->second.for_each(model); + list::iterator it; + for (it = path_list.begin(); it != path_list.end(); it++) + it->for_each(model); } void library::for_each_path(void (*info_func)(struct library::PathInfo &)) { - map::iterator it; - for (it = path_map.begin(); it != path_map.end(); it++) - it->second.get_info(info_func); + list::iterator it; + for (it = path_list.begin(); it != path_list.end(); it++) + it->get_info(info_func); } void library::update() { - map::iterator it; - for (it = path_map.begin(); it != path_map.end(); it++) - it->second.update(); + list::iterator it; + for (it = path_list.begin(); it != path_list.end(); it++) + it->update(); } void library::play_id(ino_t &id) { - map::iterator it; - for (it = path_map.begin(); it != path_map.end(); it++) { - if (it->second.play_id(id) == true) + list::iterator it; + for (it = path_list.begin(); it != path_list.end(); it++) { + if (it->play_id(id) == true) break; } } @@ -101,9 +107,9 @@ namespace libsaria bool library::get_info(ino_t &id, void (*func)(Track &)) { bool found = false; - map::iterator it; - for (it = path_map.begin(); it != path_map.end(); it++) { - if (it->second.get_info_id(id, func) == true) { + list::iterator it; + for (it = path_list.begin(); it != path_list.end(); it++) { + if (it->get_info_id(id, func) == true) { found = true; break; } @@ -114,9 +120,9 @@ namespace libsaria unsigned int library::size() { unsigned int size = 0; - map::iterator it; - for (it = path_map.begin(); it != path_map.end(); it++) - size += it->second.size(); + list::iterator it; + for (it = path_list.begin(); it != path_list.end(); it++) + size += it->size(); return size; } diff --git a/libsaria/library/library.h b/libsaria/library/library.h index c91220fd..5d3dbbbe 100644 --- a/libsaria/library/library.h +++ b/libsaria/library/library.h @@ -21,6 +21,7 @@ class LibraryPath ~LibraryPath(); void for_each(libsaria::SourceModel *); + string get_path(); void get_info(void (*)(struct libsaria::library::PathInfo &)); bool get_info_id(ino_t &, void (*)(Track &)); void insert_track(ino_t &, TrackTag &); @@ -44,7 +45,7 @@ class ScanTask : public IdleTask void run_task(); }; -map *get_library_map(); +list *get_library_paths(); LibraryPath *get_library_path(string); #endif /* LIBSARIA_LIBRARY_SOURCE_H */ diff --git a/libsaria/library/path.cpp b/libsaria/library/path.cpp index b58741bb..16bfcd69 100644 --- a/libsaria/library/path.cpp +++ b/libsaria/library/path.cpp @@ -115,20 +115,22 @@ namespace libsaria { void library::add_path(string dir) { - get_library_map()->insert( - pair(dir, LibraryPath(dir)) - ); + get_library_paths()->push_back(LibraryPath(dir)); update_path(dir); } void library::remove_path(string dir) { - map::iterator it; - it = get_library_map()->find(dir); - if (it != get_library_map()->end()) { - get_library_map()->erase(it); - save(); - refresh(); + list *path_list = get_library_paths(); + list::iterator it; + + for (it = path_list->begin(); it != path_list->end(); it++) { + if (it->get_path() == dir) { + path_list->erase(it); + save(); + refresh(); + return; + } } }