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)
This commit is contained in:
parent
6930c1ccf3
commit
8609b9a6a7
|
@ -23,7 +23,7 @@ namespace libsaria
|
|||
{
|
||||
unsigned int size;
|
||||
string dir;
|
||||
map<string, LibraryPath> *path_map = get_library_map();
|
||||
list<LibraryPath> *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<string, LibraryPath>(dir, LibraryPath(in, dir))
|
||||
);
|
||||
path_list->push_back(LibraryPath(in, dir));
|
||||
}
|
||||
}
|
||||
|
||||
void library::save()
|
||||
{
|
||||
OutFile out("library.lib");
|
||||
map<string, LibraryPath> *path_map = get_library_map();
|
||||
map<string, LibraryPath>::iterator it;
|
||||
list<LibraryPath> *path_list = get_library_paths();
|
||||
list<LibraryPath>::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 */
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
|
||||
#include <map>
|
||||
#include <list>
|
||||
#include <string>
|
||||
using namespace std;
|
||||
|
||||
|
@ -9,19 +9,20 @@ using namespace std;
|
|||
#include <libsaria/library.h>
|
||||
#include "library.h"
|
||||
|
||||
static map<string, LibraryPath> path_map;
|
||||
static list<LibraryPath> path_list;
|
||||
|
||||
map<string, LibraryPath> *get_library_map()
|
||||
list<LibraryPath> *get_library_paths()
|
||||
{
|
||||
return &path_map;
|
||||
return &path_list;
|
||||
}
|
||||
|
||||
LibraryPath *get_library_path(string dir)
|
||||
{
|
||||
map<string, LibraryPath>::iterator it;
|
||||
it = path_map.find(dir);
|
||||
if (it != path_map.end())
|
||||
return &it->second;
|
||||
list<LibraryPath>::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<string, LibraryPath>::iterator it;
|
||||
for (it = path_map.begin(); it != path_map.end(); it++)
|
||||
it->second.for_each(model);
|
||||
list<LibraryPath>::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<string, LibraryPath>::iterator it;
|
||||
for (it = path_map.begin(); it != path_map.end(); it++)
|
||||
it->second.get_info(info_func);
|
||||
list<LibraryPath>::iterator it;
|
||||
for (it = path_list.begin(); it != path_list.end(); it++)
|
||||
it->get_info(info_func);
|
||||
}
|
||||
|
||||
void library::update()
|
||||
{
|
||||
map<string, LibraryPath>::iterator it;
|
||||
for (it = path_map.begin(); it != path_map.end(); it++)
|
||||
it->second.update();
|
||||
list<LibraryPath>::iterator it;
|
||||
for (it = path_list.begin(); it != path_list.end(); it++)
|
||||
it->update();
|
||||
}
|
||||
|
||||
void library::play_id(ino_t &id)
|
||||
{
|
||||
map<string, LibraryPath>::iterator it;
|
||||
for (it = path_map.begin(); it != path_map.end(); it++) {
|
||||
if (it->second.play_id(id) == true)
|
||||
list<LibraryPath>::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<string, LibraryPath>::iterator it;
|
||||
for (it = path_map.begin(); it != path_map.end(); it++) {
|
||||
if (it->second.get_info_id(id, func) == true) {
|
||||
list<LibraryPath>::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<string, LibraryPath>::iterator it;
|
||||
for (it = path_map.begin(); it != path_map.end(); it++)
|
||||
size += it->second.size();
|
||||
list<LibraryPath>::iterator it;
|
||||
for (it = path_list.begin(); it != path_list.end(); it++)
|
||||
size += it->size();
|
||||
return size;
|
||||
}
|
||||
|
||||
|
|
|
@ -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<string, LibraryPath> *get_library_map();
|
||||
list<LibraryPath> *get_library_paths();
|
||||
LibraryPath *get_library_path(string);
|
||||
|
||||
#endif /* LIBSARIA_LIBRARY_SOURCE_H */
|
||||
|
|
|
@ -115,20 +115,22 @@ namespace libsaria
|
|||
{
|
||||
void library::add_path(string dir)
|
||||
{
|
||||
get_library_map()->insert(
|
||||
pair<string, LibraryPath>(dir, LibraryPath(dir))
|
||||
);
|
||||
get_library_paths()->push_back(LibraryPath(dir));
|
||||
update_path(dir);
|
||||
}
|
||||
|
||||
void library::remove_path(string dir)
|
||||
{
|
||||
map<string, LibraryPath>::iterator it;
|
||||
it = get_library_map()->find(dir);
|
||||
if (it != get_library_map()->end()) {
|
||||
get_library_map()->erase(it);
|
||||
save();
|
||||
refresh();
|
||||
list<LibraryPath> *path_list = get_library_paths();
|
||||
list<LibraryPath>::iterator it;
|
||||
|
||||
for (it = path_list->begin(); it != path_list->end(); it++) {
|
||||
if (it->get_path() == dir) {
|
||||
path_list->erase(it);
|
||||
save();
|
||||
refresh();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue