mirror of
https://github.com/GitJournal/GitJournal.git
synced 2025-06-30 19:36:25 +08:00
SortedNotesFolder: Massive optimization
Use a binary search instead of sequential
This commit is contained in:
@ -85,15 +85,28 @@ class SortedNotesFolder with NotesFolderNotifier implements NotesFolder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int _insertInCorrectPos(Note note) {
|
int _insertInCorrectPos(Note note) {
|
||||||
var i = 0;
|
var i = _getInsertPos(note, 0, _notes.length);
|
||||||
for (; i < _notes.length; i++) {
|
_notes.insert(i, note);
|
||||||
var n = _notes[i];
|
|
||||||
if (_sortFunc(n, note) > 0) {
|
return i;
|
||||||
break;
|
}
|
||||||
|
|
||||||
|
int _getInsertPos(Note note, int low, int high) {
|
||||||
|
int mid;
|
||||||
|
while (low <= high) {
|
||||||
|
mid = low + ((high - low) ~/ 2);
|
||||||
|
|
||||||
|
var r = _sortFunc(_notes[mid], note);
|
||||||
|
if (r == 0) {
|
||||||
|
return mid;
|
||||||
|
} else if (r < 0) {
|
||||||
|
return low = mid + 1;
|
||||||
|
} else {
|
||||||
|
return high = mid - 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_notes.insert(i, note);
|
|
||||||
return i;
|
return mid;
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
Reference in New Issue
Block a user