IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Sep 14, 2004, 10:01:52 AM (22 years ago)
Author:
desonia
Message:

Updated files in src/collections to use new psError functionality. Also
cleaned up the code where needed, removing unnecessary error conditions,
etc.

File:
1 edited

Legend:

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

    r1440 r1807  
    1 
    21/** @file  psBitSet.c
    32 *
     
    109 *
    1110 *  @author Ross Harman, MHPCC
    12  *
    13  *  @version $Revision: 1.16 $ $Name: not supported by cvs2svn $
    14  *  @date $Date: 2004-08-09 23:34:57 $
     11 *  @author Robert DeSonia, MHPCC
     12 *
     13 *  @version $Revision: 1.17 $ $Name: not supported by cvs2svn $
     14 *  @date $Date: 2004-09-14 20:01:52 $
    1515 *
    1616 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
    1717 */
    1818
    19 /******************************************************************************/
    20 
    21 /*  INCLUDE FILES                                                             */
    22 
    23 /******************************************************************************/
    2419#include <string.h>
    2520#include <stdio.h>
     
    3227#include "psError.h"
    3328#include "psAbort.h"
    34 
    35 /******************************************************************************/
    36 
    37 /*  DEFINE STATEMENTS                                                         */
    38 
    39 /******************************************************************************/
    40 
    41 // None
    42 
    43 /******************************************************************************/
    44 
    45 /*  TYPE DEFINITIONS                                                          */
    46 
    47 /******************************************************************************/
    48 
    49 // None
    50 
    51 /*****************************************************************************/
    52 
    53 /*  GLOBAL VARIABLES                                                         */
    54 
    55 /*****************************************************************************/
    56 
    57 // None
    58 
    59 /*****************************************************************************/
    60 
    61 /*  FILE STATIC VARIABLES                                                    */
    62 
    63 /*****************************************************************************/
    64 
    65 // None
    66 
    67 /*****************************************************************************/
    68 
    69 /*  FUNCTION IMPLEMENTATION - LOCAL                                          */
    70 
    71 /*****************************************************************************/
    72 static void psBitSetFree(psBitSet* restrict inBitSet);
     29#include "psString.h"
     30
     31#include "psCollectionsErrors.h"
     32
     33enum {
     34    UNKNOWN_OP,
     35    AND_OP,
     36    OR_OP,
     37    XOR_OP,
     38    NOT_OP
     39};
     40
     41static void bitSetFree(psBitSet* restrict inBitSet);
    7342
    7443/** Private function to create a mask.
     
    9059}
    9160
    92 /*****************************************************************************/
    93 
    94 /* FUNCTION IMPLEMENTATION - PUBLIC                                          */
    95 
    96 /*****************************************************************************/
     61static void bitSetFree(psBitSet* restrict inBitSet)
     62{
     63    if (inBitSet == NULL) {
     64        return;
     65    }
     66    psFree(inBitSet->bits);
     67}
     68
    9769psBitSet* psBitSetAlloc(int n)
    9870{
     
    10072    psBitSet* newObj = NULL;
    10173
    102     if (n <= 0) {
    103         psError(__func__, " : Line %d - Allocation size must be > 0: size = %d", __LINE__, n);
    104         return 0;
     74    if (n < 0) {
     75        psErrorMsg(PS_ERRORNAME_DOMAIN "psBitSetAlloc",
     76                   PS_ERR_BAD_PARAMETER_VALUE, true,
     77                   PS_ERRORTEXT_psBitSet_ALLOC_NEG_SIZE,
     78                   n);
     79        return NULL;
    10580    }
    10681
    10782    numBytes = ceil(n / 8.0);
    10883    newObj = psAlloc(sizeof(psBitSet));
    109     if (newObj == NULL) {
    110         psAbort(__func__, " : Line %d - Failed to allocate memory", __LINE__);
    111     }
    112     p_psMemSetDeallocator(newObj, (psFreeFcn) psBitSetFree);
     84    p_psMemSetDeallocator(newObj, (psFreeFcn) bitSetFree);
    11385    newObj->n = numBytes;
    11486
     
    11688    /* @i@ */
    11789    newObj->bits = psAlloc(sizeof(char) * numBytes);
    118     if (newObj->bits == NULL) {
    119         psAbort(__func__, " : Line %d - Failed to allocate memory", __LINE__);
    120     }
    121 
    122     memset(newObj->bits, numBytes, 0);
     90
     91    memset(newObj->bits, 0, numBytes);
    12392
    12493    return newObj;
    12594}
    12695
    127 static void psBitSetFree(psBitSet* restrict inBitSet)
    128 {
    129     if (inBitSet == NULL) {
    130         psError(__func__, " : Line %d - Null psBitSet for inBitSet argument", __LINE__);
    131         return;
    132     }
    133     psFree(inBitSet->bits);
    134 }
    135 
    13696psBitSet* psBitSetSet(psBitSet* inBitSet, int bit)
    13797{
     
    13999
    140100    if (inBitSet == NULL) {
    141         psError(__func__, " : Line %d - Null psBitSet for inBitSet argument", __LINE__);
     101        psErrorMsg(PS_ERRORNAME_DOMAIN "psBitSetSet",
     102                   PS_ERR_BAD_PARAMETER_NULL, true,
     103                   PS_ERRORTEXT_psBitSet_SET_NULL);
    142104        return inBitSet;
    143     } else if (bit < 0) {
    144         psError(__func__, " : Line %d - Bit position too small: %d", __LINE__, bit);
    145         return inBitSet;
    146     } else if (bit > inBitSet->n * 8 - 1) {
    147         psError(__func__, " : Line %d - Bit position too large: %d", __LINE__, bit);
     105    } else if ( (bit < 0) ||
     106                (bit > inBitSet->n * 8 - 1) ) {
     107        psErrorMsg(PS_ERRORNAME_DOMAIN "psBitSetSet",
     108                   PS_ERR_BAD_PARAMETER_VALUE, true,
     109                   PS_ERRORTEXT_psBitSet_BIT_OUTOFRANGE,
     110                   bit,inBitSet->n * 8 - 1);
    148111        return inBitSet;
    149112    }
     
    155118}
    156119
     120psBitSet* psBitSetClear(psBitSet* inBitSet, int bit)
     121{
     122    char *byte = NULL;
     123
     124    if (inBitSet == NULL) {
     125        psErrorMsg(PS_ERRORNAME_DOMAIN "psBitSetClear",
     126                   PS_ERR_BAD_PARAMETER_NULL, true,
     127                   PS_ERRORTEXT_psBitSet_SET_NULL);
     128        return inBitSet;
     129    } else if ( (bit < 0) ||
     130                (bit > inBitSet->n * 8 - 1) ) {
     131        psErrorMsg(PS_ERRORNAME_DOMAIN "psBitSetClear",
     132                   PS_ERR_BAD_PARAMETER_VALUE, true,
     133                   PS_ERRORTEXT_psBitSet_BIT_OUTOFRANGE,
     134                   bit,inBitSet->n * 8 - 1);
     135        return inBitSet;
     136    }
     137    // Variable byte is the byte in the array that contains the bit to be set
     138    byte = inBitSet->bits + bit / 8;
     139    *byte &= ! mask(bit);
     140
     141    return inBitSet;
     142}
     143
    157144bool psBitSetTest(const psBitSet* inBitSet, int bit)
    158145{
     
    160147
    161148    if (inBitSet == NULL) {
    162         psError(__func__, " : Line %d - Null psBitSet for inBitSet argument", __LINE__);
    163         return 0;
    164     } else if (bit < 0) {
    165         psError(__func__, " : Line %d - Bit position too small: %d", __LINE__, bit);
    166         return 0;
    167     } else if (bit > inBitSet->n * 8 - 1) {
    168         psError(__func__, " : Line %d - Bit position too large: %d", __LINE__, bit);
    169         return 0;
    170     }
     149        psErrorMsg(PS_ERRORNAME_DOMAIN "psBitSetTest",
     150                   PS_ERR_BAD_PARAMETER_NULL, true,
     151                   PS_ERRORTEXT_psBitSet_SET_NULL);
     152        return false;
     153    } else if ( (bit < 0) ||
     154                (bit > inBitSet->n * 8 - 1) ) {
     155        psErrorMsg(PS_ERRORNAME_DOMAIN "psBitSetTest",
     156                   PS_ERR_BAD_PARAMETER_VALUE, true,
     157                   PS_ERRORTEXT_psBitSet_BIT_OUTOFRANGE,
     158                   bit,inBitSet->n * 8 - 1);
     159        return false;
     160    }
     161
    171162    // Variable byte is the byte in the array that contains the bit to be tested
    172163    byte = inBitSet->bits + bit / 8;
    173     if ((int)(*byte & mask(bit)) == 0) {
    174         return 0;
    175     }
    176 
    177     return 1;
     164    return ((*byte & mask(bit)) != 0);
    178165}
    179166
     
    183170    int i = 0;
    184171    int n = 0;
    185     char tempChar = '0';
    186     char *outBits = NULL;
    187     char *inBits1 = NULL;
    188     char *inBits2 = NULL;
     172    char* outBits = NULL;
     173    char* inBits1 = NULL;
     174    char* inBits2 = NULL;
     175    int op = UNKNOWN_OP;
    189176
    190177    if (inBitSet1 == NULL) {
    191         psError(__func__, " : Line %d - Null psBitSet for inBitSet1 argument", __LINE__);
    192         return outBitSet;
    193     }
     178        psErrorMsg(PS_ERRORNAME_DOMAIN "psBitSetOp",
     179                   PS_ERR_BAD_PARAMETER_NULL, true,
     180                   PS_ERRORTEXT_psBitSet_FIRST_OPERAND_NULL);
     181        psFree(outBitSet);
     182        return NULL;
     183    }
     184    inBits1 = inBitSet1->bits;
    194185
    195186    if (operator == NULL) {
    196         psError(__func__, " : Line %d - Null input operator\n", __LINE__);
    197         return outBitSet;
    198     }
    199 
    200     if (inBitSet2 == NULL) {
    201         psError(__func__, " : Line %d - Null psBitSet for inBitSet2 argument", __LINE__);
    202         return outBitSet;
     187        psErrorMsg(PS_ERRORNAME_DOMAIN "psBitSetOp",
     188                   PS_ERR_BAD_PARAMETER_NULL, true,
     189                   PS_ERRORTEXT_psBitSet_OPERATOR_NULL);
     190        psFree(outBitSet);
     191        return NULL;
     192    }
     193
     194    // make operator all caps
     195    int tempStrLen = strlen(operator);
     196    char* tempStr = psAlloc(tempStrLen+1);
     197
     198    for (int lcv=0;lcv<tempStrLen;lcv++) {
     199        tempStr[lcv] = (char)toupper(operator[lcv]);
     200    }
     201    tempStr[tempStrLen] = '\0';
     202
     203    // parse the operator
     204    if (strcmp(operator,"AND")==0) {
     205        op = AND_OP;
     206    } else if (strcmp(operator,"OR")==0) {
     207        op = OR_OP;
     208    } else if (strcmp(operator,"XOR")==0) {
     209        op = XOR_OP;
     210    } else if (strcmp(operator,"NOT")==0) {
     211        op = NOT_OP;
     212    } else {
     213        psFree(tempStr);
     214        psFree(outBitSet);
     215        psErrorMsg(PS_ERRORNAME_DOMAIN "psBitSetOp",
     216                   PS_ERR_BAD_PARAMETER_VALUE, true,
     217                   PS_ERRORTEXT_psBitSet_OPERATOR_INVALID,
     218                   operator);
     219        return NULL;
     220    }
     221    psFree(tempStr);
     222
     223    if (op != NOT_OP) {
     224        if (inBitSet2 == NULL) {
     225            psErrorMsg(PS_ERRORNAME_DOMAIN "psBitSetOp",
     226                       PS_ERR_BAD_PARAMETER_NULL, true,
     227                       PS_ERRORTEXT_psBitSet_SECOND_OPERAND_NULL);
     228            psFree(outBitSet);
     229            return NULL;
     230        }
     231
     232        if (inBitSet1->n != inBitSet2->n) {
     233            psErrorMsg(PS_ERRORNAME_DOMAIN "psBitSetOp",
     234                       PS_ERR_BAD_PARAMETER_SIZE, true,
     235                       PS_ERRORTEXT_psBitSet_OPERANDS_SIZE_DIFFER);
     236            psFree(outBitSet);
     237            return NULL;
     238        }
     239        inBits2 = inBitSet2->bits;
    203240    }
    204241
    205242    if (outBitSet == NULL) {
    206         outBitSet = psBitSetAlloc(inBitSet1->n * 8);
    207     }
    208 
    209     if (inBitSet1->n != inBitSet2->n || outBitSet->n != inBitSet1->n) {
    210         psError(__func__, " : Line %d - psBitSet sizes not the same", __LINE__);
    211         return outBitSet;
     243        outBitSet = psBitSetAlloc(inBitSet1->n*8);
     244    } else if (outBitSet->n != inBitSet1->n) {
     245        outBitSet->n = inBitSet1->n;
     246        outBitSet->bits = psRealloc(outBitSet->bits, inBitSet1->n);
    212247    }
    213248
    214249    n = outBitSet->n;
    215250    outBits = outBitSet->bits;
    216     inBits1 = inBitSet1->bits;
    217     inBits2 = inBitSet2->bits;
    218 
    219     tempChar = toupper(operator[0]);
    220     switch (tempChar) {
    221     case 'A':
     251
     252    switch (op) {
     253    case AND_OP:
    222254        for (i = 0; i < n; i++) {
    223255            outBits[i] = inBits1[i] & inBits2[i];
    224256        }
    225257        break;
    226     case 'O':
     258    case OR_OP:
    227259        for (i = 0; i < n; i++) {
    228260            outBits[i] = inBits1[i] | inBits2[i];
    229261        }
    230262        break;
    231     case 'X':
     263    case XOR_OP:
    232264        for (i = 0; i < n; i++) {
    233265            outBits[i] = inBits1[i] ^ inBits2[i];
    234266        }
    235267        break;
     268    case NOT_OP:
     269        for (i = 0; i < n; i++) {
     270            outBits[i] = ~inBits1[i];
     271        }
     272        break;
    236273    default:
    237         psError(__func__, " : Line %d - Invalid psBitMask binary operation: %s", __LINE__, operator);
     274        psAbort(PS_ERRORNAME_DOMAIN "psBitSetOp",
     275                "Unexpected error - operator parsed successfully but not valid?");
    238276    }
    239277
     
    243281psBitSet* psBitSetNot(psBitSet* outBitSet, const psBitSet* restrict inBitSet)
    244282{
    245     int i = 0;
    246     int n = 0;
    247     char *outBits = NULL;
    248     char *inBits = NULL;
    249 
    250     if (inBitSet == NULL) {
    251         psError(__func__, " : Line %d - Null psBitSet for inBitSet argument", __LINE__);
    252         return outBitSet;
    253     }
    254 
    255     n = inBitSet->n;
    256     if (n == 0) {
    257         psError(__func__, " : Line %d - No elements in inBitSet", __LINE__);
    258         return outBitSet;
    259     }
     283    if (inBitSet == NULL) {
     284        psErrorMsg(PS_ERRORNAME_DOMAIN "psBitSetNot",
     285                   PS_ERR_BAD_PARAMETER_NULL, true,
     286                   PS_ERRORTEXT_psBitSet_OPERAND_NULL);
     287        psFree(outBitSet);
     288        return NULL;
     289    }
     290
     291    outBitSet = psBitSetOp(outBitSet,inBitSet,"NOT",NULL);
    260292
    261293    if (outBitSet == NULL) {
    262         outBitSet = psBitSetAlloc(n * 8);
    263     }
    264 
    265     if (inBitSet->n != outBitSet->n) {
    266         psError(__func__, " : Line %d - psBitSet sizes not the same", __LINE__);
    267         return outBitSet;
    268     }
    269 
    270     outBits = outBitSet->bits;
    271     inBits = inBitSet->bits;
    272 
    273     for (i = 0; i < n; i++) {
    274         outBits[i] = ~inBits[i];
     294        psErrorMsg(PS_ERRORNAME_DOMAIN "psBitSetNot",
     295                   PS_ERR_UNKNOWN, false,
     296                   PS_ERRORTEXT_psBitSet_NOT_OP_FAILED);
    275297    }
    276298
     
    284306    char *outString = psAlloc((size_t) numBits + 1);
    285307
    286     if (outString == NULL) {
    287         psAbort(__func__, " : Line %d - Failed to allocate memory", __LINE__);
    288     }
    289 
    290308    for (i = 0; i < numBits; i++) {
    291         outString[numBits - i - 1] = (psBitSetTest(inBitSet, i) == 1) ? '1' : '0';
     309        outString[numBits - i - 1] = psBitSetTest(inBitSet, i) ? '1' : '0';
    292310    }
    293311
Note: See TracChangeset for help on using the changeset viewer.