Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
Binary file added MathLab/MathLab.cpp
Binary file not shown.
41 changes: 41 additions & 0 deletions MathLab/MathLab.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28010.2041
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "MathLab", "MathLab.vcxproj", "{3314C41D-F478-4CB2-A408-F0B113C7AF2C}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "MathLabTests", "..\MathLabTests\MathLabTests.vcxproj", "{5DBEC0A4-DD81-4B5D-81E7-22940933E8F3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{3314C41D-F478-4CB2-A408-F0B113C7AF2C}.Debug|x64.ActiveCfg = Debug|x64
{3314C41D-F478-4CB2-A408-F0B113C7AF2C}.Debug|x64.Build.0 = Debug|x64
{3314C41D-F478-4CB2-A408-F0B113C7AF2C}.Debug|x86.ActiveCfg = Debug|Win32
{3314C41D-F478-4CB2-A408-F0B113C7AF2C}.Debug|x86.Build.0 = Debug|Win32
{3314C41D-F478-4CB2-A408-F0B113C7AF2C}.Release|x64.ActiveCfg = Release|x64
{3314C41D-F478-4CB2-A408-F0B113C7AF2C}.Release|x64.Build.0 = Release|x64
{3314C41D-F478-4CB2-A408-F0B113C7AF2C}.Release|x86.ActiveCfg = Release|Win32
{3314C41D-F478-4CB2-A408-F0B113C7AF2C}.Release|x86.Build.0 = Release|Win32
{5DBEC0A4-DD81-4B5D-81E7-22940933E8F3}.Debug|x64.ActiveCfg = Debug|x64
{5DBEC0A4-DD81-4B5D-81E7-22940933E8F3}.Debug|x64.Build.0 = Debug|x64
{5DBEC0A4-DD81-4B5D-81E7-22940933E8F3}.Debug|x86.ActiveCfg = Debug|Win32
{5DBEC0A4-DD81-4B5D-81E7-22940933E8F3}.Debug|x86.Build.0 = Debug|Win32
{5DBEC0A4-DD81-4B5D-81E7-22940933E8F3}.Release|x64.ActiveCfg = Release|x64
{5DBEC0A4-DD81-4B5D-81E7-22940933E8F3}.Release|x64.Build.0 = Release|x64
{5DBEC0A4-DD81-4B5D-81E7-22940933E8F3}.Release|x86.ActiveCfg = Release|Win32
{5DBEC0A4-DD81-4B5D-81E7-22940933E8F3}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {9A5CE5FA-B1B1-4AF3-9E24-6C95A4696F5F}
EndGlobalSection
EndGlobal
176 changes: 176 additions & 0 deletions MathLab/MathLab.vcxproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>15.0</VCProjectVersion>
<ProjectGuid>{3314C41D-F478-4CB2-A408-F0B113C7AF2C}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>MathLab</RootNamespace>
<WindowsTargetPlatformVersion>10.0.17134.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeaderFile>
</PrecompiledHeaderFile>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeaderFile>
</PrecompiledHeaderFile>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeaderFile>
</PrecompiledHeaderFile>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeaderFile>
</PrecompiledHeaderFile>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="blocks.h" />
<ClInclude Include="block_sequence.h" />
<ClInclude Include="factory.h" />
<ClInclude Include="tuple_serialization.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="blocks.cpp" />
<ClCompile Include="block_sequence.cpp" />
<ClCompile Include="factory.cpp" />
<ClCompile Include="MathLab.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
45 changes: 45 additions & 0 deletions MathLab/MathLab.vcxproj.filters
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="Resource Files">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="blocks.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="factory.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="block_sequence.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="tuple_serialization.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="MathLab.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="blocks.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="factory.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="block_sequence.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>
77 changes: 77 additions & 0 deletions MathLab/block_sequence.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#include "block_sequence.h"
#include <numeric>
#include <sstream>

namespace {
std::pair<std::string, std::istringstream> extract_type(const std::string& line)
{
std::string block_type;
std::istringstream line_stream(line);
line_stream >> block_type;
return std::make_pair(std::move(block_type), std::move(line_stream));
}
}

namespace mathlab {

block_sequence::block_sequence(factory& factory) : factory_(factory) {}

std::string block_sequence::append_from(std::istream& input_stream) {
std::string block_type;
std::istringstream constants;
std::string invalid_lines;
std::string line;
while (!input_stream.eof()) {
std::getline(input_stream, line);
std::tie(block_type, constants) = extract_type(line);
if (!block_type.empty()) {
std::unique_ptr<block> block;
try {
block = factory_.create(block_type, constants);
}
catch (std::invalid_argument&) {
// Deliberately empty - handle same as unknown command
}
if (block != nullptr)
blocks_.push_back(std::make_pair(std::move(block_type), std::move(block)));
else
invalid_lines += line + "\n";
}
}
return invalid_lines;
}

std::string block_sequence::load_from(std::istream& input_stream) {
blocks_.clear();
return append_from(input_stream);
}

std::ostream& block_sequence::dump(std::ostream& to_stream, bool with_line_numbers) const {
int position = 1;
for (auto& name_with_block : blocks_) {
if (with_line_numbers)
to_stream << position++ << ": ";
to_stream << name_with_block.first << ' ' << *name_with_block.second << std::endl;
}
return to_stream;
}

double block_sequence::eval(double input) const {
return std::accumulate(
blocks_.cbegin(),
blocks_.cend(),
input,
[](double val, const std::pair<std::string, std::unique_ptr<block>> &name_with_block) { return name_with_block.second->eval(val); });
}

void block_sequence::remove_at(unsigned index) {
if (index < blocks_.size())
blocks_.erase(blocks_.begin() + index);
}

void block_sequence::move_to_beginning(unsigned index) {
if (index < blocks_.size() && index > 0) {
std::swap(blocks_[0], blocks_[index]);
}
}
}
24 changes: 24 additions & 0 deletions MathLab/block_sequence.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once
#include "factory.h"
#include <vector>
#include <memory>
#include <string>

namespace mathlab {
class block_sequence {
std::vector<std::pair<std::string, std::unique_ptr<block>>> blocks_;
factory& factory_;
public:
block_sequence(factory& factory);
// Appends one or more block from supplied stream
// Returns invalid lines
std::string append_from(std::istream& input_stream);
// Load sequence from supplied stream
// Returns invalid lines
std::string load_from(std::istream& input_stream);
std::ostream& dump(std::ostream& to_stream, bool with_line_numbers) const;
double eval(double input) const;
void remove_at(unsigned index);
void move_to_beginning(unsigned index);
};
}
9 changes: 9 additions & 0 deletions MathLab/blocks.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "blocks.h"

namespace mathlab
{
std::ostream& operator<<(std::ostream& stream, const block& block) {
block.dump(stream);
return stream;
}
}
Loading