An open API service indexing awesome lists of open source software.

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

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.