text:
#define EIGEN_USE_THREADS

#include <atomic>
#include <utility>

#include "tensorflow/core/common_runtime/function.h"
#include "tensorflow/core/framework/allocator.h"
#include "tensorflow/core/framework/dataset.h"
#include "tensorflow/core/framework/partial_tensor_shape.h"
#include "tensorflow/core/framework/tensor.h"
#include "tensorflow/core/kernels/data/captured_function.h"
#include "tensorflow/core/kernels/inplace_ops_functor.h"
#include "tensorflow/core/lib/core/blocking_counter.h"
#include "tensorflow/core/lib/core/errors.h"
#include "tensorflow/core/lib/gtl/cleanup.h"
#include "tensorflow/core/lib/random/random.h"
#include "tensorflow/core/lib/strings/strcat.h"
#include "tensorflow/core/platform/cpu_info.h"
#include "tensorflow/core/platform/numa.h"
#include "tensorflow/core/platform/tracing.h"
#include "tensorflow/core/util/ptr_util.h"

namespace tensorflow {
namespace data {
namespace {

// kWindowSize is the fixed constant controlling the number of batch outputs
// each NumaWorkerBlock may be processing at a time. This is currently a
// constant and not user configurable to enable future performance optimizations
// in the implementation.
const int64 kWindowSize = 10;

// Define a helper for more consistent logging.
#define WORKER_VLOG(verbose_level)                                           \
  VLOG(verbose_level) << "WorkerThread (" << numa_node << ", " << thread_num \
                      << "): "

// See documentation in ../ops/dataset_ops.cc for a high-level
// description of the following op.

class NumaMapAndBatchDatasetOp : public UnaryDatasetOpKernel {
 public:
  explicit NumaMapAndBatchDatasetOp(OpKernelConstruction* ctx)
      : UnaryDatasetOpKernel(ctx) {
    OP_REQUIRES_OK(ctx, ctx->GetAttr("f", &func_));
    OP_REQUIRES_OK(ctx, ctx->GetAttr("output_types", &output_types_));
    OP_REQUIRES_OK(ctx, ctx->GetAttr("output_shapes", &output_shapes_));
  }

 protected:
  void MakeDataset(OpKernelContext* ctx, DatasetBase* input,
                   DatasetBase** output) override {
    int64 batch_size;
    OP_REQUIRES_OK(ctx, ParseScalarArgument(ctx, "batch_size", &batch_size));
    OP_REQUIRES(
        ctx, batch_size > 0,
        errors::InvalidArgument("batch_size must be greater than zero."));

    int64 num_parallel_calls;
    OP_REQUIRES_OK(ctx, ParseScalarArgument(ctx, "num_parallel_calls",
                                            &num_parallel_calls));
    OP_REQUIRES(ctx, num_parallel_calls > 0 || num_parallel_calls == kAutoTune,
                errors::InvalidArgument(
                    "num_parallel_calls must be greater than zero."));

    bool drop_remainder;
    OP_REQUIRES_OK(ctx,
                   ParseScalarArgument(ctx, "drop_remainder", &drop_remainder));

    std::unique_ptr<CapturedFunction> captured_func;
    OP_REQUIRES_OK(
        ctx, CapturedFunction::Create(func_, ctx, "other_arguments",
                                      /* use_inter_op_parallelism = */ false,
                                      &captured_func));

    *output = new Dataset(ctx, input, batch_size, num_parallel_calls,
                          drop_remainder, output_types_, output_shapes_, func_,
                          std::move(captured_func));
  }

 private:
  class Dataset : public DatasetBase {
   public:
    Dataset(OpKernelContext* ctx, const DatasetBase* input, int64 batch_size,
            int64 num_parallel_calls, bool drop_remainder,
            const DataTypeVector& output_types,
            const std::vector<PartialTensorShape>& output_shapes,
            const NameAttrList& func,
            std::unique_ptr<CapturedFunction> captured_func)
        : DatasetBase(DatasetContext(ctx)),
          input_(input),
          batch_size_(batch_size),
          num_parallel_calls_(num_parallel_calls),
          drop_remainder_(drop_remainder),
          output_types_(output_types),
          output_shapes_(output_shapes),
          func_(func),
          captured_func_(std::move(captured_func)) {
      input_->Ref();
    }

    ~Dataset() override { input_->Unref(); }

    std::unique_ptr<IteratorBase> MakeIteratorInternal(
        const string& prefix) const override {
      return std::unique_ptr<IteratorBase>(
          new Iterator({this, strings::StrCat(prefix, "::NumaMapAndBatch")}));
    }

    const DataTypeVector& output_dtypes() const override {
      return output_types_;
    }

    const std::vector<PartialTensorShape>& output_shapes() const override {
      return output_shapes_;
    }

    string DebugString() const override {
      return "NumaMapAndBatchDatasetOp::Dataset";
    }

   protected:
    Status AsGraphDefInternal(SerializationContext* ctx,
                              DatasetGraphDefBuilder* b,
                              Node** output) const override {
      TF_RETURN_IF_ERROR(b->AddFunction(ctx, func_.name()));
      Node* input_graph_node = nullptr;
      TF_RETURN_IF_ERROR(b->AddInputDataset(ctx, input_, &input_graph_node));
      Node* batch_size_node;
      TF_RETURN_IF_ERROR(b->AddScalar(batch_size_, &batch_size_node));
      Node* num_parallel_calls_node;
      TF_RETURN_IF_ERROR(
          b->AddScalar(num_parallel_calls_, &num_parallel_calls_node));
      Node* drop_remainder_node;
      TF_RETURN_IF_ERROR(b->AddScalar(drop_remainder_, &drop_remainder_node));

      DataTypeVector other_arguments_types;
      other_arguments_types.reserve(captured_func_->captured_inputs().size());
      std::vector<Node*> other_arguments;
      other_arguments.reserve(captured_func_->captured_inputs().size());
      for (const Tensor& t : captured_func_->captured_inputs()) {
        Node* node;
        TF_RETURN_IF_ERROR(b->AddTensor(t, &node));
        other_arguments.emplace_back(node);
        other_arguments_types.emplace_back(t.dtype());
      }
      AttrValue f;
      b->BuildAttrValue(func_, &f);
      AttrValue other_arguments_types_attr;
      b->BuildAttrValue(other_arguments_types, &other_arguments_types_attr);

      TF_RETURN_IF_ERROR(b->AddDataset(
          this,
          {std::make_pair(0, input_graph_node),
           std::make_pair(2, batch_size_node),
           std::make_pair(3, num_parallel_calls_node),
           std::make_pair(4, drop_remainder_node)},  // Single tensor inputs.
          {std::make_pair(1, other_arguments)},      // Tensor list inputs.
          {std::make_pair("f", f),
           std::make_pair("Targuments", other_arguments_types_attr)},  // Attrs
          output));
      return Status::OK();
    }

   private:
    class Iterator : public DatasetIterator<Dataset> {
     public:
      explicit Iterator(const Params& params)
          : DatasetIterator<Dataset>(params),
            mu_(std::make_shared<mutex>()),
            autotune_cond_var_(std::make_shared<condition_variable>()),
            num_parallel_calls_(std::make_shared<model::SharedState>(
                params.dataset->num_parallel_calls_, mu_, autotune_cond_var_)) {
      }

      ~Iterator() override {
        mutex_lock l(*mu_);
        cancelled_ = true;
        VLOG(3) << "NumaMapAndBatchIterator::~Iterator: cancelling operations.";
        for (size_t i = 0; i < workers_.size(); ++i) {
          workers_[i]->manager.Cancel();
        }
        VLOG(3) << "NumaMapAndBatchIterator::~Iterator: waiting for threads to "
                   "shut down.";
      }

      Status Initialize(IteratorContext* ctx) override {
        mutex_lock l(*mu_);
        if (num_parallel_calls_->value == kAutoTune) {
          num_parallel_calls_->value = port::NumSchedulableCPUs();
          num_parallel_calls_->tunable = true;
        }
        TF_RETURN_IF_ERROR(
            dataset()->input_->MakeIterator(ctx, prefix(), &input_impl_));
        TF_RETURN_IF_ERROR(dataset()->captured_func_->Instantiate(ctx));
        return Status::OK();
      }

      Status GetNextInternal(IteratorContext* ctx,
                             std::vector<Tensor>* out_tensors,
                             bool* end_of_sequence) override {
        auto cleanup = gtl::MakeCleanup(
            [] { VLOG(3) << "GetNextInternal call returning."; });
        NumaWorkerBlock* worker = nullptr;
        {
          mutex_lock l(*mu_);
          VLOG(3) << "GetNextInternal call; current block: " << cur_block_;
          if (global_end_of_input_) {
            *end_of_sequence = true;
            return Status::OK();
          }
          TF_RETURN_IF_ERROR(EnsureBackgroundThreadsStarted(ctx));
          worker = workers_[cur_block_].get();
          cur_block_ = (cur_block_ + 1) % workers_.size();
        }
        bool global_end_of_input_local = false;
        Status s = worker->manager.GetBatch(ctx, dataset()->drop_remainder_,
                                            &global_end_of_input_local,
                                            out_tensors, end_of_sequence);
        if (global_end_of_input_local) {
          mutex_lock l(*mu_);
          global_end_of_input_ = global_end_of_input_local;
        }
        return s;
      }

     protected:
      std::shared_ptr<model::Node> CreateNode(
          IteratorContext* ctx, model::Node::Args args) const override {
        return model::MakeAsyncKnownRatioNode(
            std::move(args), dataset()->batch_size_,
            {model::MakeParameter("parallelism", num_parallel_calls_, /*min=*/1,
                                  /*max=*/port::NumSchedulableCPUs())});
      }

      Status SaveInternal(IteratorStateWriter* writer) override {
        mutex_lock l(*mu_);
        for (size_t i = 0; i < workers_.size(); ++i) {
          if (!workers_[i]->manager.Quiesce()) {
            return errors::Cancelled(
                "The iterator was deleted before it could reach a "
                "checkpointable state.");
          }
        }

        TF_RETURN_IF_ERROR(SaveInput(writer, input_impl_));
        TF_RETURN_IF_ERROR(
            writer->WriteScalar(full_name("num_workers"), workers_.size()));

        for (size_t i = 0; i < workers_.size(); ++i) {
          size_t index = (cur_block_ + i) % workers_.size();
          TF_RETURN_IF_ERROR(workers_[index]->manager.Save(writer, this, i));
        }
        return Status::OK();
      }

      Status RestoreInternal(IteratorContext* ctx,
                             IteratorStateReader* reader) override {
        mutex_lock l(*mu_);
        TF_RETURN_IF_ERROR(RestoreInput(ctx, reader, input_impl_));
        int64 num_workers = -1;
        TF_RETURN_IF_ERROR(
            reader->ReadScalar(full_name("num_workers"), &num_workers));
        // Note: num_workers can be 0 if the iterator wasn't started when
        // first checkpointed.
        if (num_workers < 0) {
          return errors::DataLoss(
              "When restoring from checkpoint, we encountered a data "
              "consistency error: num_workers has an invalid value: ",
              num_workers);
        }
        if (port::NUMAEnabled()) {
          int actual_numa_domains = port::NUMANumNodes();
          if (actual_numa_domains != num_workers && num_workers > 0) {
            LOG(WARNING) << "# NUMA domains mismatch when restoring from "
                            "checkpoint: checkpoint has "
                         << num_workers
                         << " NUMA domains, while this host has: "
                         << actual_numa_domains << " NUMA domains.";
          }
        }
        if (num_workers > 1 && !port::NUMAEnabled()) {
          LOG(WARNING) << "NUMA is not enabled for this process, but restoring "
                          "a checkpoint that assumes "
                       << num_workers << " NUMA domains.";
        }
        workers_.resize(num_workers);
        for (size_t i = 0; i < num_workers; ++i) {
          workers_[i] = MakeUnique<NumaWorkerBlock>(this);
          TF_RETURN_IF_ERROR(
              workers_[i]->manager.Restore(ctx, reader, this, i));
        }
        cur_block_ = 0;
        return Status::OK();
      }

     private:
      // NumaBlockManager manages all the state for a set of threads pinned to a
      // single NUMA domain.
      //
      // The methods can be divided into 3 categories based on who should call
      // them:
      //
      //  (1) RunnerThread: WaitForInputSpace, PushInputs, SetEndOfInput.
      //  (2) WorkerThread: RetrieveInput, GetBatchTensors.
      //      RecordBatchEntryComplete
      //  (3) Client threads: GetBatch, Cancel, Save, Restore.
      //
      // Internally, we manage state in a circular buffer of size `kWindowSize`.
      // There are 3 pointers into the circular buffer, and must maintain the
      // following order: (1) next_input_batch_ (corresponding to the next input
      // batch to be pulled from the input iterator), (2) next_input_
      // (corresponding to the batch the WorkerThreads should pull from for
      // their next inputs), and (3) next_output_ corresponding to the next
      // value to be consumed by the output iterator.
      //
      // Methods return errors::Cancelled if the iteration is cancelled before
      // completing.
      //
      // NumaBlockManager is thread safe.
      class NumaBlockManager {
       public:
        explicit NumaBlockManager(Iterator* itr) : itr_(itr) {}

        // WaitForInputSpace blocks until there is space in the circular buffer
        // to begin processing a new batch of elements.
        //
        // Returns true when there is space, false if the Iterator is cancelled.
        bool WaitForInputSpace(IteratorContext* ctx) {
          mutex_lock l(mu_);

          size_t next = (next_input_batch_ + 1) % kWindowSize;
          DCHECK(next < kWindowSize) << next;

          // Wait for space in the circular buffer.
          while (!cancelled_ && batches_[next].state != BatchState::kEmpty) {
            VLOG(3) << "Waiting for input space; next: " << next
                    << ", next_output_: " << next_output_
                    << ", next_input_batch_: " << next_input_batch_;
            itr_->RecordStop(ctx);
            runner_cond_var_.wait(l);
            itr_->RecordStart(ctx);
          }
          if (cancelled_) {
            VLOG(3) << "WaitForInputSpace cancelled.";
            return false;
          }

          DCHECK(batches_[next].state == BatchState::kEmpty);

          next_input_batch_ = next;
          return true;
        }

        // PushInputs sets the inputs for the next batch as retrieved from the
        // input iterator.
        void PushInputs(const Status& status,
                        std::vector<std::vector<Tensor>> inputs) {
          mutex_lock l(mu_);

          DCHECK(next_input_ < kWindowSize) << next_input_;
          DCHECK(batches_[next_input_batch_].state == BatchState::kEmpty);
          DCHECK(batches_[next_input_batch_].next_input_to_process == 0)
              << batches_[next_input_batch_].next_input_to_process;
          DCHECK(batches_[next_input_batch_].status.ok())
              << batches_[next_input_batch_].status;

          batches_[next_input_batch_].inputs.swap(inputs);
          batches_[next_input_batch_].state = BatchState::kInputsFilled;
          batches_[next_input_batch_].status.Update(status);
          if (batches_[next_input_batch_].status.ok()) {
            worker_cond_var_.notify_all();
          } else {
            client_cond_var_.notify_all();
            batches_[next_input_batch_].error_index = 0;
          }
        }

        // SetEndOfInput records the fact that we have reached the end of the
        // input iterator, and that we should return end_of_sequence = true when
        // we have exhaused all buffered batches.
        void SetEndOfInput() {
          mutex_lock l(mu_);
          reached_eof_ = true;
          worker_cond_var_.notify_all();
          client_cond_var_.notify_all();
        }

        // RetrieveInput gets the next input tuple to be mapped by a worker
        // thread.
        //
        // Returns true if an input was retrieved, false if the iterator has
        // been cancelled.
        bool RetrieveInput(IteratorContext* ctx, std::vector<Tensor>* input,
                           uint64* index, size_t* sequence_number) {
          mutex_lock l(mu_);

          // Wait for inputs to be ready.
          while (!cancelled_ &&
                 batches_[next_input_].state != BatchState::kInputsFilled) {
            itr_->RecordStop(ctx);
            worker_cond_var_.wait(l);
            itr_->RecordStart(ctx);
          }

          if (cancelled_) {
            return false;
          }

          DCHECK(batches_[next_input_].next_input_to_process <
                 batches_[next_input_].inputs.size())
              << "next_input_: " << next_input_ << ", next_input_to_process: "
              << batches_[next_input_].next_input_to_process
              << ", inputs.size(): " << batches_[next_input_].inputs.size()
              << ", state: " << static_cast<int32>(batches_[next_input_].state)
              << ", this: " << this;
          *index = batches_[next_input_].next_input_to_process;
          *sequence_number = next_input_;
          input->swap(batches_[next_input_]
                          .inputs[batches_[next_input_].next_input_to_process]);
          // Increment pointers.
          batches_[next_input_].next_input_to_process++;

          if (batches_[next_input_].next_input_to_process ==
              batches_[next_input_].inputs.size()) {
            batches_[next_input_].state = BatchState::kAllMapsStarted;
            next_input_ = (next_input_ + 1) % kWindowSize;
          }
          return true;
        }

        // GetBatchTensors returns a pointer to the output batch tensors for the
        // worker thread to copy into.
        //
        // allocate_output is a function taking a batch size, and a pointer to
        // the output tuple of Tensors to allocate them. The allocate_output
        // function is called at most once per output batch.
        std::vector<Tensor>* GetBatchTensors(
            size_t sequence_number,
            std::function<void(size_t, std::vector<Tensor>*)> allocate_output) {
          mutex_lock l(mu_);
          DCHECK(sequence_number < kWindowSize) << sequence_number;
          DCHECK(batches_[sequence_number].state == BatchState::kInputsFilled ||
                 batches_[sequence_number].state == BatchState::kAllMapsStarted)
              << sequence_number;

          if (batches_[sequence_number].outputs.empty()) {
            allocate_output(batches_[sequence_number].inputs.size(),
                            &batches_[sequence_number].outputs);
          }
          return &batches_[sequence_number].outputs;
        }

        // RecordBatchEntryComplete records an element of the batch has finished
        // copying into the output tensors.
        void RecordBatchEntryComplete(size_t sequence_number, uint64 index,
                                      Status s) {
          mutex_lock l(mu_);
          DCHECK(sequence_number < kWindowSize) << sequence_number;
          DCHECK(batches_[sequence_number].state == BatchState::kInputsFilled ||
                 batches_[sequence_number].state == BatchState::kAllMapsStarted)
              << sequence_number;

          batches_[sequence_number].num_outputs_complete++;
          if (!s.ok() && batches_[sequence_number].error_index > index) {
            batches_[sequence_number].status = s;
            batches_[sequence_number].error_index = index;
          }

          if (batches_[sequence_number].num_outputs_complete ==
              batches_[sequence_number].inputs.size()) {
            DCHECK(batches_[sequence_number].state ==
                   BatchState::kAllMapsStarted);
            batches_[sequence_number].state = BatchState::kOutputsComplete;
            batches_[sequence_number].inputs.clear();  // Eagerly save memory.
            batches_[sequence_number].inputs.shrink_to_fit();
            client_cond_var_.notify_all();
          }
        }

        // GetBatch retrieves the next output batch tensors.
        Status GetBatch(IteratorContext* ctx, bool drop_remainder,
                        bool* global_eof, std::vector<Tensor>* out_tensor,
                        bool* end_of_sequence) {
          mutex_lock l(mu_);
          // Wait until one of 3 conditions occurs:
          //  (1) we're cancelled.
          //  (2) the state becomes kOutputsComplete
          //  (3) state is empty && reached_eof.
          while (!cancelled_ &&
                 batches_[next_output_].state != BatchState::kOutputsComplete &&
                 !(reached_eof_ &&
                   batches_[next_output_].state == BatchState::kEmpty)) {
            VLOG(3) << "Waiting in GetBatch.";
            itr_->RecordStop(ctx);
            client_cond_var_.wait(l);
            itr_->RecordStart(ctx);
          }

          if (cancelled_) {
            return errors::Cancelled(
                "Cancelled in NumaMapAndBatch::GetNext call.");
          }

          if (reached_eof_ &&
              batches_[next_output_].state == BatchState::kEmpty) {
            VLOG(4) << "GetBatch returning end of sequence.";
            *end_of_sequence = true;
            *global_eof = true;
            return Status::OK();
          }

          VLOG(3) << "Returning output index: " << next_output_
                  << ", this: " << this;

          *end_of_sequence = false;
          Status s = batches_[next_output_].status;
          if (s.ok()) {
            out_tensor->swap(batches_[next_output_].outputs);
          }
          // Handle early termination.
          if (errors::IsOutOfRange(s)) {
            *global_eof = true;
            s = Status::OK();
            if (drop_remainder || batches_[next_output_].error_index == 0) {
              *end_of_sequence = true;
            } else {
              std::vector<Tensor> true_outputs;
              for (size_t i = 0; i < batches_[next_output_].outputs.size();
                   ++i) {
                TensorShape component_shape(
                    batches_[next_output_].outputs[i].shape());
                component_shape.set_dim(0, batches_[next_output_].error_index);
                AllocatorAttributes attr;
                attr.set_gpu_compatible(true);
                true_outputs.emplace_back(
                    ctx->allocator(attr),
                    batches_[next_output_].outputs[i].dtype(), component_shape);
                TF_RETURN_IF_ERROR(CopyPartialBatch(
                    &true_outputs.back(), batches_[next_output_].outputs[i],
                    batches_[next_output_].error_index));
              }
              out_tensor->swap(true_outputs);
            }
          }

          batches_[next_output_].Reset();
          next_output_ = (next_output_ + 1) % kWindowSize;
          runner_cond_var_.notify_all();

          return s;
        }

        void Cancel() {
          mutex_lock l(mu_);
          VLOG(3) << "Cancelling NUMA block.";
          cancelled_ = true;
          runner_cond_var_.notify_all();
          worker_cond_var_.notify_all();
          client_cond_var_.notify_all();
        }

        // Waits until all the worker threads have completed their work and all
        // internal state has reached a "safe-point" where we can safely
        // checkpoint.
        //
        // Returns true if completed successfully, false if cancelled while
        // waiting.
        bool Quiesce() {
          mutex_lock l(mu_);
          VLOG(3) << "Waiting until the operations have quiesced.";
          while (!cancelled_ && !AllMapOperationsFinished()) {
            client_cond_var_.wait(l);
          }
          if (cancelled_) {
            return false;
          }
          return true;
        }

        Status Save(IteratorStateWriter* writer, Iterator* itr, size_t index) {
          mutex_lock l(mu_);
          string prefix = itr->full_name(strings::StrCat("numa_block_", index));
          if (reached_eof_) {
            TF_RETURN_IF_ERROR(writer->WriteScalar(
                strings::StrCat(prefix, "_end_of_input"), ""));
          }
          for (size_t i = 0; i < kWindowSize; ++i) {
            size_t index = (next_output_ + i) % kWindowSize;
            if (batches_[index].state == BatchState::kEmpty) {
              break;
            }
            string batch_prefix = strings::StrCat(prefix, "_batch_", i);
            TF_RETURN_IF_ERROR(writer->WriteScalar(
                strings::StrCat(batch_prefix, "_code"),
                static_cast<int64>(batches_[index].status.code())));
            if (!batches_[index].status.ok()) {
              TF_RETURN_IF_ERROR(
                  writer->WriteScalar(strings::StrCat(batch_prefix, "_msg"),
                                      batches_[index].status.error_message()));
              TF_RETURN_IF_ERROR(writer->WriteScalar(
                  strings::StrCat(batch_prefix, "_error_index"),
                  batches_[index].error_index));
            }

            TF_RETURN_IF_ERROR(writer->WriteScalar(
                strings::StrCat(batch_prefix, "_output_size"),
                batches_[index].outputs.size()));
            for (size_t j = 0; j < batches_[index].outputs.size(); ++j) {
              string tensor_prefix =
                  strings::StrCat(batch_prefix, "_output_", j);
              if (!batches_[index].status.ok()) {
                DCHECK(batches_[index].error_index >= 0 &&
                       batches_[index].error_index <
                           itr_->dataset()->batch_size_);
                // If the batch is not full, we only store the first
                // `error_index` values. The rest of the batch tensor might not
                // be initialized, and accessing that will raise msan errors.
                TF_RETURN_IF_ERROR(writer->WriteTensor(
                    tensor_prefix, batches_[index].outputs[j].Slice(
                                       0, batches_[index].error_index)));
              } else {
                TF_RETURN_IF_ERROR(writer->WriteTensor(
                    tensor_prefix, batches_[index].outputs[j]));
              }
            }
          }
          return Status::OK();
        }

        Status Restore(IteratorContext* ctx, IteratorStateReader* reader,
                       Iterator* itr, size_t index) {
          mutex_lock l(mu_);
          if (reached_eof_) {
            return errors::FailedPrecondition(
                "Already reached the end of the sequence.");
          }
          string prefix = itr->full_name(strings::StrCat("numa_block_", index));
          reached_eof_ =
              reader->Contains(strings::StrCat(prefix, "_end_of_input"));
          for (size_t i = 0; i < kWindowSize; ++i) {
            string batch_prefix = strings::StrCat(prefix, "_batch_", i);
            if (!reader->Contains(strings::StrCat(batch_prefix, "_code"))) {
              break;
            }
            Batch batch;
            batch.state = BatchState::kOutputsComplete;
            int64 code_int;
            TF_RETURN_IF_ERROR(reader->ReadScalar(
                strings::StrCat(batch_prefix, "_code"), &code_int));
            error::Code code = static_cast<error::Code>(code_int);
            if (code != error::Code::OK) {
              string error_message;
              TF_RETURN_IF_ERROR(reader->ReadScalar(
                  strings::StrCat(batch_prefix, "_msg"), &error_message));
              batch.status = Status(code, error_message);
              int64 error_index_int = -1;
              TF_RETURN_IF_ERROR(reader->ReadScalar(
                  strings::StrCat(batch_prefix, "_error_index"),
                  &error_index_int));
              if (error_index_int < 0 ||
                  error_index_int > itr->dataset()->batch_size_) {
                return errors::FailedPrecondition(
                    "Error index out of bounds when restoring from checkpoint; "
                    "error index: ",
                    error_index_int);
              }
              batch.error_index = static_cast<size_t>(error_index_int);
            }
            int64 output_size = -1;
            TF_RETURN_IF_ERROR(reader->ReadScalar(
                strings::StrCat(batch_prefix, "_output_size"), &output_size));
            batch.outputs.reserve(output_size);
            for (size_t j = 0; j < output_size; ++j) {
              string tensor_name = strings::StrCat(batch_prefix, "_output_", j);
              Tensor t;
              TF_RETURN_IF_ERROR(reader->ReadTensor(tensor_name, &t));
              batch.outputs.emplace_back(std::move(t));
            }
            batches_[i] = std::move(batch);
          }
          return Status::OK();
        }

       private:
        bool AllMapOperationsFinished() EXCLUSIVE_LOCKS_REQUIRED(mu_) {
          for (size_t i = 0; i < kWindowSize; ++i) {
            if (batches_[i].state == BatchState::kInputsFilled ||
                batches_[i].state == BatchState::kAllMapsStarted) {
              return false;
            }
            if (batches_[i].state != BatchState::kOutputsComplete &&
                !reached_eof_) {
              return false;
            }
          }
          return true;
        }

        // Batches begin in the `kEmpty` state. Once the RunnerThread has
        // filled the `inputs` to a `Batch`, it transitions to the
        // `kInputsFilled` state. At this point, the Worker threads run the map
        // function and copy the outputs appropriately. Once all worker threads
        // have started, it transitions to `kAllMapsStarted`. After the outputs
        // are complete, the GetNext call can consume the outputs, and return
        // the batch to the kEmpty state.
        enum class BatchState {
          kEmpty,
          kInputsFilled,
          kAllMapsStarted,
          kOutputsComplete,
        };

        // Batch captures all the state of an output batch as it progresses
        // through the machinery. Once the RunnerThread fills inputs, it
        // transitions to `kInputsFilled`. At this point, the worker threads can
        // work on it, incrementing outputs_complete for every element of the
        // input set that is copied into the output Tensors. Once all the input
        // tuples have been processed (i.e. num_outputs_complete ==
        // inputs.size()), it transitions to the `kOutputsComplete` stage, where
        // it is ready to be returned by a `GetBatch` call (called from
        // `GetNextInternal`).
        struct Batch {
          BatchState state;
          // Aggregates the Status of the input iterator's GetNext
          // calls, in addition to the Status of the map function invocations.
          //
          // In the case where multiple non-OK statuses are encountered, we
          // return the first one encountered.
          Status status;
          // In order to return the correct error status, we keep track of the
          // error_index.
          size_t error_index;
          // The batch_size input tuples (or fewer in the case of the last
          // batch).
          // TODO(saeta): Avoid re-allocating vectors all the time!
          std::vector<std::vector<Tensor>> inputs;
          std::vector<Tensor> outputs;
          size_t next_input_to_process;
          size_t num_outputs_complete;

          Batch() { Reset(); }

          // Resets the Batch state (e.g. after consuming the outputs).
          void Reset() {
            state = BatchState::kEmpty;
            status = Status::OK();
            inputs.clear();
            inputs.shrink_to_fit();
            outputs.clear();
            outputs.shrink_to_fit();
            next_input_to_process = 0;
            num_outputs_complete = 0;
            error_index = -1;
          }
        };

        Iterator* itr_;  // Not owned.
        mutex mu_;
        Batch batches_[kWindowSize] GUARDED_BY(mu_);
        size_t next_input_batch_ GUARDED_BY(mu_) = -1;
        size_t next_input_ GUARDED_BY(mu_) = 0;
        size_t next_output_ GUARDED_BY(mu_) = 0;
        bool cancelled_ GUARDED_BY(mu_) = false;
        bool reached_eof_ GUARDED_BY(mu_) = false;

        // The runner thread waits on this condition variable for space to be
        // available. When the client thread takes a value out of the circular
        // buffer, it notifies this condition variable that space is now
        // available.
        condition_variable runner_cond_var_ GUARDED_BY(mu_);
        // The worker threads wait on this condition variable for available
        // inputs. When the runner thread makes new inputs available, it
        // notifies this condition variable.
        condition_variable worker_cond_var_ GUARDED_BY(mu_);
        // The client threads wait on this condition variable for avaiable
        // batched outputs. When worker threads complete a batch, they notify
        // this condition variable.
        condition_variable client_cond_var_ GUARDED_BY(mu_);
      };
      // Mark NumaBlockManager as a friend of Iterator in order to call
      // protected Iterator methods during checkpointing.
      friend NumaBlockManager;

      struct NumaWorkerBlock {
        NumaBlockManager manager;
        // TODO(saeta): Migrate to BackgroundWorker.
        std::vector<std::unique_ptr<Thread>> threads;

        explicit NumaWorkerBlock(Iterator* itr) : manager(itr) {}
      };

      static void CustomNumaWorkerBlockDeleter(NumaWorkerBlock* ptr) {
        ptr->~NumaWorkerBlock();
        port::NUMAFree(ptr, sizeof(NumaWorkerBlock));
      }
      static void DefaultNumaWorkerBlockDeleter(NumaWorkerBlock* ptr) {
        delete ptr;
      }

      static Status CopyPartialBatch(Tensor* output, const Tensor& value,
                                     int64 num_elements) {
        switch (value.dtype()) {
#define HANDLE_TYPE(type)                                         \
  case DataTypeToEnum<type>::value: {                             \
    auto output_t = output->flat_outer_dims<type>();              \
    auto value_t = value.flat_outer_dims<type>();                 \
    for (size_t i = 0; i < num_elements; i++) {                   \
      output_t.template chip<0>(i) = value_t.template chip<0>(i); \
    }                                                             \
    return Status::OK();                                          \
  }
          TF_CALL_DATASET_TYPES(HANDLE_TYPE);
#undef HANDLE_TYPE
          default:
            return errors::InvalidArgument("Unsupported data type: ",
                                           DataTypeString(value.dtype()));
        }
        return Status::OK();
      }

      Status EnsureBackgroundThreadsStarted(IteratorContext* ctx)
          EXCLUSIVE_LOCKS_REQUIRED(*mu_) {
        if (curr_num_parallel_calls_ >= num_parallel_calls_->value) {
          // All necessary threads have been started.
          curr_num_parallel_calls_ = num_parallel_calls_->value;
          return Status::OK();
        }

        VLOG(4) << "Starting workers";
        bool numa_enabled = port::NUMAEnabled();

        if (!numa_enabled) {
          LOG(INFO) << "NUMA not enabled on this host.";
        }

        int num_numa_nodes = port::NUMANumNodes();
        if (num_numa_nodes < 1) {
          return errors::Internal("The number of NUMA nodes is invalid: ",
                                  num_numa_nodes);
        }

        // Only resize when empty to support restoring from checkpoints.
        if (workers_.empty()) {
          VLOG(3) << "# NUMA Nodes: " << num_numa_nodes
                  << ", # Parallel Calls: " << num_parallel_calls_->value;
          workers_.resize(num_numa_nodes);
        } else {
          num_numa_nodes = workers_.size();
        }

        // Round up num_parallel_calls, with a minimum of 1.
        const size_t num_threads_per_block =
            std::max(1LL, (num_parallel_calls_->value + num_numa_nodes - 1) /
                              num_numa_nodes);

        VLOG(3) << "Starting " << num_threads_per_block * num_numa_nodes
                << " worker threads, with " << num_threads_per_block
                << " threads per block.";

        // Only allocate new_ctx if required.
        std::shared_ptr<IteratorContext> new_ctx;

        for (int i = 0; i < num_numa_nodes; ++i) {
          if (!workers_[i]) {
            if (numa_enabled) {
              // Allocate in appropriate NUMA domain.
              // 4k page align.
              void* ptr = port::NUMAMalloc(i, sizeof(NumaWorkerBlock), 0);
              if (ptr != nullptr) {
                NumaWorkerBlock* block = new (ptr) NumaWorkerBlock(this);
                workers_[i] =
                    std::unique_ptr<NumaWorkerBlock,
                                    std::function<void(NumaWorkerBlock*)>>(
                        block, CustomNumaWorkerBlockDeleter);
              } else {
                LOG(ERROR) << "Could not NUMA-allocate worker block: " << i;
              }
            }
            // If the NUMA allocation fails, or NUMA is not enabled.
            if (!workers_[i]) {
              workers_[i] =
                  std::unique_ptr<NumaWorkerBlock,
                                  std::function<void(NumaWorkerBlock*)>>(
                      new NumaWorkerBlock(this), DefaultNumaWorkerBlockDeleter);
            }
          }
          // Be sure to start threads if num_parallel_calls_ has changed.
          for (size_t j = workers_[i]->threads.size();
               j < num_threads_per_block; ++j) {
            VLOG(3) << "Starting worker " << i << ", " << j;
            if (!new_ctx) {
              new_ctx = std::make_shared<IteratorContext>(*ctx);
            }
            workers_[i]->threads.emplace_back(ctx->env()->StartThread(
                {}, strings::StrCat("tf_data_numa_map_and_batch_", i, "_", j),
                [this, new_ctx, i, j]() { WorkerThread(new_ctx, i, j); }));
            VLOG(3) << "Worker " << i << ", " << j << " successfully started.";
          }
        }
        if (!runner_thread_) {
          if (!new_ctx) {
            new_ctx = std::make_shared<IteratorContext>(*ctx);
          }
          runner_thread_.reset(ctx->env()->StartThread(
              {}, "tf_data_numa_map_and_batch",
              [this, new_ctx] { RunnerThread(new_ctx); }));
        }
        VLOG(3) << "All workers & runner thread started.";
        return Status::OK();
      }

      void AllocateOutput(IteratorContext* ctx, size_t batch_size,
                          const std::vector<Tensor>& map_fn_outputs,
                          std::vector<Tensor>* batch_outputs) {
        DCHECK(dataset()->output_dtypes().size() ==
               dataset()->output_shapes().size());
        DCHECK(map_fn_outputs.size() == dataset()->output_dtypes().size());
        for (size_t i = 0; i < dataset()->output_dtypes().size(); ++i) {
          TensorShape component_shape({static_cast<uint32>(batch_size)});
          component_shape.AppendShape(map_fn_outputs.at(i).shape());
          AllocatorAttributes attr;
          attr.set_gpu_compatible(true);
          batch_outputs->emplace_back(ctx->allocator(attr),
                                      map_fn_outputs.at(i).dtype(),
                                      component_shape);
        }
      }

      void RunnerThread(std::shared_ptr<IteratorContext> ctx)
          LOCKS_EXCLUDED(mu_) {
        RecordStart(ctx.get());
        auto cleanup = gtl::MakeCleanup([this, &ctx] {
          // Set end of input on all the managers in order to clean up in an
          // orderly fashion.
          VLOG(3) << "Setting End of Input on workers_[*]->manager";
          for (size_t i = 0; i < workers_.size(); ++i) {
            workers_[i]->manager.SetEndOfInput();
          }
          RecordStop(ctx.get());
        });

        const size_t num_blocks = workers_.size();

        while (true) {
          for (size_t block = 0; block < num_blocks; ++block) {
            VLOG(4) << "RunnerThread waiting for input space in block: "
                    << block;
            if (TF_PREDICT_FALSE(
                    !workers_[block]->manager.WaitForInputSpace(ctx.get()))) {
              VLOG(3) << "RunnerThread exiting due to cancellation.";
              return;
            }
            VLOG(4) << "RunnerThread has space; pulling on upstream for block "
                    << block;

            Status s;
            std::vector<std::vector<Tensor>> inputs;
            bool end_of_sequence = false;
            for (size_t i = 0; i < dataset()->batch_size_; ++i) {
              std::vector<Tensor> tuple;
              s.Update(
                  input_impl_->GetNext(ctx.get(), &tuple, &end_of_sequence));
              if (!s.ok()) {
                break;
              }
              if (end_of_sequence) {
                VLOG(4) << "Runner thread encountered end of sequence.";
                if (dataset()->drop_remainder_) {
                  return;
                }
                break;
              }
              inputs.push_back(std::move(tuple));
            }

            VLOG(4) << "Moving inputs to block " << block
                    << ", which has size: " << inputs.size();
            if (!s.ok() || !inputs.empty()) {
              workers_[block]->manager.PushInputs(s, std::move(inputs));
              VLOG(4) << "Inputs moved into block " << block;
            }
            if (end_of_sequence) {
              return;
            }
          }
        }
      }

      void WorkerThread(std::shared_ptr<IteratorContext> ctx,
                        const int numa_node, const int thread_num) {
        RecordStart(ctx.get());
        WORKER_VLOG(3) << "started.";
        auto stop_cleanup =
            gtl::MakeCleanup([this, numa_node, thread_num, &ctx]() {
              RecordStop(ctx.get());
              WORKER_VLOG(3) << "exiting.";
            });

        NumaWorkerBlock* block = workers_[numa_node].get();
        port::NUMASetThreadNodeAffinity(numa_node);
        const int num_numa_nodes = port::NUMANumNodes();
        const int minimum_num_parallel_calls = thread_num * num_numa_nodes;

        while (true) {
          // Put threads to sleep based on autotuner.
          {
            mutex_lock l(*mu_);
            while (minimum_num_parallel_calls >= num_parallel_calls_->value &&
                   !cancelled_) {
              RecordStop(ctx.get());
              autotune_cond_var_->wait(l);
              RecordStart(ctx.get());
            }
            if (cancelled_) {
              return;
            }
          }

          std::vector<Tensor> input;
          uint64 index = 0;
          size_t sequence_number = 0;
          WORKER_VLOG(4) << "retrieving input.";
          {
            tracing::ScopedActivity trace(
                "NumaMapAndBatch::Iterator::Worker::RetrieveInput");
            if (!block->manager.RetrieveInput(ctx.get(), &input, &index,
                                              &sequence_number)) {
              return;
            }
          }

          WORKER_VLOG(4) << "retrieved input; index: " << index
                         << ", sequence_number: " << sequence_number;

          std::vector<Tensor> return_values;
          Status s;
          {
            tracing::ScopedActivity trace(
                "NumaMapAndBatch::Iterator::Worker::FunctionExecution");
            s = dataset()->captured_func_->Run(ctx.get(), std::move(input),
                                               &return_values);
          }
          WORKER_VLOG(4) << "ran function for index: " << index
                         << ", sequence_number: " << sequence_number;

          if (s.ok()) {
            std::vector<Tensor>* output = block->manager.GetBatchTensors(
                sequence_number,
                [this, ctx, &return_values](size_t batch_size,
                                            std::vector<Tensor>* output) {
                  AllocateOutput(ctx.get(), batch_size, return_values, output);
                });
            WORKER_VLOG(4) << "copying tensors to batch output.";
            {
              tracing::ScopedActivity trace(
                  "NumaMapAndBatch::Iterator::Worker::BatchCopy");
              for (size_t i = 0; i < return_values.size() && s.ok(); ++i) {
                Tensor& tensor = return_values.at(i);
                Tensor* batch = &output->at(i);
                if (tensor.NumElements() !=
                    (batch->NumElements() / batch->dim_size(0))) {
                  s.Update(errors::InvalidArgument(
                      "Cannot add tensor to the batch: number of elements does "
                      "not match. Shapes are: [tensor]: ",
                      tensor.shape().DebugString(),
                      ", [batch]: ", batch->shape().DebugString()));
                  break;
                }
                s.Update(batch_util::CopyElementToSlice(std::move(tensor),
                                                        batch, index));
              }
            }
          }

          block->manager.RecordBatchEntryComplete(sequence_number, index, s);
          WORKER_VLOG(4) << "finished index: " << index
                         << ", sequence_number: " << sequence_number;
        }
      }

      // mu_ protects shared internal state and is used to coordinate between
      // the auto-tuner, client threads, worker threads, and the runner thread.
      const std::shared_ptr<mutex> mu_;
      const std::shared_ptr<condition_variable> autotune_cond_var_;
      // The maximum number of parallel calls (can be auto-tuned).
      const std::shared_ptr<model::SharedState> num_parallel_calls_;

      // Caches the last-seen value of num_parallel_calls_->value to
      // short-circuit starting workers.
      int64 curr_num_parallel_calls_ GUARDED_BY(*mu_) = 0;

      std::unique_ptr<IteratorBase> input_impl_;
      int64 cur_block_ GUARDED_BY(*mu_) = 0;
      bool global_end_of_input_ GUARDED_BY(*mu_) = false;
      bool cancelled_ GUARDED_BY(*mu_) = false;
      std::vector<std::unique_ptr<NumaWorkerBlock,
                                  std::function<void(NumaWorkerBlock*)>>>
          workers_;  // Const after initialization.
      std::unique_ptr<Thread> runner_thread_ GUARDED_BY(*mu_);
    };

    const DatasetBase* const input_;
    const int64 batch_size_;
    const int64 num_parallel_calls_;
    const bool drop_remainder_;
    const DataTypeVector output_types_;
    const std::vector<PartialTensorShape> output_shapes_;
    const NameAttrList func_;
    const std::unique_ptr<CapturedFunction> captured_func_;
  };

  DataTypeVector output_types_;
  std::vector<PartialTensorShape> output_shapes_;
  NameAttrList func_;
};

REGISTER_KERNEL_BUILDER(
    Name("ExperimentalNumaMapAndBatchDataset").Device(DEVICE_CPU),
    NumaMapAndBatchDatasetOp);

}  // namespace
}  // namespace data
}  // namespace tensorflow


text:
package fr.renoux.gaston.model

/** The higher the weight of a person, the more its preferences matter when calculating a score for a set of preferences
  * involving multiple persons. */
final case class Weight(value: Double) extends AnyVal {
  @inline def *(s: Score): Score = Score(value * s.value) // scalastyle:ignore method.name
}

object Weight extends (Double => Weight) {

  val Default: Weight = Weight(1.0)

  /** Multiply all arguments weights (at least one) together */
  def combine(w: Weight, ws: Weight*): Weight = Weight(
    ws.map(_.value).foldLeft(w.value)(_ * _)
  )

  /** Implementation of the Fractional typeclass for Weight */
  implicit object WeightIsFractional extends Fractional[Weight] {
    override def plus(x: Weight, y: Weight): Weight = Weight(x.value + y.value)

    override def minus(x: Weight, y: Weight): Weight = Weight(x.value - y.value)

    override def times(x: Weight, y: Weight): Weight = Weight(x.value * y.value)

    override def div(x: Weight, y: Weight): Weight = Weight(x.value / y.value)

    override def negate(x: Weight): Weight = Weight(-x.value)

    override def fromInt(x: Int): Weight = Weight(x.toDouble)

    override def toInt(x: Weight): Int = x.value.toInt

    override def toLong(x: Weight): Long = x.value.toLong

    override def toFloat(x: Weight): Float = x.value.toFloat

    override def toDouble(x: Weight): Double = x.value

    override def compare(x: Weight, y: Weight): Int = x.value.compareTo(y.value)

    override def abs(x: Weight): Weight = Weight(math.abs(x.value))

    override def parseString(str: String): Option[Weight] = str.toDoubleOption.map(Weight)
  }

}

redpajama_set_name: RedPajamaGithub


text:
/**
 * Removes the '@' sign mentions in a tweet text.
 */

const filterMentions = tweet => {
    let message = tweet.replace(/@\S+/img, '');
    message = message.replace(/\s+/img, ' ');
    return message.trim();
};

module.exports = filterMentions;


text:
"""
Using UIVERSION = Swagger UI 2.
"""

from flask import Flask, jsonify

from flasgger import Swagger

app = Flask(__name__)
app.config['SWAGGER'] = {
    'title': 'Colors API Using Swagger UI 2 (old)',
    'uiversion': 2
}
Swagger(app)

@app.route('/colors/<palette>/')
def colors(palette):
    """Example endpoint return a list of colors by palette
    This is using docstring for specifications
    ---
    tags:
      - colors
    parameters:
      - name: palette
        in: path
        type: string
        enum: ['all', 'rgb', 'cmyk']
        required: true
        default: all
        description: Which palette to filter?
    operationId: get_colors
    consumes:
      - application/json
    produces:
      - application/json
    security:
      colors_auth:
        - 'write:colors'
        - 'read:colors'
    schemes: ['http', 'https']
    deprecated: false
    externalDocs:
      description: Project repository
      url: http://github.com/rochacbruno/flasgger
    definitions:
      Palette:
        type: object
        properties:
          palette_name:
            type: array
            items:
              $ref: '#/definitions/Color'
      Color:
        type: string
    responses:
      200:
        description: A list of colors (may be filtered by palette)
        schema:
          $ref: '#/definitions/Palette'
        examples:
          rgb: ['red', 'green', 'blue']
    """
    all_colors = {
        'cmyk': ['cyan', 'magenta', 'yellow', 'black'],
        'rgb': ['red', 'green', 'blue']
    }
    if palette == 'all':
        result = all_colors
    else:
        result = {palette: all_colors.get(palette)}

    return jsonify(result)

def test_swag(client, specs_data):
    """
    This test is runs automatically in Travis CI

    :param client: Flask app test client
    :param specs_data: {'url': {swag_specs}} for every spec in app
    """
    for url, spec in specs_data.items():
        assert 'Palette' in spec['definitions']
        assert 'Color' in spec['definitions']
        assert 'colors' in spec['paths']['/colors/{palette}/']['get']['tags']

if __name__ == "__main__":
    app.run(debug=True)


text:
require "spec_helper"
require "httpi/response"

describe HTTPI::Response do

  context "normal" do
    let(:response) { HTTPI::Response.new 200, {}, Fixture.xml }

    describe "#error?" do
      it "returns false" do
        expect(response).not_to be_an_error
      end
    end

    describe "#headers" do
      it "returns the HTTP response headers" do
        expect(response.headers).to eq({})
      end
    end

    describe "#code" do
      it "returns the HTTP response code" do
        expect(response.code).to eq(200)
      end

      it "always returns an Integer" do
        response = HTTPI::Response.new "200", {}, ""
        expect(response.code).to eq(200)
      end
    end

    describe "#multipart" do
      it "returns false" do
        expect(response).not_to be_multipart
      end
    end

    describe "#cookies" do
      it "returns an empty list" do
        expect(response.cookies).to eq([])
      end
    end
  end

  context "with cookies" do
    let(:response) { HTTPI::Response.new 200, { "Set-Cookie" => "some-cookie=choc-chip; Path=/; HttpOnly" }, "" }

    describe "#cookies" do
      it "returns a list of cookies" do
        cookie = response.cookies.first
        expect(cookie).to be_a(HTTPI::Cookie)
      end
    end
  end

  context "empty" do
    let(:response) { HTTPI::Response.new 204, {}, nil }

    describe "#body" do
      it "returns an empty String" do
        expect(response.body).to eq("")
      end
    end
  end

  context "multipart" do
    let(:response) { HTTPI::Response.new 200, { "Content-Type" => "multipart/related" }, "multipart" }

    describe "#multipart" do
      it "returns true" do
        expect(response).to be_multipart
      end
    end
  end

  context "error" do
    let(:response) { HTTPI::Response.new 404, {}, "" }

    describe "#error?" do
      it "returns true" do
        expect(response).to be_an_error
      end
    end
  end

  context "gzipped" do
    let(:response) { HTTPI::Response.new 200, { "Content-Encoding" => "gzip" }, Fixture.gzip }

    describe "#headers" do
      it "returns the HTTP response headers" do
        expect(response.headers).to eq({ "Content-Encoding" => "gzip" })
      end
    end

    describe "#body" do
      it "returns the (gzip decoded) HTTP response body" do
        expect(response.body).to eq(Fixture.xml)
      end

      it "bubbles Zlib errors" do
        arbitrary_error = Class.new(ArgumentError)
        Zlib::GzipReader.expects(:new).raises(arbitrary_error)
        expect { response.body }.to raise_error(arbitrary_error)
      end
    end

    describe "#raw_body" do
      it "returns the raw HTML response body" do
        expect(response.raw_body).to eq(Fixture.gzip)
      end
    end
  end

  context "DIME" do
    let(:response) { HTTPI::Response.new 200, { "Content-Type" => "application/dime" }, Fixture.dime }

    describe "#headers" do
      it "returns the HTTP response headers" do
        expect(response.headers).to eq({ "Content-Type" => "application/dime" })
      end
    end

    describe "#body" do
      it "returns the (dime decoded) HTTP response body" do
        expect(response.body).to eq(Fixture.xml_dime)
      end
    end

    describe "#raw_body" do
      it "returns the raw HTML response body" do
        expect(response.raw_body).to eq(Fixture.dime)
      end
    end

    describe "#attachments" do
      it "returns proper attachment when given a dime response" do
        response.attachments.first.data == File.read(File.expand_path("../../fixtures/attachment.gif", __FILE__))
      end
    end
  end

end


text:
'use strict';

const common = require('../common');
if (!common.hasCrypto)
  common.skip('missing crypto');
const h2 = require('http2');

const serverTimeout = common.platformTimeout(200);
const callTimeout = common.platformTimeout(20);
const minRuns = Math.ceil(serverTimeout / callTimeout) * 2;
const mustNotCall = common.mustNotCall();

const server = h2.createServer();
server.timeout = serverTimeout;

server.on('request', (req, res) => res.end());
server.on('timeout', mustNotCall);

server.listen(0, common.mustCall(() => {
  const port = server.address().port;

  const url = `http://localhost:${port}`;
  const client = h2.connect(url);
  makeReq(minRuns);

  function makeReq(attempts) {
    const request = client.request({
      ':path': '/foobar',
      ':method': 'GET',
      ':scheme': 'http',
      ':authority': `localhost:${port}`,
    });
    request.resume();
    request.end();

    request.on('end', () => {
      if (attempts) {
        setTimeout(() => makeReq(attempts - 1), callTimeout);
      } else {
        server.removeListener('timeout', mustNotCall);
        client.close();
        server.close();
      }
    });
  }
}));


text:
if [[ -z "$PROFILE_NAME" ]]; then
    echo "Error: Missing provision profile name"
    exit 1
fi

if [[ ! -e "./sh/release/certificates.tar.enc" ]]; then
    echo "Error: Missing encrypted certificates."
    exit 1
fi
openssl aes-256-cbc -K $encrypted_fd699ad937e2_key -iv $encrypted_fd699ad937e2_iv -in ./sh/release/certificates.tar.enc -out ./sh/release/certificates.tar -d
tar xvf ./sh/release/certificates.tar -C ./sh/release/certificates


text:
package org.renjin.primitives.annotations.processor.args;

import com.sun.codemodel.JCodeModel;
import com.sun.codemodel.JExpr;
import com.sun.codemodel.JExpression;
import com.sun.codemodel.JVar;
import org.renjin.primitives.annotations.InvokeAsCharacter;
import org.renjin.primitives.annotations.processor.ApplyMethodContext;
import org.renjin.primitives.annotations.processor.JvmMethod.Argument;
import org.renjin.primitives.annotations.processor.WrapperRuntime;

public class UsingAsCharacter extends ArgConverterStrategy {

  public UsingAsCharacter(Argument formal) {
    super(formal);
  }

  public static boolean accept(Argument formal) {
    return formal.isAnnotatedWith(InvokeAsCharacter.class);
  }

  @Override
  public JExpression convertArgument(ApplyMethodContext parent, JExpression sexp) {
    return parent.classRef(WrapperRuntime.class).staticInvoke("invokeAsCharacter")
            .arg(parent.getContext())
            .arg(parent.getEnvironment())
            .arg(sexp);
  }

  @Override
  public JExpression getTestExpr(JCodeModel codeModel, JVar sexpVariable) {
    return JExpr.TRUE;
  }
}


text:
namespace RotatingWalkInMatrix.Common
{
    using System;
    using System.Text;

    public class Matrix
    {
        public const int MaxSize = 100;
        private const int DirectionsCount = 8;

        private readonly int[,] body;
        private static readonly MatrixCoordinates[] directions;

        static Matrix()
        {
            directions = GetDirections();
        }

        public Matrix(int size)
        {
            if (size < 1 || size > MaxSize)
            {
                throw new ArgumentOutOfRangeException(
                    "size", string.Format("Matrix size must be in the range [1..{0}]", MaxSize));
            }

            this.body = new int[size, size];
        }

        public void Walk()
        {
            int currentCellValue = 1;
            MatrixCoordinates currentCoordinates = new MatrixCoordinates(0, 0);
            MatrixCoordinates currentDirection = Directions.Southeast;

            while (true)
            {
                this.body[currentCoordinates.Row, currentCoordinates.Column] = currentCellValue;

                if (!this.CanContinue(currentCoordinates))
                {
                    bool newCoordinatesFound = this.TryFindNewCoordinates(out currentCoordinates);
                    if (newCoordinatesFound)
                    {
                        currentCellValue++;
                        this.body[currentCoordinates.Row, currentCoordinates.Column] = currentCellValue;
                        currentDirection = Directions.Southeast;
                    }
                    else
                    {
                        break;
                    }
                }

                while (!this.CanGoToCoordinates(currentCoordinates + currentDirection))
                {
                    this.UpdateDirectionClockwise(ref currentDirection);
                }

                currentCoordinates += currentDirection;
                currentCellValue++;
            }
        }

        private void UpdateDirectionClockwise(ref MatrixCoordinates direction)
        {
            int currentDirectionIndex = 0;

            for (int i = 0; i < DirectionsCount; i++)
            {
                if (direction == directions[i])
                {
                    currentDirectionIndex = i;
                    break;
                }
            }

            int newDirectionIndex = 0;
            if (currentDirectionIndex == DirectionsCount - 1)
            {
                newDirectionIndex = 0;
            }
            else
            {
                newDirectionIndex = currentDirectionIndex + 1;
            }

            direction = directions[newDirectionIndex];
        }

        private bool CanContinue(MatrixCoordinates newCoordinates)
        {
            for (int i = 0; i < directions.Length; i++)
            {
                if (this.CanGoToCoordinates(newCoordinates + directions[i]))
                {
                    return true;
                }
            }

            return false;
        }

        private bool TryFindNewCoordinates(out MatrixCoordinates newCoordinates)
        {
            newCoordinates = new MatrixCoordinates(0, 0);

            for (int row = 0; row < this.body.GetLength(0); row++)
            {
                for (int col = 0; col < this.body.GetLength(1); col++)
                {
                    if (this.body[row, col] == 0)
                    {
                        newCoordinates.Row = row;
                        newCoordinates.Column = col;
                        return true;
                    }
                }
            }

            return false;
        }

        private bool CanGoToCoordinates(MatrixCoordinates coordinates)
        {
            bool validRow = (0 <= coordinates.Row && coordinates.Row < this.body.GetLength(0));
            bool validCol = (0 <= coordinates.Column && coordinates.Column < this.body.GetLength(1));

            return validRow && validCol && this.body[coordinates.Row, coordinates.Column] == 0;
        }

        public override string ToString()
        {
            StringBuilder output = new StringBuilder();
            for (int row = 0; row < this.body.GetLength(0); row++)
            {
                for (int col = 0; col < this.body.GetLength(1); col++)
                {
                    output.AppendFormat("{0, 5}", this.body[row, col]);
                }

                output.AppendLine();
            }

            output.Length -= 2;
            return output.ToString();
        }

        private static MatrixCoordinates[] GetDirections()
        {
            MatrixCoordinates[] directions = new MatrixCoordinates[DirectionsCount];
            directions[0] = Directions.Southeast;
            directions[1] = Directions.South;
            directions[2] = Directions.Southwest;
            directions[3] = Directions.West;
            directions[4] = Directions.Northwest;
            directions[5] = Directions.North;
            directions[6] = Directions.Northeast;
            directions[7] = Directions.East;

            return directions;
        }
    }
}


text:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
	xmlns:facebook="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
	android:background="@drawable/photo1_small">
	<LinearLayout
		android:layout_width="match_parent"
		android:layout_height="match_parent"
		android:orientation="vertical"
		android:clickable="true"
	>

	    <LinearLayout
			android:layout_height="0dp"
			android:layout_width="fill_parent"
			android:layout_weight="1"
			android:gravity="top"
			android:orientation="horizontal"
		>
			<LinearLayout
				android:id="@+id/selfProfile"
				android:layout_height="match_parent"
				android:layout_width="match_parent"
				android:gravity="center"
			>
			</LinearLayout>

		</LinearLayout>

		<android.support.v4.view.ViewPager
			android:id="@+id/pager"
			android:layout_width="match_parent"
			android:layout_height="0px"
			android:layout_weight="6" >
		</android.support.v4.view.ViewPager>

		<LinearLayout
			android:layout_height="wrap_content"
			android:layout_width="match_parent"
			android:orientation="horizontal"
			android:layout_weight="0"
			android:gravity="center|bottom"
			android:measureWithLargestChild="true"
		>

			<Button
				android:id="@+id/loginOtherButton"
				android:layout_width="wrap_content"
				android:layout_height="wrap_content"
				android:textStyle="bold"
				android:text="@string/login"
				android:textColor="#fff"
			/>

			<Button
				android:id="@+id/logoutButton"
				android:layout_width="wrap_content"
				android:layout_height="wrap_content"
				android:textStyle="bold"
				android:text="@string/logout"
				android:textColor="#fff"
			/>

		</LinearLayout>
	</LinearLayout>
	<com.pachakutech.meetspace.VerticalAutoFitTextView
		android:id="@+id/nfcStatusBar"
		android:clickable="false"
		android:layout_width="wrap_content"
		android:layout_height="fill_parent"
		android:layout_gravity="left"
		android:orientation="vertical"
		android:alpha="0.4"
		android:textStyle="bold"
		android:textColor="@color/black" />
	<com.pachakutech.meetspace.VerticalAutoFitTextView
		android:id="@+id/roomStatusBar"
		android:clickable="false"
		android:layout_width="wrap_content"
		android:layout_height="fill_parent"
		android:layout_gravity="right"
		android:orientation="vertical"
		android:alpha="0.4"
		android:textStyle="bold"
		android:textColor="@color/black" />

</FrameLayout>


text:
package org.apache.isis.viewer.bdd.common.fixtures;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import org.apache.isis.applib.clock.Clock;
import org.apache.isis.viewer.bdd.common.AliasRegistry;
import org.apache.isis.viewer.bdd.common.CellBinding;
import org.apache.isis.viewer.bdd.common.ScenarioCell;

public class DebugClockPeer extends AbstractFixturePeer {

    private final CellBinding cellBinding;
    private final static SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("dd-MMM-yyyy HH:mm");

    public DebugClockPeer(final AliasRegistry aliasesRegistry, final CellBinding cellBinding) {
        super(aliasesRegistry, cellBinding);
        this.cellBinding = cellBinding;
    }

    public ScenarioCell getCurrentCell() {
        return cellBinding.getCurrentCell();
    }

    public String getFormattedClockTime() {
        final Calendar cal = Clock.getTimeAsCalendar();
        final String formattedDate = DATE_FORMAT.format(cal.getTime());
        return formattedDate;
    }

}


text:
#pragma once

#include <liblox/common.h>

/**
 * Architecture hooks for initialization.
 */
arch_specific void kernel_setup_devices(void);

/**
 * Initializes the kernel, and task switches into the scheduler.
 */
does_not_return void kernel_init(void);

/**
 * Checks if the kernel is initialized.
 *
 * Returns true after the kernel is fully initialized.
 */
bool kernel_is_initialized(void);


text:
from bayesianABTest import sampleMeanForLogNormal, probabilityOfABetterThanB
from numpy.random import lognormal
from numpy import mean

# Generate random data with Log Normal Distribution
data_A = lognormal(mean=4.05, sigma=1.0, size=500)
data_B = lognormal(mean=4.00,  sigma=1.0, size=500)

# Get samples from the posterior
A_mean = sampleMeanForLogNormal(A_data)
B_mean = sampleMeanForLogNormal(B_data)

# Result:
# Probability that the mean of A is greater than the mean of B
print probabilityOfABetterThanB(A_mean,B_mean)


text:
package com.github.gigurra.math

import org.scalatest._
import org.scalatest.mock._

import scala.language.postfixOps

class NormalizeAngleSpec
  extends WordSpec
  with MockitoSugar
  with Matchers
  with OneInstancePerTest {

  "Normalizing angles" should {

    "For Floats" should {

      "Normalize degrees 0-360" in {
        -0.5f.normalizeDegrees0360 shouldBe 359.5f
        0.5f.normalizeDegrees0360 shouldBe 0.5f
        -30.0f.normalizeDegrees0360 shouldBe 330.0f
        -180.0f.normalizeDegrees0360 shouldBe 180.0f
        -360.0f.normalizeDegrees0360 shouldBe 0.0f
        -360.5f.normalizeDegrees0360 shouldBe 359.5f
        -400.0f.normalizeDegrees0360 shouldBe 320.0f
        30.0f.normalizeDegrees0360 shouldBe 30.0f
        180.0f.normalizeDegrees0360 shouldBe 180.0f
        360.0f.normalizeDegrees0360 shouldBe 0.0f
        400.0f.normalizeDegrees0360 shouldBe 40.0f
        1080.0f.normalizeDegrees0360 shouldBe 0.0f
        -1080.0f.normalizeDegrees0360 shouldBe 0.0f
      }

      "Normalize degrees +-180" in {
        -0.5f.normalizeDegreesPm180 shouldBe -0.5f
        0.5f.normalizeDegreesPm180 shouldBe 0.5f
        -30.0f.normalizeDegreesPm180 shouldBe -30.0f
        -180.0f.normalizeDegreesPm180 shouldBe 180.0f
        -360.0f.normalizeDegreesPm180 shouldBe 0.0f
        -360.5f.normalizeDegreesPm180 shouldBe -0.5f
        -400.0f.normalizeDegreesPm180 shouldBe -40.0f
        30.0f.normalizeDegreesPm180 shouldBe 30.0f
        180.0f.normalizeDegreesPm180 shouldBe 180.0f
        360.0f.normalizeDegreesPm180 shouldBe 0.0f
        400.0f.normalizeDegreesPm180 shouldBe 40.0f
        1080.0f.normalizeDegreesPm180 shouldBe 0.0f
        -1080.0f.normalizeDegreesPm180 shouldBe 0.0f
      }

      "Normalize radians 0-2pi" in {
        -0.5f.toRadians.normalizeRadians0TwoPi shouldBe 359.5f.toRadians
        0.5f.toRadians.normalizeRadians0TwoPi shouldBe 0.5f.toRadians
        -30.0f.toRadians.normalizeRadians0TwoPi shouldBe 330.0f.toRadians
        -180.0f.toRadians.normalizeRadians0TwoPi shouldBe 180.0f.toRadians
        -360.0f.toRadians.normalizeRadians0TwoPi shouldBe 0.0f.toRadians
        -360.5f.toRadians.normalizeRadians0TwoPi shouldBe 359.5f.toRadians
        -400.0f.toRadians.normalizeRadians0TwoPi shouldBe 320.0f.toRadians
        30.0f.toRadians.normalizeRadians0TwoPi shouldBe 30.0f.toRadians
        180.0f.toRadians.normalizeRadians0TwoPi shouldBe 180.0f.toRadians
        360.0f.toRadians.normalizeRadians0TwoPi shouldBe 0.0f.toRadians
        400.0f.toRadians.normalizeRadians0TwoPi shouldBe 40.0f.toRadians
        1080.0f.toRadians.normalizeRadians0TwoPi shouldBe 0.0f.toRadians
        -1080.0f.toRadians.normalizeRadians0TwoPi shouldBe 0.0f.toRadians
      }

      "Normalize degrees +-pi" in {
        -0.5f.toRadians.normalizeRadiansPmPi shouldBe -0.5f.toRadians
        0.5f.toRadians.normalizeRadiansPmPi shouldBe 0.5f.toRadians
        -30.0f.toRadians.normalizeRadiansPmPi shouldBe -30.0f.toRadians
        -180.0f.toRadians.normalizeRadiansPmPi shouldBe 180.0f.toRadians
        -360.0f.toRadians.normalizeRadiansPmPi shouldBe 0.0f.toRadians
        -360.5f.toRadians.normalizeRadiansPmPi shouldBe -0.5f.toRadians
        -400.0f.toRadians.normalizeRadiansPmPi shouldBe -40.0f.toRadians
        30.0f.toRadians.normalizeRadiansPmPi shouldBe 30.0f.toRadians
        180.0f.toRadians.normalizeRadiansPmPi shouldBe 180.0f.toRadians
        360.0f.toRadians.normalizeRadiansPmPi shouldBe 0.0f.toRadians
        400.0f.toRadians.normalizeRadiansPmPi shouldBe 40.0f.toRadians
        1080.0f.toRadians.normalizeRadiansPmPi shouldBe 0.0f.toRadians
        -1080.0f.toRadians.normalizeRadiansPmPi shouldBe 0.0f.toRadians
      }
    }
  }
}


text:
//
//  SettingsViewController.m
//  JChat
//
//  Created by Aimee Amador on 1/17/14.
//  Copyright (c) 2014 JChat. All rights reserved.
//

#import "SettingsViewController.h"
#import "UIImage+Thumbnail.h"
#import "Styles.h"

@interface SettingsViewController ()

@property (nonatomic, strong) UIImagePickerController *imagePicker;
@end

@implementation SettingsViewController
@synthesize photoContainerView;
@synthesize picPhotoLabel;
@synthesize photoView;

- (void)viewDidLoad
{
    [super viewDidLoad];

    //Display Profile Picture
    PFUser *user = [PFUser currentUser];

    PFFile *profilePic =  [user objectForKey:@"profilePic"];

    if(profilePic != nil)
    {
        if ([profilePic.url length] > 0) {
            [profilePic getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
                if (!error) {
                    UIImage *image = [UIImage imageWithData:data];
                    photoView.image = image;
                }
                else{
                    photoView.image = [UIImage imageNamed:@"icon_person"];
                }
            }
             ];}
    }

}

-(IBAction)saveBtn:(id)sender {
    UIImage *img = self.photoView.image;

    NSData *fileData;
    NSString *fileName;
    NSString *fileType;

    if (img != nil) {

        fileData = UIImagePNGRepresentation(img);
        fileName = [[PFUser currentUser] username];
        fileName = [fileName stringByAppendingString:@".png"];
        fileType = @"image";
    }

    PFFile *file = [PFFile fileWithName:fileName data:fileData];

    PFUser *user = [PFUser currentUser];
    [user setObject:file forKey:@"profilePic"];

    [user saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (error) {

            //NSLog(@"Error: %@ %@", error, [error userInfo]);
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"エラー!"
                                                            message:@"もう一度お願いします" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
            [alert show];

            } else {

            //[self refresh:nil];
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"完了"
                                                            message:@"プロフィール用のイメージの登録が完了しました!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
            [alert show];
            }
    }];

            /*
            // Create a PFObject around a PFFile and associate it with the current user
            PFObject *userPhoto = [PFObject objectWithClassName:@"UserPhoto"];
            [userPhoto setObject:file forKey:@"imageFile"];

            // Set the access control list to current user for security purposes
            userPhoto.ACL = [PFACL ACLWithUser:[PFUser currentUser]];

            PFUser *user = [PFUser currentUser];
            [userPhoto setObject:user forKey:@"user"];

            [userPhoto saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                if (!error) {
                    //[self refresh:nil];
                }
                else{
                    // Log details of the failure
                    NSLog(@"Error: %@ %@", error, [error userInfo]);
                }
            }];
             */
}

- (IBAction)didTapPhoto:(id)sender {
    NSLog(@"Did Tap Photo!");
    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        NSLog(@"No camera detected!");
        [self pickPhoto];
        return;
    }

    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Take a Photo",@"Pick from Photo Library", nil];
    [actionSheet showInView:self.view];
}

-(UIImagePickerController *) imagePicker
{
    if (_imagePicker == nil) {
        _imagePicker = [[UIImagePickerController alloc] init];
        _imagePicker.delegate = self;
    }
    return _imagePicker;
}

-(void) takePhoto
{
    self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    [self.navigationController presentViewController:self.imagePicker animated:YES completion:nil];
}

-(void) pickPhoto
{
    self.imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    if (self.imagePicker == nil) {
        NSLog(@"It's nil!");
    }
    else
    {
        NSLog(@"Not nil!");
    }

    [self.navigationController presentViewController:self.imagePicker animated:YES completion:nil];
}

#pragma mark UIImagePickerControllerDelegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    [picker dismissViewControllerAnimated:YES completion:nil];

    UIImage *image = info[UIImagePickerControllerOriginalImage];

    CGFloat side = 72.f;
    side *= [[UIScreen mainScreen] scale];

    UIImage *thumbnail = [image createThumbnailToFillSize:CGSizeMake(side, side)];
    self.photoView.image = thumbnail;
    //
}

#pragma mark UIActionSheetDelegate

- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == actionSheet.cancelButtonIndex) return;

    switch (buttonIndex) {
        case 0:
            [self takePhoto];
            break;
        case 1:
            [self pickPhoto];
            break;
    }
}

@end


text:
package com.oracle.test.input;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collection;

/**
 * File reader to read object from the file with the help of provided parser.
 * @author hemant gaur
 *
 * @param <T>
 */
public class DashboardFileReader<T> {
	Parser<String, T> inputParser;

	public DashboardFileReader(Parser<String, T> inputParser){
		this.inputParser=inputParser;
	}
	/**
	 * Reads file and parse it using parser.
	 * @param pathToFile
	 * @return collection of object using parser.
	 * @throws IOException
	 * @throws ParseException
	 */
	public Collection<T> readFile(String pathToFile) throws IOException, ParseException{
		Collection<T> list = new ArrayList<>();
		try (BufferedReader br = new BufferedReader(new FileReader(pathToFile)))
		{
			String line;
			//ignore first line
			br.readLine();
			while ((line = br.readLine()) != null) {
				list.add(inputParser.parse(line));
			}

		} catch (IOException e) {
			e.printStackTrace();
		}
		return list;
	}

}


text:
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Firebase.Extensions;
using Firebase.Firestore;
using NUnit.Framework;
using UnityEngine.TestTools;

using static Tests.TestAsserts;

namespace Tests {

  public abstract class FirestoreIntegrationTests {
    private const int AUTO_ID_LENGTH = 20;
    private const string AUTO_ID_ALPHABET =
        "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    private static System.Random random = new System.Random();
    private static string AutoId() {
      string result = "";
      for (int i = 0; i < AUTO_ID_LENGTH; i++) {
        result += AUTO_ID_ALPHABET[random.Next(0, AUTO_ID_ALPHABET.Length)];
      }

      return result;
    }

    protected int mainThreadId = -1;
    [UnitySetUp]
    protected IEnumerator CheckAndFixDependencies() {
      yield return AwaitSuccess(Firebase.FirebaseApp.CheckAndFixDependenciesAsync());
    }

    [UnitySetUp]
    protected IEnumerator InitializeMainThreadId() {
      yield return AwaitSuccess(Task.Run((() => {})).ContinueWithOnMainThread((task => {
        mainThreadId = Thread.CurrentThread.ManagedThreadId;
      })));
    }

    protected FirebaseFirestore db => FirebaseFirestore.DefaultInstance;

    protected CollectionReference TestCollection() {
      return db.Collection("test-collection_" + AutoId());
    }

    protected DocumentReference TestDocument() {
      return TestCollection().Document();
    }

    protected Dictionary<string, object> TestData(long n = 1) {
      return new Dictionary<string, object> {
        { "name", "room " + n },
        { "active", true },
        { "list", new List<object> { "one", 2L, "three", 4L } },
        { "metadata", new Dictionary<string, object> { { "createdAt", n },
                                                       { "deep",
                                                         new Dictionary<string, object> {
                                                           { "field", "deep-field-" + n },
                                                           { "nested-list",
                                                             new List<object> { "a", "b", "c" } },
                                                         } } } }
        // TODO(b/181775697): Add other types here too.
      };
    }

    protected static List<Dictionary<string, object>> QuerySnapshotToValues(QuerySnapshot snap) {
      List<Dictionary<string, object>> result = new List<Dictionary<string, object>>();
      foreach (DocumentSnapshot doc in snap) {
        result.Add(doc.ToDictionary());
      }
      return result;
    }
  }
}


text:
#pragma once
#include <aws/support/Support_EXPORTS.h>
#include <aws/support/SupportRequest.h>
#include <aws/core/utils/memory/stl/AWSString.h>
#include <utility>

namespace Aws
{
namespace Support
{
namespace Model
{

  /**
   *

See Also:
   AWS
   * API Reference

   */
  class AWS_SUPPORT_API RefreshTrustedAdvisorCheckRequest : public SupportRequest
  {
  public:
    RefreshTrustedAdvisorCheckRequest();
    Aws::String SerializePayload() const override;

    Aws::Http::HeaderValueCollection GetRequestSpecificHeaders() const override;

    /**
     *
The unique identifier for the Trusted Advisor check to refresh. Note:
     * Specifying the check ID of a check that is automatically refreshed causes an
     * InvalidParameterValue error.

     */
    inline const Aws::String& GetCheckId() const{ return m_checkId; }

    /**
     *
The unique identifier for the Trusted Advisor check to refresh. Note:
     * Specifying the check ID of a check that is automatically refreshed causes an
     * InvalidParameterValue error.

     */
    inline void SetCheckId(const Aws::String& value) { m_checkIdHasBeenSet = true; m_checkId = value; }

    /**
     *
The unique identifier for the Trusted Advisor check to refresh. Note:
     * Specifying the check ID of a check that is automatically refreshed causes an
     * InvalidParameterValue error.

     */
    inline void SetCheckId(Aws::String&& value) { m_checkIdHasBeenSet = true; m_checkId = std::move(value); }

    /**
     *
The unique identifier for the Trusted Advisor check to refresh. Note:
     * Specifying the check ID of a check that is automatically refreshed causes an
     * InvalidParameterValue error.

     */
    inline void SetCheckId(const char* value) { m_checkIdHasBeenSet = true; m_checkId.assign(value); }

    /**
     *
The unique identifier for the Trusted Advisor check to refresh. Note:
     * Specifying the check ID of a check that is automatically refreshed causes an
     * InvalidParameterValue error.

     */
    inline RefreshTrustedAdvisorCheckRequest& WithCheckId(const Aws::String& value) { SetCheckId(value); return *this;}

    /**
     *
The unique identifier for the Trusted Advisor check to refresh. Note:
     * Specifying the check ID of a check that is automatically refreshed causes an
     * InvalidParameterValue error.

     */
    inline RefreshTrustedAdvisorCheckRequest& WithCheckId(Aws::String&& value) { SetCheckId(std::move(value)); return *this;}

    /**
     *
The unique identifier for the Trusted Advisor check to refresh. Note:
     * Specifying the check ID of a check that is automatically refreshed causes an
     * InvalidParameterValue error.

     */
    inline RefreshTrustedAdvisorCheckRequest& WithCheckId(const char* value) { SetCheckId(value); return *this;}

  private:
    Aws::String m_checkId;
    bool m_checkIdHasBeenSet;
  };

} // namespace Model
} // namespace Support
} // namespace Aws


text:
class InformationMailerPreview < ActionMailer::Preview
  def new_application
    test_leave_application = LeaveApplication.last
    InformationMailer.new_application(test_leave_application)
  end
end


text:
//===--- Class.h - Compiler/runtime class-metadata values -------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This header provides target-independent information about class
// metadata.
//
//===----------------------------------------------------------------------===//

#ifndef SWIFT_ABI_CLASS_H
#define SWIFT_ABI_CLASS_H

#include <stdint.h>

namespace swift {

/// Objective-C class flags, stored in the ro-data.
enum class ObjCClassFlags : uint32_t {
  /// This class is a metaclass.
  Meta                 = 0x00001,

  /// This class is a root class.
  Root                 = 0x00002,

  /// This class provides a non-trivial .cxx_construct or .cxx_destruct
  /// implementation.
  HasCXXStructors      = 0x00004,

  /// This class has hidden visibility.
  Hidden               = 0x00010,

  /// This class has the exception attribute.
  Exception            = 0x00020,

  /// This class provides a metadata update callback trailing the ro-data.
  /// Note that we're re-using the obsolete flag above.
  HasMetadataUpdateCallback = 0x00040,

  /// (Obsolete) ARC-specific: this class has a .release_ivars method.
  HasIvarReleaser      = 0x00040,

  /// This class implementation was compiled under ARC.
  CompiledByARC        = 0x00080,

  /// This class provides a non-trivial .cxx_destruct method, but
  /// its .cxx_construct is trivial.  For backwards compatibility,
  /// when setting this flag, HasCXXStructors must be set as well.
  HasCXXDestructorOnly = 0x00100
};
inline ObjCClassFlags &operator|=(ObjCClassFlags &lhs, ObjCClassFlags rhs) {
  lhs = ObjCClassFlags(uint32_t(lhs) | uint32_t(rhs));
  return lhs;
}
inline ObjCClassFlags operator|(ObjCClassFlags lhs, ObjCClassFlags rhs) {
  return (lhs |= rhs);
}

}

#endif /* SWIFT_ABI_CLASS_H */


text:
using ProtoBuf;

namespace Kyru.Network.UdpMessages
{
	[ProtoContract]
	internal sealed class KeepObjectResponse
	{
		[ProtoMember(1)]
		internal bool HasObject;
	}
}


text:
/*global chrome: false*/
ListIt.lvent.once('setup:views', function(L, barr) {
  'use strict';
  // Make Pages
  L.addPage('main', new L.views.MainPage());
});

ListIt.lvent.once('setup:views:after', function(L, barr) {
  'use strict';
  L.router.route('options', 'options', function() {
    // Intercept options page request and open a new window.
    this.navigate('', {trigger: false});
    window.open('/options.html', '_new');
  });

  L.router.route('help', 'help', function() {
    this.navigate('', {trigger: false});
    window.open('/help.html', '_new');
  });

  // Close on hotkey press.
  // Chrom's hotkey handling is inconsistant. On windows, the global hotkey is
  // fired even if the sidebar is focused. On linux however, the global hotkey is not fired.
  chrome.commands.getAll(function(cmds) {
    var cmd = _.findWhere(cmds, {name: "_execute_browser_action"});
    if (cmd && cmd.shortcut) {
      $(document).bind('keydown.close', cmd.shortcut, function(e) {
        e.stopImmediatePropagation();
        e.preventDefault();
        window.close();
      });
    }
  });
});


text:
@interface Flight (Info)
+ (double)durationForFlight:(Flight*)flight;
@end


text:
var searchData=
[
  ['notification_2ecpp',['Notification.cpp',['../_notification_8cpp.html',1,'']]],
  ['notification_2eh',['Notification.h',['../_notification_8h.html',1,'']]]
];