text:
// See docs in ../ops/parsing_ops.cc.

#include <numeric>
#include <unordered_set>
#include <vector>

#include "tensorflow/core/common_runtime/metrics.h"
#include "tensorflow/core/example/example.pb.h"
#include "tensorflow/core/example/feature.pb.h"
#include "tensorflow/core/framework/common_shape_fns.h"
#include "tensorflow/core/framework/numeric_op.h"
#include "tensorflow/core/framework/register_types.h"
#include "tensorflow/core/lib/core/errors.h"
#include "tensorflow/core/lib/gtl/array_slice.h"
#include "tensorflow/core/platform/logging.h"
#include "tensorflow/core/platform/protobuf.h"
#include "tensorflow/core/util/example_proto_fast_parsing.h"
#include "tensorflow/core/util/example_proto_helper.h"
#include "tensorflow/core/util/sparse/sparse_tensor.h"
#include "tensorflow/core/util/work_sharder.h"

namespace tensorflow {

namespace {
constexpr char kParseExampleV2[] = "ParseExampleV2";
constexpr char kParseSequenceExampleV2[] = "ParseSequenceExampleV2";
}  // namespace

// Note: this kernel is used by both the ParseExample op and the ParseExampleV2
// op.  It automatically determines which op was used by checking if the
// "ragged_value_types" attribute exists.
class ParseExampleOp : public OpKernel {
 public:
  explicit ParseExampleOp(OpKernelConstruction* ctx)
      : OpKernel(ctx), op_version_(ctx->def().op() == kParseExampleV2 ? 2 : 1) {
    OP_REQUIRES_OK(ctx, attrs_.Init(ctx, op_version_));
  }

  void Compute(OpKernelContext* ctx) override {
    const Tensor* names;
    const Tensor* serialized;
    std::vector<string> dense_keys_t;
    std::vector<string> sparse_keys_t;
    std::vector<string> ragged_keys_t;
    OpInputList dense_defaults;

    // Grab the inputs.
    OP_REQUIRES_OK(ctx, ctx->input("serialized", &serialized));
    OP_REQUIRES_OK(ctx, ctx->input("names", &names));
    if (op_version_ == 2) {
      OP_REQUIRES_OK(ctx, GetTensorKeys(ctx, "dense_keys", &dense_keys_t));
      OP_REQUIRES_OK(ctx, GetTensorKeys(ctx, "sparse_keys", &sparse_keys_t));
      OP_REQUIRES_OK(ctx, GetTensorKeys(ctx, "ragged_keys", &ragged_keys_t));
    } else {
      OP_REQUIRES_OK(ctx, GetInputListKeys(ctx, "dense_keys", &dense_keys_t));
      OP_REQUIRES_OK(ctx, GetInputListKeys(ctx, "sparse_keys", &sparse_keys_t));
    }
    std::call_once(flag_, [&dense_keys_t, &sparse_keys_t, &ragged_keys_t]() {
      metrics::RecordParseDenseFeature(dense_keys_t.size());
      metrics::RecordParseSparseFeature(sparse_keys_t.size());
      metrics::RecordParseRaggedFeature(ragged_keys_t.size());
    });
    OP_REQUIRES_OK(ctx, ctx->input_list("dense_defaults", &dense_defaults));

    // Validate input tensor shapes.
    OP_REQUIRES_OK(
        ctx, CheckInputShapes(serialized, names, dense_defaults, dense_keys_t,
                              sparse_keys_t, ragged_keys_t));

    example::FastParseExampleConfig config =
        MakeConfig(dense_keys_t, sparse_keys_t, ragged_keys_t, dense_defaults);

    example::Result result;
    if (TensorShapeUtils::IsVector(serialized->shape())) {
      OP_REQUIRES_OK(
          ctx, ParseExampleVector(config, serialized, names, ctx, &result));
    } else {
      OP_REQUIRES_OK(ctx, ParseExampleScalar(config, serialized, ctx, &result));
    }
    OP_REQUIRES_OK(ctx, WriteOutput(result, ctx));
  }

 protected:
  // Copies keys from tensor to std::vector<string>.
  Status GetTensorKeys(OpKernelContext* ctx, const string& input_name,
                       std::vector<string>* keys) const {
    const Tensor* key_t;
    TF_RETURN_IF_ERROR(ctx->input(input_name, &key_t));
    keys->reserve(key_t->NumElements());
    auto keys_flat = key_t->flat<tstring>();
    for (int i = 0; i < keys_flat.size(); ++i) {
      keys->push_back(keys_flat(i));
    }
    return Status::OK();
  }

  // Copies keys from OpInputList of scalar to std::vector<string>.
  Status GetInputListKeys(OpKernelContext* ctx, const string& input_name,
                          std::vector<string>* keys) const {
    OpInputList key_list;
    TF_RETURN_IF_ERROR(ctx->input_list(input_name, &key_list));
    keys->reserve(key_list.size());
    for (const auto& key : key_list) {
      keys->push_back(key.scalar<tstring>()());
    }
    return Status::OK();
  }

  // Validates the shapes of input tensors.
  Status CheckInputShapes(const Tensor* serialized, const Tensor* names,
                          const OpInputList& dense_defaults,
                          const std::vector<string>& dense_keys_t,
                          const std::vector<string>& sparse_keys_t,
                          const std::vector<string>& ragged_keys_t) const {
    if (op_version_ == 2) {
      if (TensorShapeUtils::IsMatrixOrHigher(serialized->shape())) {
        return errors::InvalidArgument(
            "Expected serialized to be a scalar or vector, got shape: ",
            serialized->shape().DebugString());
      }
    } else {
      if (!TensorShapeUtils::IsVector(serialized->shape())) {
        return errors::InvalidArgument(
            "Expected serialized to be a vector, got shape: ",
            serialized->shape().DebugString());
      }
    }
    if (names->NumElements() > 0 && names->shape() != serialized->shape()) {
      return errors::InvalidArgument(
          "Expected names have the same shape as serialized: name.shape=",
          names->shape().DebugString(),
          ", serialized.shape=", serialized->shape().DebugString());
    }
    if (op_version_ == 2) {
      if (dense_keys_t.size() != attrs_.num_dense) {
        return errors::InvalidArgument(
            "Expected len(dense_keys) == len(dense_types) but got: ",
            dense_keys_t.size(), " vs. ", attrs_.num_dense);
      }
      if (sparse_keys_t.size() != attrs_.num_sparse) {
        return errors::InvalidArgument(
            "Expected len(sparse_keys) == num_sparse but got: ",
            sparse_keys_t.size(), " vs. ", attrs_.num_sparse);
      }
      if (ragged_keys_t.size() != attrs_.num_ragged) {
        return errors::InvalidArgument(
            "Expected len(ragged_keys) == len(ragged_value_types) but got: ",
            ragged_keys_t.size(), " vs. ", attrs_.num_ragged);
      }
    }

    if (dense_defaults.size() != attrs_.num_dense) {
      return errors::InvalidArgument(
          "Expected len(dense_defaults) == len(dense_keys) but got: ",
          dense_defaults.size(), " vs. ", attrs_.num_dense);
    }

    for (int d = 0; d < static_cast<int>(attrs_.num_dense); ++d) {
      const Tensor& def_value = dense_defaults[d];
      if (attrs_.variable_length[d]) {
        if (def_value.NumElements() != 1) {
          return errors::InvalidArgument(
              "dense_shape[", d, "] is a variable length shape: ",
              attrs_.dense_shapes[d].DebugString(),
              ", therefore "
              "def_value[",
              d,
              "] must contain a single element ("
              "the padding element).  But its shape is: ",
              def_value.shape().DebugString());
        }
      } else if (def_value.NumElements() > 0) {
        if (!attrs_.dense_shapes[d].IsCompatibleWith(def_value.shape())) {
          return errors::InvalidArgument(
              "def_value[", d, "].shape() == ", def_value.shape().DebugString(),
              " is not compatible with dense_shapes_[", d,
              "] == ", attrs_.dense_shapes[d].DebugString());
        }
      }
      if (def_value.dtype() != attrs_.dense_types[d]) {
        return errors::InvalidArgument(
            "dense_defaults[", d,
            "].dtype() == ", DataTypeString(def_value.dtype()),
            " != dense_types_[", d,
            "] == ", DataTypeString(attrs_.dense_types[d]));
      }
    }
    return Status::OK();
  }

  // Populates the FastParseExampleConfig from keys & defaults.
  example::FastParseExampleConfig MakeConfig(
      const std::vector<string>& dense_keys_t,
      const std::vector<string>& sparse_keys_t,
      const std::vector<string>& ragged_keys_t,
      const OpInputList& dense_defaults) const {
    example::FastParseExampleConfig config;
    for (int d = 0; d < attrs_.num_dense; ++d) {
      config.dense.push_back({dense_keys_t[d], attrs_.dense_types[d],
                              attrs_.dense_shapes[d], dense_defaults[d],
                              attrs_.variable_length[d],
                              attrs_.elements_per_stride[d]});
    }
    for (int d = 0; d < attrs_.num_sparse; ++d) {
      config.sparse.push_back({sparse_keys_t[d], attrs_.sparse_types[d]});
    }
    for (int d = 0; d < attrs_.num_ragged; ++d) {
      config.ragged.push_back({ragged_keys_t[d], attrs_.ragged_value_types[d],
                               attrs_.ragged_split_types[d]});
    }
    return config;
  }

  // Parses a single example.
  Status ParseExampleScalar(const example::FastParseExampleConfig& config,
                            const Tensor* serialized, OpKernelContext* ctx,
                            example::Result* result) const {
    const tstring& serialized_proto = serialized->scalar<tstring>()();
    return FastParseSingleExample(config, serialized_proto, result);
  }

  // Parses a vector of examples.
  Status ParseExampleVector(const example::FastParseExampleConfig& config,
                            const Tensor* serialized, const Tensor* names,
                            OpKernelContext* ctx,
                            example::Result* result) const {
    auto serialized_t = serialized->flat<tstring>();
    auto names_t = names->flat<tstring>();
    gtl::ArraySlice<tstring> slice(serialized_t.data(), serialized_t.size());
    gtl::ArraySlice<tstring> names_slice(names_t.data(), names_t.size());
    return FastParseExample(
        config, slice, names_slice,
        ctx->device()->tensorflow_cpu_worker_threads()->workers, result);
  }

  Status WriteOutput(const example::Result& result,
                     OpKernelContext* ctx) const {
    OpOutputList dense_values;
    OpOutputList sparse_indices;
    OpOutputList sparse_values;
    OpOutputList sparse_shapes;
    TF_RETURN_IF_ERROR(ctx->output_list("dense_values", &dense_values));
    TF_RETURN_IF_ERROR(ctx->output_list("sparse_indices", &sparse_indices));
    TF_RETURN_IF_ERROR(ctx->output_list("sparse_values", &sparse_values));
    TF_RETURN_IF_ERROR(ctx->output_list("sparse_shapes", &sparse_shapes));
    for (int d = 0; d < attrs_.num_dense; ++d) {
      dense_values.set(d, result.dense_values[d]);
    }
    for (int d = 0; d < attrs_.num_sparse; ++d) {
      sparse_indices.set(d, result.sparse_indices[d]);
      sparse_values.set(d, result.sparse_values[d]);
      sparse_shapes.set(d, result.sparse_shapes[d]);
    }
    if (op_version_ == 2) {
      OpOutputList ragged_values;
      OpOutputList ragged_splits;
      TF_RETURN_IF_ERROR(ctx->output_list("ragged_values", &ragged_values));
      TF_RETURN_IF_ERROR(ctx->output_list("ragged_row_splits", &ragged_splits));
      for (int d = 0; d < attrs_.num_ragged; ++d) {
        ragged_values.set(d, result.ragged_values[d]);
        ragged_splits.set(d, result.ragged_splits[d]);
      }
    }
    return Status::OK();
  }

  ParseExampleAttrs attrs_;
  int op_version_;
  std::once_flag flag_;
};

REGISTER_KERNEL_BUILDER(Name("ParseExample").Device(DEVICE_CPU),
                        ParseExampleOp);
REGISTER_KERNEL_BUILDER(Name("ParseExampleV2").Device(DEVICE_CPU),
                        ParseExampleOp);

class ParseSingleExampleOp : public OpKernel {
 public:
  explicit ParseSingleExampleOp(OpKernelConstruction* ctx) : OpKernel(ctx) {
    OP_REQUIRES_OK(ctx, attrs_.Init(ctx));
    metrics::RecordParseDenseFeature(attrs_.dense_keys.size());
    metrics::RecordParseSparseFeature(attrs_.sparse_keys.size());
  }

  void Compute(OpKernelContext* ctx) override {
    const Tensor* serialized;
    OpInputList dense_defaults;

    // Grab the input list arguments.
    OP_REQUIRES_OK(ctx, ctx->input("serialized", &serialized));
    OP_REQUIRES_OK(ctx, ctx->input_list("dense_defaults", &dense_defaults));

    OP_REQUIRES(ctx, TensorShapeUtils::IsScalar(serialized->shape()),
                errors::InvalidArgument(
                    "Expected serialized to be a scalar, got shape: ",
                    serialized->shape().DebugString()));
    OP_REQUIRES(ctx, dense_defaults.size() == attrs_.dense_keys.size(),
                errors::InvalidArgument(
                    "Expected len(dense_defaults) == len(dense_keys) but got: ",
                    dense_defaults.size(), " vs. ", attrs_.dense_keys.size()));

    for (size_t d = 0; d < attrs_.dense_keys.size(); ++d) {
      const Tensor& def_value = dense_defaults[d];
      if (attrs_.variable_length[d]) {
        OP_REQUIRES(ctx, def_value.NumElements() == 1,
                    errors::InvalidArgument(
                        "dense_shape[", d, "] is a variable length shape: ",
                        attrs_.dense_shapes[d].DebugString(),
                        ", therefore "
                        "def_value[",
                        d,
                        "] must contain a single element ("
                        "the padding element).  But its shape is: ",
                        def_value.shape().DebugString()));
      } else if (def_value.NumElements() > 0) {
        OP_REQUIRES(ctx,
                    attrs_.dense_shapes[d].IsCompatibleWith(def_value.shape()),
                    errors::InvalidArgument(
                        "def_value[", d,
                        "].shape() == ", def_value.shape().DebugString(),
                        " is not compatible with dense_shapes_[", d,
                        "] == ", attrs_.dense_shapes[d].DebugString()));
      }
      OP_REQUIRES(ctx, def_value.dtype() == attrs_.dense_types[d],
                  errors::InvalidArgument(
                      "dense_defaults[", d, "].dtype() == ",
                      DataTypeString(def_value.dtype()), " != dense_types_[", d,
                      "] == ", DataTypeString(attrs_.dense_types[d])));
    }

    example::Result result;

    // TODO(mrry): Build the configuration once and cache it.
    example::FastParseExampleConfig config;
    for (int d = 0; d < attrs_.dense_keys.size(); ++d) {
      config.dense.push_back({attrs_.dense_keys[d], attrs_.dense_types[d],
                              attrs_.dense_shapes[d], dense_defaults[d],
                              attrs_.variable_length[d],
                              attrs_.elements_per_stride[d]});
    }
    for (int d = 0; d < attrs_.sparse_keys.size(); ++d) {
      config.sparse.push_back({attrs_.sparse_keys[d], attrs_.sparse_types[d]});
    }

    const tstring& serialized_proto = serialized->scalar<tstring>()();

    OP_REQUIRES_OK(ctx,
                   FastParseSingleExample(config, serialized_proto, &result));

    OpOutputList dense_values;
    OpOutputList sparse_indices;
    OpOutputList sparse_values;
    OpOutputList sparse_shapes;
    OP_REQUIRES_OK(ctx, ctx->output_list("dense_values", &dense_values));
    OP_REQUIRES_OK(ctx, ctx->output_list("sparse_indices", &sparse_indices));
    OP_REQUIRES_OK(ctx, ctx->output_list("sparse_values", &sparse_values));
    OP_REQUIRES_OK(ctx, ctx->output_list("sparse_shapes", &sparse_shapes));
    for (int d = 0; d < attrs_.dense_keys.size(); ++d) {
      dense_values.set(d, result.dense_values[d]);
    }
    for (int d = 0; d < attrs_.sparse_keys.size(); ++d) {
      sparse_indices.set(d, result.sparse_indices[d]);
      sparse_values.set(d, result.sparse_values[d]);
      sparse_shapes.set(d, result.sparse_shapes[d]);
    }
  }

 protected:
  ParseSingleExampleAttrs attrs_;
};

REGISTER_KERNEL_BUILDER(Name("ParseSingleExample").Device(DEVICE_CPU),
                        ParseSingleExampleOp);

class ParseSequenceExampleOp : public OpKernel {
 public:
  explicit ParseSequenceExampleOp(OpKernelConstruction* ctx)
      : OpKernel(ctx),
        op_version_(ctx->def().op() == kParseSequenceExampleV2 ? 2 : 1) {
    OP_REQUIRES_OK(ctx, attrs_.Init(ctx, op_version_));
    metrics::RecordParseDenseFeature(attrs_.context_dense_keys.size() +
                                     attrs_.feature_list_dense_keys.size());
    metrics::RecordParseSparseFeature(attrs_.context_sparse_keys.size() +
                                      attrs_.feature_list_sparse_keys.size());
  }

  void Compute(OpKernelContext* ctx) override {
    const Tensor* debug_name;
    const Tensor* serialized;
    OpInputList context_dense_defaults;

    OP_REQUIRES_OK(ctx, ctx->input("debug_name", &debug_name));
    OP_REQUIRES_OK(ctx, ctx->input("serialized", &serialized));
    OP_REQUIRES_OK(ctx, ctx->input_list("context_dense_defaults",
                                        &context_dense_defaults));
    const Tensor* context_dense_keys = nullptr;
    const Tensor* context_sparse_keys = nullptr;
    const Tensor* context_ragged_keys = nullptr;
    const Tensor* feature_list_dense_keys = nullptr;
    const Tensor* feature_list_sparse_keys = nullptr;
    const Tensor* feature_list_ragged_keys = nullptr;
    const Tensor* feature_list_dense_missing_assumed_empty = nullptr;
    if (op_version_ == 2) {
      OP_REQUIRES_OK(ctx,
                     ctx->input("feature_list_dense_missing_assumed_empty",
                                &feature_list_dense_missing_assumed_empty));
      OP_REQUIRES_OK(ctx,
                     ctx->input("context_dense_keys", &context_dense_keys));
      OP_REQUIRES_OK(ctx,
                     ctx->input("context_sparse_keys", &context_sparse_keys));
      OP_REQUIRES_OK(ctx,
                     ctx->input("context_ragged_keys", &context_ragged_keys));
      OP_REQUIRES_OK(
          ctx, ctx->input("feature_list_dense_keys", &feature_list_dense_keys));
      OP_REQUIRES_OK(ctx, ctx->input("feature_list_sparse_keys",
                                     &feature_list_sparse_keys));
      OP_REQUIRES_OK(ctx, ctx->input("feature_list_ragged_keys",
                                     &feature_list_ragged_keys));
      std::call_once(flag_, [&]() {
        metrics::RecordParseDenseFeature(
            context_dense_keys->NumElements() +
            feature_list_dense_keys->NumElements());
        metrics::RecordParseSparseFeature(
            context_sparse_keys->NumElements() +
            feature_list_sparse_keys->NumElements());
        metrics::RecordParseRaggedFeature(
            context_ragged_keys->NumElements() +
            feature_list_ragged_keys->NumElements());
      });
    }

    // Validate input tensor shapes.
    OP_REQUIRES_OK(ctx, CheckInputShapes(
                            serialized, debug_name, context_dense_defaults,
                            context_dense_keys, context_sparse_keys,
                            context_ragged_keys, feature_list_dense_keys,
                            feature_list_sparse_keys, feature_list_ragged_keys,
                            feature_list_dense_missing_assumed_empty));

    example::FastParseExampleConfig context_config =
        MakeContextConfig(context_dense_keys, context_sparse_keys,
                          context_ragged_keys, context_dense_defaults);
    example::FastParseExampleConfig feature_list_config = MakeFeatureListConfig(
        feature_list_dense_keys, feature_list_sparse_keys,
        feature_list_ragged_keys, feature_list_dense_missing_assumed_empty);

    bool is_batch = TensorShapeUtils::IsVector(serialized->shape());
    auto serialized_t = serialized->flat<tstring>();
    auto debug_name_t = debug_name->flat<tstring>();
    gtl::ArraySlice<tstring> slice(serialized_t.data(), serialized_t.size());
    gtl::ArraySlice<tstring> names_slice(debug_name_t.data(),
                                         debug_name_t.size());

    example::Result context_result, feature_list_result;
    std::vector<Tensor> dense_feature_lengths;
    OP_REQUIRES_OK(
        ctx, FastParseSequenceExample(
                 context_config, feature_list_config, slice, names_slice,
                 ctx->device()->tensorflow_cpu_worker_threads()->workers,
                 &context_result, &feature_list_result, &dense_feature_lengths,
                 is_batch));

    OP_REQUIRES_OK(ctx, WriteOutput(context_result, feature_list_result,
                                    dense_feature_lengths, ctx));
  }

 protected:
  Status CheckInputShapes(
      const Tensor* serialized, const Tensor* names,
      const OpInputList& context_dense_defaults,

      const Tensor* context_dense_keys, const Tensor* context_sparse_keys,
      const Tensor* context_ragged_keys, const Tensor* feature_list_dense_keys,
      const Tensor* feature_list_sparse_keys,
      const Tensor* feature_list_ragged_keys,
      const Tensor* feature_list_dense_missing_assumed_empty) const {
    if (TensorShapeUtils::IsMatrixOrHigher(serialized->shape())) {
      return errors::InvalidArgument(
          "Expected serialized to be a scalar or vector, got shape: ",
          serialized->shape().DebugString());
    }
    if (op_version_ > 1) {
      if (context_dense_keys->NumElements() != attrs_.num_context_dense) {
        return errors::InvalidArgument(
            "Expected len(context_dense_keys) to match len(Tcontext_dense)");
      }
      if (context_sparse_keys->NumElements() != attrs_.num_context_sparse) {
        return errors::InvalidArgument(
            "Expected len(context_sparse_keys) to match Ncontext_sparse");
      }
      if (context_ragged_keys->NumElements() != attrs_.num_context_ragged) {
        return errors::InvalidArgument(
            "Expected len(context_ragged_keys) to match "
            "len(context_ragged_value_types)");
      }
      if (feature_list_dense_keys->NumElements() !=
          attrs_.num_feature_list_dense) {
        return errors::InvalidArgument(
            "Expected len(feature_list_dense_keys) to match "
            "Nfeature_list_dense");
      }
      if (feature_list_dense_missing_assumed_empty->NumElements() !=
          attrs_.num_feature_list_dense) {
        return errors::InvalidArgument(
            "Expected len(feature_list_dense_missing_assumed_empty to match "
            "Nfeature_list_dense");
      }
      if (feature_list_sparse_keys->NumElements() !=
          attrs_.num_feature_list_sparse) {
        return errors::InvalidArgument(
            "Expected len(feature_list_sparse_keys) to match "
            "Nfeature_list_sparse");
      }
      if (feature_list_ragged_keys->NumElements() !=
          attrs_.num_feature_list_ragged) {
        return errors::InvalidArgument(
            "Expected len(feature_list_ragged_keys) to match "
            "len(feature_list_ragged_value_types)");
      }
    }
    if (context_dense_defaults.size() != attrs_.num_context_dense) {
      return errors::InvalidArgument(
          "Expected len(context_dense_defaults) "
          "== len(context_dense_keys) but got: ",
          context_dense_defaults.size(), " vs. ", attrs_.num_context_dense);
    }
    for (int d = 0; d < attrs_.num_context_dense; ++d) {
      const Tensor& def_value = context_dense_defaults[d];
      if (def_value.NumElements() > 0) {
        if (def_value.shape() != attrs_.context_dense_shapes[d]) {
          return errors::InvalidArgument(
              "default_value[", d,
              "].shape() == ", def_value.shape().DebugString(),
              " != context_dense_shapes[", d,
              "] == ", attrs_.context_dense_shapes[d].DebugString());
        }
        if (def_value.dtype() != attrs_.context_dense_types[d]) {
          return errors::InvalidArgument(
              "context_dense_defaults[", d,
              "].dtype() == ", DataTypeString(def_value.dtype()),
              " != context_dense_types[", d,
              "] == ", DataTypeString(attrs_.context_dense_types[d]));
        }
      }
    }
    return Status::OK();
  }

  example::FastParseExampleConfig MakeContextConfig(
      const Tensor* dense_keys, const Tensor* sparse_keys,
      const Tensor* ragged_keys,
      const OpInputList& context_dense_defaults) const {
    example::FastParseExampleConfig config;
    for (int d = 0; d < attrs_.num_context_dense; ++d) {
      const tstring& key = dense_keys ? dense_keys->flat<tstring>()(d)
                                      : attrs_.context_dense_keys[d];
      config.dense.push_back({key, attrs_.context_dense_types[d],
                              attrs_.context_dense_shapes[d],
                              context_dense_defaults[d],
                              false /* attrs_.context_variable_length[d] */,
                              0 /*attrs_.context_elements_per_stride[d] */});
    }
    for (int d = 0; d < attrs_.num_context_sparse; ++d) {
      const tstring& key = sparse_keys ? sparse_keys->flat<tstring>()(d)
                                       : attrs_.context_sparse_keys[d];
      config.sparse.push_back({key, attrs_.context_sparse_types[d]});
    }
    for (int d = 0; d < attrs_.num_context_ragged; ++d) {
      config.ragged.push_back({ragged_keys->flat<tstring>()(d),
                               attrs_.context_ragged_value_types[d],
                               attrs_.context_ragged_split_types[d]});
    }
    return config;
  }

  example::FastParseExampleConfig MakeFeatureListConfig(
      const Tensor* dense_keys, const Tensor* sparse_keys,
      const Tensor* ragged_keys,
      const Tensor* feature_list_dense_missing_assumed_empty) const {
    example::FastParseExampleConfig config;
    for (int d = 0; d < attrs_.num_feature_list_dense; ++d) {
      const tstring& key = dense_keys ? dense_keys->flat<tstring>()(d)
                                      : attrs_.feature_list_dense_keys[d];
      bool missing_assumed_empty =
          feature_list_dense_missing_assumed_empty
              ? feature_list_dense_missing_assumed_empty->flat<bool>()(d)
              : attrs_.feature_list_dense_missing_assumed_empty.count(key) > 0;
      DataType dtype = attrs_.feature_list_dense_types[d];
      Tensor default_value = Tensor(dtype, TensorShape({}));
      config.dense.push_back(
          {key, dtype, attrs_.feature_list_dense_shapes[d], default_value,
           missing_assumed_empty,
           0 /*attrs_.feature_list_elements_per_stride[d] */});
    }
    for (int d = 0; d < attrs_.num_feature_list_sparse; ++d) {
      const tstring& key = sparse_keys ? sparse_keys->flat<tstring>()(d)
                                       : attrs_.feature_list_sparse_keys[d];
      config.sparse.push_back({key, attrs_.feature_list_sparse_types[d]});
    }
    for (int d = 0; d < attrs_.num_feature_list_ragged; ++d) {
      config.ragged.push_back({ragged_keys->flat<tstring>()(d),
                               attrs_.feature_list_ragged_value_types[d],
                               attrs_.feature_list_ragged_split_types[d]});
    }
    return config;
  }

  Status WriteOutput(const example::Result& context_result,
                     const example::Result& feature_list_result,
                     const std::vector<Tensor>& dense_feature_lengths,
                     OpKernelContext* ctx) const {
    OpOutputList context_sparse_indices;
    OpOutputList context_sparse_values;
    OpOutputList context_sparse_shapes;
    OpOutputList context_dense_values;
    OpOutputList feature_list_sparse_indices;
    OpOutputList feature_list_sparse_values;
    OpOutputList feature_list_sparse_shapes;
    OpOutputList feature_list_dense_values;
    OpOutputList feature_list_dense_lengths;

    TF_RETURN_IF_ERROR(
        ctx->output_list("context_sparse_indices", &context_sparse_indices));
    TF_RETURN_IF_ERROR(
        ctx->output_list("context_sparse_values", &context_sparse_values));
    TF_RETURN_IF_ERROR(
        ctx->output_list("context_sparse_shapes", &context_sparse_shapes));
    TF_RETURN_IF_ERROR(
        ctx->output_list("context_dense_values", &context_dense_values));
    TF_RETURN_IF_ERROR(
        ctx->output_list("context_sparse_indices", &context_sparse_indices));
    TF_RETURN_IF_ERROR(ctx->output_list("feature_list_sparse_indices",
                                        &feature_list_sparse_indices));
    TF_RETURN_IF_ERROR(ctx->output_list("feature_list_sparse_values",
                                        &feature_list_sparse_values));
    TF_RETURN_IF_ERROR(ctx->output_list("feature_list_sparse_shapes",
                                        &feature_list_sparse_shapes));
    TF_RETURN_IF_ERROR(ctx->output_list("feature_list_dense_values",
                                        &feature_list_dense_values));
    TF_RETURN_IF_ERROR(ctx->output_list("feature_list_dense_lengths",
                                        &feature_list_dense_lengths));
    for (int d = 0; d < attrs_.num_context_dense; ++d) {
      context_dense_values.set(d, context_result.dense_values[d]);
    }
    for (int d = 0; d < attrs_.num_feature_list_dense; ++d) {
      feature_list_dense_values.set(d, feature_list_result.dense_values[d]);
      feature_list_dense_lengths.set(d, dense_feature_lengths[d]);
    }
    for (int d = 0; d < attrs_.num_context_sparse; ++d) {
      context_sparse_indices.set(d, context_result.sparse_indices[d]);
      context_sparse_values.set(d, context_result.sparse_values[d]);
      context_sparse_shapes.set(d, context_result.sparse_shapes[d]);
    }
    for (int d = 0; d < attrs_.num_feature_list_sparse; ++d) {
      feature_list_sparse_indices.set(d, feature_list_result.sparse_indices[d]);
      feature_list_sparse_values.set(d, feature_list_result.sparse_values[d]);
      feature_list_sparse_shapes.set(d, feature_list_result.sparse_shapes[d]);
    }
    if (op_version_ == 2) {
      OpOutputList context_ragged_values;
      OpOutputList context_ragged_splits;
      OpOutputList feature_list_ragged_values;
      OpOutputList feature_list_ragged_inner_splits;
      OpOutputList feature_list_ragged_outer_splits;
      TF_RETURN_IF_ERROR(
          ctx->output_list("context_ragged_values", &context_ragged_values));
      TF_RETURN_IF_ERROR(ctx->output_list("context_ragged_row_splits",
                                          &context_ragged_splits));
      TF_RETURN_IF_ERROR(ctx->output_list("feature_list_ragged_values",
                                          &feature_list_ragged_values));
      TF_RETURN_IF_ERROR(ctx->output_list("feature_list_ragged_inner_splits",
                                          &feature_list_ragged_inner_splits));
      TF_RETURN_IF_ERROR(ctx->output_list("feature_list_ragged_outer_splits",
                                          &feature_list_ragged_outer_splits));
      for (int d = 0; d < attrs_.num_context_ragged; ++d) {
        context_ragged_values.set(d, context_result.ragged_values[d]);
        context_ragged_splits.set(d, context_result.ragged_splits[d]);
      }
      for (int d = 0; d < attrs_.num_feature_list_ragged; ++d) {
        feature_list_ragged_values.set(d, feature_list_result.ragged_values[d]);
        feature_list_ragged_outer_splits.set(
            d, feature_list_result.ragged_outer_splits[d]);
        feature_list_ragged_inner_splits.set(
            d, feature_list_result.ragged_splits[d]);
      }
    }
    return Status::OK();
  }

  ParseSequenceExampleAttrs attrs_;
  int op_version_;
  std::once_flag flag_;
};

REGISTER_KERNEL_BUILDER(Name("ParseSequenceExample").Device(DEVICE_CPU),
                        ParseSequenceExampleOp);
REGISTER_KERNEL_BUILDER(Name("ParseSequenceExampleV2").Device(DEVICE_CPU),
                        ParseSequenceExampleOp);

class ParseSingleSequenceExampleOp : public OpKernel {
 public:
  explicit ParseSingleSequenceExampleOp(OpKernelConstruction* ctx)
      : OpKernel(ctx) {
    OP_REQUIRES_OK(ctx, attrs_.Init(ctx));
  }

  void Compute(OpKernelContext* ctx) override {
    const Tensor* debug_name;
    const Tensor* serialized;
    OpInputList context_dense_keys;
    OpInputList context_sparse_keys;
    OpInputList context_dense_defaults;
    OpInputList feature_list_dense_keys;
    OpInputList feature_list_sparse_keys;
    const Tensor* feature_list_dense_missing_assumed_empty;

    OP_REQUIRES_OK(ctx, ctx->input("debug_name", &debug_name));
    OP_REQUIRES_OK(ctx, ctx->input("serialized", &serialized));
    OP_REQUIRES_OK(ctx, ctx->input("feature_list_dense_missing_assumed_empty",
                                   &feature_list_dense_missing_assumed_empty));
    OP_REQUIRES_OK(ctx,
                   ctx->input_list("context_dense_keys", &context_dense_keys));
    OP_REQUIRES_OK(ctx, ctx->input_list("feature_list_dense_keys",
                                        &feature_list_dense_keys));
    OP_REQUIRES_OK(
        ctx, ctx->input_list("context_sparse_keys", &context_sparse_keys));
    OP_REQUIRES_OK(ctx, ctx->input_list("feature_list_sparse_keys",
                                        &feature_list_sparse_keys));
    OP_REQUIRES_OK(ctx, ctx->input_list("context_dense_defaults",
                                        &context_dense_defaults));

    std::vector<string> context_dense_keys_t(attrs_.num_context_dense);
    std::vector<string> context_sparse_keys_t(attrs_.num_context_sparse);
    std::vector<string> feature_list_dense_keys_t(
        attrs_.num_feature_list_dense);
    std::vector<string> feature_list_sparse_keys_t(
        attrs_.num_feature_list_sparse);
    std::call_once(
        flag_, [&context_dense_keys_t, &context_sparse_keys_t,
                &feature_list_dense_keys_t, &feature_list_sparse_keys_t]() {
          metrics::RecordParseDenseFeature(context_dense_keys_t.size() +
                                           feature_list_dense_keys_t.size());
          metrics::RecordParseSparseFeature(context_sparse_keys_t.size() +
                                            feature_list_sparse_keys_t.size());
        });
    std::unordered_set<string> feature_list_dense_missing_assumed_empty_set;
    CHECK_EQ(context_dense_keys.size(), attrs_.num_context_dense);
    CHECK_EQ(context_sparse_keys.size(), attrs_.num_context_sparse);
    CHECK_EQ(feature_list_dense_keys.size(), attrs_.num_feature_list_dense);
    CHECK_EQ(feature_list_sparse_keys.size(), attrs_.num_feature_list_sparse);
    for (int di = 0; di < attrs_.num_context_dense; ++di) {
      OP_REQUIRES(ctx,
                  TensorShapeUtils::IsScalar(context_dense_keys[di].shape()),
                  errors::InvalidArgument(
                      "Expected context_dense_keys[", di,
                      "] to be a scalar, got shape: ",
                      context_dense_keys[di].shape().DebugString()));
      context_dense_keys_t[di] = context_dense_keys[di].scalar<tstring>()();
    }
    for (int di = 0; di < attrs_.num_context_sparse; ++di) {
      OP_REQUIRES(ctx,
                  TensorShapeUtils::IsScalar(context_sparse_keys[di].shape()),
                  errors::InvalidArgument(
                      "Expected context_sparse_keys[", di,
                      "] to be a scalar, got shape: ",
                      context_sparse_keys[di].shape().DebugString()));
      context_sparse_keys_t[di] = context_sparse_keys[di].scalar<tstring>()();
    }
    for (int di = 0; di < attrs_.num_feature_list_dense; ++di) {
      OP_REQUIRES(
          ctx, TensorShapeUtils::IsScalar(feature_list_dense_keys[di].shape()),
          errors::InvalidArgument(
              "Expected feature_list_dense_keys[", di,
              "] to be a scalar, got shape: ",
              feature_list_dense_keys[di].shape().DebugString()));
      feature_list_dense_keys_t[di] =
          feature_list_dense_keys[di].scalar<tstring>()();
    }
    for (int di = 0; di < attrs_.num_feature_list_sparse; ++di) {
      OP_REQUIRES(
          ctx, TensorShapeUtils::IsScalar(feature_list_sparse_keys[di].shape()),
          errors::InvalidArgument(
              "Expected feature_list_sparse_keys[", di,
              "] to be a scalar, got shape: ",
              feature_list_sparse_keys[di].shape().DebugString()));
      feature_list_sparse_keys_t[di] =
          feature_list_sparse_keys[di].scalar<tstring>()();
    }
    OP_REQUIRES(
        ctx,
        TensorShapeUtils::IsVector(
            feature_list_dense_missing_assumed_empty->shape()),
        errors::InvalidArgument(
            "Expected feature_list_dense_missing_assumed_empty ",
            "to be a vector, got shape: ",
            feature_list_dense_missing_assumed_empty->shape().DebugString()));
    auto feature_list_dense_missing_assumped_empty_t =
        feature_list_dense_missing_assumed_empty->vec<tstring>();
    for (int de = 0;
         de < feature_list_dense_missing_assumed_empty->NumElements(); ++de) {
      feature_list_dense_missing_assumed_empty_set.insert(
          feature_list_dense_missing_assumped_empty_t(de));
    }

    bool has_debug_name = (debug_name->NumElements() > 0);
    if (has_debug_name) {
      OP_REQUIRES(ctx, TensorShapeUtils::IsScalar(debug_name->shape()),
                  errors::InvalidArgument(
                      "Expected debug_name to be a scalar, got shape: ",
                      debug_name->shape().DebugString()));
    }
    auto debug_name_t = debug_name->scalar<tstring>();

    OP_REQUIRES(ctx, TensorShapeUtils::IsScalar(serialized->shape()),
                errors::InvalidArgument(
                    "Expected serialized to be a scalar, got shape: ",
                    serialized->shape().DebugString()));

    OP_REQUIRES(ctx, context_dense_defaults.size() == attrs_.num_context_dense,
                errors::InvalidArgument("Expected len(context_dense_defaults) "
                                        "== len(context_dense_keys) but got: ",
                                        context_dense_defaults.size(), " vs. ",
                                        attrs_.num_context_dense));

    std::vector<bool> required(attrs_.num_context_dense);
    for (int d = 0; d < attrs_.num_context_dense; ++d) {
      const Tensor& def_value = context_dense_defaults[d];
      required[d] = (def_value.NumElements() == 0);  // No default provided.

      if (def_value.NumElements() > 0) {
        OP_REQUIRES(ctx, def_value.shape() == attrs_.context_dense_shapes[d],
                    errors::InvalidArgument(
                        "def_value[", d,
                        "].shape() == ", def_value.shape().DebugString(),
                        " != context_dense_shapes_[", d,
                        "] == ", attrs_.context_dense_shapes[d].DebugString()));
        OP_REQUIRES(
            ctx, def_value.dtype() == attrs_.context_dense_types[d],
            errors::InvalidArgument(
                "context_dense_defaults[", d, "].dtype() == ",
                DataTypeString(def_value.dtype()), " != context_dense_types_[",
                d, "] == ", DataTypeString(attrs_.context_dense_types[d])));
      }
    }

    auto serialized_t = serialized->scalar<tstring>();

    OpOutputList context_sparse_indices;
    OpOutputList context_sparse_values;
    OpOutputList context_sparse_shapes;
    OpOutputList context_dense_values;
    OpOutputList feature_list_sparse_indices;
    OpOutputList feature_list_sparse_values;
    OpOutputList feature_list_sparse_shapes;
    OpOutputList feature_list_dense_values;

    OP_REQUIRES_OK(ctx, ctx->output_list("context_sparse_indices",
                                         &context_sparse_indices));
    OP_REQUIRES_OK(
        ctx, ctx->output_list("context_sparse_values", &context_sparse_values));
    OP_REQUIRES_OK(
        ctx, ctx->output_list("context_sparse_shapes", &context_sparse_shapes));
    OP_REQUIRES_OK(
        ctx, ctx->output_list("context_dense_values", &context_dense_values));
    OP_REQUIRES_OK(ctx, ctx->output_list("context_sparse_indices",
                                         &context_sparse_indices));
    OP_REQUIRES_OK(ctx, ctx->output_list("feature_list_sparse_indices",
                                         &feature_list_sparse_indices));
    OP_REQUIRES_OK(ctx, ctx->output_list("feature_list_sparse_values",
                                         &feature_list_sparse_values));
    OP_REQUIRES_OK(ctx, ctx->output_list("feature_list_sparse_shapes",
                                         &feature_list_sparse_shapes));
    OP_REQUIRES_OK(ctx, ctx->output_list("feature_list_dense_values",
                                         &feature_list_dense_values));

#ifdef TENSORFLOW_LITE_PROTOS
    SequenceExample ex;
#else
    // Allocate the SequenceExample on an arena. Provides better memory locality
    // and greatly speeds up destruction.
    protobuf::ArenaOptions options;
    // We have some hint of what the final proto size will be based on the size
    // of the serialized bytes- use this to set a custom allocation strategy.
    // Note that the default allocation strategy is quite conservative (min
    // block size of 256 bytes, and a max of 8 kilobytes).
    const size_t block_size = serialized_t().size() * 1.1;
    options.start_block_size = std::max(options.start_block_size, block_size);
    options.max_block_size = std::max(options.max_block_size, block_size);
    protobuf::Arena arena(options);
    auto& ex = *protobuf::Arena::CreateMessage<SequenceExample>(&arena);
#endif
    OP_REQUIRES(
        ctx, ParseProtoUnlimited(&ex, serialized_t()),
        errors::InvalidArgument("Could not parse example input, value: '",
                                serialized_t(), "'"));

    const tstring& name = (has_debug_name) ? debug_name_t() : "<unknown>";
    const Features& context = ex.context();
    const auto& context_dict = context.feature();

    // Context Dense -----------------------------------------------------------

    // Preallocate context_dense_values, since we know their sizes
    for (int d = 0; d < attrs_.num_context_dense; ++d) {
      TensorShape out_shape;
      for (const int dim : attrs_.context_dense_shapes[d].dim_sizes())
        out_shape.AddDim(dim);
      Tensor* out = nullptr;
      OP_REQUIRES_OK(ctx, context_dense_values.allocate(d, out_shape, &out));
    }

    for (int d = 0; d < attrs_.num_context_dense; ++d) {
      const tstring& key = context_dense_keys_t[d];
      const DataType& dtype = attrs_.context_dense_types[d];
      const TensorShape& shape = attrs_.context_dense_shapes[d];

      const auto& feature_found = context_dict.find(key);
      OP_REQUIRES(
          ctx, (feature_found != context_dict.end()) || !required[d],
          errors::InvalidArgument("Name: ", name, ", Context feature '", key,
                                  "' is required but could not be found."));
      if (feature_found != context_dict.end()) {
        const Feature& f = feature_found->second;
        bool types_match;
        OP_REQUIRES_OK(ctx, CheckTypesMatch(f, dtype, &types_match));
        OP_REQUIRES(
            ctx, types_match,
            errors::InvalidArgument("Name: ", name, ", Context feature: ", key,
                                    ".  Data types don't match. ",
                                    "Expected type: ", DataTypeString(dtype),
                                    "  Feature is: ", f.DebugString()));

        OP_REQUIRES_OK(ctx, FeatureDenseCopy(0, name, key, dtype, shape, f,
                                             context_dense_values[d]));
      } else {
        RowDenseCopy(0, dtype, context_dense_defaults[d],
                     context_dense_values[d]);
      }
    }

    // Context Sparse ----------------------------------------------------------
    for (int d = 0; d < attrs_.num_context_sparse; ++d) {
      const tstring& key = context_sparse_keys_t[d];
      const DataType& dtype = attrs_.context_sparse_types[d];

      const auto& feature_found = context_dict.find(key);
      bool feature_has_data =  // Found key & data type is set
          (feature_found != context_dict.end() &&
           (feature_found->second.kind_case() != Feature::KIND_NOT_SET));

      if (feature_has_data) {
        const Feature& f = feature_found->second;
        bool types_match;
        OP_REQUIRES_OK(ctx, CheckTypesMatch(f, dtype, &types_match));
        OP_REQUIRES(
            ctx, types_match,
            errors::InvalidArgument("Name: ", name, ", Context feature: ", key,
                                    ".  Data types don't match. ",
                                    "Expected type: ", DataTypeString(dtype),
                                    "  Feature is: ", f.DebugString()));

        Tensor feature_values = FeatureSparseCopy(0, key, dtype, f);
        const int64 num_elements = feature_values.NumElements();
        TensorShape indices_shape({num_elements, 1});
        Tensor* sp_indices_d = nullptr;
        Tensor* sp_shape_d = nullptr;
        OP_REQUIRES_OK(ctx, context_sparse_indices.allocate(d, indices_shape,
                                                            &sp_indices_d));
        context_sparse_values.set(d, feature_values);
        OP_REQUIRES_OK(ctx, context_sparse_shapes.allocate(d, TensorShape({1}),
                                                           &sp_shape_d));
        auto shape_t = sp_shape_d->vec<int64>();
        shape_t(0) = num_elements;
        auto indices_t = sp_indices_d->matrix<int64>();
        std::iota(indices_t.data(), indices_t.data() + num_elements, 0);
      } else {
        TensorShape indices_shape({0, 1});
        TensorShape values_shape({0});
        Tensor* sp_indices_d = nullptr;
        Tensor* sp_values_d = nullptr;
        Tensor* sp_shape_d = nullptr;
        OP_REQUIRES_OK(ctx, context_sparse_indices.allocate(d, indices_shape,
                                                            &sp_indices_d));
        OP_REQUIRES_OK(
            ctx, context_sparse_values.allocate(d, values_shape, &sp_values_d));
        OP_REQUIRES_OK(ctx, context_sparse_shapes.allocate(d, TensorShape({1}),
                                                           &sp_shape_d));
        auto shape_t = sp_shape_d->vec<int64>();
        shape_t(0) = 0;
      }
    }

    // Feature List Dense ------------------------------------------------------

    // Preallocate context_dense_values, since we can infer their
    // sizes
    const FeatureLists& feature_lists = ex.feature_lists();
    const auto& feature_list_dict = feature_lists.feature_list();
    FeatureList empty_feature_list;  // Placeholder for missing FLs

    for (int d = 0; d < attrs_.num_feature_list_dense; ++d) {
      const tstring& key = feature_list_dense_keys_t[d];
      const DataType& dtype = attrs_.feature_list_dense_types[d];
      const TensorShape& shape = attrs_.feature_list_dense_shapes[d];

      const auto& feature_list_found = feature_list_dict.find(key);
      bool feature_list_missing =
          (feature_list_found == feature_list_dict.end());
      bool feature_list_allowed_missing =
          (feature_list_dense_missing_assumed_empty_set.count(key) > 0);

      OP_REQUIRES(
          ctx, !feature_list_missing || feature_list_allowed_missing,
          errors::InvalidArgument("Name: ", name, ", Feature list '", key,
                                  "' is required but could not be found.  "
                                  "Did you mean to include it in "
                                  "feature_list_dense_missing_assumed_empty or "
                                  "feature_list_dense_defaults?"));

      TensorShape out_shape;
      const FeatureList& fl = (feature_list_missing)
                                  ? empty_feature_list
                                  : feature_list_found->second;
      out_shape.AddDim(fl.feature_size());
      for (const int dim : attrs_.feature_list_dense_shapes[d].dim_sizes()) {
        out_shape.AddDim(dim);
      }
      Tensor* out = nullptr;
      OP_REQUIRES_OK(ctx,
                     feature_list_dense_values.allocate(d, out_shape, &out));

      for (int64 t = 0; t < fl.feature_size(); ++t) {
        const Feature& f = fl.feature(t);
        bool types_match;
        OP_REQUIRES_OK(ctx, CheckTypesMatch(f, dtype, &types_match));
        OP_REQUIRES(ctx, types_match,
                    errors::InvalidArgument(
                        "Name: ", name, ", Feature list: ", key, ", Index: ", t,
                        ".  Data types don't match. ",
                        "Expected type: ", DataTypeString(dtype),
                        "  Feature is: ", f.DebugString()));
        OP_REQUIRES_OK(ctx, FeatureDenseCopy(t, name, key, dtype, shape, f,
                                             feature_list_dense_values[d]));
      }
    }

    // Feature List Sparse -----------------------------------------------------
    for (int d = 0; d < attrs_.num_feature_list_sparse; ++d) {
      const tstring& key = feature_list_sparse_keys_t[d];
      const DataType& dtype = attrs_.feature_list_sparse_types[d];

      const auto& feature_list_found = feature_list_dict.find(key);
      bool feature_list_has_data =  // Found key
          (feature_list_found != feature_list_dict.end());

      std::vector<Tensor> sparse_values_tmp;
      int64 feature_list_size = 0;
      if (feature_list_has_data) {
        const FeatureList& fl = feature_list_found->second;
        feature_list_size = fl.feature_size();
        for (int64 t = 0; t < feature_list_size; ++t) {
          const Feature& f = fl.feature(t);
          bool types_match;
          OP_REQUIRES_OK(ctx, CheckTypesMatch(f, dtype, &types_match));
          OP_REQUIRES(
              ctx, f.kind_case() == Feature::KIND_NOT_SET || types_match,
              errors::InvalidArgument("Name: ", name, ", Feature List: ", key,
                                      ", Index: ", t,
                                      ".  Data types don't match. ",
                                      "Expected type: ", DataTypeString(dtype),
                                      "  Feature is: ", f.DebugString()));
          sparse_values_tmp.push_back(FeatureSparseCopy(t, key, dtype, f));
        }
      } else {
        sparse_values_tmp.push_back(Tensor(dtype, TensorShape({0})));
      }

      int64 total_num_features = 0;
      int64 max_num_features = 0;
      for (int t = 0; t < feature_list_size; ++t) {
        const Tensor& v = sparse_values_tmp[t];
        const int64 num_elements = v.shape().num_elements();
        total_num_features += num_elements;
        max_num_features = std::max(max_num_features, num_elements);
      }

      TensorShape indices_shape({total_num_features, 2});
      TensorShape values_shape({total_num_features});
      Tensor* sp_indices_d = nullptr;
      Tensor* sp_values_d = nullptr;
      Tensor* sp_shape_d = nullptr;
      OP_REQUIRES_OK(ctx, feature_list_sparse_indices.allocate(d, indices_shape,
                                                               &sp_indices_d));
      OP_REQUIRES_OK(ctx, feature_list_sparse_values.allocate(d, values_shape,
                                                              &sp_values_d));
      OP_REQUIRES_OK(ctx, feature_list_sparse_shapes.allocate(
                              d, TensorShape({2}), &sp_shape_d));
      auto shape_t = sp_shape_d->vec<int64>();
      shape_t(0) = feature_list_size;
      shape_t(1) = max_num_features;

      int64 offset = 0;

      for (int t = 0; t < feature_list_size; ++t) {
        const int64 num_elements = CopyIntoSparseTensor(
            sparse_values_tmp[t], t, offset, sp_indices_d, sp_values_d);
        offset += num_elements;
      }
    }
  }

 protected:
  ParseSingleSequenceExampleAttrs attrs_;
  std::once_flag flag_;
};

REGISTER_KERNEL_BUILDER(Name("ParseSingleSequenceExample").Device(DEVICE_CPU),
                        ParseSingleSequenceExampleOp);

#ifndef IS_MOBILE_PLATFORM
// when using lite protos on mobile, decoding JSON is not available.

class DecodeJSONExampleOp : public OpKernel {
 public:
  explicit DecodeJSONExampleOp(OpKernelConstruction* ctx) : OpKernel(ctx) {
    resolver_.reset(protobuf::util::NewTypeResolverForDescriptorPool(
        "type.googleapis.com", protobuf::DescriptorPool::generated_pool()));
  }

  void Compute(OpKernelContext* ctx) override {
    const Tensor* json_examples;
    OP_REQUIRES_OK(ctx, ctx->input("json_examples", &json_examples));
    Tensor* binary_examples;
    OP_REQUIRES_OK(
        ctx, ctx->allocate_output("binary_examples", json_examples->shape(),
                                  &binary_examples));

    for (int i = 0; i < json_examples->NumElements(); ++i) {
      const tstring& json_example = json_examples->flat<tstring>()(i);
      protobuf::io::ArrayInputStream in(json_example.data(),
                                        json_example.size());
      TStringOutputStream out(&binary_examples->flat<tstring>()(i));
      auto status = protobuf::util::JsonToBinaryStream(
          resolver_.get(), "type.googleapis.com/tensorflow.Example", &in, &out);
      OP_REQUIRES(ctx, status.ok(),
                  errors::InvalidArgument("Error while parsing JSON: ",
                                          string(status.error_message())));
    }
  }

 private:
  std::unique_ptr<protobuf::util::TypeResolver> resolver_;
};

REGISTER_KERNEL_BUILDER(Name("DecodeJSONExample").Device(DEVICE_CPU),
                        DecodeJSONExampleOp);
#endif

}  // namespace tensorflow


text:
from django.apps import AppConfig

class MainConfig(AppConfig):
    name = 'main'
    verbose_name = ' Team Members and Mebership Requests'

redpajama_set_name: RedPajamaGithub


text:
'use strict';

require('angular').module('anyHarvest')
.component('photoUpload', {
  template: require('./photo-upload.html'),
  controllerAs: 'photoUploadCtrl',
  bindings: {
    photo: '<',
    handleSubmit: '<',
  },
});


text:
package hyperkube

import (
	"errors"
	"flag"
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"path"

	"github.com/kubernetes-incubator/service-catalog/pkg/version"
	"k8s.io/klog"

	"github.com/spf13/pflag"

	utiltemplate "github.com/kubernetes-incubator/service-catalog/pkg/kubernetes/pkg/util/template"
	"k8s.io/apimachinery/pkg/util/wait"
	"k8s.io/apiserver/pkg/server"
	utilflag "k8s.io/component-base/cli/flag"
	"k8s.io/component-base/logs"

	"github.com/kubernetes-incubator/service-catalog/pkg"
)

// HyperKube represents a single binary that can morph/manage into multiple
// servers.
type HyperKube struct {
	Name string // The executable name, used for help and soft-link invocation
	Long string // A long description of the binary.  It will be word-wrapped before output.

	servers             []Server
	baseFlags           *pflag.FlagSet
	out                 io.Writer
	helpFlagVal         bool
	printVersionFlagVal bool
	makeSymlinksFlagVal bool
}

// AddServer adds a server to the HyperKube object.
func (hk *HyperKube) AddServer(s *Server) {
	hk.servers = append(hk.servers, *s)
	hk.servers[len(hk.servers)-1].hk = hk
}

// FindServer will find a specific server named name.
func (hk *HyperKube) FindServer(name string) (*Server, error) {
	for _, s := range hk.servers {
		if s.Name() == name || s.AlternativeName == name {
			return &s, nil
		}
	}
	return nil, fmt.Errorf("Server not found: %s", name)
}

// Servers returns a list of all of the registered servers
func (hk *HyperKube) Servers() []Server {
	return hk.servers
}

// Flags returns a flagset for "global" flags.
func (hk *HyperKube) Flags() *pflag.FlagSet {
	if hk.baseFlags == nil {
		hk.baseFlags = pflag.NewFlagSet(hk.Name, pflag.ContinueOnError)
		hk.baseFlags.SetOutput(ioutil.Discard)
		hk.baseFlags.SetNormalizeFunc(utilflag.WordSepNormalizeFunc)
		hk.baseFlags.BoolVarP(&hk.helpFlagVal, "help", "h", false, "help for "+hk.Name)
		hk.baseFlags.BoolVar(&hk.printVersionFlagVal, "version", false, "Print version information and quit")
		hk.baseFlags.BoolVar(&hk.makeSymlinksFlagVal, "make-symlinks", false, "create a symlink for each server in current directory")
		hk.baseFlags.MarkHidden("make-symlinks") // hide this flag from appearing in servers' usage output

		// These will add all of the "global" flags (defined with both the
		// flag and pflag packages) to the new flag set we have.
		hk.baseFlags.AddGoFlagSet(flag.CommandLine)
		hk.baseFlags.AddFlagSet(pflag.CommandLine)

	}
	return hk.baseFlags
}

// Out returns the io.Writer that is used for all usage/error information
func (hk *HyperKube) Out() io.Writer {
	if hk.out == nil {
		hk.out = os.Stderr
	}
	return hk.out
}

// SetOut sets the output writer for all usage/error information
func (hk *HyperKube) SetOut(w io.Writer) {
	hk.out = w
}

// Print is a convenience method to Print to the defined output
func (hk *HyperKube) Print(i ...interface{}) {
	fmt.Fprint(hk.Out(), i...)
}

// Println is a convenience method to Println to the defined output
func (hk *HyperKube) Println(i ...interface{}) {
	fmt.Fprintln(hk.Out(), i...)
}

// Printf is a convenience method to Printf to the defined output
func (hk *HyperKube) Printf(format string, i ...interface{}) {
	fmt.Fprintf(hk.Out(), format, i...)
}

// Run the server.  This will pick the appropriate server and run it.
func (hk *HyperKube) Run(args []string, stopCh <-chan struct{}) error {
	// If we are called directly, parse all flags up to the first real
	// argument.  That should be the server to run.
RunAgain:
	command := args[0]
	serverName := path.Base(command)
	args = args[1:]
	if serverName == hk.Name {

		baseFlags := hk.Flags()
		baseFlags.SetInterspersed(false) // Only parse flags up to the next real command
		err := baseFlags.Parse(args)
		if err != nil || hk.helpFlagVal {
			if err != nil {
				hk.Println("Error:", err)
			}
			hk.Usage()
			return err
		}

		if hk.makeSymlinksFlagVal {
			return hk.MakeSymlinks(command)
		}

		if hk.printVersionFlagVal {
			pkg.PrintAndExit()
		}

		args = baseFlags.Args()
		if len(args) > 0 && len(args[0]) > 0 {
			serverName = args[0]
			args = args[1:]
		} else {
			err = errors.New("no server specified")
			hk.Printf("Error: %v\n\n", err)
			hk.Usage()
			return err
		}
	}

	s, err := hk.FindServer(serverName)
	if err != nil {
		if len(args) > 0 {
			goto RunAgain // the first args was popped off at start of Run, try again with new args
		}
		hk.Printf("Error: %v\n\n", err)
		hk.Usage()
		return err
	}

	s.Flags().AddFlagSet(hk.Flags())
	err = s.Flags().Parse(args)
	if err != nil || hk.helpFlagVal {
		if err != nil {
			hk.Printf("Error: %v\n\n", err)
		}
		s.Usage()
		return err
	}

	if hk.printVersionFlagVal {
		pkg.PrintAndExit()
	}

	logs.InitLogs()
	defer logs.FlushLogs()
	klog.Infof("Service Catalog version %s (built %s)", version.Get().String(), version.Get().BuildDate)
	if !s.RespectsStopCh {
		// For commands that do not respect the stopCh, we run them in a go
		// routine and leave them running when stopCh is closed.
		errCh := make(chan error)
		go func() {
			errCh <- s.Run(s, s.Flags().Args(), wait.NeverStop)
		}()
		select {
		case <-stopCh:
			return errors.New("interrupted") // This error text is ignored.
		case err = <-errCh:
			// fall-through
		}
	} else {
		err = s.Run(s, s.Flags().Args(), stopCh)
	}
	if err != nil {
		hk.Println("Error:", err)
	}

	return err
}

// RunToExit will run the hyperkube and then call os.Exit with an appropriate exit code.
func (hk *HyperKube) RunToExit(args []string) {
	stopCh := server.SetupSignalHandler()
	if err := hk.Run(args, stopCh); err != nil {
		os.Exit(1)
	}
}

// Usage will write out a summary for all servers that this binary supports.
func (hk *HyperKube) Usage() {
	tt := `{{if .Long}}{{.Long | trim | wrap ""}}
{{end}}Usage

  {{.Name}} <server> [flags]

Servers
{{range .Servers}}
  {{.Name}}
{{.Long | trim | wrap "    "}}{{end}}
Call '{{.Name}} --make-symlinks' to create symlinks for each server in the local directory.
Call '{{.Name}} <server> --help' for help on a specific server.
`
	utiltemplate.ExecuteTemplate(hk.Out(), tt, hk)
}

// MakeSymlinks will create a symlink for each registered hyperkube server in the local directory.
func (hk *HyperKube) MakeSymlinks(command string) error {
	wd, err := os.Getwd()
	if err != nil {
		return err
	}

	var errs bool
	for _, s := range hk.servers {
		link := path.Join(wd, s.Name())

		err := os.Symlink(command, link)
		if err != nil {
			errs = true
			hk.Println(err)
		}
	}

	if errs {
		return errors.New("error creating one or more symlinks")
	}
	return nil
}


text:
package Paws::WAFRegional::DeleteWebACL;
  use Moose;
  has ChangeToken => (is => 'ro', isa => 'Str', required => 1);
  has WebACLId => (is => 'ro', isa => 'Str', required => 1);

  use MooseX::ClassAttribute;

  class_has _api_call => (isa => 'Str', is => 'ro', default => 'DeleteWebACL');
  class_has _returns => (isa => 'Str', is => 'ro', default => 'Paws::WAFRegional::DeleteWebACLResponse');
  class_has _result_key => (isa => 'Str', is => 'ro');
1;

### main pod documentation begin ###

=head1 NAME

Paws::WAFRegional::DeleteWebACL - Arguments for method DeleteWebACL on Paws::WAFRegional

=head1 DESCRIPTION

This class represents the parameters used for calling the method DeleteWebACL on the
AWS WAF Regional service. Use the attributes of this class
as arguments to method DeleteWebACL.

You shouldn't make instances of this class. Each attribute should be used as a named argument in the call to DeleteWebACL.

As an example:

  $service_obj->DeleteWebACL(Att1 => $value1, Att2 => $value2, ...);

Values for attributes that are native types (Int, String, Float, etc) can passed as-is (scalar values). Values for complex Types (objects) can be passed as a HashRef. The keys and values of the hashref will be used to instance the underlying object.

=head1 ATTRIBUTES

=head2 B<REQUIRED> ChangeToken => Str

The value returned by the most recent call to GetChangeToken.

=head2 B<REQUIRED> WebACLId => Str

The C<WebACLId> of the WebACL that you want to delete. C<WebACLId> is
returned by CreateWebACL and by ListWebACLs.

=head1 SEE ALSO

This class forms part of L<Paws>, documenting arguments for method DeleteWebACL in L<Paws::WAFRegional>

=head1 BUGS and CONTRIBUTIONS

The source code is located here: https://github.com/pplu/aws-sdk-perl

Please report bugs to: https://github.com/pplu/aws-sdk-perl/issues

=cut


text:
using System;
using System.Collections.Generic;

namespace Azure.Management.Network.Models
{
    /// <summary> Describes the properties of a connection monitor. </summary>
    public partial class ConnectionMonitorResultProperties : ConnectionMonitorParameters
    {
        /// <summary> Initializes a new instance of ConnectionMonitorResultProperties. </summary>
        public ConnectionMonitorResultProperties()
        {
        }

        /// <summary> Initializes a new instance of ConnectionMonitorResultProperties. </summary>
        /// <param name="source"> Describes the source of connection monitor. </param>
        /// <param name="destination"> Describes the destination of connection monitor. </param>
        /// <param name="autoStart"> Determines if the connection monitor will start automatically once created. </param>
        /// <param name="monitoringIntervalInSeconds"> Monitoring interval in seconds. </param>
        /// <param name="endpoints"> List of connection monitor endpoints. </param>
        /// <param name="testConfigurations"> List of connection monitor test configurations. </param>
        /// <param name="testGroups"> List of connection monitor test groups. </param>
        /// <param name="outputs"> List of connection monitor outputs. </param>
        /// <param name="notes"> Optional notes to be associated with the connection monitor. </param>
        /// <param name="provisioningState"> The provisioning state of the connection monitor. </param>
        /// <param name="startTime"> The date and time when the connection monitor was started. </param>
        /// <param name="monitoringStatus"> The monitoring status of the connection monitor. </param>
        /// <param name="connectionMonitorType"> Type of connection monitor. </param>
        internal ConnectionMonitorResultProperties(ConnectionMonitorSource source, ConnectionMonitorDestination destination, bool? autoStart, int? monitoringIntervalInSeconds, IList<ConnectionMonitorEndpoint> endpoints, IList<ConnectionMonitorTestConfiguration> testConfigurations, IList<ConnectionMonitorTestGroup> testGroups, IList<ConnectionMonitorOutput> outputs, string notes, ProvisioningState? provisioningState, DateTimeOffset? startTime, string monitoringStatus, ConnectionMonitorType? connectionMonitorType) : base(source, destination, autoStart, monitoringIntervalInSeconds, endpoints, testConfigurations, testGroups, outputs, notes)
        {
            ProvisioningState = provisioningState;
            StartTime = startTime;
            MonitoringStatus = monitoringStatus;
            ConnectionMonitorType = connectionMonitorType;
        }

        /// <summary> The provisioning state of the connection monitor. </summary>
        public ProvisioningState? ProvisioningState { get; }
        /// <summary> The date and time when the connection monitor was started. </summary>
        public DateTimeOffset? StartTime { get; }
        /// <summary> The monitoring status of the connection monitor. </summary>
        public string MonitoringStatus { get; }
        /// <summary> Type of connection monitor. </summary>
        public ConnectionMonitorType? ConnectionMonitorType { get; }
    }
}


text:
package com.facebook.buck.core.build.action;

import com.facebook.buck.core.model.BuildTarget;
import com.facebook.buck.core.sourcepath.SourcePath;
import com.google.common.collect.ImmutableSet;

/**
 * Interface for marking objects that the {@link com.facebook.buck.core.build.engine.BuildEngine}
 * can build. Eventually, this will become the {@link com.facebook.buck.core.rules.actions.Action}
 * interface itself. However, we keep this one around for compatibility between existing {@link
 * com.facebook.buck.core.rules.BuildRule}s and the new {@link
 * com.facebook.buck.core.rules.actions.Action}s.
 */
public interface BuildEngineAction {

  /** @return the {@link BuildTarget} of the rule corresponding to this action */
  BuildTarget getBuildTarget();

  /**
   * @return a set of dependencies required for this {@link BuildEngineAction} to build, as
   *     identified by the {@link BuildTarget}.
   */
  ImmutableSet<BuildTarget> getDependencies();

  /**
   * @return the set of outputs this {@link BuildEngineAction} builds. This is here for legacy as
   *     BuildRules deal with {@link SourcePath}
   */
  ImmutableSet<SourcePath> getSourcePathOutputs();

  /**
   * Whether this {@link BuildEngineAction} can be cached.
   *
   *
Uncached build rules are never written out to cache, never read from cache, and does not
   * count in cache statistics. This rule is useful for artifacts which cannot be easily normalized.
   */
  boolean isCacheable();

  /**
   * Executes this {@link BuildEngineAction}, called by the {@link
   * com.facebook.buck.core.build.engine.BuildEngine} to materialize the outputs declared in {@link
   * #getSourcePathOutputs()}
   *
   *
TODO(bobyf): uncomment the below and change the signature slightly once we move build engine
   * to actually use this.
   *
   *

   * void execute( ProjectFilesystem filesystem, ExecutionContext executionContext, BuildContext
   *    buildContext, BuildableContext buildableContext) throws StepFailedException,
   *    InterruptedException;
   *

   */

  /**
   * @return true if this rule should only be allowed to be executed via Remote Execution if it
   *     satisfies input size limits.
   */
  default boolean shouldRespectInputSizeLimitForRemoteExecution() {
    return true;
  }
}


text:
- Fixed typo in Readme file
- Fixed typo in description of XSOAR 6.0 configuration


text:
const waitDeath = () =>
  new Promise((resolve, reject) => {
    process.on("uncaughtException", reject);
    process.on("unhandledRejection", reject);
    process.on("SIGINT", resolve);
    process.on("SIGQUIT", resolve);
    process.on("SIGTERM", resolve);
  });

module.exports = waitDeath;


text:
function obj = setDistanceFile( obj, file )
% SETDISTANCEFILE  Set files for pairwise distances in an nlsaPairwiseDistance
% object
%
% Modified 2014/06/13

if isa( file, 'nlsaFilelist' )
    if ~isCompatible( file, getPartition( obj ) )
        error( 'Incompatible file list' )
    end
    obj.file = file;
else
    obj.file = setFile( obj.file, file );
end


text:
using System;
namespace Bank_accounts
{
    public class LoanAccount : Account , IDepositable
    {
        public LoanAccount(Customer customer, Balance balance, InterestRate interestRate)
            : base(customer, balance, interestRate)
        {

        }
        public void Deposit(decimal value)
        {
            this.balance.BalanceAmount += value;
        }
        protected override decimal CalculateInterestRate(int numOfMonths, decimal interestRate)
        {
            if (base.customer is IndividualCustomers)
            {
                return (numOfMonths-2) * interestRate;
            }
            else if (base.customer is CompanieCustomers)
            {
                return (numOfMonths-3) * interestRate;
            }
            else
            {
                throw new Exception("Invalid Customer");
            }
        }
    }
}


text:
"""
Copyright (c) 2016 Daniele Linguaglossa <d.linguaglossa@mseclab.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
from .pjf_version import PYJFUZZ_LOGLEVEL
import logging
import time
import sys

class PJFLogger(object):

    @staticmethod
    def init_logger():
        logger = logging.getLogger(__name__)
        logger.setLevel(level=PYJFUZZ_LOGLEVEL)
        filehandler = logging.FileHandler("pjf_{0}.log".format(time.strftime("%d_%m_%Y")))
        logger.addHandler(filehandler)
        sys.tracebacklimit = 10
        return logger


text:
package ru.job4j.iterator;

import java.util.Iterator;
import java.util.NoSuchElementException;

/**
 * class converter.
 */

public class Converter {

    /**
     * metod convert.
     * @param it - incoming Iterator.
     * @return iterator.
     */
    Iterator<Integer> convert(Iterator<Iterator<Integer>> it) {
        return new Iterator<Integer>() {

            private Iterator<Integer> current = it.next();

            @Override
            public boolean hasNext() {
                boolean result = false;
                while (it.hasNext() || current.hasNext()) {
                    if (current.hasNext()) {
                        result = true;
                        break;
                    } else {
                        current = it.next();
                    }
                }
                return result;
            }

            @Override
            public Integer next() {
                Integer result = null;
                while (it.hasNext() || current.hasNext()) {
                    if (current.hasNext()) {
                        result = current.next();
                        break;
                    } else {
                        current = it.next();
                    }
                }
                if (result == null) {
                    throw new NoSuchElementException();
                }
                return result;
            }
        };
    }
}


text:
package com.pavlovmedia.osgi.oss.utilities.api.component;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Hashtable;
import java.util.Map;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;

import org.osgi.service.component.ComponentFactory;
import org.osgi.service.component.ComponentInstance;

import com.pavlovmedia.osgi.oss.utilities.api.functional.ExceptionConsumer;
import com.pavlovmedia.osgi.oss.utilities.api.functional.ExceptionFunction;

/**
 * This is a class that helps simplify how we deal with component service
 * bindings and how we call them.
 *
 * This implementation is for a 1-1 case which is the general case. You could
 * however, have multiple instances of this using the same factory to track
 * different services with different lifecycles.
 *
 * @author Shawn Dempsay {@literal <sdempsay@pavlovmedia.com>}
 *
 * @param <T> Generic Type
 */
public class ComponentHolder<T> implements AutoCloseable {
    protected Optional<ComponentFactory<?>> factory = Optional.empty();
    protected Optional<ComponentInstance<?>> instance = Optional.empty();
    protected Optional<T> actual = Optional.empty();

    /**
     * This sets the inner factory instance that is used to generate
     * a component. It can only be called once before we close this
     * holder.
     *
     * @param factory - pass a Component Factor to be set
     */
    public void setFactory(final ComponentFactory<?> factory) {
        if (this.factory.isPresent()) {
          throw new IllegalStateException("There is already a factory set");
        }

        this.factory = Optional.of(factory);
    }

    /**
     * This will do all the work of provisioning a new instance using
     * the provided properties and will return true if it is successful
     * @param properties - pass a properties Map
     * @return boolean
     */
    @SuppressWarnings("unchecked")
    public boolean provision(final Map<String,Object> properties) {
        if (!factory.isPresent()) {
            throw new IllegalStateException("No factory is set");
        }
        Hashtable<String,Object> props = new Hashtable<>(properties);
        instance = Optional.ofNullable(factory.get().newInstance(props));
        if (instance.isPresent()) {
            actual = Optional.ofNullable((T) instance.get().getInstance());
            return true;
        }
        return false;
    }

    /**
     * This will execute an action with an response against the
     * instance.
     *
     * @param action It is a function with T Input to apply
     * @param <R> Return from apply
     * @return R the function result
     */
    public <R> R withService(final Function<T,R> action) {
        if (actual.isPresent()) {
            return action.apply(actual.get());
        }
        throw new IllegalStateException("No service is provisioned");
    }

    /**
     * This method is to execute a function against this instance that can throw an exception.
     * @param action the action to execute against this component instance
     * @param <R> Return from apply
     * @param <E> Exception that can be thrown
     * @return whatever the action returns
     * @throws E if the action throws an exception
     */
    public <R,E extends Exception> R withExceptionService(final ExceptionFunction<T,R,E> action) throws E {
        if (actual.isPresent()) {
            return action.apply(actual.get());
        }
        throw new IllegalStateException("No service is provisioned");
    }

    /**
     * This method is to execute an action with no response against this
     * instance
     * @param action action to execute against this component instance
     */
    public void againstService(final Consumer<T> action) {
        if (actual.isPresent()) {
            action.accept(actual.get());
        } else {
            throw new IllegalStateException("No service is provisioned");
        }
    }

    /**
     * This method is to execute an action against this instance with no
     * response but may throw an exception
     * @param action action to execute against this component instance
     * @param <E> Exception that can be thrown
     * @throws E if the action throws an exception
     */
    public <E extends Exception> void againstExceptionService(final ExceptionConsumer<T, E> action) throws E {
        if (actual.isPresent()) {
            action.consume(actual.get());
        } else {
            throw new IllegalStateException("No service is provisioned");
        }
    }

    @SuppressWarnings("unchecked")
    public T getProxy(final Class<T> clazz) {
        return (T) Proxy.newProxyInstance(clazz.getClassLoader(),
                new Class<?>[]{ clazz }, new ServiceHandler());
    }

    /**
     * This will do all the cleanup of this holder allowing it be be
     * reused.
     */
    @Override
    public void close() {
        instance.ifPresent(ComponentInstance::dispose);
        factory = Optional.empty();
        instance = Optional.empty();
        actual = Optional.empty();
    }

    /**
     * This is an inner proxy-class that can ensure
     * that we have an instance to work off of.
     *
     * @author Shawn Dempsay {@literal <sdempsay@pavlovmedia.com>}
     *
     */
    private class ServiceHandler implements InvocationHandler {

        @Override
        public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
            if (actual.isPresent()) {
                return method.invoke(actual.get(), args);
            }
            throw new IllegalStateException("No service is provisioned");
        }
    }
}


text:
using System.Collections.Generic;
using System.Linq;
using PoESkillTree.Computation.Model;
using PoESkillTree.Engine.GameModel.Items;
using PoESkillTree.Engine.GameModel.Skills;
using PoESkillTree.Engine.Utils;
using PoESkillTree.Model.Items;
using PoESkillTree.Utils;

namespace PoESkillTree.Model
{
    public class ObservableSkillCollection
    {
        private SkillEnabler? _skillEnabler;
        private ObservableSet<IReadOnlyList<Skill>>? _itemAttributesSkills;

        private readonly ObservableSet<IReadOnlyList<Gem>> _gems;
        private readonly AdditionalSkillStatApplier _additionalSkillStatApplier;

        public ObservableSet<IReadOnlyList<Skill>> Collection { get; } = new ObservableSet<IReadOnlyList<Skill>>();

        public ObservableSkillCollection(ObservableSet<IReadOnlyList<Gem>> gems, AdditionalSkillStatApplier additionalSkillStatApplier)
        {
            _gems = gems;
            _additionalSkillStatApplier = additionalSkillStatApplier;
            _additionalSkillStatApplier.StatChangedForSlot += AdditionalSkillStatApplierOnStatChangedForSlot;
            Collection.CollectionChanged += OnCollectionChanged;
        }

        public void ConnectTo(ItemAttributes itemAttributes)
        {
            if (_skillEnabler != null)
                _skillEnabler.EnabledChangedForSlots -= SkillEnablerOnEnabledChangedForSlots;
            _skillEnabler = itemAttributes.SkillEnabler;
            _itemAttributesSkills = itemAttributes.Skills;

            Collection.ResetTo(GetDefaultSkills().Select(Update));
            _itemAttributesSkills.ResetTo(Collection);

            _skillEnabler.EnabledChangedForSlots += SkillEnablerOnEnabledChangedForSlots;
        }

        public void InitializeSkillsFromGems(IEnumerable<IReadOnlyList<Skill>> skills)
        {
            Collection.ResetTo(skills.Concat(GetDefaultSkills()).Select(Update));
        }

        public void UpdateSkillsFromGems(IReadOnlyList<IReadOnlyList<Skill>> toAdd)
        {
            var slotsWithGemsOrItemSkills = _gems.Select(gs => gs.First().ItemSlot).Append(Skill.Default.ItemSlot).ToHashSet();
            var changedSlots = toAdd.Select(ss => ss.First().ItemSlot);
            var toRemove = Collection
                .Where(ss => !slotsWithGemsOrItemSkills.Contains(ss.First().ItemSlot) || changedSlots.Contains(ss.First().ItemSlot))
                .ToList();
            Collection.ExceptAndUnionWith(toRemove, toAdd.Select(Update));
        }

        private static IEnumerable<IReadOnlyList<Skill>> GetDefaultSkills() =>
            new[] {new[] {Skill.Default}};

        private IReadOnlyList<Skill> Update(IEnumerable<Skill> skills) =>
            skills.Select(Update).ToList();

        private Skill Update(Skill skill)
        {
            if (_skillEnabler != null)
            {
                skill = _skillEnabler.Apply(skill);
            }
            return _additionalSkillStatApplier.Apply(skill);
        }

        private void OnCollectionChanged(
            object sender, CollectionChangedEventArgs<IReadOnlyList<Skill>> args)
        {
            _additionalSkillStatApplier.CleanSubscriptions(args.RemovedItems, args.AddedItems);
            _itemAttributesSkills?.ExceptAndUnionWith(args.RemovedItems, args.AddedItems);
        }

        private void SkillEnablerOnEnabledChangedForSlots(object? sender, IReadOnlyCollection<ItemSlot> changedSlots)
        {
            var toRemove = Collection.Where(ss => changedSlots.Contains(ss.First().ItemSlot)).ToList();
            var toAdd = toRemove.Select(Update);
            Collection.ExceptAndUnionWith(toRemove, toAdd);
        }

        private void AdditionalSkillStatApplierOnStatChangedForSlot(object? sender, ItemSlot e)
        {
            var toRemove = Collection.Where(ss => ss.First().ItemSlot == e).ToList();
            var toAdd = toRemove.Select(Update);
            Collection.ExceptAndUnionWith(toRemove, toAdd);
        }
    }
}


text:
gem 'perftools.rb', '~> 0.5.6'

require 'tweetwine'
require 'perftools'
require 'support/common_helpers'
require 'webmock'

WebMock.disable_net_connect!

module Driver
  extend WebMock::API
  extend Tweetwine::Test::CommonHelpers

  def self.run
    stub_http_request(:get, "https://api.twitter.com/1/statuses/home_timeline.json?count=20&page=1").to_return(:body => fixture_file('home.json'))
    yield
  ensure
    WebMock.reset!
  end
end

Driver.run do
  output_path = "/tmp/tweetwine_home_bm"

  PerfTools::CpuProfiler.start(output_path) do
    1_000.times do
      Tweetwine::CLI.start %w{home}
    end
  end

  system %{pprof.rb --gif #{output_path} > #{output_path}.gif}

  puts "Profiling output: #{output_path}"
  puts "Visualization:    #{output_path}.gif"
end


text:
package com.lid.intellij.translateme.yandex;

import java.util.List;

public class TranslationResponse {

	int code;
	String lang;
	List<String> text;

	public int getCode() {
		return code;
	}

	public String getLang() {
		return lang;
	}

	public List<String> getText() {
		return text;
	}
}


text:
'use strict';

angular.module('footierepoApp').controller('UserManagementDialogController',
    ['$scope', '$stateParams', '$uibModalInstance', 'entity', 'User',
        function($scope, $stateParams, $uibModalInstance, entity, User) {

        $scope.user = entity;
        $scope.authorities = ["ROLE_USER", "ROLE_ADMIN"];
        var onSaveSuccess = function (result) {
            $scope.isSaving = false;
            $uibModalInstance.close(result);
        };

        var onSaveError = function (result) {
            $scope.isSaving = false;
        };

        $scope.save = function () {
            $scope.isSaving = true;
            if ($scope.user.id != null) {
                User.update($scope.user, onSaveSuccess, onSaveError);
            } else {
                $scope.user.langKey = 'en';
                User.save($scope.user, onSaveSuccess, onSaveError);
            }
        };

        $scope.clear = function() {
            $uibModalInstance.dismiss('cancel');
        };
}]);


text:
namespace boost { namespace python {

namespace detail
{
  template <class Data, class Class, class Policies>
  struct member
  {
      static PyObject* get(Data Class::*pm, PyObject* args_, PyObject*, Policies const& policies)
      {
          arg_from_python<Class*> c0(PyTuple_GET_ITEM(args_, 0));
          if (!c0.convertible()) return 0;

          // find the result converter
          typedef typename Policies::result_converter result_converter;
          typedef typename boost::add_reference<Data>::type source;
          typename mpl::apply1<result_converter,source>::type cr;

          if (!policies.precall(args_)) return 0;

          PyObject* result = cr( (c0(PyTuple_GET_ITEM(args_, 0)))->*pm );

          return policies.postcall(args_, result);
      }

      static PyObject* set(Data Class::*pm, PyObject* args_, PyObject*, Policies const& policies)
      {
          // check that each of the arguments is convertible
          arg_from_python<Class&> c0(PyTuple_GET_ITEM(args_, 0));
          if (!c0.convertible()) return 0;

          typedef typename add_const<Data>::type target1;
          typedef typename add_reference<target1>::type target;
          arg_from_python<target> c1(PyTuple_GET_ITEM(args_, 1));

          if (!c1.convertible()) return 0;

          if (!policies.precall(args_)) return 0;

          (c0(PyTuple_GET_ITEM(args_, 0))).*pm = c1(PyTuple_GET_ITEM(args_, 1));

          return policies.postcall(args_, detail::none());
      }
  };

  // If it's a regular class type (not an object manager or other
  // type for which we have to_python specializations, use
  // return_internal_reference so that we can do things like
  //    x.y.z =  1
  // and get the right result.
  template <class T>
  struct default_getter_policy
  {
      typedef typename add_reference<
          typename add_const<T>::type
      >::type t_cref;

      BOOST_STATIC_CONSTANT(
          bool, by_ref = to_python_value<t_cref>::uses_registry
                        && is_reference_to_class<t_cref>::value);

      typedef typename mpl::if_c<
        by_ref
        , return_internal_reference<>
        , return_value_policy<return_by_value>
      >::type type;
  };
}

template <class C, class D>
object make_getter(D C::*pm)
{
    typedef typename detail::default_getter_policy<D>::type policy;

    return objects::function_object(
        ::boost::bind(
            &detail::member<D,C,policy>::get, pm, _1, _2
            , policy())
        , 1);

}

template <class C, class D, class Policies>
object make_getter(D C::*pm, Policies const& policies)
{
    return objects::function_object(
            ::boost::bind(
                &detail::member<D,C,Policies>::get, pm, _1, _2
                , policies)
        , 1);
}

template <class C, class D>
object make_setter(D C::*pm)
{
    return objects::function_object(
        ::boost::bind(
            &detail::member<D,C,default_call_policies>::set, pm, _1, _2
            , default_call_policies())
        , 2);
}

template <class C, class D, class Policies>
object make_setter(D C::*pm, Policies const& policies)
{
    return objects::function_object(
        ::boost::bind(
            &detail::member<D,C,Policies>::set, pm, _1, _2
            , policies)
        , 2);
}

}} // namespace boost::python

#endif // DATA_MEMBERS_DWA2002328_HPP


text:
import re
from ftrace.common import ParserError
from .register import register_parser
from collections import namedtuple
#from ftrace.third_party.cnamedtuple import namedtuple

TRACEPOINT = 'clock_set_rate'

__all__ = [TRACEPOINT]

# clock_set_rate: krait1_pri_mux_clk state=300000000 cpu_id=0

ClockSetRateBase = namedtuple(TRACEPOINT,
    [
    'clk', # Clock name
    'state', # Frequency
    'cpu_id', # Target cpu
    ]
)

class ClockSetRate(ClockSetRateBase):
    __slots__ = ()
    def __new__(cls, clk, state, cpu_id):
            state = int(state)
            cpu_id = int(cpu_id)

            return super(cls, ClockSetRate).__new__(
                cls,
                clk=clk,
                state=state,
                cpu_id=cpu_id,
            )

clock_set_rate_pattern = re.compile(
        r"""
        (?P<clk>.+)\s+
        state=(?P<state>\d+)\s+
        cpu_id=(?P<cpu_id>\d+)
        """,
        re.X|re.M
)

@register_parser
def clock_set_rate(payload):
    """Parser for `clock_set_rate` tracepoint"""
    try:
        match = re.match(clock_set_rate_pattern, payload)
        if match:
            match_group_dict = match.groupdict()
            return ClockSetRate(**match_group_dict)
    except Exception, e:
        raise ParserError(e.message)