Dynamic Type-Safe Buffer Implementation
This module defines a generic container managing dynamic memory using templates. It enforces type safety and includes built-in validation for index boundaries.
#pragma once
#include <iostream>
#include <stdexcept>
#include <string>
template<typename T>
class ResizableArray {
public:
ResizableArray(int count);
ResizableArray(int count, T initValue);
ResizableArray(const ResizableArray<T>& other);
~ResizableArray();
size_t get_capacity() const;
T& retrieve(size_t idx);
const T& retrieve(size_t idx) const;
T& operator[](size_t idx);
const T& operator[](size_t idx) const;
template<typename U>
friend void display(const ResizableArray<U>& arr);
private:
int total_elements;
T* buffer;
};
template<typename T>
ResizableArray<T>::ResizableArray(int count) : total_elements{count} {
if (count < 0) { throw std::length_error("Constructor: Invalid negative capacity."); }
buffer = new T[count];
}
template<typename T>
ResizableArray<T>::ResizableArray(int count, T initValue) : total_elements{count} {
if (count < 0) { throw std::length_error("Constructor: Invalid negative capacity."); }
buffer = new T[count];
for (int i = 0; i < total_elements; ++i) {
buffer[i] = initValue;
}
}
template<typename T>
ResizableArray<T>::ResizableArray(const ResizableArray<T>& other) : total_elements(other.total_elements) {
buffer = new T[total_elements];
for (int i = 0; i < total_elements; ++i) {
buffer[i] = other.buffer[i];
}
}
template<typename T>
ResizableArray<T>::~ResizableArray() {
delete[] buffer;
}
template<typename T>
size_t ResizableArray<T>::get_capacity() const {
return static_cast<size_t>(total_elements);
}
template<typename T>
T& ResizableArray<T>::retrieve(size_t idx) {
if (idx >= total_elements) { throw std::out_of_range("Index exceeds boundary limits."); }
return buffer[idx];
}
template<typename T>
const T& ResizableArray<T>::retrieve(size_t idx) const {
if (idx >= total_elements) { throw std::out_of_range("Index exceeds boundary limits."); }
return buffer[idx];
}
template<typename T>
T& ResizableArray<T>::operator[](size_t idx) {
if (idx >= total_elements) { throw std::out_of_range("Index exceeds boundary limits."); }
return buffer[idx];
}
template<typename T>
const T& ResizableArray<T>::operator[](size_t idx) const {
if (idx >= total_elements) { throw std::out_of_range("Index exceeds boundary limits."); }
return buffer[idx];
}
template<typename U>
void display(const ResizableArray<U>& arr) {
for (size_t k = 0; k < arr.get_capacity(); ++k) {
std::cout << arr[k] << " ";
}
std::cout << "\n";
}
Functional Verification and Error Handling
The following execution script validates the generic container behavior including copy semantics and exception propagation.
#include <iostream>
#include "ResizableBuffer.hpp"
void validate_basic_ops() {
std::cout << "Run 1: Initialization and Copy Checks\n";
int limit;
std::cout << "Specify count: ";
std::cin >> limit;
ResizableArray<double> dataset_a(limit);
for (auto i = 0; i < limit; ++i) {
dataset_a.retrieve(i) = i * 0.5;
}
std::cout << "Dataset A: ";
display(dataset_a);
ResizableArray<int> dataset_b(limit, 100);
const ResizableArray<int> dataset_c(dataset_b);
std::cout << "Dataset B: "; display(dataset_b);
std::cout << "Dataset C: "; display(dataset_c);
dataset_b.retrieve(0) = 50;
dataset_b.retrieve(1) = 500;
std::cout << "Dataset B (Modified): "; display(dataset_b);
std::cout << "Dataset C (Unchanged): "; display(dataset_c);
}
void validate_errors() {
std::cout << "Run 2: Boundary Condition Testing\n";
int limit, pos;
while (std::cout << "Provide count and index: ", std::cin >> limit >> pos) {
try {
ResizableArray<int> target(limit, limit);
target.retrieve(pos) = -1;
std::cout << "Result: ";
display(target);
} catch (const std::exception& err) {
std::cout << err.what() << std::endl;
}
}
}
int main() {
validate_basic_ops();
validate_errors();
return 0;
}
External Data Integration and Sorting Logic
This section handles persisting structured data via file streams and applying custom comparison algorithms. It assumes an entity definition compatible with the read/write operators.
#include "ApplicantInfo.h"
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
bool evaluate_priority(const ApplicantInfo& lhs, const ApplicantInfo& rhs) {
if (lhs.major() < rhs.major()) return true;
if (lhs.major() == rhs.major()) {
return lhs.score() > rhs.score();
}
return false;
}
void print_records(std::ostream& os, const std::vector<ApplicantInfo>& list) {
for (const auto& item : list) {
os << item;
}
}
int process_data_files() {
std::vector<ApplicantInfo> registry;
std::ifstream input_stream("dataset_raw.txt");
if (!input_stream.is_open()) {
std::cerr << "Error loading input file.";
return -1;
}
std::string header;
std::getline(input_stream, header);
ApplicantInfo temp_entry;
while (input_stream >> temp_entry) {
registry.push_back(temp_entry);
}
std::sort(registry.begin(), registry.end(), evaluate_priority);
std::ofstream output_stream("result_sorted.txt");
if (!output_stream.is_open()) {
std::cerr << "Error writing output file.";
return -1;
}
print_records(std::cout, registry);
print_records(output_stream, registry);
return 0;
}