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

Small Json parser cleanup #1759

Merged
merged 7 commits into from
Feb 15, 2019
Merged
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
2 changes: 1 addition & 1 deletion vowpalwabbit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ set(vw_all_sources
cbify.cc explore_eval.cc topk.cc stagewise_poly.cc log_multi.cc recall_tree.cc active.cc
active_cover.cc cs_active.cc kernel_svm.cc best_constant.cc ftrl.cc svrg.cc lrqfa.cc interact.cc
comp_io.cc interactions.cc vw_validate.cc audit_regressor.cc gen_cs_example.cc cb_explore.cc
action_score.cc cb_explore_adf.cc OjaNewton.cc parse_example_json.cc baseline.cc classweight.cc
action_score.cc cb_explore_adf.cc OjaNewton.cc baseline.cc classweight.cc
vw_exception.cc no_label.cc
)

Expand Down
8 changes: 0 additions & 8 deletions vowpalwabbit/ect.cc
Original file line number Diff line number Diff line change
Expand Up @@ -217,14 +217,6 @@ uint32_t ect_predict(ect& e, single_learner& base, example& ec)
return id + 1;
}

bool member(size_t t, v_array<size_t> ar)
{
for (size_t i = 0; i < ar.size(); i++)
if (ar[i] == t)
return true;
return false;
}

void ect_train(ect& e, single_learner& base, example& ec)
{
if (e.k == 1) // nothing to do
Expand Down
5 changes: 0 additions & 5 deletions vowpalwabbit/parse_example_json.cc

This file was deleted.

54 changes: 22 additions & 32 deletions vowpalwabbit/parse_example_json.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ license as described in the file LICENSE.

#include "cb.h"
#include "best_constant.h"
#include <boost/algorithm/string.hpp>

#include <algorithm>
#include <vector>

// portability fun
#ifndef _WIN32
Expand Down Expand Up @@ -848,16 +850,10 @@ class BoolToBoolState : public BaseState<audit>
struct DecisionServiceInteraction
{
std::string eventId;

std::vector<unsigned> actions;

std::vector<float> probabilities;

float probabilityOfDrop;

float probabilityOfDrop = 0.f;
bool skipLearn{false};

DecisionServiceInteraction() : probabilityOfDrop(0.f) {}
};

template <bool audit>
Expand Down Expand Up @@ -937,8 +933,11 @@ class DecisionServiceState : public BaseState<audit>
template <bool audit>
struct Context
{
private:
std::unique_ptr<std::stringstream> error_ptr;

public:
vw* all;
std::stringstream* error_ptr;

// last "<key>": encountered
const char* key;
Expand All @@ -948,7 +947,7 @@ struct Context
BaseState<audit>* previous_state;

// the path of namespaces
v_array<Namespace<audit>> namespace_path;
std::vector<Namespace<audit>> namespace_path;

v_array<example*>* examples;
example* ex;
Expand Down Expand Up @@ -980,18 +979,10 @@ struct Context

BaseState<audit>* root_state;

Context() : error_ptr(nullptr)
Context()
{
namespace_path = v_init<Namespace<audit>>();
current_state = root_state = &default_state;
}

~Context()
{
namespace_path.delete_v();

if (error_ptr)
delete error_ptr;
current_state = &default_state;
root_state = &default_state;
}

void init(vw* pall)
Expand All @@ -1001,13 +992,12 @@ struct Context
key_length = 1;
previous_state = nullptr;
label_object_state.init(pall);
error_ptr = nullptr;
}

std::stringstream& error()
{
if (!error_ptr)
error_ptr = new std::stringstream;
error_ptr.reset(new std::stringstream{});

return *error_ptr;
}
Expand Down Expand Up @@ -1038,19 +1028,19 @@ struct Context
if (ns.feature_count > 0)
{
auto feature_group = ns.feature_group;
// avoid duplicate insertion
for (unsigned char ns_char : ex->indices)
if (ns_char == feature_group)
goto done;

ex->indices.push_back(feature_group);
// Do not insert feature_group if it already exists.
if(std::find(ex->indices.begin(), ex->indices.end(), feature_group) == ex->indices.end())
{
ex->indices.push_back(feature_group);
}
}

done:
return namespace_path.pop().return_state;
auto return_state = namespace_path.back().return_state;
jackgerrits marked this conversation as resolved.
Show resolved Hide resolved
namespace_path.pop_back();
return return_state;
}

Namespace<audit>& CurrentNamespace() { return *(namespace_path._end - 1); }
Namespace<audit>& CurrentNamespace() { return namespace_path.back(); }

bool TransitionState(BaseState<audit>* next_state)
{
Expand Down
13 changes: 4 additions & 9 deletions vowpalwabbit/parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,6 @@ uint32_t cache_numbits(io_buf* buf, int filepointer)
return cache_numbits;
}

bool member(v_array<int> ids, int id)
{
for (size_t i = 0; i < ids.size(); i++)
if (ids[i] == id)
return true;
return false;
}

void reset_source(vw& all, size_t numbits)
{
io_buf* input = all.p->input;
Expand All @@ -170,7 +162,10 @@ void reset_source(vw& all, size_t numbits)
else
{
int fd = input->files.pop();
if (!member(all.final_prediction_sink, (size_t)fd))
const auto& fps = all.final_prediction_sink;

// If the current popped file is not in the list of final predictions sinks, close it.
if(std::find(fps.cbegin(), fps.cend(), fd) == fps.cend())
io_buf::close_file_or_socket(fd);
}
input->open_file(all.p->output->finalname.begin(), all.stdin_off, io_buf::READ); // pushing is merged into
Expand Down
43 changes: 21 additions & 22 deletions vowpalwabbit/vw_core.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
<!-- <CodeAnalysisRuleSet>NativeRecommendedRules.ruleset</CodeAnalysisRuleSet>
<!-- <CodeAnalysisRuleSet>NativeRecommendedRules.ruleset</CodeAnalysisRuleSet>
<RunCodeAnalysis>false</RunCodeAnalysis> -->
<RuntimeLibrary>
</RuntimeLibrary>
Expand Down Expand Up @@ -125,32 +125,32 @@
<PreBuildEvent>
<Command>win32\make_config_h.exe</Command>
</PreBuildEvent>
<!-- <Lib>
<AdditionalDependencies Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
</AdditionalDependencies>
</Lib>
<Lib>
<AdditionalDependencies Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
</AdditionalDependencies>
<AdditionalLibraryDirectories Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
</AdditionalLibraryDirectories>
<!-- <Lib>
<AdditionalDependencies Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
</AdditionalDependencies>
</Lib>
<Lib>
<AdditionalDependencies Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
</AdditionalDependencies>
<AdditionalLibraryDirectories Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
</AdditionalLibraryDirectories>
</Lib> -->
</ItemDefinitionGroup>
<PropertyGroup>
<OutDir>$(SolutionDir)out\target\$(Configuration)\$(PlatformShortName)\</OutDir>
<IntDir>$(SolutionDir)out\int\$(Configuration)\$(PlatformShortName)\$(ProjectName)\</IntDir>
</PropertyGroup>
<!-- <PropertyGroup Condition="'$(Platform)'=='x64'">
<OutDir>$(SolutionDir)dll\x64\$(Configuration)\</OutDir>
<IntDir>$(SolutionDir)dll\x64\$(Configuration)\</IntDir>
</PropertyGroup>
<PropertyGroup Condition="'$(Platform)'=='Win32'">
<OutDir>$(SolutionDir)$(PlatformShortName)\$(Configuration)\</OutDir>
<IntDir>$(SolutionDir)$(PlatformShortName)\$(Configuration)\$(ProjectName)\</IntDir>
</PropertyGroup>
<PropertyGroup Condition="'$(Platform)'=='x64'">
<OutDir>$(SolutionDir)$(PlatformName)\$(Configuration)\</OutDir>
<IntDir>$(SolutionDir)$(PlatformShortName)\$(Configuration)\$(ProjectName)\</IntDir>
<!-- <PropertyGroup Condition="'$(Platform)'=='x64'">
<OutDir>$(SolutionDir)dll\x64\$(Configuration)\</OutDir>
<IntDir>$(SolutionDir)dll\x64\$(Configuration)\</IntDir>
</PropertyGroup>
<PropertyGroup Condition="'$(Platform)'=='Win32'">
<OutDir>$(SolutionDir)$(PlatformShortName)\$(Configuration)\</OutDir>
<IntDir>$(SolutionDir)$(PlatformShortName)\$(Configuration)\$(ProjectName)\</IntDir>
</PropertyGroup>
<PropertyGroup Condition="'$(Platform)'=='x64'">
<OutDir>$(SolutionDir)$(PlatformName)\$(Configuration)\</OutDir>
<IntDir>$(SolutionDir)$(PlatformShortName)\$(Configuration)\$(ProjectName)\</IntDir>
</PropertyGroup> -->
<ItemGroup>
<ClInclude Include="active_cover.h" />
Expand Down Expand Up @@ -296,7 +296,6 @@
<ClCompile Include="lrq.cc" />
<ClCompile Include="lrqfa.cc" />
<ClCompile Include="log_multi.cc" />
<ClCompile Include="parse_example_json.cc" />
<ClCompile Include="recall_tree.cc" />
<ClCompile Include="best_constant.cc" />
<ClCompile Include="global_data.cc" />
Expand Down