<commit_before>// Copyright © 2017 <NAME>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Author: <NAME> aka xDimon
// Contacts: <EMAIL>
// File created on: 2017.04.07

// SObj.hpp


#pragma once

#include <map>
#include <algorithm>
#include <functional>
#include <stddef.h>
#include "SStr.hpp"
#include "SVal.hpp"
#include "SBool.hpp"
#include "SInt.hpp"
#include "SFloat.hpp"
#include "SNull.hpp"

class SObj : public SVal
{
private:
	std::map<std::string, SVal*> _elements;

public:
	SObj() = default;

	virtual ~SObj()
	{
		while (!_elements.empty())
		{
			auto i = _elements.begin();
			auto value = const_cast<SVal*>(i->second);
			_elements.erase(i);
			delete value;
		}
	};

	SObj(SObj&& tmp)
	{
		_elements.swap(tmp._elements);
	}

	SObj& operator=(SObj&& tmp)
	{
		_elements.swap(tmp._elements);
		return *this;
	}

	void insert(const std::string key, const SVal* value)
	{
		auto i = _elements.find(key);
		if (i != _elements.end())
		{
			auto value = const_cast<SVal*>(i->second);
			_elements.erase(i);
			delete value;
		}
		_elements.emplace(key, const_cast<SVal*>(value));
	}

	void insert(const std::string key, SVal& value)
	{
		insert(key, &value);
	}

	void insert(const std::string key, SInt::type value)
	{
		insert(key, new SInt(value));
	}

	void insert(const std::string key, SFloat::type value)
	{
		insert(key, new SFloat(value));
	}

	void insert(const std::string key, const std::string value)
	{
		insert(key, new SStr(value));
	}

	void insert(const std::string key, bool value)
	{
		insert(key, new SBool(value));
	}

	void insert(const std::string key, nullptr_t)
	{
		insert(key, new SNull());
	}

	SVal* get(const std::string& key) const
	{
		auto i = _elements.find(key);
		if (i == _elements.end())
		{
			return nullptr;
		}
		return i->second;
	}
	SVal* get(SStr* key) const
	{
		return get(key->value());
	}
	SVal* get(SStr& key) const
	{
		return get(key);
	}

	void forEach(std::function<void(const std::pair<const std::string, const SVal*>&)> handler) const
	{
		std::for_each(_elements.cbegin(), _elements.cend(), handler);
	}

	void lookup(const char *key, int64_t &value, bool strict = false) const
	{
		auto element = get(key);
		if (!element)
		{
			throw std::runtime_error(std::string() + "Field '" + key + "' not found");
		}
		if (!dynamic_cast<const SInt *>(element) && strict)
		{
			throw std::runtime_error(std::string() + "Field '" + key + "' isn't integer");
		}
		value = element->operator int();
	}

	void lookup(const std::string& key, bool &value, bool strict = false) const
	{
		auto element = get(key);
		if (!element)<commit_msg>Improve SObj<commit_after>// Copyright © 2017 <NAME>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Author: <NAME> aka xDimon
// Contacts: <EMAIL>
// File created on: 2017.04.07

// SObj.hpp


#pragma once

#include <map>
#include <algorithm>
#include <functional>
#include <stddef.h>
#include "SStr.hpp"
#include "SVal.hpp"
#include "SBool.hpp"
#include "SInt.hpp"
#include "SFloat.hpp"
#include "SNull.hpp"

class SObj : public SVal
{
private:
	std::map<std::string, SVal*> _elements;

public:
	SObj() = default;

	virtual ~SObj()
	{
		while (!_elements.empty())
		{
			auto i = _elements.begin();
			auto value = const_cast<SVal*>(i->second);
			_elements.erase(i);
			delete value;
		}
	};

	SObj(SObj&& tmp)
	{
		_elements.swap(tmp._elements);
	}

	SObj& operator=(SObj&& tmp)
	{
		_elements.swap(tmp._elements);
		return *this;
	}

	void insert(const std::string key, const SVal* value)
	{
		auto i = _elements.find(key);
		if (i != _elements.end())
		{
			auto value = const_cast<SVal*>(i->second);
			_elements.erase(i);
			delete value;
		}
		_elements.emplace(key, const_cast<SVal*>(value));
	}

	void insert(const std::string key, SVal& value)
	{
		insert(key, &value);
	}

	void insert(const std::string key, uint64_t value)
	{
		insert(key, new SInt(value));
	}
	void insert(const std::string key, int64_t value)
	{
		insert(key, new SInt(value));
	}
	void insert(const std::string key, uint32_t value)
	{
		insert(key, new SInt(value));
	}
	void insert(const std::string key, int32_t value)
	{
		insert(key, new SInt(value));
	}
	void insert(const std::string key, uint16_t value)
	{
		insert(key, new SInt(value));
	}
	void insert(const std::string key, int16_t value)
	{
		insert(key, new SInt(value));
	}
	void insert(const std::string key, uint8_t value)
	{
		insert(key, new SInt(value));
	}
	void insert(const std::string key, int8_t value)
	{
		insert(key, new SInt(value));
	}

	void insert(const std::string key, SFloat::type value)
	{
		insert(key, new SFloat(value));
	}

	void insert(const std::string key, const std::string value)
	{
		insert(key, new SStr(value));
	}
	void insert(const std::string key, const char* value)
	{
		insert(key, new SStr(value));
	}

	void insert(const std::string key, bool value)
	{
		insert(key, new SBool(value));
	}

	void insert(const std::string key, nullptr_t)
	{
		insert(key, new SNull());
	}

	SVal* get(const std::string& key) const
	{
		auto i = _elements.find(key);
		if (i == _elements.end())
		{
			return nullptr;
		}
		return i->second;
	}
	SVal* get(SStr* key) const
	{
		return get(key->value());
	}
	SVal* get(SStr& key) const
	{
		return get(key);
	}
	int64_t getAsInt(const std::string& key, int64_t defaultValue = 0LL) const
	{
		auto val = get(key);
		return val ? val->operator int() : defaultValue;
	}

	void forEach(std::function<void(const std::pair<const std::string, const SVal*>&)> handler) const
	{
		std::for_each(_elements.cbegin(), _elements.cend(), handler);
	}

	void lookup(const char *key, int64_t &value, bool strict = false) const
	{
		auto element = get(key);
		if (!element)
		{
			throw std::runtime_error(std::string() + "Field '" + key + "' not found");
		}
		if (!dynamic_cast<const SInt *>(element) && strict)
		{
			throw std::runtime_error(std::string() + "Field '" + key + "' isn't integer");
		}
		value = element->operator int();
	}
//	template<typename T>
//	void lookup(const char *key, T &value, bool strict = false) const
//	{
//		int64_t tmp;
//		lookup(key, tmp, strict);
//		value = (decltype(value))(tmp);
//	}
	void lookup(const char *key, int32_t &value, bool strict = false) const
	{
		int64_t tmp;
		lookup(key, tmp, strict);
		value = (decltype(value))(tmp);
	}
	void lookup(const char *key, uint32_t &value, bool strict = false) const
	{
		int64_t tmp;
		lookup(key, tmp, strict);
		value = (decltype(value))(tmp);
	}
	void lookup(const char *key, int16_t &value, bool strict = false) const
	{
		int64_t tmp;
		lookup(key, tmp, strict);
		value = (decltype(value))(tmp);
	}
	void lookup(const char *key, uint16_t &value, bool strict = false) const
	{
		int64_t tmp;
		lookup(key, tmp, strict);
		value = (decltype(value))(tmp);
	}
	void lookup(const char *key, int8_t &value, bool strict = false) const
	{
		int64_t tmp;
		lookup(key, tmp, strict);
		value = (decltype(value))(tmp);
	}
	void lookup(const char *key, uint8_t &value, bool strict = false) const
	{
		int64_t tmp;
		lookup(key, tmp, strict);
		value = (decltype(value))(tmp);
	}

	void lookup(const std::string& key, bool &value, bool strict = false) const
	{
		auto element = get(key);
		if (!element)
{'fraction_non_alphanumeric': 0.1557367076286653, 'fraction_numerical': 0.009611874923956685, 'mean_word_length': 3.898688915375447, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5600010', 'n_tokens_mistral': 3175, 'n_tokens_neox': 2958, 'n_words': 832}