<commit_before>/** generic_creative_filters.h                                 -*- C++ -*-
    <NAME>, 09 Aug 2013
    Copyright (c) 2013 Datacratic.  All rights reserved.

    Generic filters for creatives.

    \todo Really need to find a way to merge the generic_filters.h header with
    this one somehow. It's pretty silly to have 2 sets of class that are almost
    but not quite the same.

*/

#pragma once

#include "generic_filters.h"

namespace RTBKIT {


/******************************************************************************/
/* CREATIVE FILTER                                                            */
/******************************************************************************/

/** Convenience base class for creative filters. */
template<typename Filter>
struct CreativeFilter : public FilterBaseT<Filter>
{

    // Same semantics as addConfig but called for every creatives.
    virtual void addCreative(
            unsigned cfgIndex, unsigned crIndex, const Creative& creative) = 0;

    void addConfig(
            unsigned cfgIndex, const std::shared_ptr<AgentConfig>& config)
    {
        for (size_t i = 0; i < config->creatives.size(); ++i)
            addCreative(cfgIndex, i, config->creatives[i]);
    }

    // Same semantics as removeConfig but called for every creatives.
    virtual void removeCreative(
            unsigned cfgIndex, unsigned crIndex, const Creative& creative) = 0;

    void removeConfig(
            unsigned cfgIndex, const std::shared_ptr<AgentConfig>& config)
    {
        for (size_t i = 0; i < config->creatives.size(); ++i)
            removeCreative(cfgIndex, i, config->creatives[i]);
    }

    // Same semantics as filter but called for every impressions.
    virtual void filterImpression(
            FilterState& state, unsigned impId, const AdSpot& imp) const
    {
        ExcAssert(false);
    }

    void filter(FilterState& state) const
    {
        for (size_t i = 0; i < state.request.imp.size(); ++i) {
            filterImpression(state, i, state.request.imp[i]);
            if (state.configs().empty()) break;
        }
    }

};


/******************************************************************************/
/* ITERATIVE CREATIVE FILTER                                                  */
/******************************************************************************/

/** Simplified filter base class at the cost of runtime performance. The filter
    should overide and implemen the filterCreative or filterImpression functions.

 */
template<typename Filter>
struct IterativeCreativeFilter : public IterativeFilter<Filter>
{

    virtual void filter(FilterState& state) const
    {
        for (size_t impId = 0; impId < state.request.imp.size(); ++impId) {
            const auto& imp = state.request.imp[impId];
            auto mask = filterImpression(state, impId, imp);
            state.narrowCreativesForImp(impId, mask);
        }
    }

    // Same semantics as filter but called for every impressions.
    virtual CreativeMatrix
    filterImpression(FilterState& state, size_t impId, const AdSpot& imp)
    {
        CreativeMatrix mask;
        auto active = state.creatives(impId);

        ConfigSet matches = state.configs();
        for (size_t cfgId = matches.next();
             cfgId < matches.size();
             cfgId = matches.next(cfgId+1))
        {
            const auto& cfg = *this->configs[cfgId];

            for (size_t crId = 0; crId < cfg.creatives.size(); ++crId) {
                if (!active[crId][cfgId]) continue;

                if (filterCreative(state, imp, cfg, cfg.creatives[crId]))
                    mask.set(crId, cfgId);
            }
        }

        return mask;
    }

    // Same semantics as filter but called for every impression, agent config,
    // creatives combinations.
    virtual bool
    filterCreative(
            FilterState&, const AdSpot&, const AgentConfig&, const Creative&)
    {
        ExcAssert(false);
        return false;
    }

};


/******************************************************************************/
/* CREATIVE REGEX FILTER                                                      */
/******************************************************************************/

/** Generic include filter for regexes.
<commit_msg>When you give into the const-ness, the darkness comes<commit_after>/** generic_creative_filters.h                                 -*- C++ -*-
    <NAME>, 09 Aug 2013
    Copyright (c) 2013 Datacratic.  All rights reserved.

    Generic filters for creatives.

    \todo Really need to find a way to merge the generic_filters.h header with
    this one somehow. It's pretty silly to have 2 sets of class that are almost
    but not quite the same.

*/

#pragma once

#include "generic_filters.h"

namespace RTBKIT {


/******************************************************************************/
/* CREATIVE FILTER                                                            */
/******************************************************************************/

/** Convenience base class for creative filters. */
template<typename Filter>
struct CreativeFilter : public FilterBaseT<Filter>
{

    // Same semantics as addConfig but called for every creatives.
    virtual void addCreative(
            unsigned cfgIndex, unsigned crIndex, const Creative& creative) = 0;

    void addConfig(
            unsigned cfgIndex, const std::shared_ptr<AgentConfig>& config)
    {
        for (size_t i = 0; i < config->creatives.size(); ++i)
            addCreative(cfgIndex, i, config->creatives[i]);
    }

    // Same semantics as removeConfig but called for every creatives.
    virtual void removeCreative(
            unsigned cfgIndex, unsigned crIndex, const Creative& creative) = 0;

    void removeConfig(
            unsigned cfgIndex, const std::shared_ptr<AgentConfig>& config)
    {
        for (size_t i = 0; i < config->creatives.size(); ++i)
            removeCreative(cfgIndex, i, config->creatives[i]);
    }

    // Same semantics as filter but called for every impressions.
    virtual void filterImpression(
            FilterState& state, unsigned impId, const AdSpot& imp) const
    {
        ExcAssert(false);
    }

    void filter(FilterState& state) const
    {
        for (size_t i = 0; i < state.request.imp.size(); ++i) {
            filterImpression(state, i, state.request.imp[i]);
            if (state.configs().empty()) break;
        }
    }

};


/******************************************************************************/
/* ITERATIVE CREATIVE FILTER                                                  */
/******************************************************************************/

/** Simplified filter base class at the cost of runtime performance. The filter
    should overide and implemen the filterCreative or filterImpression functions.

 */
template<typename Filter>
struct IterativeCreativeFilter : public IterativeFilter<Filter>
{

    virtual void filter(FilterState& state) const
    {
        for (size_t impId = 0; impId < state.request.imp.size(); ++impId) {
            const auto& imp = state.request.imp[impId];
            auto mask = filterImpression(state, impId, imp);
            state.narrowCreativesForImp(impId, mask);
        }
    }

    // Same semantics as filter but called for every impressions.
    virtual CreativeMatrix
    filterImpression(FilterState& state, size_t impId, const AdSpot& imp) const
    {
        CreativeMatrix mask;
        auto active = state.creatives(impId);

        ConfigSet matches = state.configs();
        for (size_t cfgId = matches.next();
             cfgId < matches.size();
             cfgId = matches.next(cfgId+1))
        {
            const auto& cfg = *this->configs[cfgId];

            for (size_t crId = 0; crId < cfg.creatives.size(); ++crId) {
                if (!active[crId][cfgId]) continue;

                if (filterCreative(state, imp, cfg, cfg.creatives[crId]))
                    mask.set(crId, cfgId);
            }
        }

        return mask;
    }

    // Same semantics as filter but called for every impression, agent config,
    // creatives combinations.
    virtual bool
    filterCreative(
            FilterState&, const AdSpot&, const AgentConfig&, const Creative&) const
    {
        ExcAssert(false);
        return false;
    }

};


/******************************************************************************/
/* CREATIVE REGEX FILTER                                                      */
/******************************************************************************/

/** Generic include filter for regexes.

{'fraction_non_alphanumeric': 0.19461077844311378, 'fraction_numerical': 0.004375863657300783, 'mean_word_length': 2.4139150943396226, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4550686', 'n_tokens_mistral': 2268, 'n_tokens_neox': 2109, 'n_words': 664}