49 lines
1.2 KiB
C
Executable File
49 lines
1.2 KiB
C
Executable File
#include "history.h"
|
|
|
|
void hisbuf_init(HistoryBuffer *sb) {
|
|
sb->front = 0;
|
|
sb->rear = 0;
|
|
sb->size = 0;
|
|
}
|
|
|
|
int hisbuf_is_full(HistoryBuffer *sb) {
|
|
return sb->size == HISTORY_SIZE;
|
|
}
|
|
|
|
int hisbuf_push(HistoryBuffer *sb, HistoryEntry entry) {
|
|
if (sb->size < HISTORY_SIZE) {
|
|
sb->data[sb->rear] = entry;
|
|
sb->rear = (sb->rear + 1) % HISTORY_SIZE;
|
|
sb->size++;
|
|
return 1; // Success
|
|
}
|
|
return 0; // Buffer full
|
|
}
|
|
|
|
int hisbuf_pop(HistoryBuffer *sb) {
|
|
if (sb->size > 0) {
|
|
sb->front = (sb->front + 1) % HISTORY_SIZE;
|
|
sb->size--;
|
|
return 1; // Success
|
|
}
|
|
return 0; // Buffer empty
|
|
}
|
|
|
|
int hisbuf_pop_data(HistoryBuffer *sb, HistoryEntry *out) {
|
|
if (sb->size > 0) {
|
|
*out = sb->data[sb->front];
|
|
sb->front = (sb->front + 1) % HISTORY_SIZE;
|
|
sb->size--;
|
|
return 1; // Success
|
|
}
|
|
return 0; // Buffer empty
|
|
}
|
|
|
|
void hisbuf_get(HistoryBuffer *sb, HistoryEntry *out, int lookahead) {
|
|
if (lookahead >= 0 && lookahead < sb->size) {
|
|
// 가장 최근에 넣은 것(rear - 1)을 기준으로 lookahead
|
|
int index = (sb->rear - 1 - lookahead + HISTORY_SIZE) % HISTORY_SIZE;
|
|
*out = sb->data[index];
|
|
}
|
|
}
|