HTArray.h
Upload User: zlh9724
Upload Date: 2007-01-04
Package Size: 1991k
Code Size: 3k
Category:

Browser Client

Development Platform:

Unix_Linux

  1. /*                                                                Dynamic Array Pointer Class
  2.                                DYNAMIC ARRAY POINTER CLASS
  3.                                              
  4.  */
  5. /*
  6. **      (c) COPYRIGHT MIT 1995.
  7. **      Please first read the full copyright statement in the file COPYRIGH.
  8. */
  9. /*
  10.    This module implements a flexible array of pointers. It is a general utility module.
  11.    An array is a structure which may be extended.  These routines create and append data
  12.    to arrays, automatically reallocating them as necessary.  It is garanteed that the last
  13.    entry in an array is NULLThis module is implemented by HTArray.c, and it is a part of
  14.    the  W3C Reference Library.
  15.    
  16.  */
  17. #ifndef HTARRAY_H
  18. #define HTARRAY_H
  19. /*
  20. Private Data Structure
  21.    This structure should not be referenced outside this module. If I find out I'll make it
  22.    private ;-)
  23.    
  24.  */
  25. typedef struct {
  26.     int         size;           /* In numbers of elements       */
  27.     int         growby;         /* Allocation unit in elements  */
  28.     int         allocated;      /* Current size of *data        */
  29.     void **     data;           /* Pointer to malloced area or 0 */
  30. } HTArray;
  31. /*
  32. Create a new Array
  33.    Create a new array and specify the number of bytes to allocate at a time when the array
  34.    is later extended. Arbitrary but normally a trade-off time vs. memory
  35.    
  36.  */
  37. extern HTArray * HTArray_new (int grow);
  38. /*
  39. Delete an Array
  40.    Delete an array created by HTArray_new
  41.    
  42.  */
  43. extern BOOL HTArray_delete (HTArray * array);
  44. /*
  45. Clear an Array
  46.    Clears an array but keeps it around
  47.    
  48.  */
  49. extern BOOL HTArray_clear (HTArray * array);
  50. /*
  51. Append an element to the Array
  52.    Add the element to the array.
  53.    
  54.  */
  55. extern BOOL HTArray_addObject (HTArray * array, void * object);
  56. /*
  57. Traverse an Array
  58.    Fast macros to traverse a macro ending in a NULL element.
  59.    
  60.  */
  61. #define HTArray_firstObject(me, data) 
  62.         ((me) && ((data)=(me)->data) ? *(data)++ : NULL)
  63. #define HTArray_nextObject(me, data) 
  64.         ((me) && (data) ? *(data)++ : NULL)
  65. /*
  66. Sort an Array
  67.    An array can be sorted in any way you like, for example with qsort(). This module
  68.    provides an easy interface to the qsort() function using where you can define you own
  69.    comparison routine as a function of the type:
  70.    
  71.  */
  72. typedef int HTComparer (CONST void * a, CONST void * b);
  73. /*
  74.    The sort function returns YES if sorting OK, else NO.
  75.    
  76.  */
  77. extern BOOL HTArray_sort (HTArray * array, HTComparer * comp);
  78. /*
  79. Returns Data Vector
  80.    Returns a pointer to the actual data
  81.    
  82.  */
  83. #define HTArray_data(me)        ((me) ? (me)->data : NULL)
  84. /*
  85. Return Current Size
  86.    Returns the current size of the chunk
  87.    
  88.  */
  89. #define HTArray_size(me)        ((me) ? (me)->size : -1)
  90. /*
  91.  */
  92. #endif
  93. /*
  94.    End of Declaration  */