vlc_arrays.h 21.9 KB
Newer Older
1 2 3
/*****************************************************************************
 * vlc_arrays.h : Arrays and data structures handling
 *****************************************************************************
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
4
 * Copyright (C) 1999-2004 VLC authors and VideoLAN
5
 * $Id$
6 7 8 9
 *
 * Authors: Samuel Hocevar <sam@zoy.org>
 *          Clément Stenac <zorglub@videolan.org>
 *
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
10 11 12
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
13 14 15 16
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
17 18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
19
 *
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
20 21 22
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23
 *****************************************************************************/
24

25 26
#ifndef VLC_ARRAYS_H_
#define VLC_ARRAYS_H_
27

28 29 30 31 32
/**
 * \file
 * This file defines functions, structures and macros for handling arrays in vlc
 */

33 34 35 36 37 38 39
/* realloc() that never fails *if* downsizing */
static inline void *realloc_down( void *ptr, size_t size )
{
    void *ret = realloc( ptr, size );
    return ret ? ret : ptr;
}

40 41 42 43 44 45
/**
 * Simple dynamic array handling. Array is realloced at each insert/removal
 */
#define INSERT_ELEM( p_ar, i_oldsize, i_pos, elem )                           \
    do                                                                        \
    {                                                                         \
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
46
        if( !(i_oldsize) ) (p_ar) = NULL;                                       \
47
        (p_ar) = realloc( p_ar, ((i_oldsize) + 1) * sizeof(*(p_ar)) ); \
48
        if( !(p_ar) ) abort();                                                \
49 50 51 52 53
        if( (i_oldsize) - (i_pos) )                                           \
        {                                                                     \
            memmove( (p_ar) + (i_pos) + 1, (p_ar) + (i_pos),                  \
                     ((i_oldsize) - (i_pos)) * sizeof( *(p_ar) ) );           \
        }                                                                     \
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
54
        (p_ar)[(i_pos)] = elem;                                                 \
55 56 57 58
        (i_oldsize)++;                                                        \
    }                                                                         \
    while( 0 )

59
#define REMOVE_ELEM( p_ar, i_size, i_pos )                                    \
60 61
    do                                                                        \
    {                                                                         \
62
        if( (i_size) - (i_pos) - 1 )                                          \
63 64 65
        {                                                                     \
            memmove( (p_ar) + (i_pos),                                        \
                     (p_ar) + (i_pos) + 1,                                    \
66
                     ((i_size) - (i_pos) - 1) * sizeof( *(p_ar) ) );          \
67
        }                                                                     \
68 69
        if( i_size > 1 )                                                      \
            (p_ar) = realloc_down( p_ar, ((i_size) - 1) * sizeof( *(p_ar) ) );\
70 71 72 73 74
        else                                                                  \
        {                                                                     \
            free( p_ar );                                                     \
            (p_ar) = NULL;                                                    \
        }                                                                     \
75
        (i_size)--;                                                           \
76 77 78
    }                                                                         \
    while( 0 )

79 80 81 82 83 84 85 86
#define TAB_INIT( count, tab )                  \
  do {                                          \
    (count) = 0;                                \
    (tab) = NULL;                               \
  } while(0)

#define TAB_CLEAN( count, tab )                 \
  do {                                          \
87
    free( tab );                                \
88 89 90
    (count)= 0;                                 \
    (tab)= NULL;                                \
  } while(0)
91

92
#define TAB_APPEND_CAST( cast, count, tab, p )             \
93
  do {                                          \
94
    if( (count) > 0 )                           \
95
        (tab) = cast realloc( tab, sizeof( *(tab) ) * ( (count) + 1 ) ); \
96
    else                                        \
97
        (tab) = cast malloc( sizeof( *(tab) ) );    \
98
    if( !(tab) ) abort();                       \
99 100 101
    (tab)[count] = (p);                         \
    (count)++;                                  \
  } while(0)
102

103 104 105
#define TAB_APPEND( count, tab, p )             \
    TAB_APPEND_CAST( , count, tab, p )

106
#define TAB_FIND( count, tab, p, idx )          \
107
  do {                                          \
108 109
    for( (idx) = 0; (idx) < (count); (idx)++ )  \
        if( (tab)[(idx)] == (p) )               \
110
            break;                              \
111 112
    if( (idx) >= (count) )                      \
        (idx) = -1;                             \
113
  } while(0)
114

115

116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
#define TAB_ERASE( count, tab, index )      \
  do {                                      \
        if( (count) > 1 )                   \
        {                                   \
            memmove( ((void**)(tab) + index),    \
                     ((void**)(tab) + index+1),  \
                     ( (count) - index - 1 ) * sizeof( *(tab) ) );\
        }                                   \
        (count)--;                          \
        if( (count) == 0 )                  \
        {                                   \
            free( tab );                    \
            (tab) = NULL;                   \
        }                                   \
  } while(0)

132
#define TAB_REMOVE( count, tab, p )             \
133
  do {                                          \
134 135 136
        int i_index;                            \
        TAB_FIND( count, tab, p, i_index );     \
        if( i_index >= 0 )                      \
137
            TAB_ERASE( count, tab, i_index );   \
138
  } while(0)
139

140 141
#define TAB_INSERT_CAST( cast, count, tab, p, index ) do { \
    if( (count) > 0 )                           \
142
        (tab) = cast realloc( tab, sizeof( *(tab) ) * ( (count) + 1 ) ); \
143
    else                                        \
144
        (tab) = cast malloc( sizeof( *(tab) ) );       \
145
    if( !(tab) ) abort();                       \
146 147 148 149 150 151 152 153 154 155 156
    if( (count) - (index) > 0 )                 \
        memmove( (void**)(tab) + (index) + 1,   \
                 (void**)(tab) + (index),       \
                 ((count) - (index)) * sizeof(*(tab)) );\
    (tab)[(index)] = (p);                       \
    (count)++;                                  \
} while(0)

#define TAB_INSERT( count, tab, p, index )      \
    TAB_INSERT_CAST( , count, tab, p, index )

157
/**
Clément Stenac's avatar
Clément Stenac committed
158
 * Binary search in a sorted array. The key must be comparable by < and >
159 160 161 162 163 164 165
 * \param entries array of entries
 * \param count number of entries
 * \param elem key to check within an entry (like .id, or ->i_id)
 * \param zetype type of the key
 * \param key value of the key
 * \param answer index of answer within the array. -1 if not found
 */
166 167
#define BSEARCH( entries, count, elem, zetype, key, answer ) \
   do {  \
168 169 170
    int low = 0, high = count - 1;   \
    answer = -1; \
    while( low <= high ) {\
171
        int mid = ((unsigned int)low + (unsigned int)high) >> 1;\
172 173 174 175 176 177 178 179 180 181
        zetype mid_val = entries[mid] elem;\
        if( mid_val < key ) \
            low = mid + 1; \
        else if ( mid_val > key ) \
            high = mid -1;  \
        else    \
        {   \
            answer = mid;  break;   \
        }\
    } \
182
 } while(0)
183

184

185 186 187 188 189 190
/************************************************************************
 * Dynamic arrays with progressive allocation
 ************************************************************************/

/* Internal functions */
#define _ARRAY_ALLOC(array, newsize) {                                      \
191
    (array).i_alloc = newsize;                                              \
192 193
    (array).p_elems = realloc( (array).p_elems, (array).i_alloc *           \
                               sizeof(*(array).p_elems) );                  \
194
    if( !(array).p_elems ) abort();                                         \
195 196 197
}

#define _ARRAY_GROW1(array) {                                               \
198
    if( (array).i_alloc < 10 )                                              \
199
        _ARRAY_ALLOC(array, 10 )                                            \
200
    else if( (array).i_alloc == (array).i_size )                            \
201 202 203
        _ARRAY_ALLOC(array, (int)(array.i_alloc * 1.5) )                    \
}

204
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
205 206 207 208 209 210 211 212 213 214 215

/* API */
#define DECL_ARRAY(type) struct {                                           \
    int i_alloc;                                                            \
    int i_size;                                                             \
    type *p_elems;                                                          \
}

#define TYPEDEF_ARRAY(type, name) typedef DECL_ARRAY(type) name;

#define ARRAY_INIT(array)                                                   \
216
  do {                                                                      \
217 218
    (array).i_alloc = 0;                                                    \
    (array).i_size = 0;                                                     \
219 220
    (array).p_elems = NULL;                                                 \
  } while(0)
221

Clément Stenac's avatar
Clément Stenac committed
222
#define ARRAY_RESET(array)                                                  \
223
  do {                                                                      \
224 225
    (array).i_alloc = 0;                                                    \
    (array).i_size = 0;                                                     \
226 227
    free( (array).p_elems ); (array).p_elems = NULL;                        \
  } while(0)
Clément Stenac's avatar
Clément Stenac committed
228

229 230
#define ARRAY_APPEND(array, elem)                                           \
  do {                                                                      \
231
    _ARRAY_GROW1(array);                                                    \
232 233
    (array).p_elems[(array).i_size] = elem;                                 \
    (array).i_size++;                                                       \
234
  } while(0)
235

236 237
#define ARRAY_INSERT(array,elem,pos)                                        \
  do {                                                                      \
238
    _ARRAY_GROW1(array);                                                    \
239 240 241
    if( (array).i_size - pos ) {                                            \
        memmove( (array).p_elems + pos + 1, (array).p_elems + pos,          \
                 ((array).i_size-pos) * sizeof(*(array).p_elems) );         \
242
    }                                                                       \
243 244
    (array).p_elems[pos] = elem;                                            \
    (array).i_size++;                                                       \
245
  } while(0)
246

247 248 249 250 251 252
#define _ARRAY_SHRINK(array) {                                              \
    if( (array).i_size > 10 && (array).i_size < (int)((array).i_alloc / 1.5) ) {  \
        _ARRAY_ALLOC(array, (array).i_size + 5);                            \
    }                                                                       \
}

253 254
#define ARRAY_REMOVE(array,pos)                                             \
  do {                                                                      \
255
    if( (array).i_size - (pos) - 1 )                                        \
256
    {                                                                       \
257 258
        memmove( (array).p_elems + pos, (array).p_elems + pos + 1,          \
                 ( (array).i_size - pos - 1 ) *sizeof(*(array).p_elems) );  \
259
    }                                                                       \
260
    (array).i_size--;                                                       \
261
    _ARRAY_SHRINK(array);                                                   \
262
  } while(0)
263

Clément Stenac's avatar
Clément Stenac committed
264 265 266
#define ARRAY_VAL(array, pos) array.p_elems[pos]

#define ARRAY_BSEARCH(array, elem, zetype, key, answer) \
267
    BSEARCH( (array).p_elems, (array).i_size, elem, zetype, key, answer)
Clément Stenac's avatar
Clément Stenac committed
268 269 270

#define FOREACH_ARRAY( item, array ) { \
    int fe_idx; \
271
    for( fe_idx = 0 ; fe_idx < (array).i_size ; fe_idx++ ) \
Clément Stenac's avatar
Clément Stenac committed
272
    { \
273
        item = (array).p_elems[fe_idx];
Clément Stenac's avatar
Clément Stenac committed
274 275 276

#define FOREACH_END() } }

277 278

/************************************************************************
Sam Hocevar's avatar
Sam Hocevar committed
279
 * Dynamic arrays with progressive allocation (Preferred API)
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
 ************************************************************************/
typedef struct vlc_array_t
{
    int i_count;
    void ** pp_elems;
} vlc_array_t;

static inline void vlc_array_init( vlc_array_t * p_array )
{
    memset( p_array, 0, sizeof(vlc_array_t) );
}

static inline void vlc_array_clear( vlc_array_t * p_array )
{
    free( p_array->pp_elems );
    memset( p_array, 0, sizeof(vlc_array_t) );
}

298 299
static inline vlc_array_t * vlc_array_new( void )
{
300
    vlc_array_t * ret = (vlc_array_t *)malloc( sizeof(vlc_array_t) );
301
    if( ret ) vlc_array_init( ret );
302 303 304
    return ret;
}

305 306
static inline void vlc_array_destroy( vlc_array_t * p_array )
{
307 308
    if( !p_array )
        return;
309 310 311 312 313
    vlc_array_clear( p_array );
    free( p_array );
}


314 315 316 317 318 319 320 321
/* Read */
static inline int
vlc_array_count( vlc_array_t * p_array )
{
    return p_array->i_count;
}

static inline void *
322
vlc_array_item_at_index( vlc_array_t * p_array, int i_index )
323 324 325 326
{
    return p_array->pp_elems[i_index];
}

327 328 329 330 331 332 333 334 335 336 337 338
static inline int
vlc_array_index_of_item( vlc_array_t * p_array, void * item )
{
    int i;
    for( i = 0; i < p_array->i_count; i++)
    {
        if( p_array->pp_elems[i] == item )
            return i;
    }
    return -1;
}

339 340 341 342
/* Write */
static inline void
vlc_array_insert( vlc_array_t * p_array, void * p_elem, int i_index )
{
343
    TAB_INSERT_CAST( (void **), p_array->i_count, p_array->pp_elems, p_elem, i_index );
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
}

static inline void
vlc_array_append( vlc_array_t * p_array, void * p_elem )
{
    vlc_array_insert( p_array, p_elem, p_array->i_count );
}

static inline void
vlc_array_remove( vlc_array_t * p_array, int i_index )
{
    if( i_index >= 0 )
    {
        if( p_array->i_count > 1 )
        {
            memmove( p_array->pp_elems + i_index,
                     p_array->pp_elems + i_index+1,
                     ( p_array->i_count - i_index - 1 ) * sizeof( void* ) );
        }
        p_array->i_count--;
        if( p_array->i_count == 0 )
        {
            free( p_array->pp_elems );
            p_array->pp_elems = NULL;
        }
    }
}


373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
/************************************************************************
 * Dictionaries
 ************************************************************************/

/* 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 inline uint64_t DictHash( const char *psz_string, int hashsize )
{
    uint64_t i_hash = 0;
    if( psz_string )
    {
        while( *psz_string )
        {
            i_hash += *psz_string++;
            i_hash += i_hash << 10;
            i_hash ^= i_hash >> 8;
        }
    }
    return i_hash % hashsize;
}

396
typedef struct vlc_dictionary_entry_t
397 398 399 400
{
    char *   psz_key;
    void *   p_value;
    struct vlc_dictionary_entry_t * p_next;
401
} vlc_dictionary_entry_t;
402 403 404 405

typedef struct vlc_dictionary_t
{
    int i_size;
406
    vlc_dictionary_entry_t ** p_entries;
407 408 409 410 411 412
} vlc_dictionary_t;

static void * const kVLCDictionaryNotFound = NULL;

static inline void vlc_dictionary_init( vlc_dictionary_t * p_dict, int i_size )
{
413 414
    p_dict->p_entries = NULL;

415 416
    if( i_size > 0 )
    {
417 418 419
        p_dict->p_entries = (vlc_dictionary_entry_t **)calloc( i_size, sizeof(*p_dict->p_entries) );
        if( !p_dict->p_entries )
            i_size = 0;
420
    }
421 422 423
    p_dict->i_size = i_size;
}

424 425 426
static inline void vlc_dictionary_clear( vlc_dictionary_t * p_dict,
                                         void ( * pf_free )( void * p_data, void * p_obj ),
                                         void * p_obj )
427
{
428
    if( p_dict->p_entries )
429
    {
430
        for( int i = 0; i < p_dict->i_size; i++ )
431
        {
432
            vlc_dictionary_entry_t * p_current, * p_next;
433 434 435
            p_current = p_dict->p_entries[i];
            while( p_current )
            {
436
                p_next = p_current->p_next;
437 438
                if( pf_free != NULL )
                    ( * pf_free )( p_current->p_value, p_obj );
439
                free( p_current->psz_key );
440 441 442
                free( p_current );
                p_current = p_next;
            }
443
        }
444
        free( p_dict->p_entries );
445
        p_dict->p_entries = NULL;
446 447 448 449
    }
    p_dict->i_size = 0;
}

450 451 452 453 454
static inline int
vlc_dictionary_has_key( const vlc_dictionary_t * p_dict, const char * psz_key )
{
    if( !p_dict->p_entries )
        return 0;
455

456 457 458
    int i_pos = DictHash( psz_key, p_dict->i_size );
    return p_dict->p_entries[i_pos] != NULL;
}
459 460 461 462

static inline void *
vlc_dictionary_value_for_key( const vlc_dictionary_t * p_dict, const char * psz_key )
{
463 464 465
    if( !p_dict->p_entries )
        return kVLCDictionaryNotFound;

466
    int i_pos = DictHash( psz_key, p_dict->i_size );
467
    vlc_dictionary_entry_t * p_entry = p_dict->p_entries[i_pos];
468 469 470 471

    if( !p_entry )
        return kVLCDictionaryNotFound;

472
    /* Make sure we return the right item. (Hash collision) */
473 474 475 476 477 478
    do {
        if( !strcmp( psz_key, p_entry->psz_key ) )
            return p_entry->p_value;
        p_entry = p_entry->p_next;
    } while( p_entry );

479
    return kVLCDictionaryNotFound;
480 481 482 483 484
}

static inline int
vlc_dictionary_keys_count( const vlc_dictionary_t * p_dict )
{
485
    vlc_dictionary_entry_t * p_entry;
486
    int i, count = 0;
487 488 489 490

    if( !p_dict->p_entries )
        return 0;

491 492 493 494 495 496 497 498 499 500
    for( i = 0; i < p_dict->i_size; i++ )
    {
        for( p_entry = p_dict->p_entries[i]; p_entry; p_entry = p_entry->p_next ) count++;
    }
    return count;
}

static inline char **
vlc_dictionary_all_keys( const vlc_dictionary_t * p_dict )
{
501
    vlc_dictionary_entry_t * p_entry;
502 503 504
    char ** ppsz_ret;
    int i, count = vlc_dictionary_keys_count( p_dict );

505
    ppsz_ret = (char**)malloc(sizeof(char *) * (count + 1));
506
    if( unlikely(!ppsz_ret) )
507
        return NULL;
508

509 510 511 512 513 514 515 516 517 518 519 520
    count = 0;
    for( i = 0; i < p_dict->i_size; i++ )
    {
        for( p_entry = p_dict->p_entries[i]; p_entry; p_entry = p_entry->p_next )
            ppsz_ret[count++] = strdup( p_entry->psz_key );
    }
    ppsz_ret[count] = NULL;
    return ppsz_ret;
}

static inline void
__vlc_dictionary_insert( vlc_dictionary_t * p_dict, const char * psz_key,
521
                         void * p_value, bool rebuild )
522
{
523 524 525
    if( !p_dict->p_entries )
        vlc_dictionary_init( p_dict, 1 );

526
    int i_pos = DictHash( psz_key, p_dict->i_size );
527
    vlc_dictionary_entry_t * p_entry;
528

529
    p_entry = (vlc_dictionary_entry_t *)malloc(sizeof(*p_entry));
530 531 532 533 534 535 536 537
    p_entry->psz_key = strdup( psz_key );
    p_entry->p_value = p_value;
    p_entry->p_next = p_dict->p_entries[i_pos];
    p_dict->p_entries[i_pos] = p_entry;
    if( rebuild )
    {
        /* Count how many items there was */
        int count;
538 539
        for( count = 1; p_entry->p_next; count++ )
            p_entry = p_entry->p_next;
540 541 542 543
        if( count > 3 ) /* XXX: this need tuning */
        {
            /* Here it starts to be not good, rebuild a bigger dictionary */
            struct vlc_dictionary_t new_dict;
544
            int i_new_size = ( (p_dict->i_size+2) * 3) / 2; /* XXX: this need tuning */
545 546 547 548 549
            int i;
            vlc_dictionary_init( &new_dict, i_new_size );
            for( i = 0; i < p_dict->i_size; i++ )
            {
                p_entry = p_dict->p_entries[i];
550
                while( p_entry )
551 552 553
                {
                    __vlc_dictionary_insert( &new_dict, p_entry->psz_key,
                                             p_entry->p_value,
554
                                             false /* To avoid multiple rebuild loop */);
555
                    p_entry = p_entry->p_next;
556
                }
557
            }
558

559
            vlc_dictionary_clear( p_dict, NULL, NULL );
560
            p_dict->i_size = new_dict.i_size;
561
            p_dict->p_entries = new_dict.p_entries;
562 563 564 565 566 567 568
        }
    }
}

static inline void
vlc_dictionary_insert( vlc_dictionary_t * p_dict, const char * psz_key, void * p_value )
{
569
    __vlc_dictionary_insert( p_dict, psz_key, p_value, true );
570 571 572
}

static inline void
573 574 575
vlc_dictionary_remove_value_for_key( const vlc_dictionary_t * p_dict, const char * psz_key,
                                     void ( * pf_free )( void * p_data, void * p_obj ),
                                     void * p_obj )
576
{
577 578 579
    if( !p_dict->p_entries )
        return;

580
    int i_pos = DictHash( psz_key, p_dict->i_size );
581 582
    vlc_dictionary_entry_t * p_entry = p_dict->p_entries[i_pos];
    vlc_dictionary_entry_t * p_prev;
583 584 585 586 587 588 589 590 591

    if( !p_entry )
        return; /* Not found, nothing to do */

    /* Hash collision */
    p_prev = NULL;
    do {
        if( !strcmp( psz_key, p_entry->psz_key ) )
        {
592 593
            if( pf_free != NULL )
                ( * pf_free )( p_entry->p_value, p_obj );
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
            if( !p_prev )
                p_dict->p_entries[i_pos] = p_entry->p_next;
            else
                p_prev->p_next = p_entry->p_next;
            free( p_entry->psz_key );
            free( p_entry );
            return;
        }
        p_prev = p_entry;
        p_entry = p_entry->p_next;
    } while( p_entry );

    /* No key was found */
}

609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
#ifdef __cplusplus
// C++ helpers
template <typename T>
void vlc_delete_all( T &container )
{
    typename T::iterator it = container.begin();
    while ( it != container.end() )
    {
        delete *it;
        ++it;
    }
    container.clear();
}

#endif

625
#endif