Skip to content

Commit

Permalink
Merge pull request #15 from TravisWheelerLab/betterRestrict
Browse files Browse the repository at this point in the history
Better restrict
  • Loading branch information
Sawwave authored Mar 16, 2024
2 parents 1e9b886 + 7b3828d commit c86ef63
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 46 deletions.
28 changes: 8 additions & 20 deletions src/FastaVector.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,10 @@ void fastaVectorDealloc(struct FastaVector *fastaVector) {
fastaVectorStringDealloc(&fastaVector->header);
fastaVectorMetadataVectorDealloc(&fastaVector->metadata);
}
#ifdef __cplusplus
enum FastaVectorReturnCode
fastaVectorReadFasta(const char *__restrict__ const fileSrc,
struct FastaVector *fastaVector) {
#else

enum FastaVectorReturnCode
fastaVectorReadFasta(const char *restrict const fileSrc,
fastaVectorReadFasta(const char *_RESTRICT_ const fileSrc,
struct FastaVector *fastaVector) {
#endif

FILE *fastaFile = fopen(fileSrc, "r");
if (__builtin_expect(!fastaFile, false)) {
Expand Down Expand Up @@ -174,16 +169,10 @@ fastaVectorReadFasta(const char *restrict const fileSrc,
return FASTA_VECTOR_OK;
}

#ifdef __cplusplus
fastaVectorWriteFasta(const char *__restrict__ const fileSrc,
struct FastaVector *fastaVector,
uint32_t fileLineLength) {
#else
enum FastaVectorReturnCode
fastaVectorWriteFasta(const char *restrict const fileSrc,
fastaVectorWriteFasta(const char *_RESTRICT_ const fileSrc,
struct FastaVector *fastaVector,
uint32_t fileLineLength) {
#endif
FILE *fastaFile = fopen(fileSrc, "w+");
if (!fastaFile) {
return FASTA_VECTOR_FILE_WRITE_FAIL;
Expand Down Expand Up @@ -304,9 +293,8 @@ fastaVectorAddSequenceToList(struct FastaVector *fastaVector, char *header,

// return NULL for headerPtr and 0 for header length if the fastaVector does not
// have a header for the given headerIndex
void fastaVectorFastaGetHeader(struct FastaVector *fastaVector,
size_t headerIndex, char **headerPtr,
size_t *headerLength) {
void fastaVectorGetHeader(struct FastaVector *fastaVector, size_t headerIndex,
char **headerPtr, size_t *headerLength) {
if (__builtin_expect(headerIndex >= fastaVector->metadata.count, false)) {
*headerLength = 0;
*headerPtr = NULL;
Expand All @@ -326,9 +314,9 @@ void fastaVectorFastaGetHeader(struct FastaVector *fastaVector,

// return NULL for headerPtr and 0 for header length if the fastaVector does not
// have a header for the given headerIndex
void fastaVectorFastaGetSequence(struct FastaVector *fastaVector,
size_t sequenceIndex, char **sequencePtr,
size_t *sequenceLength) {
void fastaVectorGetSequence(struct FastaVector *fastaVector,
size_t sequenceIndex, char **sequencePtr,
size_t *sequenceLength) {
if (__builtin_expect(sequenceIndex >= fastaVector->metadata.count, false)) {
*sequenceLength = 0;
*sequencePtr = NULL;
Expand Down
33 changes: 13 additions & 20 deletions src/FastaVector.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
#include <stdint.h>
#include <stdlib.h>

#ifdef __cplusplus
#define _RESTRICT_ __restrict__
#else
#define _RESTRICT_ restrict
#endif

/// @brief Primary struct that stores data for a given FASTA file.
struct FastaVector {
/// @brief Storage for the fasta sequence data.
Expand Down Expand Up @@ -98,15 +104,9 @@ void fastaVectorDealloc(struct FastaVector *fastaVector);
* takes any properly formed fasta file, and loads all headers and sequences.
*
*/
#ifdef __cplusplus
enum FastaVectorReturnCode
fastaVectorReadFasta(const char *__restrict__ const fileSrc,
struct FastaVector *fastaVector);
#else
enum FastaVectorReturnCode
fastaVectorReadFasta(const char *restrict const fileSrc,
fastaVectorReadFasta(const char *_RESTRICT_ const fileSrc,
struct FastaVector *fastaVector);
#endif

/**
* @relates FastaVector
Expand All @@ -124,15 +124,9 @@ fastaVectorReadFasta(const char *restrict const fileSrc,
* This function will overwrite the file at the given path.
*
*/
#ifdef __cplusplus
enum FastaVectorReturnCode
fastaVectorWriteFasta(const char *__restrict__ const filePath,
fastaVectorWriteFasta(const char *_RESTRICT_ const filePath,
struct FastaVector *fastaVector, uint32_t fileLineLength);
#else
enum FastaVectorReturnCode
fastaVectorWriteFasta(const char *restrict const filePath,
struct FastaVector *fastaVector, uint32_t fileLineLength);
#endif

/**
* @relates FastaVector
Expand Down Expand Up @@ -168,9 +162,8 @@ fastaVectorAddSequenceToList(struct FastaVector *fastaVector, char *header,
*
*
*/
void fastaVectorFastaGetHeader(struct FastaVector *fastaVector,
size_t headerIndex, char **headerPtr,
size_t *headerLength);
void fastaVectorGetHeader(struct FastaVector *fastaVector, size_t headerIndex,
char **headerPtr, size_t *headerLength);

/**
* @relates FastaVector
Expand All @@ -187,9 +180,9 @@ void fastaVectorFastaGetHeader(struct FastaVector *fastaVector,
* TODO: Should we return a static code in case the index was out of bounds?
*
*/
void fastaVectorFastaGetSequence(struct FastaVector *fastaVector,
size_t sequenceIndex, char **sequencePtr,
size_t *sequenceLength);
void fastaVectorGetSequence(struct FastaVector *fastaVector,
size_t sequenceIndex, char **sequencePtr,
size_t *sequenceLength);

/**
* @relates FastaVector
Expand Down
6 changes: 3 additions & 3 deletions tests/fileReadTest/fileReadTest.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void readTest(char **const testHeaderStrings, char **const testSequenceStrings,
const size_t expectedSequenceLength = strlen(testSequenceStrings[i]);
size_t headerLength;
char *headerPtr;
fastaVectorFastaGetHeader(&fastaVector, i, &headerPtr, &headerLength);
fastaVectorGetHeader(&fastaVector, i, &headerPtr, &headerLength);
sprintf(buffer, "header length of %zu did not match expected length %zu.",
headerLength, expectedHeaderLength);

Expand All @@ -38,7 +38,7 @@ void readTest(char **const testHeaderStrings, char **const testSequenceStrings,

char *sequencePtr;
size_t sequenceLength;
fastaVectorFastaGetSequence(&fastaVector, i, &sequencePtr, &sequenceLength);
fastaVectorGetSequence(&fastaVector, i, &sequencePtr, &sequenceLength);
sprintf(buffer, "sequence length of %zu did not match expected length %zu.",
sequenceLength, expectedSequenceLength);
testAssertString(sequenceLength == expectedSequenceLength, buffer);
Expand All @@ -53,7 +53,7 @@ void readTest(char **const testHeaderStrings, char **const testSequenceStrings,

char terminator = headerPtr[headerLength - 1];
sprintf(buffer,
"header index %zu was not null terminated! (found char %c after "
"header index %zu was not null terminated! (found char %u after "
"the end of the header)",
i, terminator);
testAssertString(terminator == '\0', buffer);
Expand Down
6 changes: 3 additions & 3 deletions tests/fileWriteTest/fileWriteTest.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ void fastaVectorFileWriteTest(const size_t numSequences, const char *fileSrc) {
char *sequenceFromVector;
size_t headerLengthFromVector;
size_t sequenceLengthFromVector;
fastaVectorFastaGetHeader(&fastaVector, sequenceNum, &headerFromVector,
fastaVectorGetHeader(&fastaVector, sequenceNum, &headerFromVector,
&headerLengthFromVector);
fastaVectorFastaGetSequence(&fastaVector, sequenceNum, &sequenceFromVector,
fastaVectorGetSequence(&fastaVector, sequenceNum, &sequenceFromVector,
&sequenceLengthFromVector);
sprintf(buffer, "header was supposed to be length %zu, but got %zu.",
headerLength, headerLengthFromVector);
Expand Down Expand Up @@ -155,7 +155,7 @@ void fastaVectorFileWriteTest(const size_t numSequences, const char *fileSrc) {
if (fastaVector.header.count != fastaReadVector.header.count) {
size_t lastHeaderLen;
char *lastHeaderPtr;
fastaVectorFastaGetHeader(&fastaVector, fastaVector.metadata.count - 1,
fastaVectorGetHeader(&fastaVector, fastaVector.metadata.count - 1,
&lastHeaderPtr, &lastHeaderLen);
printf("headers did not match, \n h1: %.*s\n h2: %.*s\n hr: %.*s\n",
(int)fastaVector.header.count, fastaVector.header.charData,
Expand Down

0 comments on commit c86ef63

Please sign in to comment.