C CASTLE / MODULE NOTES
CASTLE/Modules/Algorithm

MODULE 01 / 15

Algorithm

Deterministic, STL-free generic algorithms for Castle iterators and raw pointers, including search, copy, transform, remove, sort, and ordered lookup.

#include "castle/algorithm/algorithm.hpp"

Overview

Deterministic, STL-free generic algorithms for Castle iterators and raw pointers. Use this header when you need common search, copy, transform, remove, sort, and ordered-lookup operations without heap allocation or recursion-heavy implementations.

Dependencies

Public API

APIDescription
find, find_if, find_if_notLinear search over an input range; return the first matching iterator or last.
count, count_ifCount matching elements in an input range; O(N).
all_of, any_of, none_ofPredicate checks over an input range with short-circuit behavior.
equalSequence equality checks for 3-iterator and 4-iterator forms, with optional predicate overloads.
mismatchReturns castle::pair of the first mismatching iterators, with optional predicate overloads.
copy, copy_n, copy_if, moveCopy or move ranges into an output iterator; no allocation.
fill, fill_n, transform, generateOverwrite or generate values in-place or into an output range.
replace, replace_ifIn-place replacement on forward ranges.
remove, remove_if, uniqueNon-erasing algorithms that compact the logical range and return the new end iterator.
sortIn-place heapsort for random-access iterators; deterministic O(N log N) worst case and O(1) extra storage.
lower_bound, upper_bound, binary_searchOrdered-range lookup helpers for forward iterators.
min, max, min3, max3Value comparisons without STL dependency.
min_element, max_elementFind the smallest or largest element in a forward range.

Usage example

See samples/sample_algorithm.cpp.

#include "castle/algorithm/algorithm.hpp"

int values[6] = {4, 1, 7, 1, 9, 2};
castle::sort(values, values + 6);

int* found = castle::find(values, values + 6, 7);
bool has_nine = castle::binary_search(values, values + 6, 9);

(void)found;
(void)has_nine;

Constraints & notes

  • No heap allocation, exceptions, RTTI, or STL algorithms.
  • Iterator categories are checked with static_assert; unsupported iterator types fail at compile time.
  • sort() uses iterative heapsort, not quicksort or mergesort.
  • remove, remove_if, and unique do not resize containers; callers must treat the returned iterator as the new logical end.
  • Ordered lookup assumes the range is already sorted with the same comparator.