/*
 * Copyright 2022 Google LLC
 *
 * 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.
 */

#ifndef FCP_AGGREGATION_CORE_TENSOR_AGGREGATOR_H_
#define FCP_AGGREGATION_CORE_TENSOR_AGGREGATOR_H_

#include <vector>

#include "fcp/aggregation/core/aggregator.h"
#include "fcp/aggregation/core/input_tensor_list.h"
#include "fcp/aggregation/core/tensor.h"
#include "fcp/base/monitoring.h"

namespace fcp {
namespace aggregation {

using OutputTensorList = std::vector<Tensor>;

// TensorAggregator is a base class for implementing Aggregation intrinsics
// with Tensor being an input and output type for the aggregation.
class TensorAggregator
    : public Aggregator<InputTensorList, OutputTensorList, TensorAggregator> {
 public:
  ~TensorAggregator() override = default;

  // Implementation of the base Aggregator class methods.
  Status Accumulate(InputTensorList tensors) override;
  bool CanReport() const override;
  StatusOr<OutputTensorList> Report() && override;

  // Returns the number of aggregated inputs.
  virtual int GetNumInputs() const = 0;

 protected:
  // Construct TensorAggregator
  explicit TensorAggregator() {}

  // The actual implementation of the tensor aggregation to be provided by
  // a derived class.
  virtual Status AggregateTensors(InputTensorList tensors) = 0;

  // Checks if the current TensorAggregator is valid e.g. the resulting output
  // hasn't been consumed.
  virtual Status CheckValid() const = 0;

  // Consumes the output of this TensorAggregator.
  virtual OutputTensorList TakeOutputs() && = 0;

 private:
  // Extracts the aggregated tensor and makes the current aggregator "consumed".
  OutputTensorList TakeTensors() &&;
};

}  // namespace aggregation
}  // namespace fcp

#endif  // FCP_AGGREGATION_CORE_TENSOR_AGGREGATOR_H_
