<commit_before>#ifndef SMALLFOLK_H
#define SMALLFOLK_H

#include <string>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <map>
#include <unordered_map>
#include <cmath>
#include <memory>
#include <cassert>
#include <functional>
#include <exception>
#include <stdarg.h>
#include <stdio.h>

class smallfolk_exception : public std::exception
{
public:
    static size_t const size = 2048;

    smallfolk_exception(const char * format, ...) : std::exception()
    {
        char buffer[size];
        va_list args;
        va_start(args, format);
        vsnprintf_s(buffer, size, format, args);
        errmsg = buffer;
        va_end(args);
    }

    virtual const char* what() const throw()
    {
        return errmsg.c_str();
    }

    std::string errmsg;
};

enum LuaTypeTag
{
    TNIL,
    TSTRING,
    TNUMBER,
    TTABLE,
    TBOOL,
};

class LuaVal
{
public:

    // static nil value, same as LuaVal();
    static LuaVal const & nil()
    {
        static LuaVal const nil;
        return nil;
    }

    // returns the string representation of the value info similar to lua tostring
    std::string tostring() const
    {
        switch (tag)
        {
            case TBOOL:
                if (b)
                    return "true";
                else
                    return "false";
            case TNIL:
                return "nil";
            case TSTRING:
                return s;
            case TNUMBER:
                return tostring(d);
            case TTABLE:
                return tostring(tbl_ptr);
            default:
                break;
        }
        throw smallfolk_exception("tostring invalid or unhandled tag %i", tag);
        return std::string();
    }

    static struct LuaValHasher
    {
        size_t operator()(LuaVal const & v) const
        {
            return std::hash<std::string>()(v.tostring());
        }
    };

    typedef std::unordered_map< LuaVal, LuaVal, LuaValHasher> LuaTable;
    typedef std::unique_ptr< LuaTable > TblPtr; // circular reference memleak if insert self to self

    LuaVal(const LuaTypeTag tag) : tag(tag), tbl_ptr(tag == TTABLE ? new LuaTable() : nullptr), d(0), b(false)
    {
        if (istable() && !tbl_ptr)
            throw smallfolk_exception("creating table LuaVal with nullptr table");
    }
    LuaVal() : tag(TNIL), tbl_ptr(nullptr), d(0), b(false)
    {
    }
    LuaVal(const long d) : tag(TNUMBER), tbl_ptr(nullptr), d(d), b(false)
    {
    }
    LuaVal(const unsigned long d) : tag(TNUMBER), tbl_ptr(nullptr), d(d), b(false)
    {
    }
    LuaVal(const int d) : tag(TNUMBER), tbl_ptr(nullptr), d(d), b(false)
    {
    }
    LuaVal(const unsigned int d) : tag(TNUMBER), tbl_ptr(nullptr), d(d), b(false)
    {
    }
    LuaVal(const float d) : tag(TNUMBER), tbl_ptr(nullptr), d(d), b(false)
    {
    }
    LuaVal(const double d) : tag(TNUMBER), tbl_ptr(nullptr), d(d), b(false)
    {
    }
    LuaVal(const std::string& s) : tag(TSTRING), tbl_ptr(nullptr), s(s), d(0), b(false)
    {
    }
    LuaVal(const char* s) : tag(TSTRING), tbl_ptr(nullptr), s(s), d(0), b(false)
    {
    }
    LuaVal(const bool b) : tag(TBOOL), tbl_ptr(nullptr), d(0), b(b)
    {
    }
    LuaVal(LuaTable const & luatable) : tag(TTABLE), tbl_ptr(new LuaTable(luatable)), d(0), b(false)
    {
        if (!tbl_ptr)
            throw smallfolk_exception("creating table LuaVal with nullptr table");
    }
    LuaVal(LuaVal const & val) : tag(val.tag), tbl_ptr(val.tag == TTABLE ? new LuaTable(*val.tbl_ptr.get()) : nullptr), s(val.s), d(val.d), b(val.b)
    {
        if (istable())
        {
            if (!tbl_ptr)
                throw smallfolk_exception("creating table LuaVal with nullptr table");
        }
    }

    bool isstring() const { return tag == TSTRING; }
    bool isnumber() const { return tag == TNUMBER; }
    bool istable() const { return tag == TTABLE; }
    bool isbool() const { return tag == TBOOL; }
    bool isnil() const { return tag == TNIL; }

    // create a table (same as LuaVal(TTABLE);)
    static LuaVal table(LuaTable arr = LuaTable())
    {
        return LuaVal(arr);
    }

    // gettable
    LuaVal get(LuaVal const & k) const
    {
        if (!istable())<commit_msg>Add initializer list<commit_after>#ifndef SMALLFOLK_H
#define SMALLFOLK_H

#include <string>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <map>
#include <unordered_map>
#include <cmath>
#include <memory>
#include <cassert>
#include <functional>
#include <exception>
#include <stdarg.h>
#include <stdio.h>

class smallfolk_exception : public std::exception
{
public:
    static size_t const size = 2048;

    smallfolk_exception(const char * format, ...) : std::exception()
    {
        char buffer[size];
        va_list args;
        va_start(args, format);
        vsnprintf_s(buffer, size, format, args);
        errmsg = buffer;
        va_end(args);
    }

    virtual const char* what() const throw()
    {
        return errmsg.c_str();
    }

    std::string errmsg;
};

enum LuaTypeTag
{
    TNIL,
    TSTRING,
    TNUMBER,
    TTABLE,
    TBOOL,
};

class LuaVal
{
public:

    // static nil value, same as LuaVal();
    static LuaVal const & nil()
    {
        static LuaVal const nil;
        return nil;
    }

    // returns the string representation of the value info similar to lua tostring
    std::string tostring() const
    {
        switch (tag)
        {
            case TBOOL:
                if (b)
                    return "true";
                else
                    return "false";
            case TNIL:
                return "nil";
            case TSTRING:
                return s;
            case TNUMBER:
                return tostring(d);
            case TTABLE:
                return tostring(tbl_ptr);
            default:
                break;
        }
        throw smallfolk_exception("tostring invalid or unhandled tag %i", tag);
        return std::string();
    }

    static struct LuaValHasher
    {
        size_t operator()(LuaVal const & v) const
        {
            return std::hash<std::string>()(v.tostring());
        }
    };

    typedef std::unordered_map< LuaVal, LuaVal, LuaValHasher> LuaTable;
    typedef std::unique_ptr< LuaTable > TblPtr; // circular reference memleak if insert self to self

    LuaVal(const LuaTypeTag tag) : tag(tag), tbl_ptr(tag == TTABLE ? new LuaTable() : nullptr), d(0), b(false)
    {
        if (istable() && !tbl_ptr)
            throw smallfolk_exception("creating table LuaVal with nullptr table");
    }
    LuaVal() : tag(TNIL), tbl_ptr(nullptr), d(0), b(false)
    {
    }
    LuaVal(const long d) : tag(TNUMBER), tbl_ptr(nullptr), d(d), b(false)
    {
    }
    LuaVal(const unsigned long d) : tag(TNUMBER), tbl_ptr(nullptr), d(d), b(false)
    {
    }
    LuaVal(const int d) : tag(TNUMBER), tbl_ptr(nullptr), d(d), b(false)
    {
    }
    LuaVal(const unsigned int d) : tag(TNUMBER), tbl_ptr(nullptr), d(d), b(false)
    {
    }
    LuaVal(const float d) : tag(TNUMBER), tbl_ptr(nullptr), d(d), b(false)
    {
    }
    LuaVal(const double d) : tag(TNUMBER), tbl_ptr(nullptr), d(d), b(false)
    {
    }
    LuaVal(const std::string& s) : tag(TSTRING), tbl_ptr(nullptr), s(s), d(0), b(false)
    {
    }
    LuaVal(const char* s) : tag(TSTRING), tbl_ptr(nullptr), s(s), d(0), b(false)
    {
    }
    LuaVal(const bool b) : tag(TBOOL), tbl_ptr(nullptr), d(0), b(b)
    {
    }
    LuaVal(LuaTable const & luatable) : tag(TTABLE), tbl_ptr(new LuaTable(luatable)), d(0), b(false)
    {
        if (!tbl_ptr)
            throw smallfolk_exception("creating table LuaVal with nullptr table");
    }
    LuaVal(LuaVal const & val) : tag(val.tag), tbl_ptr(val.tag == TTABLE ? new LuaTable(*val.tbl_ptr.get()) : nullptr), s(val.s), d(val.d), b(val.b)
    {
        if (istable())
        {
            if (!tbl_ptr)
                throw smallfolk_exception("creating table LuaVal with nullptr table");
        }
    }
    LuaVal(std::initializer_list<LuaVal const> l) : tag(TTABLE), tbl_ptr(new LuaTable()), d(0), b(false)
    {
        if (!tbl_ptr)
            throw smallfolk_exception("creating table LuaVal with nullptr table");
        LuaTable & tbl = *tbl_ptr;
        unsigned int i = 0;
        for (auto&& v : l)
            tbl[++i] = v;
    }

    bool isstring() const { return tag == TSTRING; }
    bool isnumber() const { return tag == TNUMBER; }
    bool istable() const { return tag == TTABLE; }
    bool isbool() const { return tag == TBOOL; }
    bool isnil() const { return tag == TNIL; }

    // create a table (same as LuaVal(TTABLE);)
    static LuaVal table(LuaTable arr = LuaTable())
    {
        return LuaVal(arr);
    }

    // gettable
    LuaVal get(LuaVal const & k) const
    {
        if (!istable())
{'fraction_non_alphanumeric': 0.1319294251565168, 'fraction_numerical': 0.002504268639726807, 'mean_word_length': 2.2722532588454376, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4457091', 'n_tokens_mistral': 3038, 'n_tokens_neox': 2959, 'n_words': 749}