gpt4 book ai didi

c - 如何将一行的尾部添加到哈希表中

转载 作者:太空宇宙 更新时间:2023-11-04 02:02:19 25 4
gpt4 key购买 nike

我正在尝试使用此代码将文件行添加到哈希表。目前它读取格式的文件。

289016164,279211721,462102225
341714666,132189021,299107290
362328497,466836829,47952622

那是三个逗号分隔的行。我希望它能够读取格式的行

289016164,279211721,462102225, some random text
341714666,132189021,299107290, some more random text
362328497,466836829,47952622, even more random text

应该包含每一行的结构应该是

typedef struct Row {
uint32_t a;
uint32_t b;
uint32_t t;
char text[40];
} Row;

读取文件的例程称为 readAll(见下文),我在修改它以执行此操作时遇到问题。

How can I change readAll to be able to cope with this new format?

我已经包含了大部分使用 readAll 的代码来提供一些上下文。

#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>

// Should be 37% occupied with 50m entries
#define TABLE_SIZE 0x8000000
#define MASK (TABLE_SIZE - 1)
#define BUFFER_SIZE 16384
#define END_OF_FILE (-1)
#define DEFAULT_VALUE (-1)

typedef struct Row {
uint32_t a;
uint32_t b;
uint32_t t;
} Row;

int32_t hash(int32_t a) {
return a * 428916315;
}

void insert(Row * table, Row row) {
long loc = hash(row.a) & MASK; // Entries are hashed on a
long inc = 0;
while (inc <= TABLE_SIZE) {
loc = (loc + inc) & MASK;
inc++;
if (table[loc].a == DEFAULT_VALUE) {
table[loc] = row;
break;
}
}
}

int readChar(FILE * input, char * buffer, int * pos, int * limit) {
if (*limit < *pos) {
return buffer[(*limit)++];
} else {
*limit = 0;
*pos = fread(buffer, sizeof(char), BUFFER_SIZE, input);
if (*limit < *pos) {
return buffer[(*limit)++];
} else
return END_OF_FILE;
}
}

void readAll(char * fileName, Row * table) {
char* buffer = (char*) malloc(sizeof(char) * BUFFER_SIZE);
int limit = 0;
int pos = 0;

FILE * input = fopen(fileName, "rb");

int lastRead;
Row currentRow;
uint32_t * currentElement = &(currentRow.a);

// We read rows with an FSM. We can
// roll up some of the code using the `currentElement` pointer
while (1) {
switch(lastRead = readChar(input, buffer, &pos, &limit)) {
case END_OF_FILE:
fclose(input);
return;
case ',':
if (currentElement == &(currentRow.a))
currentElement = &(currentRow.b);
else
currentElement = &(currentRow.t);
break;
case '\n':
insert(table, currentRow);
currentRow.a = 0;
currentRow.b = 0;
currentRow.t = 0;
currentElement = &(currentRow.a);
break;
default:
*currentElement = *currentElement * 10 + (lastRead - '0');
break;
}
} //printf("Read %d", lastRead);
}

int main(int argc, char** argv) {
Row* table = (Row*) malloc(sizeof(Row) * TABLE_SIZE);
memset(table, 255, sizeof(Row) * TABLE_SIZE);

readAll(argv[1], table);

//[...]
}

最佳答案

您需要识别第三个逗号并在找到时填写 .text,大致如下:

        case ',':
if (currentElement == &(currentRow.a)) {
currentElement = &(currentRow.b);
break;
}
if (currentElement == &(currentRow.b)) {
currentElement = &(currentRow.t);
break;
}
{ int i = 0;
int maxchars = sizeof(currentRow->text) - 1;

while ((lastRead = readChar(input, buffer, &pos, &limit)) != '\n') {
if (i < maxchars) currentRow->text[i++] = lastRead;
}
currentRow->text[i] = '\0';
}
/* fallthrough*/

关于c - 如何将一行的尾部添加到哈希表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25687011/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com