Splited apart some bookmark code

This is WIP on #96 as the code for handling the data is very similar
This commit is contained in:
Daniel Collin 2014-09-15 20:11:52 +02:00
parent 59363f452f
commit 9afba7fdd3

View File

@ -169,23 +169,22 @@ void TrackData_setActiveTrack(TrackData* trackData, int track)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool TrackData_hasBookmark(TrackData* trackData, int row) static bool hasMark(int* marks, int count, int row)
{ {
int middle, first, last; int middle, first, last;
int* bookmarks = trackData->bookmarks;
if (!bookmarks) if (!marks)
return false; return false;
first = 0; first = 0;
last = trackData->bookmarkCount - 1; last = count - 1;
middle = (first + last) / 2; middle = (first + last) / 2;
while (first <= last) while (first <= last)
{ {
if (bookmarks[middle] < row) if (marks[middle] < row)
first = middle + 1; first = middle + 1;
else if (bookmarks[middle] == row) else if (marks[middle] == row)
return true; return true;
else else
last = middle - 1; last = middle - 1;
@ -203,6 +202,8 @@ int compare(const void* a, const void* b)
return *(int*)a - *(int*)b; return *(int*)a - *(int*)b;
} }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static void sortArray(int* bookmarks, int count) static void sortArray(int* bookmarks, int count)
{ {
qsort(bookmarks, count, sizeof(int), compare); qsort(bookmarks, count, sizeof(int), compare);
@ -210,25 +211,26 @@ static void sortArray(int* bookmarks, int count)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void TrackData_toggleBookmark(TrackData* trackData, int row) void toogleMark(int** marksPtr, int* countPtr, int row)
{ {
int i, count = trackData->bookmarkCount; int i;
int* bookmarks = trackData->bookmarks; int* marks = *marksPtr;
int count = *countPtr;
if (!bookmarks) if (!marks)
{ {
bookmarks = trackData->bookmarks = malloc(sizeof(int)); *marksPtr = marks = malloc(sizeof(int));
*bookmarks = row; *marks = row;
trackData->bookmarkCount++; *countPtr = 1;
return; return;
} }
for (i = 0; i < count; ++i) for (i = 0; i < count; ++i)
{ {
if (bookmarks[i] == row) if (marks[i] == row)
{ {
bookmarks[i] = 0; marks[i] = 0;
sortArray(bookmarks, count); sortArray(marks, count);
return; return;
} }
} }
@ -237,63 +239,89 @@ void TrackData_toggleBookmark(TrackData* trackData, int row)
for (i = 0; i < count; ++i) for (i = 0; i < count; ++i)
{ {
if (bookmarks[i] == 0) if (marks[i] == 0)
{ {
bookmarks[i] = row; marks[i] = row;
sortArray(bookmarks, count); sortArray(marks, count);
return; return;
} }
} }
// no slot found so we will resize the array and add the bookmark at the end // no slot found so we will resize the array and add the bookmark at the end
bookmarks = trackData->bookmarks = realloc(bookmarks, sizeof(int) * (count + 1)); *marksPtr = marks = realloc(marks, sizeof(int) * (count + 1));
bookmarks[count] = row; marks[count] = row;
sortArray(bookmarks, count + 1); sortArray(marks, count + 1);
trackData->bookmarkCount = count + 1; *countPtr = count + 1;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static int getNextMark(const int* marks, int count, int row, int defValue)
{
int i;
if (!marks)
return defValue;
for (i = 0; i < count; ++i)
{
const int v = marks[i];
if (v > row)
return v;
}
return defValue;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static int getPrevMark(const int* marks, int count, int row, int defValue)
{
int i;
if (!marks)
return defValue;
for (i = count; i >= 0; --i)
{
const int v = marks[i];
if (v < row)
return v;
}
return defValue;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool TrackData_hasBookmark(TrackData* trackData, int row)
{
return hasMark(trackData->bookmarks, trackData->bookmarkCount, row);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void TrackData_toggleBookmark(TrackData* trackData, int row)
{
toogleMark(&trackData->bookmarks, &trackData->bookmarkCount, row);
} }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int TrackData_getNextBookmark(TrackData* trackData, int row) int TrackData_getNextBookmark(TrackData* trackData, int row)
{ {
int i, count = trackData->bookmarkCount; return getNextMark(trackData->bookmarks, trackData->bookmarkCount, row, trackData->endRow);
int* bookmarks = trackData->bookmarks;
if (!bookmarks)
return trackData->endRow;
for (i = 0; i < count; ++i)
{
const int v = bookmarks[i];
if (v > row)
return v;
}
return trackData->endRow;
} }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int TrackData_getPrevBookmark(TrackData* trackData, int row) int TrackData_getPrevBookmark(TrackData* trackData, int row)
{ {
int i, count = trackData->bookmarkCount - 1; return getPrevMark(trackData->bookmarks, trackData->bookmarkCount, row, trackData->startRow);
int* bookmarks = trackData->bookmarks;
if (!bookmarks)
return trackData->startRow;
for (i = count; i >= 0; --i)
{
const int v = bookmarks[i];
if (v < row)
return v;
}
return trackData->startRow;
} }