diff options
author | David Robillard <d@drobilla.net> | 2011-02-11 18:15:18 +0000 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2011-02-11 18:15:18 +0000 |
commit | 4861238c1624a193e30fc0aa78438b4977dbeb82 (patch) | |
tree | 3a740862ccb973833e4c2638010d4bbe00ac0a67 /src/collections.c | |
parent | 9749ab338c735f777be4fe6dc89082ea30044df1 (diff) | |
download | lilv-4861238c1624a193e30fc0aa78438b4977dbeb82.tar.gz lilv-4861238c1624a193e30fc0aa78438b4977dbeb82.tar.bz2 lilv-4861238c1624a193e30fc0aa78438b4977dbeb82.zip |
Use GSequence for collections with get_by_uri search methods.
Avoid constant sorting during plugin discovery.
git-svn-id: http://svn.drobilla.net/lad/trunk/slv2@2919 a436a847-0d15-0410-975c-d299462d15a1
Diffstat (limited to 'src/collections.c')
-rw-r--r-- | src/collections.c | 117 |
1 files changed, 52 insertions, 65 deletions
diff --git a/src/collections.c b/src/collections.c index 7e778a6..83c98f3 100644 --- a/src/collections.c +++ b/src/collections.c @@ -25,7 +25,9 @@ #include "slv2/pluginui.h" #include "slv2_internal.h" -#define SLV2_COLLECTION_IMPL(CollType, ElemType, prefix, free_func) \ +/* ARRAYS */ + +#define SLV2_ARRAY_IMPL(CollType, ElemType, prefix, free_func) \ \ CollType \ prefix ## _new() \ @@ -58,47 +60,63 @@ prefix ## _get_at(CollType coll, unsigned index) \ return (ElemType)g_ptr_array_index((GPtrArray*)coll, (int)index); \ } -SLV2_COLLECTION_IMPL(SLV2PluginClasses, SLV2PluginClass, - slv2_plugin_classes, &slv2_plugin_class_free) -SLV2_COLLECTION_IMPL(SLV2ScalePoints, SLV2ScalePoint, +SLV2_ARRAY_IMPL(SLV2ScalePoints, SLV2ScalePoint, slv2_scale_points, &slv2_scale_point_free) -SLV2_COLLECTION_IMPL(SLV2Values, SLV2Value, +SLV2_ARRAY_IMPL(SLV2Values, SLV2Value, slv2_values, &slv2_value_free) -SLV2_COLLECTION_IMPL(SLV2UIs, SLV2UI, - slv2_uis, &slv2_ui_free) - -/* **** PLUGIN CLASSES **** */ - -SLV2_API -SLV2PluginClass -slv2_plugin_classes_get_by_uri(SLV2PluginClasses list, SLV2Value uri) -{ - // good old fashioned binary search - int lower = 0; - int upper = ((GPtrArray*)list)->len - 1; - int i; - while (upper >= lower) { - i = lower + ((upper - lower) / 2); +/* SEQUENCE */ - SLV2PluginClass p = g_ptr_array_index(((GPtrArray*)list), i); - - const int cmp = strcmp(slv2_value_as_uri(slv2_plugin_class_get_uri(p)), - slv2_value_as_uri(uri)); +#define SLV2_SEQUENCE_IMPL(CollType, ElemType, prefix, free_func) \ +\ +CollType \ +prefix ## _new() \ +{ \ + return g_sequence_new((GDestroyNotify)free_func); \ +} \ +\ +SLV2_API \ +void \ +prefix ## _free(CollType coll) \ +{ \ + if (coll) \ + g_sequence_free((GSequence*)coll); \ +} \ +\ +SLV2_API \ +unsigned \ +prefix ## _size(CollType coll) \ +{ \ + return (coll ? g_sequence_get_length((GSequence*)coll) : 0); \ +} \ +\ +SLV2_API \ +ElemType \ +prefix ## _get_at(CollType coll, unsigned index) \ +{ \ + if (!coll || index >= prefix ## _size(coll)) { \ + return NULL; \ + } else { \ + GSequenceIter* i = g_sequence_get_iter_at_pos((GSequence*)coll, (int)index); \ + return (ElemType)g_sequence_get(i); \ + } \ +} \ +\ +SLV2_API \ +ElemType \ +prefix ## _get_by_uri(CollType coll, SLV2Value uri) \ +{ \ + return (ElemType)slv2_sequence_get_by_uri(coll, uri); \ +} - if (cmp == 0) - return p; - else if (cmp > 0) - upper = i - 1; - else - lower = i + 1; - } +SLV2_SEQUENCE_IMPL(SLV2PluginClasses, SLV2PluginClass, + slv2_plugin_classes, &slv2_plugin_class_free) +SLV2_SEQUENCE_IMPL(SLV2UIs, SLV2UI, + slv2_uis, &slv2_ui_free) - return NULL; -} -/* **** VALUES **** */ +/* VALUES */ SLV2_API bool @@ -111,34 +129,3 @@ slv2_values_contains(SLV2Values list, SLV2Value value) return false; } -/* **** PLUGIN UIS **** */ - -SLV2_API -SLV2UI -slv2_uis_get_by_uri(SLV2UIs list, SLV2Value uri) -{ - // good old fashioned binary search - - int lower = 0; - int upper = ((GPtrArray*)list)->len - 1; - int i; - - while (upper >= lower) { - i = lower + ((upper - lower) / 2); - - SLV2UI ui = g_ptr_array_index((GPtrArray*)list, i); - - const int cmp = strcmp(slv2_value_as_uri(slv2_ui_get_uri(ui)), - slv2_value_as_uri(uri)); - - if (cmp == 0) - return ui; - else if (cmp > 0) - upper = i - 1; - else - lower = i + 1; - } - - return NULL; -} - |