https://github.com/theo-mestre/interval-map
An interval_map implementation I did as part of a programming test
https://github.com/theo-mestre/interval-map
container cpp stl
Last synced: over 1 year ago
JSON representation
An interval_map implementation I did as part of a programming test
- Host: GitHub
- URL: https://github.com/theo-mestre/interval-map
- Owner: Theo-Mestre
- License: mit
- Created: 2025-01-24T07:47:13.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-01-28T16:09:10.000Z (over 1 year ago)
- Last Synced: 2025-03-23T20:44:53.631Z (over 1 year ago)
- Topics: container, cpp, stl
- Language: C++
- Homepage:
- Size: 5.86 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Interval Map Implementation
This repository contains an implementation of an `interval_map` in C++ designed for managing intervals and their associated values. The class allows you to assign values to specified intervals and perform efficient lookups, adhering to the C++ Standard Library conventions.
## Features
- Assign a value to an interval `[keyBegin, keyEnd)`.
- Efficient lookup of values associated with keys.
- Handles overlapping, contiguous, and redundant interval assignments.
- Includes a test function to validate core functionality.
### Template Parameters
- `K`: The type of the keys representing the interval boundaries.
- `V`: The type of the values associated with the intervals.
### Key Methods
- **assign**: Assigns a value to a specified interval, overwriting any previous values.
```cpp
template
void assign(K const& keyBegin, K const& keyEnd, V_forward&& val);
```
- **operator[]**: Retrieves the value associated with a key.
```cpp
const V& operator[](K const& key) const;
```
### Complexity
The time complexity of key operations in interval_map is logarithmic (O(log n) where n is the number of elements in the map).
## Example Usage
Here's a simple example of using the `interval_map` class:
```cpp
#include "interval_map.h"
int main() {
interval_map imap('A');
// Assign values to intervals
imap.assign(5, 10, 'B');
imap.assign(7, 12, 'C');
imap.assign(12, 15, 'D');
// Perform lookups
std::cout << "Value at key 6: " << imap[6] << std::endl; // Outputs 'B'
std::cout << "Value at key 10: " << imap[10] << std::endl; // Outputs 'C'
return 0;
}
```
## License
This project is licensed under the MIT License.
## Acknowledgments
Inspired by coding exercises I did in school and C++, focused on interval management programming best practices.