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" #include "gstformat.h"
static GList *_gst_formats = NULL; 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 */ 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[] = { static GstFormatDefinition standard_definitions[] = {
{ GST_FORMAT_DEFAULT, "default", "Default" }, { GST_FORMAT_DEFAULT, "default", "Default" },
{ GST_FORMAT_BYTES, "bytes", "Bytes" }, { GST_FORMAT_BYTES, "bytes", "Bytes" },
@ -53,8 +46,16 @@ _gst_format_initialize (void)
{ {
GstFormatDefinition *standards = standard_definitions; 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) { 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++; standards++;
_n_values++; _n_values++;
} }
@ -89,8 +90,9 @@ gst_format_register (const gchar *nick, const gchar *description)
format->nick = g_strdup (nick); format->nick = g_strdup (nick);
format->description = g_strdup (description); 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++; _n_values++;
return format->value; return format->value;
@ -108,57 +110,41 @@ gst_format_register (const gchar *nick, const gchar *description)
GstFormat GstFormat
gst_format_get_by_nick (const gchar *nick) gst_format_get_by_nick (const gchar *nick)
{ {
GList *walk;
GstFormatDefinition *format; GstFormatDefinition *format;
g_return_val_if_fail (nick != NULL, 0); g_return_val_if_fail (nick != NULL, 0);
walk = _gst_formats; format = g_hash_table_lookup (_nick_to_format, nick);
while (walk) { if (format != NULL)
format = (GstFormatDefinition *) walk->data; return format->value;
else
if (!strcmp (format->nick, nick)) return GST_FORMAT_UNDEFINED;
return format->value;
walk = g_list_next (walk);
}
return GST_FORMAT_UNDEFINED;
} }
/** /**
* gst_format_get_details: * gst_format_get_details:
* @format: The format to get details of * @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. * 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 const GstFormatDefinition *
gst_format_get_details (GstFormat format, const gchar **nick, const gchar **description) gst_format_get_details (GstFormat format)
{ {
GList *walk; return g_hash_table_lookup (_format_to_nick, GINT_TO_POINTER (format));
GstFormatDefinition *walk_format; }
g_return_val_if_fail (nick != NULL, FALSE); /**
g_return_val_if_fail (description != NULL, FALSE); * gst_format_get_definitions:
*
walk = _gst_formats; * Get a list of all the registered formats.
*
while (walk) { * Returns: A GList of #GstFormatDefinition.
walk_format = (GstFormatDefinition *) walk->data; */
const GList *
if (walk_format->value == format) { gst_format_get_definitions (void)
*nick = walk_format->nick; {
*description = walk_format->description; return _gst_formats;
return TRUE;
}
walk = g_list_next (walk);
}
return FALSE;
} }

View file

@ -40,6 +40,14 @@ typedef enum {
GST_FORMAT_UNITS = 6, GST_FORMAT_UNITS = 6,
} GstFormat; } GstFormat;
typedef struct _GstFormatDefinition GstFormatDefinition;
struct _GstFormatDefinition
{
GstFormat value;
gchar *nick;
gchar *description;
};
#define GST_FORMATS_FUNCTION(functionname, a...) \ #define GST_FORMATS_FUNCTION(functionname, a...) \
static const GstFormat* \ static const GstFormat* \
functionname (GstPad *pad) \ 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); GstFormat gst_format_get_by_nick (const gchar *nick);
/* query for format details */ /* 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 G_END_DECLS

View file

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