Switch HashTable to shared_ptr and add missing returns

Use std::shared_ptr<Track> in HashTable to avoid raw pointer ownership
issues. Also add missing return statements in Database ID lookup helpers.
This commit is contained in:
2026-06-25 11:33:29 +02:00
parent 00c8d9b843
commit 2849183f1f
4 changed files with 12 additions and 10 deletions
+3 -3
View File
@@ -53,8 +53,8 @@ class HashTable {
HashTable ( HashTable && ) noexcept = default; HashTable ( HashTable && ) noexcept = default;
HashTable &operator= ( HashTable && ) noexcept = default; HashTable &operator= ( HashTable && ) noexcept = default;
void ht_insert ( const std::string &key, Track* t ); void ht_insert ( const std::string &key, std::shared_ptr<Track> t );
Track* ht_lookup ( const std::string &key ) const; std::shared_ptr<Track> ht_lookup ( const std::string &key ) const;
bool ht_delete ( const std::string &key ); bool ht_delete ( const std::string &key );
std::vector<std::string> listAll () const; std::vector<std::string> listAll () const;
@@ -72,7 +72,7 @@ class HashTable {
struct Entry { struct Entry {
std::string key; std::string key;
uint64_t hash = 0; uint64_t hash = 0;
Track* track; std::shared_ptr<Track> track;
}; };
// helpers // helpers
+3 -1
View File
@@ -252,7 +252,7 @@ std::optional<int> db::getAlbumId( const std::string& albumTitle ) {
if (sqlite3_step( stmt ) == SQLITE_ROW ) { if (sqlite3_step( stmt ) == SQLITE_ROW ) {
id = sqlite3_column_int( stmt, 0 ); id = sqlite3_column_int( stmt, 0 );
} }
return id;
} }
std::optional<int> db::getArtistId( const std::string& artistName ) { std::optional<int> db::getArtistId( const std::string& artistName ) {
@@ -266,6 +266,7 @@ std::optional<int> db::getArtistId( const std::string& artistName ) {
if (sqlite3_step( stmt ) == SQLITE_ROW ) { if (sqlite3_step( stmt ) == SQLITE_ROW ) {
id = sqlite3_column_int( stmt, 0 ); id = sqlite3_column_int( stmt, 0 );
} }
return id;
} }
std::optional<int> db::getGenreId( const std::string& genre ) { std::optional<int> db::getGenreId( const std::string& genre ) {
@@ -279,4 +280,5 @@ std::optional<int> db::getGenreId( const std::string& genre ) {
if (sqlite3_step( stmt ) == SQLITE_ROW ) { if (sqlite3_step( stmt ) == SQLITE_ROW ) {
id = sqlite3_column_int( stmt, 0 ); id = sqlite3_column_int( stmt, 0 );
} }
return id;
} }
+3 -3
View File
@@ -108,7 +108,7 @@ std::ptrdiff_t ht::probe ( const std::string &key, uint64_t hash, bool insert )
#endif #endif
} // probe } // probe
void ht::ht_insert( const std::string& key, Track* t ) { void ht::ht_insert( const std::string& key, std::shared_ptr<Track> t ) {
//double loadFactor = static_cast<double> ( size_ ) / static_cast<double> ( ctrl_.size()); //double loadFactor = static_cast<double> ( size_ ) / static_cast<double> ( ctrl_.size());
if ( loadFactor_ > MAX_LOAD ) rehash( ctrl_.size() * 2 ); if ( loadFactor_ > MAX_LOAD ) rehash( ctrl_.size() * 2 );
@@ -121,7 +121,7 @@ void ht::ht_insert( const std::string& key, Track* t ) {
tmp.key = key; tmp.key = key;
tmp.hash = hash; tmp.hash = hash;
tmp.track = t; tmp.track = t;
entries_.at( idx ) = std::move( tmp ); entries_.at( idx ) = std::move(tmp);
++size_; ++size_;
} else { } else {
entries_.at( idx ).track = t; entries_.at( idx ).track = t;
@@ -129,7 +129,7 @@ void ht::ht_insert( const std::string& key, Track* t ) {
ctrl_.at( idx ) = fingerprint( hash ); ctrl_.at( idx ) = fingerprint( hash );
} }
Track* ht::ht_lookup( const std::string& key ) const { std::shared_ptr<Track> ht::ht_lookup( const std::string& key ) const {
uint64_t hash = hashString( key ); uint64_t hash = hashString( key );
auto idx = probe( key, hash, false ); auto idx = probe( key, hash, false );
if ( idx != -1 ) return entries_.at( static_cast<std::size_t> ( idx )).track; if ( idx != -1 ) return entries_.at( static_cast<std::size_t> ( idx )).track;
+3 -3
View File
@@ -61,8 +61,8 @@ void PlayerLocal::scanLibrary(){
void PlayerLocal::loadLibrary() { void PlayerLocal::loadLibrary() {
db_.fetchAll( library ); db_.fetchAll( library );
for ( auto t : library ) { for ( auto t : library ) {
tracks_.ht_insert(t.title.toCString(), &t ); tracks_.ht_insert(t.title.toCString(), std::make_shared<Track> (t) );
artists_.ht_insert( t.artist.toCString(), &t ); artists_.ht_insert( t.artist.toCString(), std::make_shared<Track> (t) );
albums_.ht_insert( t.album.toCString(), &t ); albums_.ht_insert( t.album.toCString(), std::make_shared<Track> (t) );
} }
} }