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;
|
unsigned int size;
|
||||||
string dir;
|
string dir;
|
||||||
map<string, LibraryPath> *path_map = get_library_map();
|
list<LibraryPath> *path_list = get_library_paths();
|
||||||
InFile in("library.lib");
|
InFile in("library.lib");
|
||||||
|
|
||||||
if (!in.good())
|
if (!in.good())
|
||||||
|
@ -32,21 +32,19 @@ namespace libsaria
|
||||||
|
|
||||||
for (unsigned int i = 0; i < size; i++) {
|
for (unsigned int i = 0; i < size; i++) {
|
||||||
dir = in.read_str();
|
dir = in.read_str();
|
||||||
path_map->insert(
|
path_list->push_back(LibraryPath(in, dir));
|
||||||
pair<string, LibraryPath>(dir, LibraryPath(in, dir))
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void library::save()
|
void library::save()
|
||||||
{
|
{
|
||||||
OutFile out("library.lib");
|
OutFile out("library.lib");
|
||||||
map<string, LibraryPath> *path_map = get_library_map();
|
list<LibraryPath> *path_list = get_library_paths();
|
||||||
map<string, LibraryPath>::iterator it;
|
list<LibraryPath>::iterator it;
|
||||||
|
|
||||||
out.write_lui(path_map->size(), true);
|
out.write_lui(path_list->size(), true);
|
||||||
for (it = path_map->begin(); it != path_map->end(); it++)
|
for (it = path_list->begin(); it != path_list->end(); it++)
|
||||||
it->second.save(out);
|
it->save(out);
|
||||||
}
|
}
|
||||||
|
|
||||||
}; /* namespace: libsaria */
|
}; /* namespace: libsaria */
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
|
|
||||||
#include <map>
|
#include <list>
|
||||||
#include <string>
|
#include <string>
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
@ -9,19 +9,20 @@ using namespace std;
|
||||||
#include <libsaria/library.h>
|
#include <libsaria/library.h>
|
||||||
#include "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)
|
LibraryPath *get_library_path(string dir)
|
||||||
{
|
{
|
||||||
map<string, LibraryPath>::iterator it;
|
list<LibraryPath>::iterator it;
|
||||||
it = path_map.find(dir);
|
for (it = path_list.begin(); it != path_list.end(); it++) {
|
||||||
if (it != path_map.end())
|
if (it->get_path() == dir)
|
||||||
return &it->second;
|
return &(*it);
|
||||||
|
}
|
||||||
return NULL;
|
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 &))
|
void LibraryPath::get_info(void (*info_func)(struct libsaria::library::PathInfo &))
|
||||||
{
|
{
|
||||||
struct libsaria::library::PathInfo info;
|
struct libsaria::library::PathInfo info;
|
||||||
|
@ -70,30 +76,30 @@ namespace libsaria
|
||||||
|
|
||||||
void library::for_each(SourceModel *model)
|
void library::for_each(SourceModel *model)
|
||||||
{
|
{
|
||||||
map<string, LibraryPath>::iterator it;
|
list<LibraryPath>::iterator it;
|
||||||
for (it = path_map.begin(); it != path_map.end(); it++)
|
for (it = path_list.begin(); it != path_list.end(); it++)
|
||||||
it->second.for_each(model);
|
it->for_each(model);
|
||||||
}
|
}
|
||||||
|
|
||||||
void library::for_each_path(void (*info_func)(struct library::PathInfo &))
|
void library::for_each_path(void (*info_func)(struct library::PathInfo &))
|
||||||
{
|
{
|
||||||
map<string, LibraryPath>::iterator it;
|
list<LibraryPath>::iterator it;
|
||||||
for (it = path_map.begin(); it != path_map.end(); it++)
|
for (it = path_list.begin(); it != path_list.end(); it++)
|
||||||
it->second.get_info(info_func);
|
it->get_info(info_func);
|
||||||
}
|
}
|
||||||
|
|
||||||
void library::update()
|
void library::update()
|
||||||
{
|
{
|
||||||
map<string, LibraryPath>::iterator it;
|
list<LibraryPath>::iterator it;
|
||||||
for (it = path_map.begin(); it != path_map.end(); it++)
|
for (it = path_list.begin(); it != path_list.end(); it++)
|
||||||
it->second.update();
|
it->update();
|
||||||
}
|
}
|
||||||
|
|
||||||
void library::play_id(ino_t &id)
|
void library::play_id(ino_t &id)
|
||||||
{
|
{
|
||||||
map<string, LibraryPath>::iterator it;
|
list<LibraryPath>::iterator it;
|
||||||
for (it = path_map.begin(); it != path_map.end(); it++) {
|
for (it = path_list.begin(); it != path_list.end(); it++) {
|
||||||
if (it->second.play_id(id) == true)
|
if (it->play_id(id) == true)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -101,9 +107,9 @@ namespace libsaria
|
||||||
bool library::get_info(ino_t &id, void (*func)(Track &))
|
bool library::get_info(ino_t &id, void (*func)(Track &))
|
||||||
{
|
{
|
||||||
bool found = false;
|
bool found = false;
|
||||||
map<string, LibraryPath>::iterator it;
|
list<LibraryPath>::iterator it;
|
||||||
for (it = path_map.begin(); it != path_map.end(); it++) {
|
for (it = path_list.begin(); it != path_list.end(); it++) {
|
||||||
if (it->second.get_info_id(id, func) == true) {
|
if (it->get_info_id(id, func) == true) {
|
||||||
found = true;
|
found = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -114,9 +120,9 @@ namespace libsaria
|
||||||
unsigned int library::size()
|
unsigned int library::size()
|
||||||
{
|
{
|
||||||
unsigned int size = 0;
|
unsigned int size = 0;
|
||||||
map<string, LibraryPath>::iterator it;
|
list<LibraryPath>::iterator it;
|
||||||
for (it = path_map.begin(); it != path_map.end(); it++)
|
for (it = path_list.begin(); it != path_list.end(); it++)
|
||||||
size += it->second.size();
|
size += it->size();
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -21,6 +21,7 @@ class LibraryPath
|
||||||
~LibraryPath();
|
~LibraryPath();
|
||||||
|
|
||||||
void for_each(libsaria::SourceModel *);
|
void for_each(libsaria::SourceModel *);
|
||||||
|
string get_path();
|
||||||
void get_info(void (*)(struct libsaria::library::PathInfo &));
|
void get_info(void (*)(struct libsaria::library::PathInfo &));
|
||||||
bool get_info_id(ino_t &, void (*)(Track &));
|
bool get_info_id(ino_t &, void (*)(Track &));
|
||||||
void insert_track(ino_t &, TrackTag &);
|
void insert_track(ino_t &, TrackTag &);
|
||||||
|
@ -44,7 +45,7 @@ class ScanTask : public IdleTask
|
||||||
void run_task();
|
void run_task();
|
||||||
};
|
};
|
||||||
|
|
||||||
map<string, LibraryPath> *get_library_map();
|
list<LibraryPath> *get_library_paths();
|
||||||
LibraryPath *get_library_path(string);
|
LibraryPath *get_library_path(string);
|
||||||
|
|
||||||
#endif /* LIBSARIA_LIBRARY_SOURCE_H */
|
#endif /* LIBSARIA_LIBRARY_SOURCE_H */
|
||||||
|
|
|
@ -115,20 +115,22 @@ namespace libsaria
|
||||||
{
|
{
|
||||||
void library::add_path(string dir)
|
void library::add_path(string dir)
|
||||||
{
|
{
|
||||||
get_library_map()->insert(
|
get_library_paths()->push_back(LibraryPath(dir));
|
||||||
pair<string, LibraryPath>(dir, LibraryPath(dir))
|
|
||||||
);
|
|
||||||
update_path(dir);
|
update_path(dir);
|
||||||
}
|
}
|
||||||
|
|
||||||
void library::remove_path(string dir)
|
void library::remove_path(string dir)
|
||||||
{
|
{
|
||||||
map<string, LibraryPath>::iterator it;
|
list<LibraryPath> *path_list = get_library_paths();
|
||||||
it = get_library_map()->find(dir);
|
list<LibraryPath>::iterator it;
|
||||||
if (it != get_library_map()->end()) {
|
|
||||||
get_library_map()->erase(it);
|
for (it = path_list->begin(); it != path_list->end(); it++) {
|
||||||
save();
|
if (it->get_path() == dir) {
|
||||||
refresh();
|
path_list->erase(it);
|
||||||
|
save();
|
||||||
|
refresh();
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue