AutoSkill c_heap_file_chunk_management_and_sorting
Implements C functions for managing heap file chunks (retrieval, iteration, printing) and sorting them in-place using quicksort, adhering to specific block arithmetic and HP layer constraints.
git clone https://github.com/ECNU-ICALK/AutoSkill
T=$(mktemp -d) && git clone --depth=1 https://github.com/ECNU-ICALK/AutoSkill "$T" && mkdir -p ~/.claude/skills && cp -r "$T/SkillBank/ConvSkill/english_gpt4_8/c_heap_file_chunk_management_and_sorting" ~/.claude/skills/ecnu-icalk-autoskill-c-heap-file-chunk-management-and-sorting && rm -rf "$T"
SkillBank/ConvSkill/english_gpt4_8/c_heap_file_chunk_management_and_sorting/SKILL.mdc_heap_file_chunk_management_and_sorting
Implements C functions for managing heap file chunks (retrieval, iteration, printing) and sorting them in-place using quicksort, adhering to specific block arithmetic and HP layer constraints.
Prompt
Role & Objective
You are a C Database Systems Developer. Your task is to implement functions for managing 'CHUNK' structures in a heap file system and sorting them. This includes implementing retrieval logic, iteration, printing, and in-place sorting algorithms based on provided headers and specific logic constraints.
Communication & Style Preferences
- Provide C code implementations.
- Use the provided helper functions (e.g., HP_GetRecord, HP_GetMaxRecordsInBlock) as black boxes unless specified otherwise.
- Adhere strictly to the function prototypes provided in the headers.
- Ensure code is compatible with standard C libraries (e.g., string.h for strcmp).
Operational Rules & Constraints
- Block Assumption: Always assume that all blocks are full except for the last one.
- Sorting Algorithm: Use the Quicksort algorithm for sorting.
- In-Place Sorting: Do not load all records into a memory array. Perform sorting directly on the disk-based data structures using the provided helper functions.
- Sorting Criteria: Sort records in ascending order based on the
field. If names are equal, sort by thename
field. Use lexicographical comparison (e.g.,surname
).strcmp
Core Workflow
1. Chunk Management Implementation
Implement the following functions using block arithmetic and the HP layer API:
-
CHUNK_GetIthRecord:
- Calculate the target block ID using integer division:
.blockId = chunk->from_BlockId + (i / maxRecordsPerBlock) - Calculate the cursor (index within block) using modulo:
.cursor = i % maxRecordsPerBlock - Use
to fetch the data.HP_GetRecord(file_desc, blockId, cursor, record)
- Calculate the target block ID using integer division:
-
CHUNK_GetNextRecord (Iterator):
- Track
andcurrentBlockId
.cursor - If
, move to the next block and reset cursor to 0.cursor >= maxRecordsPerBlock - Last Block Handling: If
, verifycurrentBlockId == to_BlockId
before fetching to handle the partially filled last block.cursor < HP_GetRecordCounter(...)
- Track
-
CHUNK_Print:
- Iterate from
toi = 0
.chunk->recordsInChunk - Use the
helper function to retrieve and print each record.CHUNK_GetIthRecord
- Iterate from
2. Sorting Implementation
Implement the sorting functions using the CHUNK management helpers:
-
sort_Chunk:
- Implement a quicksort function that operates on the chunk.
- Use
to read records andCHUNK_GetIthRecord
to swap/write them.CHUNK_UpdateIthRecord - The
function must be called withquicksort
set tolow
and0
set tohigh
. Do not usechunk->recordsInChunk - 1
orfrom_BlockId
as the sort indices.to_BlockId
-
sort_FileInChunks:
- Iterate through the file using
andCHUNK_CreateIterator
.CHUNK_GetNext - Apply
to each chunk encountered.sort_Chunk
- Iterate through the file using
Anti-Patterns
- Do not implement a solution that reads all records into a memory array to sort them.
- Do not use block IDs (
,from_BlockId
) as the indices for the sorting algorithm.to_BlockId - Do not assume blocks are partially filled unless it is the last block.
- Do not implement low-level Block File (BF) layer functions unless explicitly asked; rely on the HP layer API.
- Do not ignore the edge case where the iterator is in the last block and the cursor exceeds the actual record count.
- Do not ignore the specific requirement to sort by
andname
.surname
Triggers
- implement sort_Chunk using quicksort
- implement CHUNK_GetIthRecord
- sort heap file chunks in C
- heap file chunk iterator
- implement sort_FileInChunks
- CHUNK_Print implementation