- vlc_cleanup_pop( );
- }
-}
-
-/*****************************************************************************
- * HashString: our cool hash function
- *****************************************************************************
- * This function is not intended to be crypto-secure, we only want it to be
- * fast and not suck too much. This one is pretty fast and did 0 collisions
- * in wenglish's dictionary.
- *****************************************************************************/
-static uint32_t HashString( const char *psz_string )
-{
- uint32_t i_hash = 0;
-
- while( *psz_string )
- {
- i_hash += *psz_string++;
- i_hash += i_hash << 10;
- i_hash ^= i_hash >> 8;
- }
-
- return i_hash;
-}
-
-/*****************************************************************************
- * Insert: find an empty slot to insert a new variable
- *****************************************************************************
- * We use a recursive inner function indexed on the hash. This function does
- * nothing in the rare cases where a collision may occur, see Lookup()
- * to see how we handle them.
- * XXX: does this really need to be written recursively?
- *****************************************************************************/
-static int Insert( variable_t *p_vars, int i_count, const char *psz_name )
-{
- if( i_count == 0 )
- {
- return 0;
- }
-
- return InsertInner( p_vars, i_count, HashString( psz_name ) );
-}
-
-static int InsertInner( variable_t *p_vars, int i_count, uint32_t i_hash )
-{
- int i_middle;
-
- if( i_hash <= p_vars[0].i_hash )
- {
- return 0;
- }
-
- if( i_hash >= p_vars[i_count - 1].i_hash )
- {
- return i_count;
- }
-
- i_middle = i_count / 2;
-
- /* We know that 0 < i_middle */
- if( i_hash < p_vars[i_middle].i_hash )
- {
- return InsertInner( p_vars, i_middle, i_hash );
- }
-
- /* We know that i_middle + 1 < i_count */
- if( i_hash > p_vars[i_middle + 1].i_hash )
- {
- return i_middle + 1 + InsertInner( p_vars + i_middle + 1,
- i_count - i_middle - 1,
- i_hash );
- }
-
- return i_middle + 1;
-}
-
-static int u32cmp( const void *key, const void *data )
-{
- const variable_t *p_var = data;
- uint32_t hash = *(const uint32_t *)key ;
-
- if( hash > p_var->i_hash )
- return 1;
- if( hash < p_var->i_hash )
- return -1;
- return 0;
-}
-
-/*****************************************************************************
- * Lookup: find an existing variable given its name
- *****************************************************************************
- * We use a recursive inner function indexed on the hash. Care is taken of
- * possible hash collisions.
- *****************************************************************************/
-static int Lookup( variable_t *p_vars, size_t i_count, const char *psz_name )
-{
- variable_t *p_var;
- uint32_t i_hash;
-
- i_hash = HashString( psz_name );
- p_var = bsearch( &i_hash, p_vars, i_count, sizeof( *p_var ), u32cmp );
-
- /* Hash not found */
- if( p_var == NULL )
- return -1;
-
- assert( i_count > 0 );
-
- /* Find the first entry with the right hash */
- while( (p_var > p_vars) && (i_hash == p_var[-1].i_hash) )
- p_var--;
-
- assert( p_var->i_hash == i_hash );
-
- /* Hash collision should be very unlikely, but we cannot guarantee
- * it will never happen. So we do an exhaustive search amongst all
- * entries with the same hash. Typically, there is only one anyway. */
- for( variable_t *p_end = p_vars + i_count;
- (p_var < p_end) && (i_hash == p_var->i_hash);
- p_var++ )
- {
- if( !strcmp( psz_name, p_var->psz_name ) )
- return p_var - p_vars;
- }
-
- /* Hash found, but entry not found */
- return -1;