<commit_before>#include <iostream>
#include <atomic>
#include <erl_nif.h>

ERL_NIF_TERM ATOM_OK;
ERL_NIF_TERM ATOM_TRUE;
ERL_NIF_TERM ATOM_FALSE;

ErlNifResourceType* ONEUP_RESOURCE_TYPE;

// holds the value of all oneup counters that have been created
// and not yet garbage collected.
std::atomic<long> num_counters_v;

// There are four functions that may be called during the lifetime
// of a NIF. load, reload, upgrade, and unload. Any of these functions
// can be left unspecified by passing NULL to the ERL_NIF_INIT macro.
//
// NIFs are awesome.

// Return value of 0 indicates success.
// Docs: http://erlang.org/doc/man/erl_nif.html#load

void
free_resource(ErlNifEnv* env, void* obj)
{
    num_counters_v.operator--();
}

static int
load(ErlNifEnv* env, void** priv, ERL_NIF_TERM load_info)
{
    const char* mod = "oneup";
    const char* name = "oneup";
    ErlNifResourceFlags flags = ErlNifResourceFlags(
        ERL_NIF_RT_CREATE | ERL_NIF_RT_TAKEOVER
    );

    ONEUP_RESOURCE_TYPE = enif_open_resource_type(
        env, mod, name, free_resource, flags, NULL
    );
    if(ONEUP_RESOURCE_TYPE == NULL)
        return -1;

    ATOM_OK = enif_make_atom(env, "ok");
    ATOM_TRUE = enif_make_atom(env, "true");
    ATOM_FALSE = enif_make_atom(env, "false");

    return 0;
}

static ERL_NIF_TERM
new_counter(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    ERL_NIF_TERM ret;
    std::atomic<long> *value;

    value = (std::atomic<long>*)enif_alloc_resource(
        ONEUP_RESOURCE_TYPE, sizeof(std::atomic<long>)
    );
    if(value == NULL)
        return enif_make_badarg(env);
    value->store(0);

    ret = enif_make_resource(env, value);
    enif_release_resource(value);

    num_counters_v.operator++();

    return ret;
}

static ERL_NIF_TERM
inc(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    std::atomic<long> *value;

    if(argc != 1) {
        return enif_make_badarg(env);
    }
    if(!enif_get_resource(env, argv[0], ONEUP_RESOURCE_TYPE, (void**) &value)) {
        return enif_make_badarg(env);
    }
    value->operator++();

    return ATOM_OK;
}

static ERL_NIF_TERM
inc2(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    std::atomic<long> *value;
    long int inc;

    if(argc != 2) {
        return enif_make_badarg(env);
    }
    if(!enif_get_resource(env, argv[0], ONEUP_RESOURCE_TYPE, (void**) &value)) {
        return enif_make_badarg(env);
    }
    if(!enif_get_long(env, argv[1], &inc)) {
        return enif_make_badarg(env);
    }
    value->operator+=((long)inc);

    return ATOM_OK;
}

static ERL_NIF_TERM
set(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    std::atomic<long> *value;
    long int new_value;

    if(argc != 2) {
        return enif_make_badarg(env);
    }
    if(!enif_get_resource(env, argv[0], ONEUP_RESOURCE_TYPE, (void**) &value)) {
        return enif_make_badarg(env);
    }
    if(!enif_get_long(env, argv[1], &new_value)) {
        return enif_make_badarg(env);
    }

    return enif_make_long(env, value->exchange((long) new_value));
}

static ERL_NIF_TERM
set_min(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])<commit_msg>Update oneup.cc<commit_after>#include <iostream>
#include <atomic>
#include <erl_nif.h>

ERL_NIF_TERM ATOM_OK;
ERL_NIF_TERM ATOM_TRUE;
ERL_NIF_TERM ATOM_FALSE;

ErlNifResourceType* ONEUP_RESOURCE_TYPE;

// holds the value of all oneup counters that have been created
// and not yet garbage collected.
std::atomic<long> num_counters_v;

// There are four functions that may be called during the lifetime
// of a NIF. load, reload, upgrade, and unload. Any of these functions
// can be left unspecified by passing NULL to the ERL_NIF_INIT macro.
//
// NIFs are awesome.

// Return value of 0 indicates success.
// Docs: http://erlang.org/doc/man/erl_nif.html#load

void
free_resource(ErlNifEnv* env, void* obj)
{
    num_counters_v.operator--();
}

static int
load(ErlNifEnv* env, void** priv, ERL_NIF_TERM load_info)
{
    const char* mod = "oneup";
    const char* name = "oneup";
    ErlNifResourceFlags flags = ErlNifResourceFlags(
        ERL_NIF_RT_CREATE | ERL_NIF_RT_TAKEOVER
    );

    ONEUP_RESOURCE_TYPE = enif_open_resource_type(
        env, mod, name, free_resource, flags, NULL
    );
    if(ONEUP_RESOURCE_TYPE == NULL)
        return -1;

    ATOM_OK = enif_make_atom(env, "ok");
    ATOM_TRUE = enif_make_atom(env, "true");
    ATOM_FALSE = enif_make_atom(env, "false");

    return 0;
}

static ERL_NIF_TERM
new_counter(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    ERL_NIF_TERM ret;
    std::atomic<long> *value;

    value = (std::atomic<long>*)enif_alloc_resource(
        ONEUP_RESOURCE_TYPE, sizeof(std::atomic<long>)
    );
    if(value == NULL)
        return enif_make_badarg(env);
    value->store(0);

    ret = enif_make_resource(env, value);
    enif_release_resource(value);

    num_counters_v.operator++();

    return ret;
}

static ERL_NIF_TERM
inc(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    std::atomic<long> *value;

    if(argc != 1) {
        return enif_make_badarg(env);
    }
    if(!enif_get_resource(env, argv[0], ONEUP_RESOURCE_TYPE, (void**) &value)) {
        return enif_make_badarg(env);
    }
    value->operator++();

    return enif_make_long(env, value->operator++());
}

static ERL_NIF_TERM
inc2(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    std::atomic<long> *value;
    long int inc;

    if(argc != 2) {
        return enif_make_badarg(env);
    }
    if(!enif_get_resource(env, argv[0], ONEUP_RESOURCE_TYPE, (void**) &value)) {
        return enif_make_badarg(env);
    }
    if(!enif_get_long(env, argv[1], &inc)) {
        return enif_make_badarg(env);
    }
    
    return enif_make_long(env, value->operator+=((long)inc));
}

static ERL_NIF_TERM
set(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    std::atomic<long> *value;
    long int new_value;

    if(argc != 2) {
        return enif_make_badarg(env);
    }
    if(!enif_get_resource(env, argv[0], ONEUP_RESOURCE_TYPE, (void**) &value)) {
        return enif_make_badarg(env);
    }
    if(!enif_get_long(env, argv[1], &new_value)) {
        return enif_make_badarg(env);
    }

    return enif_make_long(env, value->exchange((long) new_value));
}

static ERL_NIF_TERM
set_min(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{'fraction_non_alphanumeric': 0.12592475995592634, 'fraction_numerical': 0.004092554698567606, 'mean_word_length': 3.703182827535159, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 32, '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': '6222592', 'n_tokens_mistral': 2501, 'n_tokens_neox': 2455, 'n_words': 515}