// Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved.
//
// NVIDIA CORPORATION and its licensors retain all intellectual property
// and proprietary rights in and to this software, related documentation
// and any modifications thereto.  Any use, reproduction, disclosure or
// distribution of this software and related documentation without an express
// license agreement from NVIDIA CORPORATION is strictly prohibited.
//
#include <OgnAdd2IntArrayDatabase.h>
#include <algorithm>
#include <numeric>

class OgnAdd2IntArray
{
public:
    static bool compute(OgnAdd2IntArrayDatabase& db)
    {
        const auto& a = db.inputs.a();
        const auto& b = db.inputs.b();
        auto& output = db.outputs.output();

        // If either array has only a single member that value will be added to all members of the other array.
        // The case when both have size 1 is handled by the first "if".
        if (a.size() == 1)
        {
            output = b;
            std::for_each(output.begin(), output.end(), [a](int& value) { value += a[0]; });
        }
        else if (b.size() == 1)
        {
            output = a;
            std::for_each(output.begin(), output.end(), [b](int& value) { value += b[0]; });
        }
        else if (a.size() != b.size())
        {
            db.logWarning("Attempted to add arrays of different sizes - %zu and %zu", a.size(), b.size());
            return false;
        }
        else if (a.size() > 0)
        {
            output.resize(a.size());
            std::transform(a.begin(), a.end(), b.begin(), output.begin(), std::plus<int>());
        }
        return true;
    }
};

REGISTER_OGN_NODE()
