Skip to content

Commit

Permalink
feat: pattern matching (#1)
Browse files Browse the repository at this point in the history
* pattern matching
* refactor
  • Loading branch information
Nanahuse authored Jun 2, 2024
1 parent 1ac8599 commit 943d5d0
Show file tree
Hide file tree
Showing 9 changed files with 791 additions and 62 deletions.
2 changes: 2 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
BasedOnStyle: Google
ColumnLimit: 120
20 changes: 11 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
cmake_minimum_required(VERSION 3.20)
project(const_mapper CXX)
project(const_mapper_tests CXX)

set(CMAKE_C_COMPILER "/usr/bin/clang")
set(CMAKE_CXX_COMPILER "/usr/bin/clang++")
set(CMAKE_CXX_STANDARD 17)

enable_testing()

include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/refs/tags/v1.14.0.zip
)
FetchContent_MakeAvailable(googletest)

find_package(GTest REQUIRED)

Expand All @@ -19,7 +15,13 @@ include_directories(${PROJECT_NAME}
${GTEST_INCLUDE_DIRS}
)

add_executable(${PROJECT_NAME} test/test_const_mapper.cpp)
target_compile_options(${PROJECT_NAME} PRIVATE -Wall -Wextra -Wpedantic -Werror)
add_executable(${PROJECT_NAME}
test/main.cpp
test/test_const_mapper.cpp
test/test_performance.cpp
test/test_utils.cpp
test/test_example.cpp
)
target_compile_options(${PROJECT_NAME} PRIVATE -O0 -Wall -Wextra -Wpedantic -Werror)

target_link_libraries(${PROJECT_NAME} ${GTEST_LIBRARIES})
95 changes: 84 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

## Features

- **Only depends STL**: not need to add extra library.
- **Static Definition**: Enables compile-time definition of constant mappings, ensuring high efficiency and reduced runtime overhead.
- **Bi-Directional Conversion**: Supports conversions in both directions between any defined pairs, making it a highly flexible solution.
- **Only depends on STL**: no need to add any extra libraries.
- **Single Header File**: All you need is include `const_mapper.hpp`.
- **Compile-Time Mapping**: Supports compile-time mapping. As well as at runtime mapping.
- **All-Directional Conversion**: Supports conversions in all directions between any pairs.
- **Pattern Matching**: Supports complex conversion by pattern matching.
- **Support C++17 or later**

## Installation
Expand All @@ -15,15 +17,86 @@ This is single header file library.<br>
Just include the header file in your source code.

## Example

[test_const_mapper.cpp](test/test_const_mapper.cpp) will help for you understanding how to use this library.

### Simple Conversion
```cpp
using namespace const_mapper;
constexpr auto map = ConstMapper<3, std::string_view, int, std::uint8_t>{{{
{"value_0", 0, 0},
{"value_1", 1, 10},
{"value_2", 2, 20},
}}};

// str0 == "value_1";
constexpr auto str0 = map.to<std::string_view, int>(1);

// str1 == "value_2";
constexpr auto str1 = map.to<std::string_view, std::uint8_t>(20);

// int0 == 2;
constexpr auto int0 = map.to<int, std::uint8_t>(20);
```
### Pattern Conversion
```cpp
constexpr auto map = const_mapper::ConstMapper<3, std::string_view, int, std::uint8_t>{{{
{"value_0", 0, 0},
{"value_1", 1, 10},
{"value_2", 2, 20}}}};

constexpr auto str1 = map.to<std::string_view, int>(1); // str == "value_1";
constexpr auto str2 = map.to<std::string_view, std::uint8_t>(20); // str == "value_2";
constexpr auto uint0 = map.to<std::uint8_t, int>(0); // str == "value_2";
using namespace const_mapper;
constexpr auto map = ConstMapper<6, std::string_view, Range<int>, Anyable<int>>{{{
{"less2 & 1", {CompareType::LessThan, 2}, 1},
{"less2 & 2", {CompareType::LessThan, 2}, 2},
{"larger5", {CompareType::LargerThan, 5}, {}},
{"Any", {}, {}},
}}};
// str0 == "less2 & 1"
constexpr auto str0 = map.pattern_match(std::make_tuple(Result{}, 1, 1));
// str1 == "less2 & 2"
constexpr auto str1 = map.pattern_match(std::make_tuple(Result{}, 1, 2));
// str2 == "larger5"
constexpr auto str2 = map.pattern_match(std::make_tuple(Result{}, 6, -1));
// str3 == "Any"
constexpr auto str3 = map.pattern_match(std::make_tuple(Result{}, 5, -1));
```

## Performance Test

check test detail -> [test_performance.cpp](test/test_performance.cpp)


### optimization: -O0
```
[----------] 5 tests from Performance
[ RUN ] Performance.ref_std_unordered_map
[ OK ] Performance.ref_std_unordered_map (619 ms)
[ RUN ] Performance.ref_std_map
[ OK ] Performance.ref_std_map (983 ms)
[ RUN ] Performance.const_mapper_to
[ OK ] Performance.const_mapper_to (731 ms)
[ RUN ] Performance.const_mapper_pattern_match
[ OK ] Performance.const_mapper_pattern_match (1299 ms)
[ RUN ] Performance.const_mapper_pattern_match_pick_up_2_values
[ OK ] Performance.const_mapper_pattern_match_pick_up_2_values (1880 ms)
[----------] 5 tests from Performance (5513 ms total)
```

### optimization: -O3
```
[----------] 5 tests from Performance
[ RUN ] Performance.ref_std_unordered_map
[ OK ] Performance.ref_std_unordered_map (42 ms)
[ RUN ] Performance.ref_std_map
[ OK ] Performance.ref_std_map (139 ms)
[ RUN ] Performance.const_mapper_to
[ OK ] Performance.const_mapper_to (35 ms)
[ RUN ] Performance.const_mapper_pattern_match
[ OK ] Performance.const_mapper_pattern_match (36 ms)
[ RUN ] Performance.const_mapper_pattern_match_pick_up_2_values
[ OK ] Performance.const_mapper_pattern_match_pick_up_2_values (38 ms)
[----------] 5 tests from Performance (292 ms total)
```

## License
Expand Down
Loading

0 comments on commit 943d5d0

Please sign in to comment.