IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 578


Ignore:
Timestamp:
May 5, 2004, 10:43:57 AM (22 years ago)
Author:
harman
Message:

Updated per SDR documentation bug fixes

Location:
trunk/psLib/src
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/src/collections/psArray.c

    r502 r578  
    88 *  @author Ross Harman, MHPCC
    99 *   
    10  *  @version $Revision: 1.6 $ $Name: not supported by cvs2svn $
    11  *  @date $Date: 2004-04-22 21:15:01 $
     10 *  @version $Revision: 1.7 $ $Name: not supported by cvs2svn $
     11 *  @date $Date: 2004-05-05 20:43:22 $
    1212 *
    1313 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    4848/*****************************************************************************/
    4949
    50 static void* p_psArrayAlloc(psType *arrType, psElemType elemType, int nalloc, int elemSize)
     50static void* p_psArrayAlloc(psArray *restrict psArr, psElemType elemType, int nalloc, int elemSize)
    5151{
    5252    void *arr = NULL;
    5353
    54     arrType->dimen = PS_DIMEN_VECTOR;
    55     arrType->type = elemType;
     54    psArr->type.dimen = PS_DIMEN_VECTOR;
     55    psArr->type.type = elemType;
     56    psArr->nalloc = nalloc;
     57    psArr->n = 0;
    5658
    5759    if(nalloc != 0) {
     
    6264}
    6365
    64 static psVoidPtrArray *p_psVoidPtrArrayRealloc(psVoidPtrArray *restrict psArr, int nalloc)
    65 {
    66     if(nalloc < psArr->n) {                                         // For decrease in psArray size
     66static void* p_psArrayRealloc(psArray *restrict psArr, void *arr, int nalloc, int elemSize)
     67{
     68    // For decrease in psArray size
     69    if(nalloc < psArr->n) {
    6770        psArr->n = nalloc;
    68         psArr->nalloc = nalloc;
    69         psArr->arr = psRealloc(psArr->arr, nalloc*sizeof(void*));   // For increase in psArray size
    70     } else if(nalloc > psArr->nalloc) {
    71         psArr->nalloc = nalloc;
    72         psArr->arr = psRealloc(psArr->arr, nalloc*sizeof(void*));
    73     } else if(psArr == NULL) {                                      // For creating new psArray with realloc
     71    }
     72
     73    psArr->nalloc = nalloc;
     74
     75    return psRealloc(arr, nalloc*elemSize);
     76}
     77
     78/*****************************************************************************/
     79/* FUNCTION IMPLEMENTATION - PUBLIC                                          */
     80/*****************************************************************************/
     81psArray* psIntArrayAlloc(int nalloc)
     82{
     83    psArray *psArr = NULL;
     84    psArr = psAlloc(sizeof(psArray));
     85    psArr->arr.intArr = (int*)p_psArrayAlloc(psArr, PS_TYPE_INT, nalloc, sizeof(int));
     86    return psArr;
     87}
     88
     89psArray *psIntArrayRealloc(psArray *restrict psArr, int nalloc)
     90{
     91    if(psArr == NULL) {                                 // For creating new psArray with realloc
     92        psArr = psIntArrayAlloc(nalloc);
     93    } else if(psArr->nalloc != nalloc) {                // No need to realloc to same size
     94        psArr->arr.intArr = p_psArrayRealloc(psArr, psArr->arr.intArr, nalloc, sizeof(int));
     95    }
     96
     97    return psArr;
     98}
     99
     100void psIntArrayFree(psArray *restrict psArr)
     101{
     102    if (psArr == NULL) {
     103        return;
     104    }
     105
     106    psFree(psArr->arr.intArr);
     107    psFree(psArr);
     108}
     109
     110psArray* psFloatArrayAlloc(int nalloc)
     111{
     112    psArray *psArr = NULL;
     113    psArr = psAlloc(sizeof(psArray));
     114    psArr->arr.fltArr = (float*)p_psArrayAlloc(psArr, PS_TYPE_FLOAT, nalloc, sizeof(float));
     115    return psArr;
     116}
     117
     118psArray *psFloatArrayRealloc(psArray *restrict psArr, int nalloc)
     119{
     120    if(psArr == NULL) {                                 // For creating new psArray with realloc
     121        psArr = psFloatArrayAlloc(nalloc);
     122    } else if(psArr->nalloc != nalloc) {                // No need to realloc to same size
     123        psArr->arr.fltArr = p_psArrayRealloc(psArr, psArr->arr.fltArr, nalloc, sizeof(float));
     124    }
     125
     126    return psArr;
     127}
     128
     129void psFloatArrayFree(psArray *restrict psArr)
     130{
     131    if (psArr == NULL) {
     132        return;
     133    }
     134    psFree(psArr->arr.fltArr);
     135    psFree(psArr);
     136}
     137
     138psArray* psDoubleArrayAlloc(int nalloc)
     139{
     140    psArray *psArr = NULL;
     141    psArr = psAlloc(sizeof(psArray));
     142    psArr->arr.dblArr = (double*)p_psArrayAlloc(psArr, PS_TYPE_DOUBLE, nalloc, sizeof(double));
     143    return psArr;
     144}
     145
     146psArray *psDoubleArrayRealloc(psArray *restrict psArr, int nalloc)
     147{
     148    if(psArr == NULL) {                                 // For creating new psArray with realloc
     149        psArr = psDoubleArrayAlloc(nalloc);
     150    } else if(psArr->nalloc != nalloc) {                // No need to realloc to same size
     151        psArr->arr.dblArr = p_psArrayRealloc(psArr, psArr->arr.dblArr, nalloc, sizeof(double));
     152    }
     153
     154    return psArr;
     155}
     156
     157void psDoubleArrayFree(psArray *restrict psArr)
     158{
     159    if (psArr == NULL) {
     160        return;
     161    }
     162    psFree(psArr->arr.dblArr);
     163    psFree(psArr);
     164}
     165
     166psArray* psComplexArrayAlloc(int nalloc)
     167{
     168    psArray *psArr = NULL;
     169    psArr = psAlloc(sizeof(psArray));
     170    psArr->arr.cFltArr = (complex float*)p_psArrayAlloc(psArr, PS_TYPE_COMPLEX, nalloc, sizeof(complex float));
     171    return psArr;
     172}
     173
     174psArray *psComplexArrayRealloc(psArray *restrict psArr, int nalloc)
     175{
     176    if(psArr == NULL) {                                 // For creating new psArray with realloc
     177        psArr = psComplexArrayAlloc(nalloc);
     178    } else if(psArr->nalloc != nalloc) {                // No need to realloc to same size
     179        psArr->arr.cFltArr = p_psArrayRealloc(psArr, psArr->arr.cFltArr, nalloc, sizeof(complex float));
     180    }
     181
     182    return psArr;
     183}
     184
     185void psComplexArrayFree(psArray *restrict psArr)
     186{
     187    if (psArr == NULL) {
     188        return;
     189    }
     190    psFree(psArr->arr.cFltArr);
     191    psFree(psArr);
     192}
     193
     194psArray* psVoidPtrArrayAlloc(int nalloc)
     195{
     196    psArray *psArr = NULL;
     197    psArr = psAlloc(sizeof(psArray));
     198    psArr->arr.ptrArr = p_psArrayAlloc(psArr, PS_TYPE_OTHER, nalloc, sizeof(void *));
     199    return psArr;
     200}
     201
     202psArray *psVoidPtrArrayRealloc(psArray *restrict psArr, int nalloc)
     203{
     204    int i = 0;
     205
     206    for (i = nalloc; i < psArr->n; i++) {               // For reduction in array size
     207        psMemDecrRefCounter(psArr->arr.ptrArr[i]);
     208    }
     209
     210    if(psArr == NULL) {                                 // For creating new psArray with realloc
    74211        psArr = psVoidPtrArrayAlloc(nalloc);
    75     }
    76 
    77     return psArr;
    78 }
    79 
    80 static void p_psVoidPtrArrayFree(psVoidPtrArray *restrict psArr)
    81 {
    82     if (psArr == NULL) {
    83         return;
    84     }
    85     psFree(psArr->arr);
    86     psFree(psArr);
    87 }
    88 
    89 /*****************************************************************************/
    90 /* FUNCTION IMPLEMENTATION - PUBLIC                                          */
    91 /*****************************************************************************/
    92 
    93 psIntArray* psIntArrayAlloc(int nalloc)
    94 {
    95     psIntArray *psArr = NULL;
    96     psArr = (psIntArray*)psAlloc(sizeof(psIntArray));
    97     psArr->nalloc = nalloc;
    98     psArr->n = 0;
    99     psArr->arr = (int*)p_psArrayAlloc(&psArr->type, PS_TYPE_INT, nalloc, sizeof(int));
    100 
    101     return psArr;
    102 }
    103 
    104 psIntArray *psIntArrayRealloc(psIntArray *psArr, int nalloc)
    105 {
    106     if(nalloc < psArr->n) {                                         // For decrease in psArray size
    107         psArr->n = nalloc;
    108         psArr->nalloc = nalloc;
    109         psArr->arr = psRealloc(psArr->arr, nalloc*sizeof(int));     // For increase in psArray size
    110     } else if(nalloc > psArr->nalloc) {
    111         psArr->nalloc = nalloc;
    112         psArr->arr = psRealloc(psArr->arr, nalloc*sizeof(int));
    113     } else if(psArr == NULL) {                                      // For creating new psArray with realloc
    114         psArr = psIntArrayAlloc(nalloc);
    115     }
    116 
    117     return psArr;
    118 }
    119 
    120 void psIntArrayFree(psIntArray *restrict psArr)
    121 {
    122     if (psArr == NULL) {
    123         return;
    124     }
    125 
    126     psFree(psArr->arr);
    127     psFree(psArr);
    128 }
    129 
    130 psFloatArray* psFloatArrayAlloc(int nalloc)
    131 {
    132     psFloatArray *psArr = NULL;
    133     psArr = (psFloatArray*)psAlloc(sizeof(psFloatArray));
    134     psArr->nalloc = nalloc;
    135     psArr->n = 0;
    136     psArr->arr = (float*)p_psArrayAlloc(&psArr->type, PS_TYPE_FLOAT, nalloc, sizeof(float));
    137 
    138     return psArr;
    139 }
    140 
    141 psFloatArray *psFloatArrayRealloc(psFloatArray *psArr, int nalloc)
    142 {
    143     if(nalloc < psArr->n) {                                         // For decrease in psArray size
    144         psArr->n = nalloc;
    145         psArr->nalloc = nalloc;
    146         psArr->arr = psRealloc(psArr->arr, nalloc*sizeof(float));   // For increase in psArray size
    147     } else if(nalloc > psArr->nalloc) {
    148         psArr->nalloc = nalloc;
    149         psArr->arr = psRealloc(psArr->arr, nalloc*sizeof(float));
    150     } else if(psArr == NULL) {                                      // For creating new psArray with realloc
    151         psArr = psFloatArrayAlloc(nalloc);
    152     }
    153 
    154     return psArr;
    155 }
    156 
    157 void psFloatArrayFree(psFloatArray *restrict psArr)
    158 {
    159     if (psArr == NULL) {
    160         return;
    161     }
    162     psFree(psArr->arr);
    163     psFree(psArr);
    164 }
    165 
    166 psDoubleArray* psDoubleArrayAlloc(int nalloc)
    167 {
    168     psDoubleArray *psArr = NULL;
    169     psArr = (psDoubleArray*)psAlloc(sizeof(psDoubleArray));
    170     psArr->nalloc = nalloc;
    171     psArr->n = 0;
    172     psArr->arr = (double*)p_psArrayAlloc(&psArr->type, PS_TYPE_DOUBLE, nalloc, sizeof(double));
    173 
    174     return psArr;
    175 }
    176 
    177 psDoubleArray *psDoubleArrayRealloc(psDoubleArray *psArr, int nalloc)
    178 {
    179     if(nalloc < psArr->n) {                                         // For decrease in psArray size
    180         psArr->n = nalloc;
    181         psArr->nalloc = nalloc;
    182         psArr->arr = psRealloc(psArr->arr, nalloc*sizeof(double));  // For increase in psArray size
    183     } else if(nalloc > psArr->nalloc) {
    184         psArr->nalloc = nalloc;
    185         psArr->arr = psRealloc(psArr->arr, nalloc*sizeof(double));
    186     } else if(psArr == NULL) {                                      // For creating new psArray with realloc
    187         psArr = psDoubleArrayAlloc(nalloc);
    188     }
    189 
    190     return psArr;
    191 }
    192 
    193 void psDoubleArrayFree(psDoubleArray *restrict psArr)
    194 {
    195     if (psArr == NULL) {
    196         return;
    197     }
    198     psFree(psArr->arr);
    199     psFree(psArr);
    200 }
    201 
    202 psComplexArray* psComplexArrayAlloc(int nalloc)
    203 {
    204     psComplexArray *psArr = NULL;
    205     psArr = (psComplexArray*)psAlloc(sizeof(psComplexArray));
    206     psArr->nalloc = nalloc;
    207     psArr->n = 0;
    208     psArr->arr = (complex float*)p_psArrayAlloc(&psArr->type, PS_TYPE_COMPLEX, nalloc, sizeof(complex float));
    209 
    210     return psArr;
    211 }
    212 
    213 psComplexArray *psComplexArrayRealloc(psComplexArray *psArr, int nalloc)
    214 {
    215     if(nalloc < psArr->n) {                                                 // For decrease in psArray size
    216         psArr->n = nalloc;
    217         psArr->nalloc = nalloc;
    218         psArr->arr = psRealloc(psArr->arr, nalloc*sizeof(complex float));   // For increase in psArray size
    219     } else if(nalloc > psArr->nalloc) {
    220         psArr->nalloc = nalloc;
    221         psArr->arr = psRealloc(psArr->arr, nalloc*sizeof(complex float));
    222     } else if(psArr == NULL) {                                              // For creating new psArray with realloc
    223         psArr = psComplexArrayAlloc(nalloc);
    224     }
    225 
    226     return psArr;
    227 }
    228 
    229 void psComplexArrayFree(psComplexArray *restrict psArr)
    230 {
    231     if (psArr == NULL) {
    232         return;
    233     }
    234     psFree(psArr->arr);
    235     psFree(psArr);
    236 }
    237 
    238 psVoidPtrArray* psVoidPtrArrayAlloc(int nalloc)
    239 {
    240     psVoidPtrArray *psArr = NULL;
    241     psArr = (psVoidPtrArray*)psAlloc(sizeof(psVoidPtrArray));
    242     psArr->nalloc = nalloc;
    243     psArr->n = 0;
    244     psArr->arr = (void*)p_psArrayAlloc(&psArr->type, PS_TYPE_OTHER, nalloc, sizeof(void*));
    245 
    246     return psArr;
    247 }
    248 
    249 psVoidPtrArray *psVoidPtrArrayRealloc(psVoidPtrArray *restrict psArr, int nalloc, void (*elemFree)(void *))
    250 {
    251     int i = 0;
    252 
    253     if(psArr == NULL) {
    254         return psArr;
    255     }
    256 
    257     for (i = nalloc; i < psArr->n; i++) {
    258         if(elemFree == NULL) {
    259             psMemDecrRefCounter(psArr->arr[i]);
    260         } else {
    261             elemFree(psMemDecrRefCounter(psArr->arr[i]));
    262         }
    263     }
    264 
    265     return p_psVoidPtrArrayRealloc(psArr, nalloc);
    266 }
    267 
    268 void psVoidPtrArrayFree(psVoidPtrArray *restrict psArr, void (*elemFree)(void *))
     212    } else if(psArr->nalloc != nalloc) {                // No need to realloc to same size
     213        psArr->arr.ptrArr = p_psArrayRealloc(psArr, psArr->arr.ptrArr, nalloc, sizeof(void *));
     214    }
     215
     216    return psArr;
     217}
     218
     219void psVoidPtrArrayFree(psArray *restrict psArr, void (*elemFree)(void *))
    269220{
    270221    int i = 0;
     
    276227    for(i = 0; i < psArr->nalloc; i++) {
    277228        if(elemFree == NULL) {
    278             psMemDecrRefCounter(psArr->arr[i]);
     229            psMemDecrRefCounter(psArr->arr.ptrArr[i]);
    279230        } else {
    280             elemFree(psMemDecrRefCounter(psArr->arr[i]));
     231            elemFree(psMemDecrRefCounter(psArr->arr.ptrArr[i]));
    281232        }
    282233    }
    283     p_psVoidPtrArrayFree(psArr);
    284 }
    285 
     234
     235    // Free pointer array
     236    psFree(psArr->arr.ptrArr);
     237    psFree(psArr);
     238}
     239
  • trunk/psLib/src/collections/psArray.h

    r511 r578  
    88 *  @author Ross Harman, MHPCC
    99 *   
    10  *  @version $Revision: 1.7 $ $Name: not supported by cvs2svn $
    11  *  @date $Date: 2004-04-23 21:33:59 $
     10 *  @version $Revision: 1.8 $ $Name: not supported by cvs2svn $
     11 *  @date $Date: 2004-05-05 20:43:57 $
    1212 *
    1313 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    7575psType;
    7676
    77 /** An array of integers.
    78  *
    79  * Struct for maintaining an array of integer numbers.
     77/** An array of generic primitive types.
     78 *
     79 * Struct for maintaining an array of frequently used primitive types.
    8080 *
    8181 */
    8282typedef struct
    8383{
    84     psType type;    ///< Type of data.
    85     int nalloc;     ///< Total number of elements available.
    86     int n;          ///< Number of elements in use.
    87     int *arr;       ///< Array data.
     84    psType type;                ///< Type of data.
     85    int nalloc;                 ///< Total number of elements available.
     86    int n;                      ///< Number of elements in use.
     87
     88    union {
     89        int *intArr;            ///< Integer array.
     90        float *fltArr;          ///< Single precision floating point array.
     91        double *dblArr;         ///< Double precision floating point array.
     92        complex float *cFltArr; ///< Single precision floating pont complex array.
     93        void **ptrArr;          ///< Void pointer array.
     94    }arr;                       ///< Union with array data.
    8895}
    89 psIntArray;
    90 
    91 /** An array of floats.
    92  *
    93  * Struct for maintaining an array of single precision floating point numbers.
    94  *
    95  */
    96 typedef struct
    97 {
    98     psType type;    ///< Type of data.
    99     int nalloc;     ///< Total number of elements available.
    100     int n;          ///< Number of elements in use.
    101     float *arr;     ///< Array data.
    102 }
    103 psFloatArray;
    104 
    105 /** An array of doubles.
    106  *
    107  * Struct for maintaining an array of double precision floating point numbers.
    108  *
    109  */
    110 typedef struct
    111 {
    112     psType type;    ///< Type of data.
    113     int nalloc;     ///< Total number of elements available.
    114     int n;          ///< Number of elements in use.
    115     double *arr;    ///< Array data.
    116 }
    117 psDoubleArray;
    118 
    119 /** An array of complex numbers.
    120  *
    121  * Struct for maintaining an array of single precision floating point complex numbers.
    122  *
    123  */
    124 typedef struct
    125 {
    126     psType type;        ///< Type of data.
    127     int nalloc;         ///< Total number of elements available.
    128     int n;              ///< Number of elements in use
    129     complex float *arr; ///< Array data.
    130 }
    131 psComplexArray;
    132 
    133 /** An array of void pointers.
    134  *
    135  * Struct for maintaining an array of void pointers. This struct needs a deallocation function that accepts
    136  * deallocation of the array elements.
    137  *
    138  */
    139 typedef struct
    140 {
    141     psType type;    ///< Type of data.
    142     int nalloc;     ///< Number of total elements.
    143     int n;          ///< Number of elements in use.
    144     void **arr;     ///< Aray data.
    145 }
    146 psVoidPtrArray;
    147 
     96psArray;
    14897
    14998/*****************************************************************************/
     
    155104 * Uses psLib memory allocation functions to create an array of integers as defined by the psIntArray struct.
    156105 *
    157  */
    158 psIntArray *psIntArrayAlloc(
     106 * @return psArray* Pointer to psArray.
     107 *
     108 */
     109psArray *psIntArrayAlloc(
    159110    int nalloc  ///< Total number of elements to make available.
    160111);
     
    165116 * struct.
    166117 *
    167  */
    168 psIntArray *psIntArrayRealloc(
    169     psIntArray *myArray,    ///< Array to reallocate.
     118 * @return psArray* Pointer to psArray.
     119 *
     120 */
     121psArray *psIntArrayRealloc(
     122    psArray *myArray,       ///< Array to reallocate.
    170123    int nalloc              ///< Total number of elements to make available.
    171124);
     
    177130 * struct.
    178131 *
     132 * @return void
     133 *
    179134 */
    180135void psIntArrayFree(
    181     psIntArray *restrict psArr  ///< Array to free.
     136    psArray *restrict psArr  ///< Array to free.
    182137);
    183138
     
    186141 * Uses psLib memory allocation functions to create an array of floats as defined by the psFloatArray struct.
    187142 *
    188  */
    189 psFloatArray *psFloatArrayAlloc(
     143 * @return psArray* Pointer to psArray.
     144 *
     145 */
     146psArray *psFloatArrayAlloc(
    190147    int nalloc  ///< Total number of elements to make available.
    191148);
     
    196153 * struct.
    197154 *
    198  */
    199 psFloatArray *psFloatArrayRealloc(
    200     psFloatArray *psArr,    ///< Array to reallocate.
     155 * @return psArray* Pointer to psArray.
     156 *
     157 */
     158psArray *psFloatArrayRealloc(
     159    psArray *psArr,         ///< Array to reallocate.
    201160    int nalloc              ///< Total number of elements to make available.
    202161);
     
    207166 * struct.
    208167 *
     168 * @return void
     169 *
    209170 */
    210171void psFloatArrayFree(
    211     psFloatArray *restrict myArray  ///< Array to free.
     172    psArray *restrict myArray  ///< Array to free.
    212173);
    213174
     
    217178 * struct.
    218179 *
    219  */
    220 psDoubleArray *psDoubleArrayAlloc(
     180 * @return psArray* Pointer to psArray.
     181 *
     182 */
     183psArray *psDoubleArrayAlloc(
    221184    int nalloc  ///< Total number of elements to make available.
    222185);
     
    227190 * struct.
    228191 *
    229  */
    230 psDoubleArray *psDoubleArrayRealloc(
    231     psDoubleArray *psArr,   ///< Array to reallocate.
     192 * @return psArray* Pointer to psArray.
     193 *
     194 */
     195psArray *psDoubleArrayRealloc(
     196    psArray *psArr,         ///< Array to reallocate.
    232197    int nalloc              ///< Total number of elements to make available.
    233198);
     
    238203 * struct.
    239204 *
     205 * @return void
     206 *
    240207 */
    241208void psDoubleArrayFree(
    242     psDoubleArray *restrict psArr  ///< Array to free.
     209    psArray *restrict psArr  ///< Array to free.
    243210);
    244211
     
    248215 * numbers as defined by the psComplexArray struct.
    249216 *
    250  */
    251 psComplexArray *psComplexArrayAlloc(int nalloc) ///< Total number of elements to make available.
     217 * @return psArray* Pointer to psArray.
     218 *
     219 */
     220psArray *psComplexArrayAlloc(int nalloc) ///< Total number of elements to make available.
    252221;
    253222
     
    257226 * numbers as defined by the psComplexArray struct.
    258227 *
    259  */
    260 psComplexArray *psComplexArrayRealloc(
    261     psComplexArray *myArray,    ///< Array to reallocate.
     228 * @return psArray* Pointer to psArray.
     229 *
     230 */
     231psArray *psComplexArrayRealloc(
     232    psArray *psArr,            ///< Array to reallocate.
    262233    int nalloc                  ///< Total number of elements to make available.
    263234);
     
    268239 * numbers as defined by the psComplexArray struct.
    269240 *
     241 * @return void
     242 *
    270243 */
    271244void psComplexArrayFree(
    272     psComplexArray *restrict psArr  ///< Array to free.
     245    psArray *restrict psArr  ///< Array to free.
    273246);
    274247
     
    278251 * struct.
    279252 *
    280  */
    281 psVoidPtrArray *psVoidPtrArrayAlloc(
     253 * @return psArray* Pointer to psArray.
     254 *
     255 */
     256psArray *psVoidPtrArrayAlloc(
    282257    int nalloc  ///< Number of elements to use.
    283258);
     
    286261 *
    287262 * Uses psLib memory allocation functions to reallocate an array of void pointers as defined by the
    288  * psVoidPtrArray struct.
    289  *
    290  */
    291 psVoidPtrArray *psVoidPtrArrayRealloc(
    292     psVoidPtrArray *restrict psArr, ///< Array to reallocate.
    293     int nalloc,                     ///< Number of elements.
    294     void (*elemFree)(void *)        ///< Callback function responsible for removing array data.
     263 * psVoidPtrArray struct. If the array size is increased or decreased, this function does not allocate or
     264 * deallocate the contents of individual array elements. The user must do this after calling this function.
     265 *
     266 * @return psArray* Pointer to psArray.
     267 *
     268 */
     269psArray *psVoidPtrArrayRealloc(
     270    psArray *restrict psArr,        ///< Void pointer array to destroy.
     271    int nalloc                      ///< Number of elements.
    295272);
    296273
     
    298275 *
    299276 * Uses psLib memory allocation functions to deallocate an array of void pointers as defined by the
    300  * psVoidPtrArray struct.
     277 * psVoidPtrArray struct. This function does not free the array elements unless the user provides a callback
     278 * function. NULL may be passed as an alternative to supplying a callback, but the user must remember to
     279 * delete the array elements afterwards. The user must not delete or deallocate the array elements prior to
     280 * calling this function, as it requires valid elements for memory reference decrementation operations.
     281 *
     282 * @return void
    301283 *
    302284 */
    303285void psVoidPtrArrayFree(
    304     psVoidPtrArray *restrict psArr,     ///< Array to destroy.
    305     void (*elemFree)(void *)            ///< Callback function responsible for removing array data.
     286    psArray *restrict psArr,            ///< Void pointer array to destroy.
     287    void (*elemFree)(void *)            ///< Optional callback function to remove array elements.
    306288);
    307289
  • trunk/psLib/src/collections/psBitSet.c

    r542 r578  
    1 /** @file  psBitMask.c
     1/** @file  psBitSet.c
    22 *
    3  *  @brief Creates an array of bits of arbitrary length.
     3 *  @brief Creates an array of bytes of arbitrary length for storing individual bits.
    44 *
    5  *  Bit masks are useful for turning options on and off. This module provides functions to create an array of
    6  *  bits of arbitrary length and manipulate them with basic binary operations. A print function is also
    7  *  provided to display the entire set of bits in binary form.
     5 *  Bit masks are useful tools for toggling various flags and options. This set of functions module provides
     6 *  a mechanism to create an array of bits of arbitrary length and manipulate them with basic binary
     7 *  operations. A print function is also provided to display the entire set of bits in binary format as a
     8 *  string.
    89 *
    910 *  @author Ross Harman, MHPCC
    1011 *   
    11  *  @version $Revision: 1.1 $ $Name: not supported by cvs2svn $
    12  *  @date $Date: 2004-04-28 17:47:24 $
     12 *  @version $Revision: 1.2 $ $Name: not supported by cvs2svn $
     13 *  @date $Date: 2004-05-05 20:43:22 $
    1314 *
    1415 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    2223#include <ctype.h>
    2324
    24 #include "psBitMask.h"
     25#include "psBitSet.h"
    2526#include "psMemory.h"
     27#include "psError.h"
     28
     29/******************************************************************************/
     30/*  DEFINE STATEMENTS                                                         */
     31/******************************************************************************/
     32
     33// None
     34
     35/******************************************************************************/
     36/*  TYPE DEFINITIONS                                                          */
     37/******************************************************************************/
     38
     39// None
     40
     41/*****************************************************************************/
     42/*  GLOBAL VARIABLES                                                         */
     43/*****************************************************************************/
     44
     45// None
     46
     47/*****************************************************************************/
     48/*  FILE STATIC VARIABLES                                                    */
     49/*****************************************************************************/
     50
     51// None
    2652
    2753/*****************************************************************************/
    2854/*  FUNCTION IMPLEMENTATION - LOCAL                                          */
    2955/*****************************************************************************/
    30 static char* getByte(int bit, const psBitMask *restrict inMask)
     56
     57/** Private function to return a byte.
     58 *
     59 *  Finds the byte containing the bit within the byte array.
     60 *
     61 *  @return  char*: Pointer to byte in which bit is contained.
     62 */
     63static char* getByte(int bit, const psBitSet *restrict inMask)
    3164{
    3265    int index = 0;
     
    3871}
    3972
     73/** Private function to create a mask.
     74 *
     75 *  Creates an eight bit mask with the given bit set. All other bits in the byte are zero. The input bit uses
     76 *  zero-based indexing, and is the cumulitive index within the array, not the localized byte's bit position.
     77 *
     78 *  @return  char*: Pointer to byte in which bit is contained.
     79 */
    4080static char mask(int bit)
    4181{
     
    4989/* FUNCTION IMPLEMENTATION - PUBLIC                                          */
    5090/*****************************************************************************/
    51 psBitMask* psBitMaskAlloc(int n)
     91psBitSet* psBitSetAlloc(int n)
    5292{
    53     psBitMask *newObj = psAlloc(sizeof(psBitMask));
     93    psBitSet *newObj = psAlloc(sizeof(psBitSet));
    5494    newObj->n = n;
    5595    newObj->bits = psAlloc(sizeof(char)*n);
     
    5999}
    60100
    61 void psBitMaskFree(psBitMask *restrict inMask)
     101void psBitSetFree(psBitSet *restrict inBitSet)
    62102{
    63     psFree(inMask->bits);
    64     psFree(inMask);
     103    psFree(inBitSet->bits);
     104    psFree(inBitSet);
    65105}
    66106
    67 psBitMask* psBitMaskSet(psBitMask *inMask, int bit)
     107psBitSet* psBitSetSet(psBitSet *inBitSet, int bit)
    68108{
    69     char* byte = getByte(bit, inMask);
     109    char* byte = getByte(bit, inBitSet);
    70110    *byte |= mask(bit);
    71111
    72     return inMask;
     112    return inBitSet;
    73113}
    74114
    75 int psBitMaskTest(const psBitMask *inMask, int bit)
     115int psBitSetTest(const psBitSet *inBitSet, int bit)
    76116{
    77     char* byte = getByte(bit, inMask);
     117    char* byte = getByte(bit, inBitSet);
    78118    if((*byte&mask(bit)) == 0) {
    79119        return 0;
     
    83123}
    84124
    85 psBitMask* psBitMaskOp(psBitMask *outMask, const psBitMask *restrict inMask1, char *operator,
    86                        const psBitMask *restrict inMask2)
     125psBitSet* psBitSetOp(psBitSet *outBitSet, const psBitSet *restrict inBitSet1, char *operator,
     126                     const psBitSet *restrict inBitSet2)
    87127{
    88128    int i = 0;
     
    93133    char *inBits2 = NULL;
    94134
    95     if(outMask == NULL) {
    96         printf("Null output psBitMask\n");
    97         return outMask;
     135    if(outBitSet == NULL) {
     136        psError(__func__, " : Line %d - Null psBitSet for outBitSet argument\n", __LINE__);
     137        return outBitSet;
    98138    }
    99139
    100     if(inMask1 == NULL) {
    101         printf("Null input psBitMask1\n");
    102         return outMask;
     140    if(inBitSet1 == NULL) {
     141        psError(__func__, " : Line %d - Null psBitSet for inBitSet1 argument\n", __LINE__);
     142        return outBitSet;
    103143    }
    104144
    105145    if(operator == NULL) {
    106         printf("Null input operator\n");
    107         return outMask;
    108 
     146        psError(__func__, " : Line %d - Null input operator\n", __LINE__);
     147        return outBitSet;
    109148    }
    110149
    111     if(inMask2 == NULL) {
    112         printf("Null input psBitMask2\n");
    113         return outMask;
     150    if(inBitSet2 == NULL) {
     151        psError(__func__, " : Line %d - Null psBitSet for inBitSet2 argument\n", __LINE__);
     152        return outBitSet;
    114153    }
    115154
    116     if(inMask1->n != inMask2->n || outMask->n != inMask1->n) {
    117         printf("Mask sizes not the same\n");
    118         return outMask;
     155    if(inBitSet1->n != inBitSet2->n || outBitSet->n != inBitSet1->n) {
     156        psError(__func__, " : Line %d - psBitSet sizes not the same\n", __LINE__);
     157        return outBitSet;
    119158    }
    120159
    121     n = outMask->n;
    122     outBits = outMask->bits;
    123     inBits1 = inMask1->bits;
    124     inBits2 = inMask2->bits;
     160    n = outBitSet->n;
     161    outBits = outBitSet->bits;
     162    inBits1 = inBitSet1->bits;
     163    inBits2 = inBitSet2->bits;
    125164
    126165    tempChar = toupper(*operator);
     
    142181        break;
    143182    default:
    144         printf("Invalid psBitMask option %s\n", operator);
     183        psError(__func__, " : Line %d - Invalid psBitMask binary operation: %s\n", __LINE__);
    145184    }
    146185
    147     return outMask;
     186    return outBitSet;
    148187}
    149188
    150 char *psBitMaskToString(const psBitMask *restrict inMask)
     189char *psBitSetToString(const psBitSet *restrict inBitSet)
    151190{
    152191    int i = 0;
    153     int numBits = inMask->n*8;
    154     char* outString = (char*)malloc(numBits);
     192    int numBits = inBitSet->n*8;
     193    char* outString = psAlloc(numBits);
    155194    for(i=0; i<numBits; i++) {
    156         outString[numBits-i-1] = (psBitMaskTest(inMask, i))?'1':'0';
     195        outString[numBits-i-1] = (psBitSetTest(inBitSet, i))?'1':'0';
    157196    }
    158197
  • trunk/psLib/src/collections/psBitSet.h

    r542 r578  
    1 /** @file  psBitMask.h
     1/** @file  psBitSet.h
    22 *
    3  *  @brief Creates an array of bits of arbitrary length.
     3 *  @brief Creates an array of bytes of arbitrary length for storing individual bits.
    44 *
    5  *  Bit masks are useful for turning options on and off. This module provides functions to create an array of
    6  *  bits of arbitrary length and manipulate them with basic binary operations. A print function is also
    7  *  provided to display the entire set of bits in binary form.
     5 *  Bit masks are useful tools for toggling various flags and options. This set of functions module provides
     6 *  a mechanism to create an array of bits of arbitrary length and manipulate them with basic binary
     7 *  operations. A print function is also provided to display the entire set of bits in binary format as a
     8 *  string.
    89 *
    910 *  @author Ross Harman, MHPCC
    1011 *   
    11  *  @version $Revision: 1.1 $ $Name: not supported by cvs2svn $
    12  *  @date $Date: 2004-04-28 17:47:56 $
     12 *  @version $Revision: 1.2 $ $Name: not supported by cvs2svn $
     13 *  @date $Date: 2004-05-05 20:43:57 $
    1314 *
    1415 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
    1516 */
    1617
    17 #ifndef PSBITMASK_H
    18 #define PSBITMASK_H
     18#ifndef PSBITSET_H
     19#define PSBITSET_H
    1920
    2021/******************************************************************************/
     
    2223/******************************************************************************/
    2324
    24 /** Struct containing array of bits and its length.
     25/** Struct containing array of bytes to hold bit data and corresponding array length.
    2526 *
    2627 *  The bits in the struct are assembled in as an array of bytes with eight bits per byte. The bits are
    27  *  arranged with the LSB in first position of the first array element.
     28 *  arranged with the LSB in first (right most) position of the first array element.
    2829 */
    2930typedef struct
     
    3233    char *bits; /**< Aray of bytes holding bits */
    3334}
    34 psBitMask;
     35psBitSet;
    3536
    3637/*****************************************************************************/
     
    3839/*****************************************************************************/
    3940
    40 /** Allocate a psBitMask.
     41/** Allocate a psBitSet.
    4142 *
    42  *  Create a psBitMask with number of bytes specified by the user. All bits are set to zero upon allocation.
     43 *  Create a psBitSet with the number of bytes specified by the user. All bits are set to zero upon
     44 *  allocation.
    4345 *
    44  *  @return  psBitMask*: Pointer to struct containing array of bits and size of array.
     46 *  @return  psBitSet*: Pointer to struct containing array of bits and size of array.
    4547 */
    46 psBitMask* psBitMaskAlloc(
    47     int n   /**< Number of bytes in array */
     48psBitSet* psBitSetAlloc(
     49    int n   /**< Number of bytes in psBitSet array */
    4850);
    4951
    50 /** Free a psBitMask
     52/** Free a psBitSet
    5153 *
    52  *  Deletes a psBitMask array and its byte count.
     54 *  Deletes a psBitSet array.
    5355 */
    54 void psBitMaskFree(
    55     psBitMask *restrict inMask  /**< Pointer to psBitMask struct to be deleted. */
     56void psBitSetFree(
     57    psBitSet *restrict inMask  /**< Pointer to psBitSet to be deleted. */
    5658);
    5759
    5860/** Set a bit.
    5961 *
    60  *  Sets a bit at a given bit location. The bit is set based on a zero index with the first bit set in
    61  *  the zero bit slot of the zero element of the byte array. As an example, setting bit 3 in an array with
    62  *  two elements would result in an psBitMask that looks like 00000000 00001000.
     62 *  Sets a bit at a given bit location, either one or zero. The bit is set based on a zero index with the
     63 *  first bit set in the zero bit slot of the zero element of the byte array. As an example, setting bit 3 in
     64 *  an array with two elements would result in an psBitSet that looks like 00000000 00001000.
    6365 *
    64  *  @return  psBitMask*: Pointer to struct containing array with bit set.
     66 *  @return  psBitSet*: Pointer to struct containing psBitSet.
    6567 */
    66 psBitMask* psBitMaskSet(
    67     psBitMask *inMask, /**< Pointer to struct to be set. */
    68     int bit            /**< Bit to be set. */
     68psBitSet* psBitSetSet(
     69    psBitSet *restrict inMask,  /**< Pointer to psBitSet to be set. */
     70    int bit                     /**< Bit to be set. */
    6971);
    7072
    7173/** Test the value of a bit.
    7274 *
    73  *  Prints the value of a bit at a given bit location. The bit printed based on a zero index with the first
    74  *  bit set in the zero bit slot of the zero element of the byte array. As an example, testing bit 3 in an array with
    75  *  two elements that looks like 00000000 00001000 would retrun a value of one, since that value was already set.
     75 *  Prints the value of a bit at a given bit location, either one or zero. The resulting bit is based on a
     76 *  zero index format with the first bit set in the zero bit slot of the zero element of the byte array
     77 *  As an example, testing bit 3 in a psBitSet with two bytes that looks like 00000000 00001000 would return a
     78 *  value of one, since that is the value that was set.
    7679 *
    7780 *  @return  int: Value of bit, either one or zero.
    7881 */
    79 int psBitMaskTest(
    80     const psBitMask *inMask,    /**< Pointer to struct to be tested. */
    81     int bit                     /**< Bit to be tested. */
     82int psBitSetTest(
     83    const psBitSet *restrict inMask,    /**< Pointer psBitSet to be tested. */
     84    int bit                             /**< Bit to be tested. */
    8285);
    8386
    84 /** Perform a binary operation on two psBitMasks
     87/** Perform a binary operation on two psBitSets
    8588 *
    86  *  Perform an AND, OR, or XOR on two psBitMasks. If the BitMasks are not the same size, the operation will not
    87  *  be performed and an error message will be printed.
     89 *  Perform an AND, OR, or XOR on two psBitSets. If the BitMasks are not the same size, the operation will not
     90 *  be performed and an error message will be logged.
    8891 *
    89  *  @return  psBitMask*: Pointer to struct containing result of binary operation.
     92 *  @return  psBitSet*: Pointer to struct containing result of binary operation.
    9093 */
    91 psBitMask* psBitMaskOp(
    92     psBitMask *outMask,                 /**< Resulting psBitMask from binary operation */
    93     const psBitMask *restrict inMask1,  /**< First psBitMask on which to operate */
    94     char *operator,                     /**< Bit operation */
    95     const psBitMask *restrict inMask2   /**< First psBitMask on which to operate */
     94psBitSet* psBitSetOp(
     95    psBitSet *restrict outMask,        /**< Resulting psBitSet from binary operation */
     96    const psBitSet *restrict inMask1,  /**< First psBitSet on which to operate */
     97    char *operator,                    /**< Bit operation */
     98    const psBitSet *restrict inMask2   /**< First psBitSet on which to operate */
    9699);
    97100
    98 /** Print the contents of a psBitMask.
     101/** Convert the psBitSet to a string of ones and zeros.
    99102 *
    100  *  Prints the contents of a psBitMask in its binary form of ones and zeros. The LSB is the left-most chracter.
     103 *  Converts the contents of a psBitSet to a string representation of its binary form of ones and zeros. The
     104 *  LSB is the right-most chracter. Each set of eight characters represents one byte.
    101105 *
    102  *  @return  char*: Pointer to character array containing binary formatted data.
     106 *  @return  char*: Pointer to character array containing string data.
    103107 */
    104 char *psBitMaskToString(
    105     const psBitMask *restrict inMask /**< psBitMask to print */
    106 );
    107 
    108 /** Private function to return a byte.
    109  *
    110  *  Finds the byte containing the bit within the byte array.
    111  *
    112  *  @return  char*: Pointer to byte in which bit is contained.
    113  */
    114 static char* getByte(
    115     int bit,                            /**< Bit to index to search. */
    116     const psBitMask *restrict inMask    /**< psBitMask to search. */
    117 );
    118 
    119 /** Private function to create a mask.
    120  *
    121  *  Creates an eight bit mask with the given bit set. All other bits in the byte are zero. The input bit uses
    122  *  zero-based indexing, and is the cumulitive index within the array, not the localized byte's bit position.
    123  *
    124  *  @return  char*: Pointer to byte in which bit is contained.
    125  */
    126 static char mask(
    127     int bit /**< Bit to set within mask */
     108char *psBitSetToString(
     109    const psBitSet *restrict inMask /**< psBitSet to convert */
    128110);
    129111
  • trunk/psLib/src/collections/psSort.c

    r486 r578  
    1414 *  @author Ross Harman, MHPCC
    1515 *   
    16  *  @version $Revision: 1.5 $ $Name: not supported by cvs2svn $
    17  *  @date $Date: 2004-04-21 00:40:32 $
     16 *  @version $Revision: 1.6 $ $Name: not supported by cvs2svn $
     17 *  @date $Date: 2004-05-05 20:43:22 $
    1818 *
    1919 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    104104/*****************************************************************************/
    105105
    106 psFloatArray *psSort(
    107     psFloatArray *restrict outArray,        /**< Sorted output array. */
    108     const psFloatArray *restrict inArray    /**< Input array to sort. */
     106psArray *psSort(
     107    psArray *restrict outArray,        /**< Sorted output array. */
     108    const psArray *restrict inArray    /**< Input array to sort. */
    109109)
    110110{
     
    129129    inN = inArray->n;
    130130    outN = outArray->n;
    131     inArr = inArray->arr;
    132     outArr = outArray->arr;
     131    inArr = inArray->arr.fltArr;
     132    outArr = outArray->arr.fltArr;
    133133    elSize = sizeof(float);
    134134
     
    139139    }
    140140
     141    if(inN == 0) {
     142        psError(__func__, " : Line %d - No elements in use for input array\n", __LINE__);
     143        return outArray;
     144    }
     145
     146    if(outN == 0) {
     147        psError(__func__, " : Line %d - No elements in use for output array\n", __LINE__);
     148        return outArray;
     149    }
     150
    141151    // Copy input array values into output array
    142152    for(i=0; i<inN; i++) {
     
    150160}
    151161
    152 psIntArray *psSortIndex(
    153     psIntArray *restrict outArray,          /**< Output array of sorted indices. */
    154     const psFloatArray *restrict inArray    /**< Input array to be sorted. */
     162psArray *psSortIndex(
     163    psArray *restrict outArray,         /**< Output array of sorted indices. */
     164    const psArray *restrict inArray     /**< Input array to be sorted. */
    155165)
    156166{
     
    163173    int *outArr = NULL;
    164174    float diff = 0.0f;
    165     psFloatArray *tmpFloatArray = NULL;
     175    psArray *tmpFloatArray = NULL;
    166176
    167177    if(outArray == NULL) {
     
    178188    inN = inArray->n;
    179189    outN = outArray->n;
    180     inArr = inArray->arr;
    181     outArr = outArray->arr;
     190    inArr = inArray->arr.fltArr;
     191    outArr = outArray->arr.intArr;
    182192
    183193    if(inN != outN) {
     
    188198
    189199    tmpFloatArray = psFloatArrayAlloc(inN);
     200    tmpFloatArray->n = inN;
    190201    tmpFloatArray = psSort(tmpFloatArray, inArray);
    191202
    192203    for(i=0; i<inN; i++) {
    193         tempVal = tmpFloatArray->arr[i];
     204        tempVal = tmpFloatArray->arr.fltArr[i];
    194205        for(j=0; j<inN; j++) {
    195206            diff = fabsf(tempVal - inArr[j]);
  • trunk/psLib/src/collections/psSort.h

    r450 r578  
    1414 *  @author Ross Harman, MHPCC
    1515 *   
    16  *  @version $Revision: 1.2 $ $Name: not supported by cvs2svn $
    17  *  @date $Date: 2004-04-19 20:10:46 $
     16 *  @version $Revision: 1.3 $ $Name: not supported by cvs2svn $
     17 *  @date $Date: 2004-05-05 20:43:57 $
    1818 *
    1919 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
    2020 */
    21  
     21
    2222#ifndef PSSORT_H
    2323#define PSSORT_H
     
    3838/* FUNCTION PROTOTYPES                                                       */
    3939/*****************************************************************************/
    40  
     40
    4141/** Sort an array of floats.
    4242 *
     
    4646 */
    4747
    48 psFloatArray *psSort(psFloatArray *restrict out, const psFloatArray *restrict inArray);
     48psArray *psSort(psArray *restrict out, const psArray *restrict inArray);
    4949
    5050/** Creates an array of indices based on sort odred of float array.
     
    5656 */
    5757
    58 psIntArray *psSortIndex(psIntArray *restrict out, const psFloatArray *restrict inArray);
     58psArray *psSortIndex(psArray *restrict out, const psArray *restrict inArray);
    5959
    6060#endif
  • trunk/psLib/src/types/psArray.c

    r502 r578  
    88 *  @author Ross Harman, MHPCC
    99 *   
    10  *  @version $Revision: 1.6 $ $Name: not supported by cvs2svn $
    11  *  @date $Date: 2004-04-22 21:15:01 $
     10 *  @version $Revision: 1.7 $ $Name: not supported by cvs2svn $
     11 *  @date $Date: 2004-05-05 20:43:22 $
    1212 *
    1313 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    4848/*****************************************************************************/
    4949
    50 static void* p_psArrayAlloc(psType *arrType, psElemType elemType, int nalloc, int elemSize)
     50static void* p_psArrayAlloc(psArray *restrict psArr, psElemType elemType, int nalloc, int elemSize)
    5151{
    5252    void *arr = NULL;
    5353
    54     arrType->dimen = PS_DIMEN_VECTOR;
    55     arrType->type = elemType;
     54    psArr->type.dimen = PS_DIMEN_VECTOR;
     55    psArr->type.type = elemType;
     56    psArr->nalloc = nalloc;
     57    psArr->n = 0;
    5658
    5759    if(nalloc != 0) {
     
    6264}
    6365
    64 static psVoidPtrArray *p_psVoidPtrArrayRealloc(psVoidPtrArray *restrict psArr, int nalloc)
    65 {
    66     if(nalloc < psArr->n) {                                         // For decrease in psArray size
     66static void* p_psArrayRealloc(psArray *restrict psArr, void *arr, int nalloc, int elemSize)
     67{
     68    // For decrease in psArray size
     69    if(nalloc < psArr->n) {
    6770        psArr->n = nalloc;
    68         psArr->nalloc = nalloc;
    69         psArr->arr = psRealloc(psArr->arr, nalloc*sizeof(void*));   // For increase in psArray size
    70     } else if(nalloc > psArr->nalloc) {
    71         psArr->nalloc = nalloc;
    72         psArr->arr = psRealloc(psArr->arr, nalloc*sizeof(void*));
    73     } else if(psArr == NULL) {                                      // For creating new psArray with realloc
     71    }
     72
     73    psArr->nalloc = nalloc;
     74
     75    return psRealloc(arr, nalloc*elemSize);
     76}
     77
     78/*****************************************************************************/
     79/* FUNCTION IMPLEMENTATION - PUBLIC                                          */
     80/*****************************************************************************/
     81psArray* psIntArrayAlloc(int nalloc)
     82{
     83    psArray *psArr = NULL;
     84    psArr = psAlloc(sizeof(psArray));
     85    psArr->arr.intArr = (int*)p_psArrayAlloc(psArr, PS_TYPE_INT, nalloc, sizeof(int));
     86    return psArr;
     87}
     88
     89psArray *psIntArrayRealloc(psArray *restrict psArr, int nalloc)
     90{
     91    if(psArr == NULL) {                                 // For creating new psArray with realloc
     92        psArr = psIntArrayAlloc(nalloc);
     93    } else if(psArr->nalloc != nalloc) {                // No need to realloc to same size
     94        psArr->arr.intArr = p_psArrayRealloc(psArr, psArr->arr.intArr, nalloc, sizeof(int));
     95    }
     96
     97    return psArr;
     98}
     99
     100void psIntArrayFree(psArray *restrict psArr)
     101{
     102    if (psArr == NULL) {
     103        return;
     104    }
     105
     106    psFree(psArr->arr.intArr);
     107    psFree(psArr);
     108}
     109
     110psArray* psFloatArrayAlloc(int nalloc)
     111{
     112    psArray *psArr = NULL;
     113    psArr = psAlloc(sizeof(psArray));
     114    psArr->arr.fltArr = (float*)p_psArrayAlloc(psArr, PS_TYPE_FLOAT, nalloc, sizeof(float));
     115    return psArr;
     116}
     117
     118psArray *psFloatArrayRealloc(psArray *restrict psArr, int nalloc)
     119{
     120    if(psArr == NULL) {                                 // For creating new psArray with realloc
     121        psArr = psFloatArrayAlloc(nalloc);
     122    } else if(psArr->nalloc != nalloc) {                // No need to realloc to same size
     123        psArr->arr.fltArr = p_psArrayRealloc(psArr, psArr->arr.fltArr, nalloc, sizeof(float));
     124    }
     125
     126    return psArr;
     127}
     128
     129void psFloatArrayFree(psArray *restrict psArr)
     130{
     131    if (psArr == NULL) {
     132        return;
     133    }
     134    psFree(psArr->arr.fltArr);
     135    psFree(psArr);
     136}
     137
     138psArray* psDoubleArrayAlloc(int nalloc)
     139{
     140    psArray *psArr = NULL;
     141    psArr = psAlloc(sizeof(psArray));
     142    psArr->arr.dblArr = (double*)p_psArrayAlloc(psArr, PS_TYPE_DOUBLE, nalloc, sizeof(double));
     143    return psArr;
     144}
     145
     146psArray *psDoubleArrayRealloc(psArray *restrict psArr, int nalloc)
     147{
     148    if(psArr == NULL) {                                 // For creating new psArray with realloc
     149        psArr = psDoubleArrayAlloc(nalloc);
     150    } else if(psArr->nalloc != nalloc) {                // No need to realloc to same size
     151        psArr->arr.dblArr = p_psArrayRealloc(psArr, psArr->arr.dblArr, nalloc, sizeof(double));
     152    }
     153
     154    return psArr;
     155}
     156
     157void psDoubleArrayFree(psArray *restrict psArr)
     158{
     159    if (psArr == NULL) {
     160        return;
     161    }
     162    psFree(psArr->arr.dblArr);
     163    psFree(psArr);
     164}
     165
     166psArray* psComplexArrayAlloc(int nalloc)
     167{
     168    psArray *psArr = NULL;
     169    psArr = psAlloc(sizeof(psArray));
     170    psArr->arr.cFltArr = (complex float*)p_psArrayAlloc(psArr, PS_TYPE_COMPLEX, nalloc, sizeof(complex float));
     171    return psArr;
     172}
     173
     174psArray *psComplexArrayRealloc(psArray *restrict psArr, int nalloc)
     175{
     176    if(psArr == NULL) {                                 // For creating new psArray with realloc
     177        psArr = psComplexArrayAlloc(nalloc);
     178    } else if(psArr->nalloc != nalloc) {                // No need to realloc to same size
     179        psArr->arr.cFltArr = p_psArrayRealloc(psArr, psArr->arr.cFltArr, nalloc, sizeof(complex float));
     180    }
     181
     182    return psArr;
     183}
     184
     185void psComplexArrayFree(psArray *restrict psArr)
     186{
     187    if (psArr == NULL) {
     188        return;
     189    }
     190    psFree(psArr->arr.cFltArr);
     191    psFree(psArr);
     192}
     193
     194psArray* psVoidPtrArrayAlloc(int nalloc)
     195{
     196    psArray *psArr = NULL;
     197    psArr = psAlloc(sizeof(psArray));
     198    psArr->arr.ptrArr = p_psArrayAlloc(psArr, PS_TYPE_OTHER, nalloc, sizeof(void *));
     199    return psArr;
     200}
     201
     202psArray *psVoidPtrArrayRealloc(psArray *restrict psArr, int nalloc)
     203{
     204    int i = 0;
     205
     206    for (i = nalloc; i < psArr->n; i++) {               // For reduction in array size
     207        psMemDecrRefCounter(psArr->arr.ptrArr[i]);
     208    }
     209
     210    if(psArr == NULL) {                                 // For creating new psArray with realloc
    74211        psArr = psVoidPtrArrayAlloc(nalloc);
    75     }
    76 
    77     return psArr;
    78 }
    79 
    80 static void p_psVoidPtrArrayFree(psVoidPtrArray *restrict psArr)
    81 {
    82     if (psArr == NULL) {
    83         return;
    84     }
    85     psFree(psArr->arr);
    86     psFree(psArr);
    87 }
    88 
    89 /*****************************************************************************/
    90 /* FUNCTION IMPLEMENTATION - PUBLIC                                          */
    91 /*****************************************************************************/
    92 
    93 psIntArray* psIntArrayAlloc(int nalloc)
    94 {
    95     psIntArray *psArr = NULL;
    96     psArr = (psIntArray*)psAlloc(sizeof(psIntArray));
    97     psArr->nalloc = nalloc;
    98     psArr->n = 0;
    99     psArr->arr = (int*)p_psArrayAlloc(&psArr->type, PS_TYPE_INT, nalloc, sizeof(int));
    100 
    101     return psArr;
    102 }
    103 
    104 psIntArray *psIntArrayRealloc(psIntArray *psArr, int nalloc)
    105 {
    106     if(nalloc < psArr->n) {                                         // For decrease in psArray size
    107         psArr->n = nalloc;
    108         psArr->nalloc = nalloc;
    109         psArr->arr = psRealloc(psArr->arr, nalloc*sizeof(int));     // For increase in psArray size
    110     } else if(nalloc > psArr->nalloc) {
    111         psArr->nalloc = nalloc;
    112         psArr->arr = psRealloc(psArr->arr, nalloc*sizeof(int));
    113     } else if(psArr == NULL) {                                      // For creating new psArray with realloc
    114         psArr = psIntArrayAlloc(nalloc);
    115     }
    116 
    117     return psArr;
    118 }
    119 
    120 void psIntArrayFree(psIntArray *restrict psArr)
    121 {
    122     if (psArr == NULL) {
    123         return;
    124     }
    125 
    126     psFree(psArr->arr);
    127     psFree(psArr);
    128 }
    129 
    130 psFloatArray* psFloatArrayAlloc(int nalloc)
    131 {
    132     psFloatArray *psArr = NULL;
    133     psArr = (psFloatArray*)psAlloc(sizeof(psFloatArray));
    134     psArr->nalloc = nalloc;
    135     psArr->n = 0;
    136     psArr->arr = (float*)p_psArrayAlloc(&psArr->type, PS_TYPE_FLOAT, nalloc, sizeof(float));
    137 
    138     return psArr;
    139 }
    140 
    141 psFloatArray *psFloatArrayRealloc(psFloatArray *psArr, int nalloc)
    142 {
    143     if(nalloc < psArr->n) {                                         // For decrease in psArray size
    144         psArr->n = nalloc;
    145         psArr->nalloc = nalloc;
    146         psArr->arr = psRealloc(psArr->arr, nalloc*sizeof(float));   // For increase in psArray size
    147     } else if(nalloc > psArr->nalloc) {
    148         psArr->nalloc = nalloc;
    149         psArr->arr = psRealloc(psArr->arr, nalloc*sizeof(float));
    150     } else if(psArr == NULL) {                                      // For creating new psArray with realloc
    151         psArr = psFloatArrayAlloc(nalloc);
    152     }
    153 
    154     return psArr;
    155 }
    156 
    157 void psFloatArrayFree(psFloatArray *restrict psArr)
    158 {
    159     if (psArr == NULL) {
    160         return;
    161     }
    162     psFree(psArr->arr);
    163     psFree(psArr);
    164 }
    165 
    166 psDoubleArray* psDoubleArrayAlloc(int nalloc)
    167 {
    168     psDoubleArray *psArr = NULL;
    169     psArr = (psDoubleArray*)psAlloc(sizeof(psDoubleArray));
    170     psArr->nalloc = nalloc;
    171     psArr->n = 0;
    172     psArr->arr = (double*)p_psArrayAlloc(&psArr->type, PS_TYPE_DOUBLE, nalloc, sizeof(double));
    173 
    174     return psArr;
    175 }
    176 
    177 psDoubleArray *psDoubleArrayRealloc(psDoubleArray *psArr, int nalloc)
    178 {
    179     if(nalloc < psArr->n) {                                         // For decrease in psArray size
    180         psArr->n = nalloc;
    181         psArr->nalloc = nalloc;
    182         psArr->arr = psRealloc(psArr->arr, nalloc*sizeof(double));  // For increase in psArray size
    183     } else if(nalloc > psArr->nalloc) {
    184         psArr->nalloc = nalloc;
    185         psArr->arr = psRealloc(psArr->arr, nalloc*sizeof(double));
    186     } else if(psArr == NULL) {                                      // For creating new psArray with realloc
    187         psArr = psDoubleArrayAlloc(nalloc);
    188     }
    189 
    190     return psArr;
    191 }
    192 
    193 void psDoubleArrayFree(psDoubleArray *restrict psArr)
    194 {
    195     if (psArr == NULL) {
    196         return;
    197     }
    198     psFree(psArr->arr);
    199     psFree(psArr);
    200 }
    201 
    202 psComplexArray* psComplexArrayAlloc(int nalloc)
    203 {
    204     psComplexArray *psArr = NULL;
    205     psArr = (psComplexArray*)psAlloc(sizeof(psComplexArray));
    206     psArr->nalloc = nalloc;
    207     psArr->n = 0;
    208     psArr->arr = (complex float*)p_psArrayAlloc(&psArr->type, PS_TYPE_COMPLEX, nalloc, sizeof(complex float));
    209 
    210     return psArr;
    211 }
    212 
    213 psComplexArray *psComplexArrayRealloc(psComplexArray *psArr, int nalloc)
    214 {
    215     if(nalloc < psArr->n) {                                                 // For decrease in psArray size
    216         psArr->n = nalloc;
    217         psArr->nalloc = nalloc;
    218         psArr->arr = psRealloc(psArr->arr, nalloc*sizeof(complex float));   // For increase in psArray size
    219     } else if(nalloc > psArr->nalloc) {
    220         psArr->nalloc = nalloc;
    221         psArr->arr = psRealloc(psArr->arr, nalloc*sizeof(complex float));
    222     } else if(psArr == NULL) {                                              // For creating new psArray with realloc
    223         psArr = psComplexArrayAlloc(nalloc);
    224     }
    225 
    226     return psArr;
    227 }
    228 
    229 void psComplexArrayFree(psComplexArray *restrict psArr)
    230 {
    231     if (psArr == NULL) {
    232         return;
    233     }
    234     psFree(psArr->arr);
    235     psFree(psArr);
    236 }
    237 
    238 psVoidPtrArray* psVoidPtrArrayAlloc(int nalloc)
    239 {
    240     psVoidPtrArray *psArr = NULL;
    241     psArr = (psVoidPtrArray*)psAlloc(sizeof(psVoidPtrArray));
    242     psArr->nalloc = nalloc;
    243     psArr->n = 0;
    244     psArr->arr = (void*)p_psArrayAlloc(&psArr->type, PS_TYPE_OTHER, nalloc, sizeof(void*));
    245 
    246     return psArr;
    247 }
    248 
    249 psVoidPtrArray *psVoidPtrArrayRealloc(psVoidPtrArray *restrict psArr, int nalloc, void (*elemFree)(void *))
    250 {
    251     int i = 0;
    252 
    253     if(psArr == NULL) {
    254         return psArr;
    255     }
    256 
    257     for (i = nalloc; i < psArr->n; i++) {
    258         if(elemFree == NULL) {
    259             psMemDecrRefCounter(psArr->arr[i]);
    260         } else {
    261             elemFree(psMemDecrRefCounter(psArr->arr[i]));
    262         }
    263     }
    264 
    265     return p_psVoidPtrArrayRealloc(psArr, nalloc);
    266 }
    267 
    268 void psVoidPtrArrayFree(psVoidPtrArray *restrict psArr, void (*elemFree)(void *))
     212    } else if(psArr->nalloc != nalloc) {                // No need to realloc to same size
     213        psArr->arr.ptrArr = p_psArrayRealloc(psArr, psArr->arr.ptrArr, nalloc, sizeof(void *));
     214    }
     215
     216    return psArr;
     217}
     218
     219void psVoidPtrArrayFree(psArray *restrict psArr, void (*elemFree)(void *))
    269220{
    270221    int i = 0;
     
    276227    for(i = 0; i < psArr->nalloc; i++) {
    277228        if(elemFree == NULL) {
    278             psMemDecrRefCounter(psArr->arr[i]);
     229            psMemDecrRefCounter(psArr->arr.ptrArr[i]);
    279230        } else {
    280             elemFree(psMemDecrRefCounter(psArr->arr[i]));
     231            elemFree(psMemDecrRefCounter(psArr->arr.ptrArr[i]));
    281232        }
    282233    }
    283     p_psVoidPtrArrayFree(psArr);
    284 }
    285 
     234
     235    // Free pointer array
     236    psFree(psArr->arr.ptrArr);
     237    psFree(psArr);
     238}
     239
  • trunk/psLib/src/types/psArray.h

    r511 r578  
    88 *  @author Ross Harman, MHPCC
    99 *   
    10  *  @version $Revision: 1.7 $ $Name: not supported by cvs2svn $
    11  *  @date $Date: 2004-04-23 21:33:59 $
     10 *  @version $Revision: 1.8 $ $Name: not supported by cvs2svn $
     11 *  @date $Date: 2004-05-05 20:43:57 $
    1212 *
    1313 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    7575psType;
    7676
    77 /** An array of integers.
    78  *
    79  * Struct for maintaining an array of integer numbers.
     77/** An array of generic primitive types.
     78 *
     79 * Struct for maintaining an array of frequently used primitive types.
    8080 *
    8181 */
    8282typedef struct
    8383{
    84     psType type;    ///< Type of data.
    85     int nalloc;     ///< Total number of elements available.
    86     int n;          ///< Number of elements in use.
    87     int *arr;       ///< Array data.
     84    psType type;                ///< Type of data.
     85    int nalloc;                 ///< Total number of elements available.
     86    int n;                      ///< Number of elements in use.
     87
     88    union {
     89        int *intArr;            ///< Integer array.
     90        float *fltArr;          ///< Single precision floating point array.
     91        double *dblArr;         ///< Double precision floating point array.
     92        complex float *cFltArr; ///< Single precision floating pont complex array.
     93        void **ptrArr;          ///< Void pointer array.
     94    }arr;                       ///< Union with array data.
    8895}
    89 psIntArray;
    90 
    91 /** An array of floats.
    92  *
    93  * Struct for maintaining an array of single precision floating point numbers.
    94  *
    95  */
    96 typedef struct
    97 {
    98     psType type;    ///< Type of data.
    99     int nalloc;     ///< Total number of elements available.
    100     int n;          ///< Number of elements in use.
    101     float *arr;     ///< Array data.
    102 }
    103 psFloatArray;
    104 
    105 /** An array of doubles.
    106  *
    107  * Struct for maintaining an array of double precision floating point numbers.
    108  *
    109  */
    110 typedef struct
    111 {
    112     psType type;    ///< Type of data.
    113     int nalloc;     ///< Total number of elements available.
    114     int n;          ///< Number of elements in use.
    115     double *arr;    ///< Array data.
    116 }
    117 psDoubleArray;
    118 
    119 /** An array of complex numbers.
    120  *
    121  * Struct for maintaining an array of single precision floating point complex numbers.
    122  *
    123  */
    124 typedef struct
    125 {
    126     psType type;        ///< Type of data.
    127     int nalloc;         ///< Total number of elements available.
    128     int n;              ///< Number of elements in use
    129     complex float *arr; ///< Array data.
    130 }
    131 psComplexArray;
    132 
    133 /** An array of void pointers.
    134  *
    135  * Struct for maintaining an array of void pointers. This struct needs a deallocation function that accepts
    136  * deallocation of the array elements.
    137  *
    138  */
    139 typedef struct
    140 {
    141     psType type;    ///< Type of data.
    142     int nalloc;     ///< Number of total elements.
    143     int n;          ///< Number of elements in use.
    144     void **arr;     ///< Aray data.
    145 }
    146 psVoidPtrArray;
    147 
     96psArray;
    14897
    14998/*****************************************************************************/
     
    155104 * Uses psLib memory allocation functions to create an array of integers as defined by the psIntArray struct.
    156105 *
    157  */
    158 psIntArray *psIntArrayAlloc(
     106 * @return psArray* Pointer to psArray.
     107 *
     108 */
     109psArray *psIntArrayAlloc(
    159110    int nalloc  ///< Total number of elements to make available.
    160111);
     
    165116 * struct.
    166117 *
    167  */
    168 psIntArray *psIntArrayRealloc(
    169     psIntArray *myArray,    ///< Array to reallocate.
     118 * @return psArray* Pointer to psArray.
     119 *
     120 */
     121psArray *psIntArrayRealloc(
     122    psArray *myArray,       ///< Array to reallocate.
    170123    int nalloc              ///< Total number of elements to make available.
    171124);
     
    177130 * struct.
    178131 *
     132 * @return void
     133 *
    179134 */
    180135void psIntArrayFree(
    181     psIntArray *restrict psArr  ///< Array to free.
     136    psArray *restrict psArr  ///< Array to free.
    182137);
    183138
     
    186141 * Uses psLib memory allocation functions to create an array of floats as defined by the psFloatArray struct.
    187142 *
    188  */
    189 psFloatArray *psFloatArrayAlloc(
     143 * @return psArray* Pointer to psArray.
     144 *
     145 */
     146psArray *psFloatArrayAlloc(
    190147    int nalloc  ///< Total number of elements to make available.
    191148);
     
    196153 * struct.
    197154 *
    198  */
    199 psFloatArray *psFloatArrayRealloc(
    200     psFloatArray *psArr,    ///< Array to reallocate.
     155 * @return psArray* Pointer to psArray.
     156 *
     157 */
     158psArray *psFloatArrayRealloc(
     159    psArray *psArr,         ///< Array to reallocate.
    201160    int nalloc              ///< Total number of elements to make available.
    202161);
     
    207166 * struct.
    208167 *
     168 * @return void
     169 *
    209170 */
    210171void psFloatArrayFree(
    211     psFloatArray *restrict myArray  ///< Array to free.
     172    psArray *restrict myArray  ///< Array to free.
    212173);
    213174
     
    217178 * struct.
    218179 *
    219  */
    220 psDoubleArray *psDoubleArrayAlloc(
     180 * @return psArray* Pointer to psArray.
     181 *
     182 */
     183psArray *psDoubleArrayAlloc(
    221184    int nalloc  ///< Total number of elements to make available.
    222185);
     
    227190 * struct.
    228191 *
    229  */
    230 psDoubleArray *psDoubleArrayRealloc(
    231     psDoubleArray *psArr,   ///< Array to reallocate.
     192 * @return psArray* Pointer to psArray.
     193 *
     194 */
     195psArray *psDoubleArrayRealloc(
     196    psArray *psArr,         ///< Array to reallocate.
    232197    int nalloc              ///< Total number of elements to make available.
    233198);
     
    238203 * struct.
    239204 *
     205 * @return void
     206 *
    240207 */
    241208void psDoubleArrayFree(
    242     psDoubleArray *restrict psArr  ///< Array to free.
     209    psArray *restrict psArr  ///< Array to free.
    243210);
    244211
     
    248215 * numbers as defined by the psComplexArray struct.
    249216 *
    250  */
    251 psComplexArray *psComplexArrayAlloc(int nalloc) ///< Total number of elements to make available.
     217 * @return psArray* Pointer to psArray.
     218 *
     219 */
     220psArray *psComplexArrayAlloc(int nalloc) ///< Total number of elements to make available.
    252221;
    253222
     
    257226 * numbers as defined by the psComplexArray struct.
    258227 *
    259  */
    260 psComplexArray *psComplexArrayRealloc(
    261     psComplexArray *myArray,    ///< Array to reallocate.
     228 * @return psArray* Pointer to psArray.
     229 *
     230 */
     231psArray *psComplexArrayRealloc(
     232    psArray *psArr,            ///< Array to reallocate.
    262233    int nalloc                  ///< Total number of elements to make available.
    263234);
     
    268239 * numbers as defined by the psComplexArray struct.
    269240 *
     241 * @return void
     242 *
    270243 */
    271244void psComplexArrayFree(
    272     psComplexArray *restrict psArr  ///< Array to free.
     245    psArray *restrict psArr  ///< Array to free.
    273246);
    274247
     
    278251 * struct.
    279252 *
    280  */
    281 psVoidPtrArray *psVoidPtrArrayAlloc(
     253 * @return psArray* Pointer to psArray.
     254 *
     255 */
     256psArray *psVoidPtrArrayAlloc(
    282257    int nalloc  ///< Number of elements to use.
    283258);
     
    286261 *
    287262 * Uses psLib memory allocation functions to reallocate an array of void pointers as defined by the
    288  * psVoidPtrArray struct.
    289  *
    290  */
    291 psVoidPtrArray *psVoidPtrArrayRealloc(
    292     psVoidPtrArray *restrict psArr, ///< Array to reallocate.
    293     int nalloc,                     ///< Number of elements.
    294     void (*elemFree)(void *)        ///< Callback function responsible for removing array data.
     263 * psVoidPtrArray struct. If the array size is increased or decreased, this function does not allocate or
     264 * deallocate the contents of individual array elements. The user must do this after calling this function.
     265 *
     266 * @return psArray* Pointer to psArray.
     267 *
     268 */
     269psArray *psVoidPtrArrayRealloc(
     270    psArray *restrict psArr,        ///< Void pointer array to destroy.
     271    int nalloc                      ///< Number of elements.
    295272);
    296273
     
    298275 *
    299276 * Uses psLib memory allocation functions to deallocate an array of void pointers as defined by the
    300  * psVoidPtrArray struct.
     277 * psVoidPtrArray struct. This function does not free the array elements unless the user provides a callback
     278 * function. NULL may be passed as an alternative to supplying a callback, but the user must remember to
     279 * delete the array elements afterwards. The user must not delete or deallocate the array elements prior to
     280 * calling this function, as it requires valid elements for memory reference decrementation operations.
     281 *
     282 * @return void
    301283 *
    302284 */
    303285void psVoidPtrArrayFree(
    304     psVoidPtrArray *restrict psArr,     ///< Array to destroy.
    305     void (*elemFree)(void *)            ///< Callback function responsible for removing array data.
     286    psArray *restrict psArr,            ///< Void pointer array to destroy.
     287    void (*elemFree)(void *)            ///< Optional callback function to remove array elements.
    306288);
    307289
  • trunk/psLib/src/types/psBitSet.c

    r542 r578  
    1 /** @file  psBitMask.c
     1/** @file  psBitSet.c
    22 *
    3  *  @brief Creates an array of bits of arbitrary length.
     3 *  @brief Creates an array of bytes of arbitrary length for storing individual bits.
    44 *
    5  *  Bit masks are useful for turning options on and off. This module provides functions to create an array of
    6  *  bits of arbitrary length and manipulate them with basic binary operations. A print function is also
    7  *  provided to display the entire set of bits in binary form.
     5 *  Bit masks are useful tools for toggling various flags and options. This set of functions module provides
     6 *  a mechanism to create an array of bits of arbitrary length and manipulate them with basic binary
     7 *  operations. A print function is also provided to display the entire set of bits in binary format as a
     8 *  string.
    89 *
    910 *  @author Ross Harman, MHPCC
    1011 *   
    11  *  @version $Revision: 1.1 $ $Name: not supported by cvs2svn $
    12  *  @date $Date: 2004-04-28 17:47:24 $
     12 *  @version $Revision: 1.2 $ $Name: not supported by cvs2svn $
     13 *  @date $Date: 2004-05-05 20:43:22 $
    1314 *
    1415 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    2223#include <ctype.h>
    2324
    24 #include "psBitMask.h"
     25#include "psBitSet.h"
    2526#include "psMemory.h"
     27#include "psError.h"
     28
     29/******************************************************************************/
     30/*  DEFINE STATEMENTS                                                         */
     31/******************************************************************************/
     32
     33// None
     34
     35/******************************************************************************/
     36/*  TYPE DEFINITIONS                                                          */
     37/******************************************************************************/
     38
     39// None
     40
     41/*****************************************************************************/
     42/*  GLOBAL VARIABLES                                                         */
     43/*****************************************************************************/
     44
     45// None
     46
     47/*****************************************************************************/
     48/*  FILE STATIC VARIABLES                                                    */
     49/*****************************************************************************/
     50
     51// None
    2652
    2753/*****************************************************************************/
    2854/*  FUNCTION IMPLEMENTATION - LOCAL                                          */
    2955/*****************************************************************************/
    30 static char* getByte(int bit, const psBitMask *restrict inMask)
     56
     57/** Private function to return a byte.
     58 *
     59 *  Finds the byte containing the bit within the byte array.
     60 *
     61 *  @return  char*: Pointer to byte in which bit is contained.
     62 */
     63static char* getByte(int bit, const psBitSet *restrict inMask)
    3164{
    3265    int index = 0;
     
    3871}
    3972
     73/** Private function to create a mask.
     74 *
     75 *  Creates an eight bit mask with the given bit set. All other bits in the byte are zero. The input bit uses
     76 *  zero-based indexing, and is the cumulitive index within the array, not the localized byte's bit position.
     77 *
     78 *  @return  char*: Pointer to byte in which bit is contained.
     79 */
    4080static char mask(int bit)
    4181{
     
    4989/* FUNCTION IMPLEMENTATION - PUBLIC                                          */
    5090/*****************************************************************************/
    51 psBitMask* psBitMaskAlloc(int n)
     91psBitSet* psBitSetAlloc(int n)
    5292{
    53     psBitMask *newObj = psAlloc(sizeof(psBitMask));
     93    psBitSet *newObj = psAlloc(sizeof(psBitSet));
    5494    newObj->n = n;
    5595    newObj->bits = psAlloc(sizeof(char)*n);
     
    5999}
    60100
    61 void psBitMaskFree(psBitMask *restrict inMask)
     101void psBitSetFree(psBitSet *restrict inBitSet)
    62102{
    63     psFree(inMask->bits);
    64     psFree(inMask);
     103    psFree(inBitSet->bits);
     104    psFree(inBitSet);
    65105}
    66106
    67 psBitMask* psBitMaskSet(psBitMask *inMask, int bit)
     107psBitSet* psBitSetSet(psBitSet *inBitSet, int bit)
    68108{
    69     char* byte = getByte(bit, inMask);
     109    char* byte = getByte(bit, inBitSet);
    70110    *byte |= mask(bit);
    71111
    72     return inMask;
     112    return inBitSet;
    73113}
    74114
    75 int psBitMaskTest(const psBitMask *inMask, int bit)
     115int psBitSetTest(const psBitSet *inBitSet, int bit)
    76116{
    77     char* byte = getByte(bit, inMask);
     117    char* byte = getByte(bit, inBitSet);
    78118    if((*byte&mask(bit)) == 0) {
    79119        return 0;
     
    83123}
    84124
    85 psBitMask* psBitMaskOp(psBitMask *outMask, const psBitMask *restrict inMask1, char *operator,
    86                        const psBitMask *restrict inMask2)
     125psBitSet* psBitSetOp(psBitSet *outBitSet, const psBitSet *restrict inBitSet1, char *operator,
     126                     const psBitSet *restrict inBitSet2)
    87127{
    88128    int i = 0;
     
    93133    char *inBits2 = NULL;
    94134
    95     if(outMask == NULL) {
    96         printf("Null output psBitMask\n");
    97         return outMask;
     135    if(outBitSet == NULL) {
     136        psError(__func__, " : Line %d - Null psBitSet for outBitSet argument\n", __LINE__);
     137        return outBitSet;
    98138    }
    99139
    100     if(inMask1 == NULL) {
    101         printf("Null input psBitMask1\n");
    102         return outMask;
     140    if(inBitSet1 == NULL) {
     141        psError(__func__, " : Line %d - Null psBitSet for inBitSet1 argument\n", __LINE__);
     142        return outBitSet;
    103143    }
    104144
    105145    if(operator == NULL) {
    106         printf("Null input operator\n");
    107         return outMask;
    108 
     146        psError(__func__, " : Line %d - Null input operator\n", __LINE__);
     147        return outBitSet;
    109148    }
    110149
    111     if(inMask2 == NULL) {
    112         printf("Null input psBitMask2\n");
    113         return outMask;
     150    if(inBitSet2 == NULL) {
     151        psError(__func__, " : Line %d - Null psBitSet for inBitSet2 argument\n", __LINE__);
     152        return outBitSet;
    114153    }
    115154
    116     if(inMask1->n != inMask2->n || outMask->n != inMask1->n) {
    117         printf("Mask sizes not the same\n");
    118         return outMask;
     155    if(inBitSet1->n != inBitSet2->n || outBitSet->n != inBitSet1->n) {
     156        psError(__func__, " : Line %d - psBitSet sizes not the same\n", __LINE__);
     157        return outBitSet;
    119158    }
    120159
    121     n = outMask->n;
    122     outBits = outMask->bits;
    123     inBits1 = inMask1->bits;
    124     inBits2 = inMask2->bits;
     160    n = outBitSet->n;
     161    outBits = outBitSet->bits;
     162    inBits1 = inBitSet1->bits;
     163    inBits2 = inBitSet2->bits;
    125164
    126165    tempChar = toupper(*operator);
     
    142181        break;
    143182    default:
    144         printf("Invalid psBitMask option %s\n", operator);
     183        psError(__func__, " : Line %d - Invalid psBitMask binary operation: %s\n", __LINE__);
    145184    }
    146185
    147     return outMask;
     186    return outBitSet;
    148187}
    149188
    150 char *psBitMaskToString(const psBitMask *restrict inMask)
     189char *psBitSetToString(const psBitSet *restrict inBitSet)
    151190{
    152191    int i = 0;
    153     int numBits = inMask->n*8;
    154     char* outString = (char*)malloc(numBits);
     192    int numBits = inBitSet->n*8;
     193    char* outString = psAlloc(numBits);
    155194    for(i=0; i<numBits; i++) {
    156         outString[numBits-i-1] = (psBitMaskTest(inMask, i))?'1':'0';
     195        outString[numBits-i-1] = (psBitSetTest(inBitSet, i))?'1':'0';
    157196    }
    158197
  • trunk/psLib/src/types/psBitSet.h

    r542 r578  
    1 /** @file  psBitMask.h
     1/** @file  psBitSet.h
    22 *
    3  *  @brief Creates an array of bits of arbitrary length.
     3 *  @brief Creates an array of bytes of arbitrary length for storing individual bits.
    44 *
    5  *  Bit masks are useful for turning options on and off. This module provides functions to create an array of
    6  *  bits of arbitrary length and manipulate them with basic binary operations. A print function is also
    7  *  provided to display the entire set of bits in binary form.
     5 *  Bit masks are useful tools for toggling various flags and options. This set of functions module provides
     6 *  a mechanism to create an array of bits of arbitrary length and manipulate them with basic binary
     7 *  operations. A print function is also provided to display the entire set of bits in binary format as a
     8 *  string.
    89 *
    910 *  @author Ross Harman, MHPCC
    1011 *   
    11  *  @version $Revision: 1.1 $ $Name: not supported by cvs2svn $
    12  *  @date $Date: 2004-04-28 17:47:56 $
     12 *  @version $Revision: 1.2 $ $Name: not supported by cvs2svn $
     13 *  @date $Date: 2004-05-05 20:43:57 $
    1314 *
    1415 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
    1516 */
    1617
    17 #ifndef PSBITMASK_H
    18 #define PSBITMASK_H
     18#ifndef PSBITSET_H
     19#define PSBITSET_H
    1920
    2021/******************************************************************************/
     
    2223/******************************************************************************/
    2324
    24 /** Struct containing array of bits and its length.
     25/** Struct containing array of bytes to hold bit data and corresponding array length.
    2526 *
    2627 *  The bits in the struct are assembled in as an array of bytes with eight bits per byte. The bits are
    27  *  arranged with the LSB in first position of the first array element.
     28 *  arranged with the LSB in first (right most) position of the first array element.
    2829 */
    2930typedef struct
     
    3233    char *bits; /**< Aray of bytes holding bits */
    3334}
    34 psBitMask;
     35psBitSet;
    3536
    3637/*****************************************************************************/
     
    3839/*****************************************************************************/
    3940
    40 /** Allocate a psBitMask.
     41/** Allocate a psBitSet.
    4142 *
    42  *  Create a psBitMask with number of bytes specified by the user. All bits are set to zero upon allocation.
     43 *  Create a psBitSet with the number of bytes specified by the user. All bits are set to zero upon
     44 *  allocation.
    4345 *
    44  *  @return  psBitMask*: Pointer to struct containing array of bits and size of array.
     46 *  @return  psBitSet*: Pointer to struct containing array of bits and size of array.
    4547 */
    46 psBitMask* psBitMaskAlloc(
    47     int n   /**< Number of bytes in array */
     48psBitSet* psBitSetAlloc(
     49    int n   /**< Number of bytes in psBitSet array */
    4850);
    4951
    50 /** Free a psBitMask
     52/** Free a psBitSet
    5153 *
    52  *  Deletes a psBitMask array and its byte count.
     54 *  Deletes a psBitSet array.
    5355 */
    54 void psBitMaskFree(
    55     psBitMask *restrict inMask  /**< Pointer to psBitMask struct to be deleted. */
     56void psBitSetFree(
     57    psBitSet *restrict inMask  /**< Pointer to psBitSet to be deleted. */
    5658);
    5759
    5860/** Set a bit.
    5961 *
    60  *  Sets a bit at a given bit location. The bit is set based on a zero index with the first bit set in
    61  *  the zero bit slot of the zero element of the byte array. As an example, setting bit 3 in an array with
    62  *  two elements would result in an psBitMask that looks like 00000000 00001000.
     62 *  Sets a bit at a given bit location, either one or zero. The bit is set based on a zero index with the
     63 *  first bit set in the zero bit slot of the zero element of the byte array. As an example, setting bit 3 in
     64 *  an array with two elements would result in an psBitSet that looks like 00000000 00001000.
    6365 *
    64  *  @return  psBitMask*: Pointer to struct containing array with bit set.
     66 *  @return  psBitSet*: Pointer to struct containing psBitSet.
    6567 */
    66 psBitMask* psBitMaskSet(
    67     psBitMask *inMask, /**< Pointer to struct to be set. */
    68     int bit            /**< Bit to be set. */
     68psBitSet* psBitSetSet(
     69    psBitSet *restrict inMask,  /**< Pointer to psBitSet to be set. */
     70    int bit                     /**< Bit to be set. */
    6971);
    7072
    7173/** Test the value of a bit.
    7274 *
    73  *  Prints the value of a bit at a given bit location. The bit printed based on a zero index with the first
    74  *  bit set in the zero bit slot of the zero element of the byte array. As an example, testing bit 3 in an array with
    75  *  two elements that looks like 00000000 00001000 would retrun a value of one, since that value was already set.
     75 *  Prints the value of a bit at a given bit location, either one or zero. The resulting bit is based on a
     76 *  zero index format with the first bit set in the zero bit slot of the zero element of the byte array
     77 *  As an example, testing bit 3 in a psBitSet with two bytes that looks like 00000000 00001000 would return a
     78 *  value of one, since that is the value that was set.
    7679 *
    7780 *  @return  int: Value of bit, either one or zero.
    7881 */
    79 int psBitMaskTest(
    80     const psBitMask *inMask,    /**< Pointer to struct to be tested. */
    81     int bit                     /**< Bit to be tested. */
     82int psBitSetTest(
     83    const psBitSet *restrict inMask,    /**< Pointer psBitSet to be tested. */
     84    int bit                             /**< Bit to be tested. */
    8285);
    8386
    84 /** Perform a binary operation on two psBitMasks
     87/** Perform a binary operation on two psBitSets
    8588 *
    86  *  Perform an AND, OR, or XOR on two psBitMasks. If the BitMasks are not the same size, the operation will not
    87  *  be performed and an error message will be printed.
     89 *  Perform an AND, OR, or XOR on two psBitSets. If the BitMasks are not the same size, the operation will not
     90 *  be performed and an error message will be logged.
    8891 *
    89  *  @return  psBitMask*: Pointer to struct containing result of binary operation.
     92 *  @return  psBitSet*: Pointer to struct containing result of binary operation.
    9093 */
    91 psBitMask* psBitMaskOp(
    92     psBitMask *outMask,                 /**< Resulting psBitMask from binary operation */
    93     const psBitMask *restrict inMask1,  /**< First psBitMask on which to operate */
    94     char *operator,                     /**< Bit operation */
    95     const psBitMask *restrict inMask2   /**< First psBitMask on which to operate */
     94psBitSet* psBitSetOp(
     95    psBitSet *restrict outMask,        /**< Resulting psBitSet from binary operation */
     96    const psBitSet *restrict inMask1,  /**< First psBitSet on which to operate */
     97    char *operator,                    /**< Bit operation */
     98    const psBitSet *restrict inMask2   /**< First psBitSet on which to operate */
    9699);
    97100
    98 /** Print the contents of a psBitMask.
     101/** Convert the psBitSet to a string of ones and zeros.
    99102 *
    100  *  Prints the contents of a psBitMask in its binary form of ones and zeros. The LSB is the left-most chracter.
     103 *  Converts the contents of a psBitSet to a string representation of its binary form of ones and zeros. The
     104 *  LSB is the right-most chracter. Each set of eight characters represents one byte.
    101105 *
    102  *  @return  char*: Pointer to character array containing binary formatted data.
     106 *  @return  char*: Pointer to character array containing string data.
    103107 */
    104 char *psBitMaskToString(
    105     const psBitMask *restrict inMask /**< psBitMask to print */
    106 );
    107 
    108 /** Private function to return a byte.
    109  *
    110  *  Finds the byte containing the bit within the byte array.
    111  *
    112  *  @return  char*: Pointer to byte in which bit is contained.
    113  */
    114 static char* getByte(
    115     int bit,                            /**< Bit to index to search. */
    116     const psBitMask *restrict inMask    /**< psBitMask to search. */
    117 );
    118 
    119 /** Private function to create a mask.
    120  *
    121  *  Creates an eight bit mask with the given bit set. All other bits in the byte are zero. The input bit uses
    122  *  zero-based indexing, and is the cumulitive index within the array, not the localized byte's bit position.
    123  *
    124  *  @return  char*: Pointer to byte in which bit is contained.
    125  */
    126 static char mask(
    127     int bit /**< Bit to set within mask */
     108char *psBitSetToString(
     109    const psBitSet *restrict inMask /**< psBitSet to convert */
    128110);
    129111
Note: See TracChangeset for help on using the changeset viewer.