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.
Scanned 5/30/2026
Install via CLI
openskills install gabrielmoreira/agent-skills-mirror---
id: "09dba1f5-38ea-49e3-870b-d87e7b754b9f"
name: "c_heap_file_chunk_management_and_sorting"
description: "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."
version: "0.1.1"
tags:
- "C"
- "Database"
- "Heap File"
- "Sorting"
- "Quicksort"
- "Iterator"
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"
---
# 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.
## 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
1. **Block Assumption**: Always assume that all blocks are full except for the last one.
2. **Sorting Algorithm**: Use the Quicksort algorithm for sorting.
3. **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.
4. **Sorting Criteria**: Sort records in ascending order based on the `name` field. If names are equal, sort by the `surname` field. Use lexicographical comparison (e.g., `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 `HP_GetRecord(file_desc, blockId, cursor, record)` to fetch the data.
- **CHUNK_GetNextRecord (Iterator)**:
- Track `currentBlockId` and `cursor`.
- If `cursor >= maxRecordsPerBlock`, move to the next block and reset cursor to 0.
- **Last Block Handling**: If `currentBlockId == to_BlockId`, verify `cursor < HP_GetRecordCounter(...)` before fetching to handle the partially filled last block.
- **CHUNK_Print**:
- Iterate from `i = 0` to `chunk->recordsInChunk`.
- Use the `CHUNK_GetIthRecord` helper function to retrieve and print each record.
## 2. Sorting Implementation
Implement the sorting functions using the CHUNK management helpers:
- **sort_Chunk**:
- Implement a quicksort function that operates on the chunk.
- Use `CHUNK_GetIthRecord` to read records and `CHUNK_UpdateIthRecord` to swap/write them.
- The `quicksort` function must be called with `low` set to `0` and `high` set to `chunk->recordsInChunk - 1`. Do not use `from_BlockId` or `to_BlockId` as the sort indices.
- **sort_FileInChunks**:
- Iterate through the file using `CHUNK_CreateIterator` and `CHUNK_GetNext`.
- Apply `sort_Chunk` to each chunk encountered.
# 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`, `to_BlockId`) as the indices for the sorting algorithm.
- 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 `name` and `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
No comments yet. Be the first to comment!