Bug 883173 - Part 2: Add insertion sorting for TextTrackCueList::AddCue. r=rillian

- We expect most files to already be sorted, so an insertion sort starting from
the end of the current array should be more efficient than a general sort step
after all cues are loaded
- Added a test for insertion sorting as well.
This commit is contained in:
Rick Eyre 2013-06-18 13:52:10 -04:00
Родитель 04138a1f0d
Коммит ed18423fba
5 изменённых файлов: 97 добавлений и 1 удалений

Просмотреть файл

@ -10,6 +10,20 @@
namespace mozilla {
namespace dom {
class CompareCuesByTime
{
public:
bool Equals(TextTrackCue* aOne, TextTrackCue* aTwo) const {
return aOne->StartTime() == aTwo->StartTime() &&
aOne->EndTime() == aTwo->EndTime();
}
bool LessThan(TextTrackCue* aOne, TextTrackCue* aTwo) const {
return aOne->StartTime() < aTwo->StartTime() ||
(aOne->StartTime() == aTwo->StartTime() &&
aOne->EndTime() < aTwo->EndTime());
}
};
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_2(TextTrackCueList, mParent, mList)
NS_IMPL_CYCLE_COLLECTING_ADDREF(TextTrackCueList)
@ -69,7 +83,7 @@ TextTrackCueList::AddCue(TextTrackCue& aCue)
if (mList.Contains(&aCue)) {
return;
}
mList.AppendElement(&aCue);
mList.InsertElementSorted(&aCue, CompareCuesByTime());
}
void

Просмотреть файл

@ -47,12 +47,18 @@ public:
TextTrackCue* IndexedGetter(uint32_t aIndex, bool& aFound);
TextTrackCue* GetCueById(const nsAString& aId);
// Adds a cue to mList by performing an insertion sort on mList.
// We expect most files to already be sorted, so an insertion sort starting
// from the end of the current array should be more efficient than a general
// sort step after all cues are loaded.
void AddCue(TextTrackCue& aCue);
void RemoveCue(TextTrackCue& aCue, ErrorResult& aRv);
private:
nsCOMPtr<nsISupports> mParent;
// A sorted list of TextTrackCues sorted by earliest start time. If the start
// times are equal then it will be sorted by end time, earliest first.
nsTArray< nsRefPtr<TextTrackCue> > mList;
};

Просмотреть файл

@ -148,6 +148,7 @@ MOCHITEST_FILES = \
test_webvtt_disabled.html \
test_bug895305.html \
test_bug895091.html \
test_bug883173.html \
$(NULL)
# Don't run in suite
@ -275,6 +276,7 @@ MOCHITEST_FILES += \
basic.vtt \
region.vtt \
long.vtt \
bug883173.vtt \
$(NULL)
# Wave sample files

Просмотреть файл

@ -0,0 +1,16 @@
WEBVTT
00:03.000 --> 00:04.000
Should display fifth.
00:01.000 --> 00:02.000
Should display first.
00:01.000 --> 00:03.000
Should display second.
00:02.000 --> 00:04.000
Should display forth.
00:02.000 --> 00:03.000
Should display third.

Просмотреть файл

@ -0,0 +1,58 @@
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=883173
-->
<head>
<meta charset='utf-8'>
<title>Test for Bug 883173 - TextTrackCue(List) Sorting</title>
<script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<p id="display"></p>
<div id="content">
</div>
<pre id="test">
<script class="testbody" type="text/javascript">
SimpleTest.waitForExplicitFinish();
SpecialPowers.pushPrefEnv({"set": [["media.webvtt.enabled", true]]},
function() {
var video = document.createElement("video");
video.src = "seek.webm";
video.preload = "auto";
var trackElement = document.createElement("track");
trackElement.src = "bug883173.vtt";
trackElement.kind = "subtitles";
document.getElementById("content").appendChild(video);
video.appendChild(trackElement);
video.addEventListener("loadedmetadata",
function run_tests() {
// Re-queue run_tests() at the end of the event loop until the track
// element has loaded its data.
if (trackElement.readyState == 1) {
setTimeout(run_tests, 0);
return;
}
is(trackElement.readyState, 2, "Track::ReadyState should be set to LOADED.");
var expected = [[1, 2], [1, 3], [2, 3], [2, 4], [3, 4]];
var cueList = trackElement.track.cues;
is(cueList.length, expected.length, "Cue list length should be 5.");
for (var i = 0; i < expected.length; i++) {
is(cueList[i].startTime, expected[i][0], "Cue's start time should be " + expected[i][0]);
is(cueList[i].endTime, expected[i][1], "Cue's end time should be " + expected[i][1]);
}
SimpleTest.finish();
}
);
}
);
</script>
</pre>
</body>
</html>