Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding optional usage of C++ 17 #67

Merged
merged 6 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
cmake_minimum_required(VERSION 3.14.0)
project(trieste VERSION 1.0.0 LANGUAGES CXX)

# #############################################
# Options
option(TRIESTE_BUILD_SAMPLES "Specifies whether to build the samples" ON)
option(TRIESTE_USE_CXX17 "Specifies whether to target the C++17 standard" OFF)

set(CMAKE_BUILD_WITH_INSTALL_RPATH ON)

# #############################################
# Dependencies

include(FetchContent)

set(CMAKE_CXX_STANDARD 20)
if(TRIESTE_USE_CXX17)
set(CMAKE_CXX_STANDARD 17)
else()
set(CMAKE_CXX_STANDARD 20)
endif()

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

set(SNMALLOC_BUILD_TESTING OFF CACHE INTERNAL "Turn off snmalloc tests")
Expand All @@ -12,6 +27,7 @@ if (MSVC)
else()
set(SNMALLOC_STATIC_LIBRARY_PREFIX "")
endif()
set(SNMALLOC_USE_CXX17 ${TRIESTE_USE_CXX17})

set(RE2_BUILD_TESTING OFF CACHE INTERNAL "Turn off RE2 tests")

Expand Down Expand Up @@ -42,12 +58,6 @@ FetchContent_Declare(

FetchContent_MakeAvailable(cli11)

# #############################################
# Options
option(TRIESTE_BUILD_SAMPLES "Specifies whether to build the samples" ON)

set(CMAKE_BUILD_WITH_INSTALL_RPATH ON)

# #############################################
# Create target and set properties
add_library(trieste INTERFACE)
Expand All @@ -68,7 +78,11 @@ target_link_libraries(trieste
CLI11::CLI11
)

target_compile_features(trieste INTERFACE cxx_std_20)
if(TRIESTE_USE_CXX17)
target_compile_definitions(trieste INTERFACE cxx_std_17)
else()
target_compile_definitions(trieste INTERFACE cxx_std_20)
endif()

if(MSVC)
target_compile_options(trieste INTERFACE /W4 /WX /wd5030 /bigobj)
Expand Down
5 changes: 3 additions & 2 deletions include/trieste/pass.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "rewrite.h"

#include <vector>

namespace trieste
Expand All @@ -11,7 +12,7 @@ namespace trieste
constexpr flag bottomup = 1 << 0;
constexpr flag topdown = 1 << 1;
constexpr flag once = 1 << 2;
};
}

class PassDef;
using Pass = std::shared_ptr<PassDef>;
Expand Down Expand Up @@ -234,7 +235,7 @@ namespace trieste
add(root);
while (!path.empty())
{
auto& [node,it] = path.back();
auto& [node, it] = path.back();
if (it != node->end())
{
Node curr = *it;
Expand Down
2 changes: 1 addition & 1 deletion include/trieste/source.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ namespace trieste

auto source = std::make_shared<SourceDef>();
source->origin_ = file.string();
source->contents.resize(size);
source->contents.resize(static_cast<std::size_t>(size));
f.read(&source->contents[0], size);

if (!f)
Expand Down
30 changes: 23 additions & 7 deletions include/trieste/wf.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,26 @@ namespace trieste
tokens.end(),
std::back_inserter(offsets),
[&](const Token& t) {
return 1.0 /
(1.0 +
(alpha * (depth - target_depth) *
token_terminal_distance.at(t)));

if (token_terminal_distance.find(t) != token_terminal_distance.end())
matajoh marked this conversation as resolved.
Show resolved Hide resolved
{
std::size_t distance = token_terminal_distance.at(t);
return 1.0 /
(1.0 +
(alpha * (depth - target_depth) *
distance));
}else{
std::ostringstream err;
err << "Token " << t.str() << " not found in token_terminal_distance map" << std::endl;
err << "{";
std::string delim = "";
for(auto const& [key, val] : token_terminal_distance){
err << delim << key.str() << ":" << val;
delim = ", ";
}
err << "}" << std::endl;
throw std::runtime_error(err.str());
}
});

// compute the cumulative distribution of P(d | c, p)
Expand Down Expand Up @@ -165,7 +181,7 @@ namespace trieste
types.end(),
static_cast<std::size_t>(0),
[&](std::size_t acc, auto& type) {
if (omit.contains(type))
if (omit.find(type) != omit.end())
{
return acc + max_distance;
}
Expand Down Expand Up @@ -523,12 +539,12 @@ namespace trieste
std::size_t max_distance,
const Token& token) const
{
if (distance.contains(token))
if (distance.find(token) != distance.end())
{
return distance[token];
}

if (!shapes.contains(token))
if (shapes.find(token) == shapes.end())
{
distance[token] = 0;
}
Expand Down