-/*****************************************************************************
- * 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 the slot where to insert a variable
- *****************************************************************************/
-static size_t Insert( variable_t **pp_vars, size_t n, const char *psz_name )
-{
- size_t offset = 0;
- uint32_t hash = HashString( psz_name );
-
- if( n == 0 )
- return 0;
-
- while( n > 1 )
- {
- size_t middle = n / 2;
-
- if( hash < pp_vars[middle]->i_hash )
- {
- n = middle;
- }
- else
- {
- pp_vars += middle;
- offset += middle;
- n -= middle;
- }
- }
-
- if( hash >= pp_vars[0]->i_hash )
- offset++;
- return offset;
-}
-
-static int u32cmp( const void *key, const void *data )
-{
- const variable_t *const *pp_var = data;
- uint32_t hash = *(const uint32_t *)key ;
-
- if( hash > (*pp_var)->i_hash )
- return 1;
- if( hash < (*pp_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( vlc_object_t *obj, const char *psz_name )
-{
- vlc_object_internals_t *priv = vlc_internals( obj );
- variable_t *const *pp_vars = priv->pp_vars;
- size_t i_vars = priv->i_vars;
- variable_t *const *pp_var;
- uint32_t i_hash = HashString( psz_name );
-
- pp_var = bsearch( &i_hash, pp_vars, i_vars, sizeof( *pp_var ), u32cmp );
-
- /* Hash not found */
- if( pp_var == NULL )
- return -1;
-
- assert( i_vars > 0 );
-
- /* Find the first entry with the right hash */
- while( (pp_var > pp_vars) && (i_hash == pp_var[-1]->i_hash) )
- pp_var--;
-
- assert( (*pp_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 *const *p_end = pp_vars + i_vars;
- (pp_var < p_end) && (i_hash == (*pp_var)->i_hash);
- pp_var++ )
- {
- if( !strcmp( psz_name, (*pp_var)->psz_name ) )
- return pp_var - pp_vars;
- }
-
- /* Hash found, but entry not found */
- return -1;
-}
-