Реализация последовательности LZW - PullRequest
0 голосов
/ 08 февраля 2020

Я абсолютно потерялся в том, что я должен делать в этом C задании на программирование, которое у меня есть. Я почти уверен, что создал функцию newSequence неправильно, потому что для меня не очень ясно, что означают все переменные. Может кто-нибудь подсказать мне, как мне создать newSequence?

#include <stdlib.h>
#include "sequencecopy.h"

Sequence* newSequence(unsigned char firstByte, unsigned long long hashSize) {
    Sequence *s = malloc(sizeof(Sequence));
    s->length = 1; // each letter is one byte
    s->code = (unsigned int) firstByte; // casting char input into 4 byte value
    s->hash = (unsigned long long) s->code; // casting code into 8 byte value
    s->hash = ((s->hash << 5) + s->hash) + s->code; // hash function
    s->bucket = s->hash % hashSize; // hash code modded to hash table size
  //  s->bytes[0] = firstByte;

    return s;
}

void deleteSequence(Sequence* sequence) {
        free(sequence);
}

Sequence* copySequenceAppend(Sequence* sequence, unsigned char addByte, unsigned int hashSize) {

    return NULL;
}

unsigned int outputSequence(Sequence* sequence,
                    void (*writeFunc)(unsigned char c, void* context), void* context) {
        return 0;
}


bool identicalSequences(Sequence* a, Sequence* b) {
    return true;
}

Это последовательность заголовочных файлов .h

#ifndef SEQUENCE_H
#define SEQUENCE_H
#include <stdbool.h>

typedef struct _sequence {
    unsigned int length;        // number of bytes in the sequence
    unsigned int usage;         // number of times the sequence is used
    unsigned int code;          // compression code assigned in the dictionary
    unsigned int wordCount;     // number of words holding the bytes
    unsigned long long hash;    // raw hash code for the sequence
    unsigned long long bucket;        // hash code modded to the hash table size
    struct _sequence* next;     // next sequence in a list of sequences (collisions)
    union {                     // union data permits both character and word accessability
        unsigned char bytes[1 << SEQUENCE_ALLOC];   // buffer for characters, multiple of 8
        unsigned long long words[1];                // for accessing data as 8 byte words
    } data;
} Sequence;

/* create a new, one character Sequence using char firstByte as the character.
   hashSize is the size of the hashTable and if non-zero, a hash
   is computed for the new Sequence. */
Sequence* newSequence(unsigned char firstByte, unsigned long long hashSize);

/* either frees sequence or places it on a list of idle sequences for possible reuse */
void deleteSequence(Sequence* sequence);


/* creates a new Sequence containing the old sequence bytes with newByte appended to the end,
   hashSize has the same meaning and function as in newSequence() above. */
Sequence* copySequenceAppending(Sequence* sequence, unsigned char newByte, unsigned long long hashSize);

/* write the data bytes of sequence to the FILE* fd, return the number of bytes written. */
unsigned int outputSequence(Sequence* sequence,
                            void (*writeFunc)(unsigned char c, void* context),
                            void* context);

/* returns true if a and b contain identical sequences of data bytes, false otherwise */
bool identicalSequences(Sequence* a, Sequence* b);


#endif
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...