libsaria: Store library path as a linked list of track tags
Lookup by id will be slighly slower, but now I will have one list for each path that can be merged together and sorted to represent the library. This sounds like a good tradeoff to me, especially since I can store an iterator to the current track when deciding what to play next. This will give me much faster access to song for the current track.
This commit is contained in:
parent
9c0e61b42d
commit
6d7828b946
|
@ -30,10 +30,11 @@ class TrackTag
|
|||
TrackTag();
|
||||
TrackTag(const TrackTag &);
|
||||
TrackTag(string, ino_t);
|
||||
TrackTag(InFile &, ino_t);
|
||||
TrackTag(InFile &);
|
||||
~TrackTag();
|
||||
void save(OutFile &);
|
||||
|
||||
ino_t get_inode();
|
||||
unsigned int get_track();
|
||||
string get_title();
|
||||
string get_lenstr();
|
||||
|
|
|
@ -7,12 +7,11 @@
|
|||
class Track
|
||||
{
|
||||
private:
|
||||
ino_t inode;
|
||||
TrackTag *tags;
|
||||
|
||||
public:
|
||||
Track();
|
||||
Track(ino_t, TrackTag *);
|
||||
Track(TrackTag *);
|
||||
~Track();
|
||||
|
||||
ino_t get_inode();
|
||||
|
|
|
@ -27,7 +27,7 @@ namespace libsaria
|
|||
|
||||
if (!library::get_info(inode, func)) {
|
||||
TrackTag tag(libsaria::audio::get_current_file(), inode);
|
||||
Track track(inode, &tag);
|
||||
Track track(&tag);
|
||||
func(track);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,14 +7,12 @@
|
|||
|
||||
void LibraryPath::save(OutFile &out)
|
||||
{
|
||||
map<ino_t, TrackTag>::iterator it;
|
||||
list<TrackTag>::iterator it;
|
||||
|
||||
out.write_str(path, false);
|
||||
out.write_lui(file_map.size(), true);
|
||||
for (it = file_map.begin(); it != file_map.end(); it++) {
|
||||
out.write_ino(it->first, false);
|
||||
it->second.save(out);
|
||||
}
|
||||
out.write_lui(file_list.size(), true);
|
||||
for (it = file_list.begin(); it != file_list.end(); it++)
|
||||
it->save(out);
|
||||
}
|
||||
|
||||
namespace libsaria
|
||||
|
|
|
@ -28,10 +28,10 @@ LibraryPath *get_library_path(string dir)
|
|||
|
||||
void LibraryPath::for_each(libsaria::SourceModel *model)
|
||||
{
|
||||
map<ino_t, TrackTag>::iterator it;
|
||||
list<TrackTag>::iterator it;
|
||||
|
||||
for (it = file_map.begin(); it != file_map.end(); it++) {
|
||||
Track track = Track(it->first, &it->second);
|
||||
for (it = file_list.begin(); it != file_list.end(); it++) {
|
||||
Track track = Track(&(*it));
|
||||
model->insert(track);
|
||||
}
|
||||
}
|
||||
|
@ -49,12 +49,22 @@ void LibraryPath::get_info(void (*info_func)(struct libsaria::library::PathInfo
|
|||
info_func(info);
|
||||
}
|
||||
|
||||
list<TrackTag>::iterator LibraryPath::find_id(ino_t &id)
|
||||
{
|
||||
list<TrackTag>::iterator it;
|
||||
for (it = file_list.begin(); it != file_list.end(); it++) {
|
||||
if (it->get_inode() == id)
|
||||
break;
|
||||
}
|
||||
return it;
|
||||
}
|
||||
|
||||
bool LibraryPath::get_info_id(ino_t &id, void (*func)(Track &))
|
||||
{
|
||||
map<ino_t, TrackTag>::iterator it;
|
||||
it = file_map.find(id);
|
||||
if (it != file_map.end()) {
|
||||
Track track = Track(it->first, &it->second);
|
||||
list<TrackTag>::iterator it;
|
||||
it = find_id(id);
|
||||
if (it != file_list.end()) {
|
||||
Track track = Track(&(*it));
|
||||
func(track);
|
||||
return true;
|
||||
}
|
||||
|
@ -63,11 +73,11 @@ bool LibraryPath::get_info_id(ino_t &id, void (*func)(Track &))
|
|||
|
||||
bool LibraryPath::play_id(ino_t &id)
|
||||
{
|
||||
map<ino_t, TrackTag>::iterator it;
|
||||
it = file_map.find(id);
|
||||
if (it == file_map.end())
|
||||
list<TrackTag>::iterator it;
|
||||
it = find_id(id);
|
||||
if (it == file_list.end())
|
||||
return false;
|
||||
libsaria::audio::load(it->second.get_filepath());
|
||||
libsaria::audio::load(it->get_filepath());
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
#ifndef LIBSARIA_LIBRARY_SOURCE_H
|
||||
#define LIBSARIA_LIBRARY_SOURCE_H
|
||||
|
||||
#include <map>
|
||||
#include <list>
|
||||
#include <string>
|
||||
using namespace std;
|
||||
|
||||
|
@ -13,7 +13,9 @@ class LibraryPath
|
|||
{
|
||||
private:
|
||||
string path;
|
||||
map <ino_t, TrackTag> file_map;
|
||||
list<TrackTag> file_list;
|
||||
|
||||
list<TrackTag>::iterator find_id(ino_t &);
|
||||
|
||||
public:
|
||||
LibraryPath(string);
|
||||
|
@ -24,7 +26,7 @@ class LibraryPath
|
|||
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 &);
|
||||
void insert_track(TrackTag &);
|
||||
bool play_id(ino_t &);
|
||||
void save(OutFile &);
|
||||
void update();
|
||||
|
|
|
@ -17,7 +17,6 @@ LibraryPath::LibraryPath(string dir)
|
|||
LibraryPath::LibraryPath(InFile &in, string dir)
|
||||
{
|
||||
unsigned int size;
|
||||
ino_t inode;
|
||||
|
||||
path = dir;
|
||||
size = in.read_lui();
|
||||
|
@ -25,10 +24,8 @@ LibraryPath::LibraryPath(InFile &in, string dir)
|
|||
print("Library path size: ");
|
||||
println(size);
|
||||
|
||||
for (unsigned int i = 0; i < size; i++) {
|
||||
inode = in.read_ino();
|
||||
file_map[inode] = TrackTag(in, inode);
|
||||
}
|
||||
for (unsigned int i = 0; i < size; i++)
|
||||
file_list.push_back(TrackTag(in));
|
||||
|
||||
}
|
||||
|
||||
|
@ -36,9 +33,9 @@ LibraryPath::~LibraryPath()
|
|||
{
|
||||
}
|
||||
|
||||
void LibraryPath::insert_track(ino_t &inode, TrackTag &tag)
|
||||
void LibraryPath::insert_track(TrackTag &tag)
|
||||
{
|
||||
file_map[inode] = tag;
|
||||
file_list.push_back(tag);
|
||||
}
|
||||
|
||||
void LibraryPath::update()
|
||||
|
@ -49,7 +46,7 @@ void LibraryPath::update()
|
|||
|
||||
unsigned int LibraryPath::size()
|
||||
{
|
||||
return file_map.size();
|
||||
return file_list.size();
|
||||
}
|
||||
|
||||
|
||||
|
@ -70,7 +67,7 @@ void ScanTask::tag_file(file filepath)
|
|||
{
|
||||
try {
|
||||
TrackTag tag(dir + "/" + filepath.name, filepath.d_ino);
|
||||
library->insert_track(filepath.d_ino, tag);
|
||||
library->insert_track(tag);
|
||||
} catch (string message) {
|
||||
println(message);
|
||||
}
|
||||
|
|
|
@ -63,8 +63,9 @@ TrackTag::TrackTag(string file, ino_t inode)
|
|||
make_lenstr();
|
||||
}
|
||||
|
||||
TrackTag::TrackTag(InFile &in, ino_t inode)
|
||||
TrackTag::TrackTag(InFile &in)
|
||||
{
|
||||
inode = in.read_ino();
|
||||
filepath = in.read_str();
|
||||
title = in.read_str();
|
||||
artist = in.read_str();
|
||||
|
@ -87,6 +88,7 @@ TrackTag::~TrackTag()
|
|||
|
||||
void TrackTag::save(OutFile &out)
|
||||
{
|
||||
out.write_lui(inode, false);
|
||||
out.write_str(filepath, false);
|
||||
out.write_str(title, false);
|
||||
out.write_str(artist, false);
|
||||
|
@ -118,6 +120,11 @@ void TrackTag::make_lenstr()
|
|||
lenstr = stream.str();
|
||||
}
|
||||
|
||||
ino_t TrackTag::get_inode()
|
||||
{
|
||||
return inode;
|
||||
}
|
||||
|
||||
unsigned int TrackTag::get_track()
|
||||
{
|
||||
return track;
|
||||
|
|
|
@ -4,14 +4,12 @@
|
|||
|
||||
Track::Track()
|
||||
{
|
||||
inode = 0;
|
||||
tags = NULL;
|
||||
}
|
||||
|
||||
Track::Track(ino_t ino, TrackTag *tag)
|
||||
Track::Track(TrackTag *tag)
|
||||
{
|
||||
inode = ino;
|
||||
tags = tag;
|
||||
tags = tag;
|
||||
}
|
||||
|
||||
Track::~Track()
|
||||
|
@ -20,7 +18,9 @@ Track::~Track()
|
|||
|
||||
ino_t Track::get_inode()
|
||||
{
|
||||
return inode;
|
||||
if (tags)
|
||||
return tags->get_inode();
|
||||
return 0;
|
||||
}
|
||||
|
||||
unsigned int Track::get_track()
|
||||
|
|
Loading…
Reference in New Issue