Changeset 438
- Timestamp:
- Apr 16, 2004, 4:43:59 PM (22 years ago)
- Location:
- trunk/psLib/src/collections
- Files:
-
- 4 edited
-
Makefile (modified) (1 diff)
-
psBitMask.c (modified) (4 diffs)
-
psBitMask.h (modified) (1 diff)
-
psSort.c (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/collections/Makefile
r437 r438 13 13 psBitMask.o \ 14 14 psSort.o \ 15 16 INCLUDES = -I$(includedir) 17 18 %.o: %.c 19 @echo " Compiling $<. " 20 $(CC) $(CFLAGS) $(INCLUDE_GLOBAL) $(INCLUDES) -c $< -o $@ 15 21 16 22 libpsCollections.a: $(SRC_OBJS) -
trunk/psLib/src/collections/psBitMask.c
r437 r438 1 #include "psBitMask.h" 1 /** @file psBitMask.c 2 * 3 * @brief Creates an array of bits of arbitrary length. 4 * 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. 8 * 9 * @author Ross Harman, MHPCC 10 * 11 * @version $Revision: 1.2 $ $Name: not supported by cvs2svn $ 12 * @date $Date: 2004-04-17 02:43:59 $ 13 * 14 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii 15 */ 2 16 17 /******************************************************************************/ 18 /* INCLUDE FILES */ 19 /******************************************************************************/ 3 20 #include <stdlib.h> 4 21 #include <string.h> … … 6 23 #include <ctype.h> 7 24 8 static char* getByte(int bit, const psBitMask* inMask); 9 static char mask(int bit); 25 #include "psBitMask.h" 26 //#include "psMemory.h" 10 27 28 /*****************************************************************************/ 29 /* FUNCTION IMPLEMENTATION - LOCAL */ 30 /*****************************************************************************/ 31 static char* getByte(int bit, const psBitMask *restrict inMask) 32 { 33 int index = 0; 34 char *byte = 0; 35 index = bit/8; 36 byte = inMask->bits+index; 37 38 return byte; 39 } 40 41 static char mask(int bit) 42 { 43 char mask = 0x01; 44 mask = mask << (bit%8); 45 46 return mask; 47 } 48 49 /*****************************************************************************/ 50 /* FUNCTION IMPLEMENTATION - PUBLIC */ 51 /*****************************************************************************/ 11 52 psBitMask* psBitMaskAlloc(int n) 12 53 { 54 // psBitMask *newObj = (psBitMask*)psAlloc(sizeof(psBitMask)); 13 55 psBitMask *newObj = (psBitMask*)malloc(sizeof(psBitMask)); 14 56 newObj->n = n; … … 43 85 } 44 86 45 psBitMask* psBitMaskOp(psBitMask *outMask, const psBitMask *restrict inMask1, char *operator, const psBitMask *restrict inMask2) 87 psBitMask* psBitMaskOp(psBitMask *outMask, const psBitMask *restrict inMask1, char *operator, 88 const psBitMask *restrict inMask2) 46 89 { 47 90 int i = 0; … … 118 161 return outString; 119 162 } 120 121 /* 0 based index of byte array's byte that contains containing bit */122 static char* getByte(int bit, const psBitMask* inMask)123 {124 int index = 0;125 char *byte = 0;126 index = bit/8;127 byte = inMask->bits+index;128 129 return byte;130 }131 132 static char mask(int bit)133 {134 char mask = 0x01;135 mask = mask << (bit%8);136 137 return mask;138 } -
trunk/psLib/src/collections/psBitMask.h
r437 r438 1 /** @file psBitMask.h 2 * 3 * @brief Creates an array of bits of arbitrary length. 4 * 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. 8 * 9 * @author Ross Harman, MHPCC 10 * 11 * @version $Revision: 1.2 $ $Name: not supported by cvs2svn $ 12 * @date $Date: 2004-04-17 02:43:59 $ 13 * 14 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii 15 */ 16 17 #ifndef PSBITMASK_H 18 #define PSBITMASK_H 19 20 /******************************************************************************/ 21 /* TYPE DEFINITIONS */ 22 /******************************************************************************/ 23 24 /** Struct containing array of bits and its length. 25 * 26 * 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 */ 1 29 typedef struct { 2 int n; 3 char *bits; 30 int n; /**< Number of bytes in the array */ 31 char *bits; /**< Aray of bytes holding bits */ 4 32 } psBitMask; 5 33 6 psBitMask* psBitMaskAlloc(int n); 7 void psBitMaskFree(psBitMask *restrict inMask); 8 psBitMask* psBitMaskSet(psBitMask *inMask, int bit); 9 int psBitMaskTest(const psBitMask *inMask, int bit); 10 psBitMask* psBitMaskOp(psBitMask *outMask, const psBitMask *restrict inMask1, char *operator, const psBitMask *restrict inMask2); 11 char *psBitMaskToString(const psBitMask *restrict inMask); 12 static char* getByte(int bit, const psBitMask* inMask); 13 static char mask(int bit); 34 /*****************************************************************************/ 35 /* FUNCTION PROTOTYPES */ 36 /*****************************************************************************/ 37 38 /** Allocate a psBitMask. 39 * 40 * Create a psBitMask with number of bytes specified by the user. All bits are set to zero upon allocation. 41 * 42 * @return psBitMask*: Pointer to struct containing array of bits and size of array. 43 */ 44 psBitMask* psBitMaskAlloc( 45 int n /**< Number of bytes in array */ 46 ); 47 48 /** Free a psBitMask 49 * 50 * Deletes a psBitMask array and its byte count. 51 */ 52 void psBitMaskFree( 53 psBitMask *restrict inMask /**< Pointer to psBitMask struct to be deleted. */ 54 ); 55 56 /** Set a bit. 57 * 58 * Sets a bit at a given bit location. The bit is set based on a zero index with the first bit set in 59 * the zero bit slot of the zero element of the byte array. As an example, setting bit 3 in an array with 60 * two elements would result in an psBitMask that looks like 00000000 00001000. 61 * 62 * @return psBitMask*: Pointer to struct containing array with bit set. 63 */ 64 psBitMask* psBitMaskSet( 65 psBitMask *inMask, /**< Pointer to struct to be set. */ 66 int bit /**< Bit to be set. */ 67 ); 68 69 /** Test the value of a bit. 70 * 71 * Prints the value of a bit at a given bit location. The bit printed based on a zero index with the first 72 * 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 73 * two elements that looks like 00000000 00001000 would retrun a value of one, since that value was already set. 74 * 75 * @return int: Value of bit, either one or zero. 76 */ 77 int psBitMaskTest( 78 const psBitMask *inMask, /**< Pointer to struct to be tested. */ 79 int bit /**< Bit to be tested. */ 80 ); 81 82 /** Perform a binary operation on two psBitMasks 83 * 84 * Perform an AND, OR, or XOR on two psBitMasks. If the BitMasks are not the same size, the operation will not 85 * be performed and an error message will be printed. 86 * 87 * @return psBitMask*: Pointer to struct containing result of binary operation. 88 */ 89 psBitMask* psBitMaskOp( 90 psBitMask *outMask, /**< Resulting psBitMask from binary operation */ 91 const psBitMask *restrict inMask1, /**< First psBitMask on which to operate */ 92 char *operator, /**< Bit operation */ 93 const psBitMask *restrict inMask2 /**< First psBitMask on which to operate */ 94 ); 95 96 /** Print the contents of a psBitMask. 97 * 98 * Prints the contents of a psBitMask in its binary form of ones and zeros. The LSB is the left-most chracter. 99 * 100 * @return char*: Pointer to character array containing binary formatted data. 101 */ 102 char *psBitMaskToString( 103 const psBitMask *restrict inMask /**< psBitMask to print */ 104 ); 105 106 /** Private function to return a byte. 107 * 108 * Finds the byte containing the bit within the byte array. 109 * 110 * @return char*: Pointer to byte in which bit is contained. 111 */ 112 static char* getByte( 113 int bit, /**< Bit to index to search. */ 114 const psBitMask *restrict inMask /**< psBitMask to search. */ 115 ); 116 117 /** Private function to create a mask. 118 * 119 * Creates an eight bit mask with the given bit set. All other bits in the byte are zero. The input bit uses 120 * zero-based indexing, and is the cumulitive index within the array, not the localized byte's bit position. 121 * 122 * @return char*: Pointer to byte in which bit is contained. 123 */ 124 static char mask( 125 int bit /**< Bit to set within mask */ 126 ); 127 128 #endif -
trunk/psLib/src/collections/psSort.c
r437 r438 1 #include "psArray.h" 2 #include "psSort.h" 1 /** @file psSort.h 2 * 3 * @brief Sorts an array of floats 4 * 5 * The psSort functions use the qsort() stdlib function with a user-defined callback to sort an array of 6 * floats. The qsort function requires the starting point of the array, number of elements in the array, size 7 * of each element, and a pointer to a callback comparison function. Once called, qsort() will sort the array 8 * contents in ascending order according to the comparison function. The comparison function is called with 9 * two arguments that point to the objects being compared - in this case two floats. The comparison function 10 * must return an integer less than, equal to, or greater than zero if the first argument is considered to be 11 * respectively less than, equal to, or greater than the second. If two members compare as equal, their order 12 * in the sorted array is undefined. 13 * 14 * @author Ross Harman, MHPCC 15 * 16 * @version $Revision: 1.2 $ $Name: not supported by cvs2svn $ 17 * @date $Date: 2004-04-17 02:43:59 $ 18 * 19 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii 20 */ 21 22 /******************************************************************************/ 23 /* INCLUDE FILES */ 24 /******************************************************************************/ 3 25 4 26 #include <stdio.h> … … 7 29 #include <math.h> 8 30 #include <float.h> 9 /* qsort - sorts an array */ 10 /* The qsort() function sorts an array with nmemb elements of size size. 11 The base argument points to the start of the array. 12 13 The contents of the array are sorted in ascending order according to a 14 comparison function pointed to by compar, which is called with two 15 arguments that point to the objects being compared. 16 17 The comparison function must return an integer less than, equal to, or 18 greater than zero if the first argument is considered to be respec- 19 tively less than, equal to, or greater than the second. If two members 20 compare as equal, their order in the sorted array is undefined. 21 */ 22 static int psCompare(const void *x, const void *y); 23 24 25 psFloatArray *psSort(psFloatArray *restrict outArray, const psFloatArray *restrict inArray) 31 32 #include "psArray.h" 33 #include "psSort.h" 34 35 /******************************************************************************/ 36 /* DEFINE STATEMENTS */ 37 /******************************************************************************/ 38 39 // None 40 41 /******************************************************************************/ 42 /* TYPE DEFINITIONS */ 43 /******************************************************************************/ 44 45 // None 46 47 /*****************************************************************************/ 48 /* GLOBAL VARIABLES */ 49 /*****************************************************************************/ 50 51 // None 52 53 /*****************************************************************************/ 54 /* FILE STATIC VARIABLES */ 55 /*****************************************************************************/ 56 57 // None 58 59 /*****************************************************************************/ 60 /* FUNCTION IMPLEMENTATION - LOCAL */ 61 /*****************************************************************************/ 62 63 /** Private callback comparison function to compare two floats. 64 * 65 * The comparison function is called by qsort() with two arguments that point to the objects being 66 * compared. The comparison function must return an integer less than, equal to, or greater than zero if 67 * the first argument is considered to be respectively less than, equal to, or greater than the second. If 68 * two members compare as equal, their order in the sorted array is undefined. 69 * 70 * @return int: Result of comparsion (-1, 0, or 1). 71 */ 72 73 static int psCompare( 74 const void *x, /**< First float to compare. */ 75 const void *y /**< Second float to compare. */ 76 ) 77 { 78 float *item1 = NULL; 79 float *item2 = NULL; 80 81 if(x == NULL || y == NULL) { 82 printf("Null input argument\n"); 83 } 84 85 item1 = (float*)x; 86 item2 = (float*)y; 87 88 if(item1 == NULL || item2 == NULL) { 89 printf("Improper cast to float\n"); 90 } 91 92 if(*item1 < *item2) { 93 return -1; 94 } 95 else if(*item1 > *item2) { 96 return 1; 97 } 98 else { 99 return 0; 100 } 101 } 102 103 /** Sort an array of floats. 104 * 105 * Sorts an array of floats in ascendin order with the qsort() stdlib function. 106 * 107 * @return psFloatArray*: Pointer to sorted psFloatArray. 108 */ 109 110 psFloatArray *psSort( 111 psFloatArray *restrict outArray, /**< Sorted output array. */ 112 const psFloatArray *restrict inArray /**< Input array to sort. */ 113 ) 26 114 { 27 115 int inN = 0; … … 65 153 } 66 154 67 psIntArray *psSortIndex(psIntArray *restrict outArray, const psFloatArray *restrict inArray) 155 /** Creates an array of indices based on sort odred of float array. 156 * 157 * Sorts an array of floats and creates an integer array holding indices of sorted float values based on 158 * pre-sort index positions. 159 * 160 * @return psIntArray*: Pointer to psIntArray of sorted indices. 161 */ 162 163 psIntArray *psSortIndex( 164 psIntArray *restrict outArray, /**< Output array of sorted indices. */ 165 const psFloatArray *restrict inArray /**< Input array to be sorted. */ 166 ) 68 167 { 69 168 int inN = 0; … … 117 216 return outArray; 118 217 } 119 120 static int psCompare(const void *x, const void *y)121 {122 float *item1 = NULL;123 float *item2 = NULL;124 125 if(x == NULL || y == NULL) {126 printf("Null input argument\n");127 }128 129 item1 = (float*)x;130 item2 = (float*)y;131 132 if(item1 == NULL || item2 == NULL) {133 printf("Improper cast to float\n");134 }135 136 if(*item1 < *item2) {137 return -1;138 }139 else if(*item1 > *item2) {140 return 1;141 }142 else {143 return 0;144 }145 }
Note:
See TracChangeset
for help on using the changeset viewer.
