Commit 40371895 authored by Srikanth Raju's avatar Srikanth Raju

ML: Use bsearch() instead of reinventing

parent 30834428
...@@ -60,9 +60,15 @@ static int CreateInputItemFromMedia( media_library_t *p_ml, ...@@ -60,9 +60,15 @@ static int CreateInputItemFromMedia( media_library_t *p_ml,
struct ml_table_elt struct ml_table_elt
{ {
int column_id; int column_id;
char column_name[20]; char* column_name;
}; };
static int compare_ml_elts( const void *a, const void *b )
{
return strcmp( ( (struct ml_table_elt* )a )->column_name,
( ( struct ml_table_elt* )b )->column_name );
}
static const struct ml_table_elt ml_table_map[]= static const struct ml_table_elt ml_table_map[]=
{ {
{ ML_ALBUM_COVER, "album_cover" }, { ML_ALBUM_COVER, "album_cover" },
...@@ -479,26 +485,15 @@ int SQLToMediaArray( media_library_t *p_ml, vlc_array_t *p_result_array, ...@@ -479,26 +485,15 @@ int SQLToMediaArray( media_library_t *p_ml, vlc_array_t *p_result_array,
const int count = sizeof( ml_table_map )/ sizeof( struct ml_table_elt ); const int count = sizeof( ml_table_map )/ sizeof( struct ml_table_elt );
for( int col = 0; col < i_cols; col++ ) for( int col = 0; col < i_cols; col++ )
{ {
//binary search struct ml_table_elt key, *result = NULL;
int low = 0, high = count - 1; key.column_name = res( 0, col );
int answer = -1; result = bsearch( &key, ml_table_map, count,
while( low <= high ) { sizeof( struct ml_table_elt ), compare_ml_elts );
int mid = (low + high ) / 2;
char* mid_val = ml_table_map[mid].column_name; if( !result )
int cmp = strcmp( mid_val, res( 0, col ) );
if( cmp < 0 )
low = mid + 1;
else if ( cmp > 0 )
high = mid - 1;
else
{
answer = mid; break;
}
}
if( answer == -1 )
msg_Warn( p_ml, "unknown column: %s", res( 0, col ) ); msg_Warn( p_ml, "unknown column: %s", res( 0, col ) );
else else
indexes[col] = ml_table_map[answer].column_id; indexes[col] = result->column_id;
} }
/* Read rows 1 to i_rows */ /* Read rows 1 to i_rows */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment