This is my C++ implementation of the Piece Table data structure, which is mainly used in text editors, as it offers easy undo/redo operations and fast text modifications.
- Efficient insertions and deletions
- Undo and redo support
- Lightweight and simple implementation
- Works with large texts efficiently
- Includes unit tests with Catch2
This project uses a build script (build.sh) to automate building with CMake.
- Make the script executable
chmod +x build.sh
- Run script to build project
./build.sh
- Run main program
./build/main
- Run unit tests
./build/unit_test
The main program demonstrates basic operations with the Piece table
int main()
{
std::string text = "abcdef";
PieceTable p(text);
// Print text
std::cout << "Text is: " << p.getText() << std::endl;
// Insert text
p.insertPiece(6, "gh");
std::cout << "Text after insert: " << p.getText() << std::endl;
// Erase text
p.erasePiece(1, 3);
std::cout << "Text after erase: " << p.getText() << std::endl;
// Undo
p.undoPiece();
std::cout << "Text after undo: " << p.getText() << std::endl;
// Redo
p.redoPiece();
std::cout << "Text after redo: " << p.getText() << std::endl;
// Replace text
p.replacePiece(1, 3, "BC");
std::cout << "Text after replace: " << p.getText() << std::endl;
return 0;
}Unit tests are implemented using Catch2, a modern C++ testing framework, which is simple and efficient.