Files
CSPP-coursework/final/state.h
T
2024-12-05 01:42:50 +00:00

116 lines
3.3 KiB
C

#ifndef STATEARR_H
#define STATEARR_H
#include <stdint.h>
typedef enum LineState {
UNMODIFIED,
MODIFIED,
ADDED,
REMOVED,
} LineState;
/**
* Dynamic array structure for storing LineState values
*/
typedef struct {
uint32_t size; // Current number of elements
uint32_t capacity; // Total capacity allocated
LineState* data; // Array of LineState values
} StateArray;
void arr_print(StateArray* self);
/**
* Creates a new StateArray with the specified initial capacity
*
* All elements will have a default value of UNMODIFIED
*
* @param capacity Initial capacity in number of elements
* @return A new StateArray structure with allocated memory
*/
StateArray arr_init(int capacity);
/**
* Sets a LineState value at the specified index
* Does not perform bounds checking
*
* @param self Pointer to the StateArray to modify
* @param index Position to set the value
* @param state The LineState value to set
*/
void arr_set(StateArray* self, int index, LineState state);
/**
* Removes a LineState value at the specified index
* Shifts remaining elements left to fill the gap
*
* @param self Pointer to the StateArray to modify
* @param index Position of value to remove
* @return The LineState value that was removed
*/
LineState arr_remove(StateArray* self, int index);
/**
* Inserts a LineState value at the specified index
* Shifts existing elements right to make room
* Automatically reallocates if more capacity is needed
*
* @param self Pointer to the StateArray to modify
* @param index Position to insert the value
* @param state The LineState value to insert
* @return 0 on success, -1 if index is out of bounds
*/
int arr_insert(StateArray* self, int index, LineState state);
/**
* Gets the LineState value at the specified index
* Does not perform bounds checking
*
* @param self Pointer to the StateArray to read from
* @param index Position to get the value from
* @return The LineState value at the specified index
*/
LineState arr_get(StateArray* self, int index);
/**
* Deallocates memory used by the StateArray
* Must be called when the array is no longer needed
*
* @param self Pointer to the StateArray to deallocate
* @return 0 on success
*/
int arr_dealloc(StateArray* self);
/**
* Calculates the offset needed to map from state array index to original buffer index
* by counting ADDED lines before the given index
*
* @param self Pointer to the StateArray
* @param index The index in the state array
* @return The negative offset (number of ADDED lines before index)
*/
int arr_original_offset(StateArray* self, int index);
/**
* Calculates the offset needed to map from state array index to modified buffer index
* by counting REMOVED lines before the given index
*
* @param self Pointer to the StateArray
* @param index The index in the state array
* @return The negative offset (number of REMOVED lines before index)
*/
int arr_modified_offset(StateArray* self, int index);
/**
* Counts the number of lines that have been either added or removed
* Does not count lines that have been modified
*
* @param self Pointer to the StateArray to analyze
* @param index The index to count up to.
* @return The count of lines marked as either ADDED or REMOVED
*/
int arr_added_or_removed_before(StateArray* self, int index);
#endif