-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCursor.c
57 lines (52 loc) · 1.36 KB
/
Cursor.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <sys/stat.h>
#include "Task_2.h"
typedef struct
{
Table *table;
uint32_t row_num;
bool end_of_table; // Indicates a position one past the last element
} Cursor;
Cursor *table_start(Table *table)
{
Cursor *cursor = malloc(sizeof(Cursor));
cursor->table = table;
cursor->row_num = 0;
cursor->end_of_table = false;
}
Cursor *table_end(Table *table)
{
Cursor *cursor = malloc(sizeof(Cursor));
cursor->table = table;
cursor->row_num = table->num_rows;
cursor->end_of_table = true;
}
// Used row_slot() for now, will use cursor_location() from next task onwards
void *cursor_location(Cursor *cursor) // Old row_slot() renamed to cursor_location
{
uint32_t page_num = cursor->row_num / ROWS_PER_PAGE;
if (!(cursor->row_num % ROWS_PER_PAGE))
page_num--;
void *page = get_page(cursor->table, page_num);
uint32_t row_offset = (cursor->row_num - 1) % ROWS_PER_PAGE;
uint32_t byte_offset = row_offset * ROW_SIZE;
return page + byte_offset;
}
void cursor_advance(Cursor *cursor)
{
if (!(cursor->end_of_table))
{
cursor->row_num++;
return cursor;
}
else
printf("End of Table\n");
}
// Further implementation later ....