Use some hashtables instead of a linked list

Original commit message from CVS:
Use some hashtables instead of a linked list
This commit is contained in:
Iain Holmes 2002-09-29 17:17:28 +00:00
parent dc449746bb
commit 4b93f5a554
3 changed files with 53 additions and 58 deletions

View file

@ -27,17 +27,10 @@
#include "gstformat.h"
static GList *_gst_formats = NULL;
static GHashTable *_nick_to_format = NULL;
static GHashTable *_format_to_nick = NULL;
static gint _n_values = 1; /* we start from 1 because 0 reserved for UNDEFINED */
typedef struct _GstFormatDefinition GstFormatDefinition;
struct _GstFormatDefinition
{
GstFormat value;
gchar *nick;
gchar *description;
};
static GstFormatDefinition standard_definitions[] = {
{ GST_FORMAT_DEFAULT, "default", "Default" },
{ GST_FORMAT_BYTES, "bytes", "Bytes" },
@ -53,8 +46,16 @@ _gst_format_initialize (void)
{
GstFormatDefinition *standards = standard_definitions;
if (_nick_to_format == NULL) {
_nick_to_format = g_hash_table_new (g_str_hash, g_str_equal);
_format_to_nick = g_hash_table_new (NULL, NULL);
}
while (standards->nick) {
_gst_formats = g_list_prepend (_gst_formats, standards);
g_hash_table_insert (_nick_to_format, standards->nick, standards);
g_hash_table_insert (_format_to_nick, GINT_TO_POINTER (standards->value), standards);
_gst_formats = g_list_append (_gst_formats, standards);
standards++;
_n_values++;
}
@ -89,8 +90,9 @@ gst_format_register (const gchar *nick, const gchar *description)
format->nick = g_strdup (nick);
format->description = g_strdup (description);
_gst_formats = g_list_prepend (_gst_formats, format);
g_hash_table_insert (_nick_to_format, format->nick, format);
g_hash_table_insert (_format_to_nick, GINT_TO_POINTER (format->value), format);
_gst_formats = g_list_append (_gst_formats, format);
_n_values++;
return format->value;
@ -108,57 +110,41 @@ gst_format_register (const gchar *nick, const gchar *description)
GstFormat
gst_format_get_by_nick (const gchar *nick)
{
GList *walk;
GstFormatDefinition *format;
g_return_val_if_fail (nick != NULL, 0);
walk = _gst_formats;
while (walk) {
format = (GstFormatDefinition *) walk->data;
if (!strcmp (format->nick, nick))
return format->value;
walk = g_list_next (walk);
}
return GST_FORMAT_UNDEFINED;
format = g_hash_table_lookup (_nick_to_format, nick);
if (format != NULL)
return format->value;
else
return GST_FORMAT_UNDEFINED;
}
/**
* gst_format_get_details:
* @format: The format to get details of
* @nick: The nick of the format
* @description: The description of the format
*
* Get details about the given format.
*
* Returns: TRUE if the format was registered, FALSE otherwise
* Returns: The #GstFormatDefinition for @format or NULL on failure.
*/
gboolean
gst_format_get_details (GstFormat format, const gchar **nick, const gchar **description)
const GstFormatDefinition *
gst_format_get_details (GstFormat format)
{
GList *walk;
GstFormatDefinition *walk_format;
g_return_val_if_fail (nick != NULL, FALSE);
g_return_val_if_fail (description != NULL, FALSE);
walk = _gst_formats;
while (walk) {
walk_format = (GstFormatDefinition *) walk->data;
if (walk_format->value == format) {
*nick = walk_format->nick;
*description = walk_format->description;
return TRUE;
}
walk = g_list_next (walk);
}
return FALSE;
return g_hash_table_lookup (_format_to_nick, GINT_TO_POINTER (format));
}
/**
* gst_format_get_definitions:
*
* Get a list of all the registered formats.
*
* Returns: A GList of #GstFormatDefinition.
*/
const GList *
gst_format_get_definitions (void)
{
return _gst_formats;
}

View file

@ -40,6 +40,14 @@ typedef enum {
GST_FORMAT_UNITS = 6,
} GstFormat;
typedef struct _GstFormatDefinition GstFormatDefinition;
struct _GstFormatDefinition
{
GstFormat value;
gchar *nick;
gchar *description;
};
#define GST_FORMATS_FUNCTION(functionname, a...) \
static const GstFormat* \
functionname (GstPad *pad) \
@ -59,8 +67,8 @@ GstFormat gst_format_register (const gchar *nick, const gchar *description);
GstFormat gst_format_get_by_nick (const gchar *nick);
/* query for format details */
gboolean gst_format_get_details (GstFormat format, const gchar **nick, const gchar **description);
const GstFormatDefinition * gst_format_get_details (GstFormat format);
const GList * gst_format_get_definitions (void);
G_END_DECLS

View file

@ -137,11 +137,12 @@ static void
print_formats (const GstFormat *formats)
{
while (formats && *formats) {
const gchar *nick;
const gchar *description;
if (gst_format_get_details (*formats, &nick, &description))
g_print ("\t\t(%d):\t%s (%s)\n", *formats, nick, description);
const GstFormatDefinition *definition;
definition = gst_format_get_details (*formats);
if (definition)
g_print ("\t\t(%d):\t%s (%s)\n", *formats,
definition->nick, definition->description);
else
g_print ("\t\t(%d):\tUnknown format\n", *formats);