From: Eric Wong <e@80x24.org>
To: <dtas-all@nongnu.org>
Subject: [PATCH] tracklist: avoid needlessly building a hash for track IDs
Date: Sun, 6 Dec 2015 02:44:20 +0000 [thread overview]
Message-ID: <20151206024420.17146-1-e@80x24.org> (raw)
Building this hash is a linear operation anyways,
so there's no point in doing it when Array#index can stop
early if the track is found, to avoid unnecessary work.
---
lib/dtas/tracklist.rb | 36 +++++++++++++-----------------------
1 file changed, 13 insertions(+), 23 deletions(-)
diff --git a/lib/dtas/tracklist.rb b/lib/dtas/tracklist.rb
index 1750f26..1ae2719 100644
--- a/lib/dtas/tracklist.rb
+++ b/lib/dtas/tracklist.rb
@@ -56,25 +56,14 @@ class DTAS::Tracklist # :nodoc:
@pos = TL_DEFAULTS["pos"]
end
- def size
- @list.size
- end
-
- # caching this probably isn't worth it. a tracklist is usually
- # a few tens of tracks, maybe a hundred at most.
- def _track_id_map
- by_track_id = {}
- @list.each_with_index { |t,i| by_track_id[t.track_id] = i }
- by_track_id
- end
-
def get_tracks(track_ids)
- by_track_id = _track_id_map
- track_ids.map do |track_id|
- idx = by_track_id[track_id]
- # dtas-mpris fills in the metadata, we just return a path
- [ track_id, idx ? @list[idx].to_path : nil ]
+ want = {}
+ track_ids.each { |i| want[i] = i }
+ rv = []
+ @list.each do |t|
+ i = want[t.track_id] and rv << [ i, t.to_path ]
end
+ rv
end
def tracks
@@ -105,8 +94,7 @@ class DTAS::Tracklist # :nodoc:
def add_track(track, after_track_id = nil, set_as_current = false)
track = new_track(track)
if after_track_id
- by_track_id = _track_id_map
- idx = by_track_id[after_track_id] or
+ idx = _idx_of(after_track_id) or
raise ArgumentError, "after_track_id invalid"
@list[idx, 1] = [ @list[idx], track ]
if set_as_current
@@ -125,9 +113,12 @@ class DTAS::Tracklist # :nodoc:
track.track_id
end
+ def _idx_of(track_id)
+ @list.index { |t| t.track_id == track_id }
+ end
+
def remove_track(track_id)
- by_track_id = _track_id_map
- idx = by_track_id.delete(track_id) or return false
+ idx = _idx_of(track_id) or return false
track = @list.delete_at(idx)
len = @list.size
if @pos >= len
@@ -138,8 +129,7 @@ class DTAS::Tracklist # :nodoc:
end
def go_to(track_id, offset_hhmmss = nil)
- by_track_id = _track_id_map
- if idx = by_track_id[track_id]
+ if idx = _idx_of(track_id)
@goto_off = offset_hhmmss
return @list[@goto_pos = idx].to_path
end
--
EW
reply other threads:[~2015-12-06 2:44 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://80x24.org/dtas/README
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20151206024420.17146-1-e@80x24.org \
--to=e@80x24.org \
--cc=dtas-all@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://80x24.org/dtas.git/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).