text:
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once
#include "paddle/fluid/framework/framework.pb.h"
#include "paddle/fluid/imperative/layout_autotune.h"
#include "paddle/fluid/imperative/tracer.h"
#include "paddle/fluid/imperative/var_helper.h"
#include "paddle/phi/core/enforce.h"
#include "paddle/phi/core/errors.h"
#include "paddle/phi/core/tensor_utils.h"
namespace paddle {
namespace imperative {
template <typename VarType>
void SetOutDataLayout(std::shared_ptr<VarType> var,
                      const phi::DataLayout layout) {
  if (var != nullptr && var->Var().IsInitialized()) {
    paddle::imperative::SetDataLayout(var, layout);
    // set out_tensor's layout
    if (var->MutableVar()->IsInitialized()) {
      paddle::framework::Variable* tmp_var = var->MutableVar();
      auto* out = tmp_var->GetMutable<phi::DenseTensor>();
      phi::DenseTensorUtils::GetMutableMeta(static_cast<phi::DenseTensor*>(out))
          ->layout = layout;
    }
  }
}

template <typename VarType>
std::shared_ptr<VarType> TraceTransposeOp(
    const std::shared_ptr<VarType>& var,
    const DataLayout layout,
    const std::shared_ptr<paddle::imperative::Tracer>& tracer) {
  std::vector<int> axis;
  if (layout == DataLayout::NHWC) {
    axis = {0, 2, 3, 1};
  } else if (layout == DataLayout::NCHW) {
    axis = {0, 3, 1, 2};
  } else {
    axis = {0, 1, 2, 3};
  }
  paddle::imperative::NameVarMap<VarType> ins = {{"X", {var}}};
  auto out =
      std::shared_ptr<VarType>(new VarType(tracer->GenerateUniqueName()));
  auto x_shape =
      std::shared_ptr<VarType>(new VarType(tracer->GenerateUniqueName()));
  paddle::imperative::NameVarMap<VarType> outs = {{"Out", {out}},
                                                  {"XShape", {x_shape}}};
  paddle::framework::AttributeMap attrs = {{"axis", axis}};
  tracer->TraceOp("transpose2", ins, outs, std::move(attrs));
  paddle::imperative::SetDataLayout(out, layout);
  VLOG(4) << "Transpose " << paddle::imperative::GetNameFromVar(var) << "["
          << phi::DataLayoutToString(paddle::imperative::GetDataLayout(var))
          << "]"
          << " to " << paddle::imperative::GetNameFromVar(out) << "["
          << phi::DataLayoutToString(paddle::imperative::GetDataLayout(out))
          << "]";
  return out;
}

template <typename VarType>
class LayoutTransformer {
 public:
  explicit LayoutTransformer(const std::string& type) : type_(type) {}

  virtual ~LayoutTransformer() {}

  LayoutTransformer(const LayoutTransformer&) = delete;
  LayoutTransformer& operator=(const LayoutTransformer&) = delete;

  virtual paddle::imperative::NameVarMap<VarType> Apply(
      const paddle::imperative::NameVarMap<VarType>& ins,
      const paddle::imperative::NameVarMap<VarType>& outs,
      paddle::framework::AttributeMap* attrs,
      const std::shared_ptr<paddle::imperative::Tracer>& tracer) {
    VLOG(3) << "Optimze Layout agnostic op: " << type_;
    auto in_layout = DataLayout::UNDEFINED;
    for (auto& pair : ins) {
      for (auto& var : pair.second) {
        // Once the any input is desired layout, we set in_layout is desired
        // layout.
        if (in_layout == DataLayout::UNDEFINED) {
          in_layout = paddle::imperative::GetDataLayout(var);
        }
        if (var != nullptr && (paddle::imperative::GetDataLayout(var) ==
                               LayoutAutoTune::Instance().GetDesiredLayout())) {
          in_layout = LayoutAutoTune::Instance().GetDesiredLayout();
          break;
        }
      }
    }
    VLOG(3) << "Optimze Layout agnostic op: " << type_ << " "
            << phi::DataLayoutToString(in_layout);
    if (in_layout != DataLayout::UNDEFINED) {
      SetVarsLayout(outs, in_layout);
    }
    return ins;
  }

  // Set inputs, outputs and attributes to be optimized for the transposer.
  // Those may respectively be a subset of the corresponding original argument
  // of the operator.
  void SetArguments(const std::vector<std::string>& ins,
                    const std::vector<std::string>& outs,
                    const std::vector<std::string>& attrs) {
    ins_ = ins;
    outs_ = outs;
    attrs_ = attrs;
  }

  // Set the variables's layout to the specified layout.
  // If outs_ is not specified, it means all outputs of the operator
  // will be considered. Otherwise, it only set layout for the specified output.
  void SetVarsLayout(const paddle::imperative::NameVarMap<VarType>& outs,
                     DataLayout layout) const {
    bool not_in_out = true;
    if (!outs_.empty()) {
      for (auto& name : outs_) {
        if (outs.find(name) != outs.end()) {
          auto out_vars = outs.at(name);
          for (auto& var : out_vars) {
            if (var != nullptr) {
              paddle::imperative::SetOutDataLayout(var, layout);
            }
          }
          not_in_out = false;
        }
      }
    }

    if (not_in_out) {
      for (auto& pair : outs) {
        for (auto& var : pair.second) {
          if (var != nullptr) {
            paddle::imperative::SetOutDataLayout(var, layout);
          }
        }
      }
    }
  }

  const std::vector<std::string>& Inputs() const { return ins_; }
  const std::vector<std::string>& Outputs() const { return outs_; }
  const std::vector<std::string>& Attributes() const { return attrs_; }

  const std::string& Type() { return type_; }

 protected:
  std::string type_{};
  std::vector<std::string> ins_{};
  std::vector<std::string> outs_{};
  std::vector<std::string> attrs_{};
};

/*
 * Both functionality and performance are affected by data layout.
 * Such as operators with data_format attribute.
 */
template <typename VarType>
class HeavilyLayoutSensitiveOpTransformer : public LayoutTransformer<VarType> {
 public:
  explicit HeavilyLayoutSensitiveOpTransformer(const std::string& type)
      : LayoutTransformer<VarType>(type) {}

  paddle::imperative::NameVarMap<VarType> Apply(
      const paddle::imperative::NameVarMap<VarType>& ins,
      const paddle::imperative::NameVarMap<VarType>& outs,
      paddle::framework::AttributeMap* attrs,
      const std::shared_ptr<paddle::imperative::Tracer>& tracer) {
    VLOG(3) << "Optimze heavily layout sensitive op " << this->Type();
    paddle::imperative::NameVarMap<VarType> new_ins(ins);

    // Step 1: Adjust the data_layout attr to the desired layout
    auto desired_layout = LayoutAutoTune::Instance().GetDesiredLayout();
    std::string desired_layout_str =
        phi::DataLayoutToString(LayoutAutoTune::Instance().GetDesiredLayout());
    if (attrs->find("data_format") != attrs->end() &&
        PADDLE_GET_CONST(std::string, (*attrs)["data_format"]) !=
            desired_layout_str) {
      VLOG(4) << "Origin layout attr: "
              << PADDLE_GET_CONST(std::string, (*attrs)["data_format"])
              << ", Desired layout attr: " << desired_layout_str;
      (*attrs)["data_format"] = desired_layout_str;
    } else if (attrs->find("data_layout") != attrs->end() &&
               PADDLE_GET_CONST(std::string, (*attrs)["data_layout"]) !=
                   desired_layout_str) {
      VLOG(4) << "Origin layout attr: "
              << PADDLE_GET_CONST(std::string, (*attrs)["data_layout"])
              << ", Desired layout attr: " << desired_layout_str;
      (*attrs)["data_layout"] = desired_layout_str;
    }

    // Step 2: Transpose the specified input for Op and set the transposed var's
    // layout.
    for (auto& name : this->Inputs()) {
      if (new_ins.find(name) != new_ins.end()) {
        auto& in_vars = new_ins[name];
        for (auto& var : in_vars) {
          if (var != nullptr &&
              paddle::imperative::GetDataLayout(var) != desired_layout) {
            var = TraceTransposeOp(var, desired_layout, tracer);
          }
        }
      }
    }

    // Step 3: Set the Op's layout sensitive outs var.
    this->SetVarsLayout(outs, desired_layout);

    return new_ins;
  }
};

/*
 * The functionality may be affected layout transformation before them.
 * Such as operators with axis attribute.
 */
template <typename VarType>
class LightlyLayoutSensitiveOpTransformer : public LayoutTransformer<VarType> {
 public:
  explicit LightlyLayoutSensitiveOpTransformer(const std::string& type)
      : LayoutTransformer<VarType>(type) {}

  paddle::imperative::NameVarMap<VarType> Apply(
      const paddle::imperative::NameVarMap<VarType>& ins,
      const paddle::imperative::NameVarMap<VarType>& outs,
      paddle::framework::AttributeMap* attrs,
      const std::shared_ptr<paddle::imperative::Tracer>& tracer) {
    VLOG(3) << "Optimze lightly layout sensitive op " << this->Type();
    paddle::imperative::NameVarMap<VarType> new_ins(ins);
    // If input's layout is not tuned, transformation is unnecessary.
    // If input's layout is already tuned, it will be transformed back to NCHW.
    // TODO(zhangting): The op of this type should be adapted to the previous
    // operator output data layout. Currently only a few operators are
    // supported, and transposers need to be carefully designed to ensure that
    // they do not cause exceptions.
    auto desired_layout = LayoutAutoTune::Instance().GetDesiredLayout();
    for (auto& pair : new_ins) {
      for (auto& var : pair.second) {
        if (var != nullptr) {
          VLOG(3) << "Tune the layout from "
                  << phi::DataLayoutToString(
                         paddle::imperative::GetDataLayout(var))
                  << " to "
                  << phi::DataLayoutToString(
                         LayoutAutoTune::Instance().GetDesiredLayout());
        }
        if (var != nullptr &&
            paddle::imperative::GetDataLayout(var) == desired_layout &&
            desired_layout == DataLayout::NHWC) {
          // Set layout to UNDEFINED so that TransposeOpTransformer do
          // NHWC->NCHW transformation.
          var = TraceTransposeOp(var, DataLayout::UNDEFINED, tracer);
        }
      }
    }
    return new_ins;
  }
};

template <typename VarType>
class ElementwiseOpTransformer
    : public LightlyLayoutSensitiveOpTransformer<VarType> {
 public:
  explicit ElementwiseOpTransformer(const std::string& type)
      : LightlyLayoutSensitiveOpTransformer<VarType>(type) {}

  paddle::imperative::NameVarMap<VarType> Apply(
      const paddle::imperative::NameVarMap<VarType>& ins,
      const paddle::imperative::NameVarMap<VarType>& outs,
      paddle::framework::AttributeMap* attrs,
      const std::shared_ptr<paddle::imperative::Tracer>& tracer) {
    // [Why we need the this?]
    // The Elementwise Ops has a axis attr, it is to support broadcast.
    // When bias_attr of Conv is not false, the elementwise_add will be
    // appended, and the axis will be set to the channel dimension.
    // If the axis is set to the channel dimension, the attr transformation
    // is necessary. Otherwise, it will fall back to the
    // LayoutTransformer::Apply.
    auto& in1_vars = ins.at("X")[0];
    auto& in2_vars = ins.at("Y")[0];
    auto in_layout = paddle::imperative::GetDataLayout(in1_vars);
    // for conv's bias
    if (attrs->find("axis") != attrs->end() &&
        PADDLE_GET_CONST(int, (*attrs)["axis"]) != -1) {
      if (in_layout == DataLayout::NHWC) {
        (*attrs)["axis"] = 3;
      } else if (in_layout == DataLayout::NCHW) {
        (*attrs)["axis"] = 1;
      }
      this->SetVarsLayout(outs, in_layout);
      return ins;
    } else {
      auto in2_layout = paddle::imperative::GetDataLayout(in2_vars);
      if (in_layout == in2_layout) {
        this->SetVarsLayout(outs, in_layout);
        return ins;
      }
      return LightlyLayoutSensitiveOpTransformer<VarType>::Apply(
          ins, outs, attrs, tracer);
    }
  }
};

template <typename VarType>
class TransposeOpTransformer
    : public LightlyLayoutSensitiveOpTransformer<VarType> {
 public:
  explicit TransposeOpTransformer(const std::string& type)
      : LightlyLayoutSensitiveOpTransformer<VarType>(type) {}

  paddle::imperative::NameVarMap<VarType> Apply(
      const paddle::imperative::NameVarMap<VarType>& ins,
      const paddle::imperative::NameVarMap<VarType>& outs,
      paddle::framework::AttributeMap* attrs,
      const std::shared_ptr<paddle::imperative::Tracer>& tracer) {
    VLOG(3) << "Optimze lightly layout sensitive op " << this->Type();
    // When the input layout is the desired format, it means that there
    // is a transpose layer in the network, it is better to transpose
    // the result to the original format.
    // Instead of actually inserting a transpose Op, we fuse the inserted
    // transpose Op with the current transpose Op by transforming 'axis' attr.
    auto& in_var = ins.at("X")[0];
    auto var_layout = paddle::imperative::GetDataLayout(in_var);
    auto desired_layout = LayoutAutoTune::Instance().GetDesiredLayout();
    if (var_layout == desired_layout && desired_layout == DataLayout::NHWC) {
      auto axis = PADDLE_GET_CONST(std::vector<int>, (*attrs)["axis"]);
      // NHWC->NCHW, permutaion will be set as follows.
      std::vector<int> perm = {0, 3, 1, 2};
      // fuse the transpose Ops by transforming axis.
      std::vector<int> fusion_axis = {
          perm[axis[0]], perm[axis[1]], perm[axis[2]], perm[axis[3]]};
      (*attrs)["axis"] = fusion_axis;
    }
    return ins;
  }
};

template <typename VarType>
class FlattenOpTransformer
    : public LightlyLayoutSensitiveOpTransformer<VarType> {
 public:
  explicit FlattenOpTransformer(const std::string& type)
      : LightlyLayoutSensitiveOpTransformer<VarType>(type) {}

  paddle::imperative::NameVarMap<VarType> Apply(
      const paddle::imperative::NameVarMap<VarType>& ins,
      const paddle::imperative::NameVarMap<VarType>& outs,
      paddle::framework::AttributeMap* attrs,
      const std::shared_ptr<paddle::imperative::Tracer>& tracer) {
    VLOG(3) << "Optimze lightly layout sensitive op " << this->Type();
    // Flatten the C, H, W dimensions will not affect functionality.
    // So transformation is unnecessary. But in other cases, it needs to
    // fall back to the LightlyLayoutSensitiveOpTransformer.
    auto start_axis = PADDLE_GET_CONST(int, (*attrs)["start_axis"]);
    auto stop_axis = PADDLE_GET_CONST(int, (*attrs)["stop_axis"]);
    if (paddle::imperative::GetDataLayout(ins.at("X")[0]) ==
            LayoutAutoTune::Instance().GetDesiredLayout() &&
        start_axis == 1 && stop_axis == 3) {
      return ins;
    } else {
      return LightlyLayoutSensitiveOpTransformer<VarType>::Apply(
          ins, outs, attrs, tracer);
    }
  }
};

template <typename VarType>
class ArgmaxOpTransformer
    : public LightlyLayoutSensitiveOpTransformer<VarType> {
 public:
  explicit ArgmaxOpTransformer(const std::string& type)
      : LightlyLayoutSensitiveOpTransformer<VarType>(type) {}

  paddle::imperative::NameVarMap<VarType> Apply(
      const paddle::imperative::NameVarMap<VarType>& ins,
      const paddle::imperative::NameVarMap<VarType>& outs,
      paddle::framework::AttributeMap* attrs,
      const std::shared_ptr<paddle::imperative::Tracer>& tracer) {
    VLOG(3) << "Optimze lightly layout sensitive op " << this->Type();
    auto& in_var = ins.at("X")[0];
    auto var_layout = paddle::imperative::GetDataLayout(in_var);
    bool keep_dims = PADDLE_GET_CONST(bool, (*attrs)["keepdims"]);
    if (keep_dims) {
      if (var_layout != DataLayout::UNDEFINED) {
        std::vector<int> perm_nhwc = {0, 3, 1, 2};
        std::vector<int> perm_nchw = {0, 2, 3, 1};

        auto perm = var_layout == DataLayout::NHWC ? perm_nhwc : perm_nchw;
        switch (AttrTypeID((*attrs)["axis"])) {
          case paddle::framework::proto::AttrType::INT: {
            auto axis = PADDLE_GET_CONST(int, (*attrs)["axis"]);
            (*attrs)["axis"] = static_cast<int>(perm[axis]);
          }
          case paddle::framework::proto::AttrType::LONG: {
            auto axis = PADDLE_GET_CONST(int64_t, (*attrs)["axis"]);
            (*attrs)["axis"] = static_cast<int64_t>(perm[axis]);
          }
          default:
            VLOG(4) << "The data_type of axis is Error, axis must be int or "
                       "int64, bug got "
                    << (AttrTypeID((*attrs)["axis"]));
        }
      }
      this->SetVarsLayout(outs, var_layout);
      return ins;
    }
    return LightlyLayoutSensitiveOpTransformer<VarType>::Apply(
        ins, outs, attrs, tracer);
  }
};

template <typename VarType>
class ConcatOpTransformer
    : public LightlyLayoutSensitiveOpTransformer<VarType> {
 public:
  explicit ConcatOpTransformer(const std::string& type)
      : LightlyLayoutSensitiveOpTransformer<VarType>(type) {}

  paddle::imperative::NameVarMap<VarType> Apply(
      const paddle::imperative::NameVarMap<VarType>& ins,
      const paddle::imperative::NameVarMap<VarType>& outs,
      paddle::framework::AttributeMap* attrs,
      const std::shared_ptr<paddle::imperative::Tracer>& tracer) {
    VLOG(3) << "Optimze lightly layout sensitive op " << this->Type();
    auto& in_var = ins.at("X")[0];
    auto var_layout = paddle::imperative::GetDataLayout(in_var);
    bool need_tranppose = false;
    for (auto& pair : ins) {
      for (auto& var : pair.second) {
        if (var != nullptr &&
            (paddle::imperative::GetDataLayout(var) != var_layout)) {
          need_tranppose = true;
          break;
        }
      }
    }

    if (need_tranppose) {
      return LightlyLayoutSensitiveOpTransformer<VarType>::Apply(
          ins, outs, attrs, tracer);
    }

    if (var_layout != DataLayout::UNDEFINED) {
      std::vector<int> perm_nhwc = {0, 3, 1, 2};
      std::vector<int> perm_nchw = {0, 2, 3, 1};
      auto perm = var_layout == DataLayout::NHWC ? perm_nhwc : perm_nchw;
      auto axis = PADDLE_GET_CONST(int, (*attrs)["axis"]);
      (*attrs)["axis"] = static_cast<int>(perm[axis]);
    }
    auto axis = PADDLE_GET_CONST(int, (*attrs)["axis"]);
    VLOG(3) << "Optimze lightly layout sensitive op asdfasdfasdf axis" << axis;

    this->SetVarsLayout(outs, var_layout);
    return ins;
  }
};

}  // namespace imperative
}  // namespace paddle


text:
#include "postgres.h"

#include <math.h>

#include "access/tsmapi.h"
#include "catalog/pg_type.h"
#include "common/hashfn.h"
#include "optimizer/optimizer.h"
#include "utils/builtins.h"

#include "optimizer/cost.h"

/* Private state */
typedef struct
{
	uint64		cutoff;			/* select tuples with hash less than this */
	uint32		seed;			/* random seed */
	OffsetNumber lt;			/* last tuple returned from current block */
} BernoulliSamplerData;

static void bernoulli_samplescangetsamplesize(PlannerInfo *root,
											  RelOptInfo *baserel,
											  List *paramexprs,
											  BlockNumber *pages,
											  double *tuples);
static void bernoulli_initsamplescan(SampleScanState *node,
									 int eflags);
static void bernoulli_beginsamplescan(SampleScanState *node,
									  Datum *params,
									  int nparams,
									  uint32 seed);
static OffsetNumber bernoulli_nextsampletuple(SampleScanState *node,
											  BlockNumber blockno,
											  OffsetNumber maxoffset);

/*
 * Create a TsmRoutine descriptor for the BERNOULLI method.
 */
Datum
tsm_bernoulli_handler(PG_FUNCTION_ARGS)
{
	TsmRoutine *tsm = makeNode(TsmRoutine);

	tsm->parameterTypes = list_make1_oid(FLOAT4OID);
	tsm->repeatable_across_queries = true;
	tsm->repeatable_across_scans = true;
	tsm->SampleScanGetSampleSize = bernoulli_samplescangetsamplesize;
	tsm->InitSampleScan = bernoulli_initsamplescan;
	tsm->BeginSampleScan = bernoulli_beginsamplescan;
	tsm->NextSampleBlock = NULL;
	tsm->NextSampleTuple = bernoulli_nextsampletuple;
	tsm->EndSampleScan = NULL;

	PG_RETURN_POINTER(tsm);
}

/*
 * Sample size estimation.
 */
static void
bernoulli_samplescangetsamplesize(PlannerInfo *root,
								  RelOptInfo *baserel,
								  List *paramexprs,
								  BlockNumber *pages,
								  double *tuples)
{
	Node	   *pctnode;
	float4		samplefract;

	/* Try to extract an estimate for the sample percentage */
	pctnode = (Node *) linitial(paramexprs);
	pctnode = estimate_expression_value(root, pctnode);

	if (IsA(pctnode, Const) &&
		!((Const *) pctnode)->constisnull)
	{
		samplefract = DatumGetFloat4(((Const *) pctnode)->constvalue);
		if (samplefract >= 0 && samplefract <= 100 && !isnan(samplefract))
			samplefract /= 100.0f;
		else
		{
			/* Default samplefract if the value is bogus */
			samplefract = 0.1f;
		}
	}
	else
	{
		/* Default samplefract if we didn't obtain a non-null Const */
		samplefract = 0.1f;
	}

	/* We'll visit all pages of the baserel */
	*pages = baserel->pages;

	*tuples = clamp_row_est(baserel->tuples * samplefract);
}

/*
 * Initialize during executor setup.
 */
static void
bernoulli_initsamplescan(SampleScanState *node, int eflags)
{
	node->tsm_state = palloc0(sizeof(BernoulliSamplerData));
}

/*
 * Examine parameters and prepare for a sample scan.
 */
static void
bernoulli_beginsamplescan(SampleScanState *node,
						  Datum *params,
						  int nparams,
						  uint32 seed)
{
	BernoulliSamplerData *sampler = (BernoulliSamplerData *) node->tsm_state;
	double		percent = DatumGetFloat4(params[0]);
	double		dcutoff;

	if (percent < 0 || percent > 100 || isnan(percent))
		ereport(ERROR,
				(errcode(ERRCODE_INVALID_TABLESAMPLE_ARGUMENT),
				 errmsg("sample percentage must be between 0 and 100")));

	/*
	 * The cutoff is sample probability times (PG_UINT32_MAX + 1); we have to
	 * store that as a uint64, of course.  Note that this gives strictly
	 * correct behavior at the limits of zero or one probability.
	 */
	dcutoff = rint(((double) PG_UINT32_MAX + 1) * percent / 100);
	sampler->cutoff = (uint64) dcutoff;
	sampler->seed = seed;
	sampler->lt = InvalidOffsetNumber;

	/*
	 * Use bulkread, since we're scanning all pages.  But pagemode visibility
	 * checking is a win only at larger sampling fractions.  The 25% cutoff
	 * here is based on very limited experimentation.
	 */
	node->use_bulkread = true;
	node->use_pagemode = (percent >= 25);
}

/*
 * Select next sampled tuple in current block.
 *
 * It is OK here to return an offset without knowing if the tuple is visible
 * (or even exists).  The reason is that we do the coinflip for every tuple
 * offset in the table.  Since all tuples have the same probability of being
 * returned, it doesn't matter if we do extra coinflips for invisible tuples.
 *
 * When we reach end of the block, return InvalidOffsetNumber which tells
 * SampleScan to go to next block.
 */
static OffsetNumber
bernoulli_nextsampletuple(SampleScanState *node,
						  BlockNumber blockno,
						  OffsetNumber maxoffset)
{
	BernoulliSamplerData *sampler = (BernoulliSamplerData *) node->tsm_state;
	OffsetNumber tupoffset = sampler->lt;
	uint32		hashinput[3];

	/* Advance to first/next tuple in block */
	if (tupoffset == InvalidOffsetNumber)
		tupoffset = FirstOffsetNumber;
	else
		tupoffset++;

	/*
	 * We compute the hash by applying hash_any to an array of 3 uint32's
	 * containing the block, offset, and seed.  This is efficient to set up,
	 * and with the current implementation of hash_any, it gives
	 * machine-independent results, which is a nice property for regression
	 * testing.
	 *
	 * These words in the hash input are the same throughout the block:
	 */
	hashinput[0] = blockno;
	hashinput[2] = sampler->seed;

	/*
	 * Loop over tuple offsets until finding suitable TID or reaching end of
	 * block.
	 */
	for (; tupoffset <= maxoffset; tupoffset++)
	{
		uint32		hash;

		hashinput[1] = tupoffset;

		hash = DatumGetUInt32(hash_any((const unsigned char *) hashinput,
									   (int) sizeof(hashinput)));
		if (hash < sampler->cutoff)
			break;
	}

	if (tupoffset > maxoffset)
		tupoffset = InvalidOffsetNumber;

	sampler->lt = tupoffset;

	return tupoffset;
}

redpajama_set_name: RedPajamaGithub


text:
package org.adg.learning.algorithm.leetcode;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;

import org.adg.learning.algorithm.leetcode.utils.TreeUtils;
import org.adg.learning.algorithm.leetcode.utils.TreeUtils.TreeNode;

//538. Convert BST to Greater Tree
//Easy
//
//1767
//
//112
//
//Add to List
//
//Share
//Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus sum of all keys greater than the original key in BST.
//
//Example:
//
//Input: The root of a Binary Search Tree like this:
//              5
//            /   \
//           2     13
//
//Output: The root of a Greater Tree like this:
//             18
//            /   \
//          20     13
//Note: This question is the same as 1038: https://leetcode.com/problems/binary-search-tree-to-greater-sum-tree/
//
//Accepted
//112,651
//Submissions
//207,413
class Solution0538_convertBST {

	public TreeNode convertBST(TreeNode root) {
		if (null == root) {
			return root;
		}

		int[] sum = new int[1];
		sum[0] = 0;
		postOrder(sum, root);

		return root;
	}

	private void postOrder(int[] sum, TreeNode root) {
		if (null != root.right) {
			postOrder(sum, root.right);
		}
		sum[0] += root.val;
		root.val = sum[0];
		if (null != root.left) {
			postOrder(sum, root.left);
		}
	}

	public TreeNode convertBST1(TreeNode root) {
		if (null == root) {
			return root;
		}

		Stack<TreeNode> stack = new Stack<>();
		preOrder(stack, root);

		TreeNode prev = null;
		while (stack.size() > 0) {
			TreeNode node = stack.pop();
			if (prev != null) {
				node.val += prev.val;
			}
			prev = node;
		}

		return root;
	}

	private void preOrder(Stack<TreeNode> stack, TreeNode node) {
		if (node.left != null) {
			preOrder(stack, node.left);
		}
		stack.push(node);
		if (node.right != null) {
			preOrder(stack, node.right);
		}
	}

	public static void main(String args[]) {
		Solution0538_convertBST inst = new Solution0538_convertBST();
		List<Integer> list = new LinkedList<>(Arrays.asList(new Integer[] { 2, 0, 3, -4, 1 }));
//		List<Integer> list = new LinkedList<>(Arrays.asList(new Integer[] { 1, 0, 4, -2, null, 3 }));
		TreeNode root = TreeUtils.makeTree(list);
		TreeUtils.printTree(root);
		TreeNode newroot = inst.convertBST(root);
		System.out.println(String.format("input: %s output: %s", Arrays.toString(list.toArray()), newroot.val));
		TreeUtils.printTree(newroot);
	}
}


text:
#ifndef WEBRTC_MODULES_VIDEO_CODING_TEST_RTP_PLAYER_H_
#define WEBRTC_MODULES_VIDEO_CODING_TEST_RTP_PLAYER_H_

#include <string>
#include <vector>

#include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp_defines.h"
#include "webrtc/modules/video_coding/main/interface/video_coding_defines.h"

namespace webrtc {
class Clock;

namespace rtpplayer {

class PayloadCodecTuple {
 public:
  PayloadCodecTuple(uint8_t payload_type, const std::string& codec_name,
                    VideoCodecType codec_type)
      : name_(codec_name),
        payload_type_(payload_type),
        codec_type_(codec_type) {
  }

  const std::string& name() const { return name_; }
  uint8_t payload_type() const { return payload_type_; }
  VideoCodecType codec_type() const { return codec_type_; }

 private:
  std::string name_;
  uint8_t payload_type_;
  VideoCodecType codec_type_;
};

typedef std::vector<PayloadCodecTuple> PayloadTypes;
typedef std::vector<PayloadCodecTuple>::const_iterator PayloadTypesIterator;

// Implemented by something that can provide RTP packets, for instance a file
// format parser such as the rtp_file_reader or the pcap_file_reader.
class RtpPacketSourceInterface {
 public:
  virtual ~RtpPacketSourceInterface() {}

  // Read next RTP packet into buffer pointed to by rtp_data. On call, 'length'
  // field must be filled in with the size of the buffer. The actual size of
  // the packet is available in 'length' upon returning. Time in milliseconds
  // from start of stream is returned in 'time_ms'.
  virtual int NextPacket(uint8_t* rtp_data, uint32_t* length,
                         uint32_t* time_ms) = 0;
};

// Implemented by RtpPlayer and given to client as a means to retrieve
// information about a specific RTP stream.
class RtpStreamInterface {
 public:
  virtual ~RtpStreamInterface() {}

  // Ask for missing packets to be resent.
  virtual void ResendPackets(const uint16_t* sequence_numbers,
                             uint16_t length) = 0;

  virtual uint32_t ssrc() const = 0;
  virtual const PayloadTypes& payload_types() const = 0;
};

// Implemented by a sink. Wraps RtpData because its d-tor is protected.
class PayloadSinkInterface : public RtpData {
 public:
  virtual ~PayloadSinkInterface() {}
};

// Implemented to provide a sink for RTP data, such as hooking up a VCM to
// the incoming RTP stream.
class PayloadSinkFactoryInterface {
 public:
  virtual ~PayloadSinkFactoryInterface() {}

  // Return NULL if failed to create sink. 'stream' is guaranteed to be
  // around for as long as the RtpData. The returned object is owned by
  // the caller (RtpPlayer).
  virtual PayloadSinkInterface* Create(RtpStreamInterface* stream) = 0;
};

// The client's view of an RtpPlayer.
class RtpPlayerInterface {
 public:
  virtual ~RtpPlayerInterface() {}

  virtual int NextPacket(int64_t timeNow) = 0;
  virtual uint32_t TimeUntilNextPacket() const = 0;
  virtual void Print() const = 0;
};

RtpPlayerInterface* Create(const std::string& inputFilename,
    PayloadSinkFactoryInterface* payloadSinkFactory, Clock* clock,
    const PayloadTypes& payload_types, float lossRate, uint32_t rttMs,
    bool reordering);

}  // namespace rtpplayer
}  // namespace webrtc

#endif // WEBRTC_MODULES_VIDEO_CODING_TEST_RTP_PLAYER_H_


text:
<?php
namespace Inventory\Services;

use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Inventory\Services\SparepartService;

/*
 * @author nmt
 *
 */
class SparepartServiceFactory implements FactoryInterface
{

    /**
     *
     * {@inheritdoc}
     * @see \Zend\ServiceManager\FactoryInterface::createService()
     */
    public function createService(ServiceLocatorInterface $serviceLocator)
    {
        $sv = new SparepartService();

        return $sv;
    }
}


text:
package greycatTest.internal;

import greycat.Graph;
import greycat.Node;
import greycat.plugin.NodeFactory;
import greycat.plugin.Plugin;
import org.junit.Assert;
import org.junit.Test;
import greycat.Callback;
import greycat.GraphBuilder;
import greycat.scheduler.NoopScheduler;
import greycat.base.BaseNode;

public class NodeFactoryTest {

    static final String NAME = "HelloWorldNode";

    interface ExNode extends Node {
        String sayHello();
    }

    class ExNodeImpl extends BaseNode implements ExNode {

        public ExNodeImpl(long p_world, long p_time, long p_id, Graph p_graph) {
            super(p_world, p_time, p_id, p_graph);
        }

        @Override
        public Object get(String name) {
            if (name.equals("hello")) {
                return "world";
            }
            return super.get(name);
        }

        @Override
        public String sayHello() {
            return "HelloWorld";
        }
    }

    class ExNodePlugin implements Plugin {

        @Override
        public void start(Graph graph) {
            graph.nodeRegistry().getOrCreateDeclaration(NAME).setFactory(new NodeFactory() {
                @Override
                public Node create(long world, long time, long id, Graph graph) {
                    return new ExNodeImpl(world, time, id, graph);
                }
            });
        }

        @Override
        public void stop() {

        }

    }

    @Test
    public void heapTest() {
        test(new GraphBuilder().withScheduler(new NoopScheduler()).withPlugin(new ExNodePlugin()).build());
    }

    /**
     * @ignore ts
     */
    @Test
    public void offHeapTest() {
        /*
        OffHeapByteArray.alloc_counter = 0;
        OffHeapDoubleArray.alloc_counter = 0;
        OffHeapLongArray.alloc_counter = 0;
        OffHeapStringArray.alloc_counter = 0;

        Unsafe.DEBUG_MODE = true;

        test(new GraphBuilder().withScheduler(new NoopScheduler()).withOffHeapMemory().withMemorySize(10000).saveEvery(20).withPlugin(new BasePlugin().declareNodeType(NAME, new NodeFactory() {
            @Override
            public Node create(long world, long time, long id, Graph graph) {
                return new ExNodeImpl(world, time, id, graph);
            }
        })).build());

        Assert.assertTrue(OffHeapByteArray.alloc_counter == 0);
        Assert.assertTrue(OffHeapDoubleArray.alloc_counter == 0);
        Assert.assertTrue(OffHeapLongArray.alloc_counter == 0);
        Assert.assertTrue(OffHeapStringArray.alloc_counter == 0);
        */
    }

    private void test(final Graph graph) {

        graph.connect(new Callback<Boolean>() {
            @Override
            public void on(Boolean result) {
                Node specializedNode = graph.newTypedNode(0, 0, NAME);

                Assert.assertEquals(specializedNode.nodeTypeName(), "HelloWorldNode");

                String hw = (String) specializedNode.get("hello");
                Assert.assertTrue(hw.equals("world"));

                Node parent = graph.newNode(0, 0);
                parent.addToRelation("children", specializedNode);
                parent.traverse("children", new Callback<Node[]>() {
                    @Override
                    public void on(Node[] result) {
                        Assert.assertEquals("HelloWorld", ((ExNode) result[0]).sayHello());
                    }
                });

                specializedNode.free();
                graph.disconnect(null);
            }
        });
    }

}


text:
package server

import (
	"crypto/tls"
	"crypto/x509"
	"encoding/pem"
	"fmt"
	"net"
	"net/http"
	"sync"

	"github.com/armon/go-proxyproto"
	"github.com/opentracing/opentracing-go"
	"github.com/pkg/errors"
	log "github.com/sirupsen/logrus"
	"github.com/vulcand/route"
	"github.com/vulcand/vulcand/conntracker"
	"github.com/vulcand/vulcand/engine"
	"github.com/vulcand/vulcand/graceful"
	"github.com/vulcand/vulcand/proxy"
	"github.com/vulcand/vulcand/proxy/tracing"
	"github.com/vulcand/vulcand/stapler"
	"golang.org/x/crypto/acme"
	"golang.org/x/crypto/acme/autocert"
	"golang.org/x/crypto/ocsp"
)

type getCertificateFunc func(*tls.ClientHelloInfo) (*tls.Certificate, error)

// T contains all that is necessary to run the HTTP(s) server. Note that it is
// not thread safe and therefore requires external synchronization.
type T struct {
	lsnCfg  engine.Listener
	router  http.Handler
	stapler stapler.Stapler
	connTck conntracker.ConnectionTracker
	serveWg *sync.WaitGroup

	autoCertCache autocert.Cache

	srv          *graceful.Server
	scopedRouter http.Handler
	options      proxy.Options
	state        int
}

// New creates a new server instance.
func New(lsnCfg engine.Listener, router http.Handler, stapler stapler.Stapler,
	connTck conntracker.ConnectionTracker, autoCertCache autocert.Cache, wg *sync.WaitGroup,
	options proxy.Options,
) (*T, error) {

	scopedRouter, err := newScopeRouter(lsnCfg.Scope, router)
	if err != nil {
		return nil, err
	}
	return &T{
		lsnCfg:  lsnCfg,
		router:  router,
		options: options,

		stapler:       stapler,
		connTck:       connTck,
		autoCertCache: autoCertCache,
		serveWg:       wg,
		scopedRouter:  scopedRouter,
		state:         srvStateInit,
	}, nil
}

func (s *T) Key() engine.ListenerKey {
	return s.lsnCfg.Key()
}

func (s *T) Address() engine.Address {
	return s.lsnCfg.Address
}

func (s *T) GetFile() (*proxy.FileDescriptor, error) {
	if !s.hasListeners() || s.srv == nil {
		return nil, nil
	}
	file, err := s.srv.GetFile()
	if err != nil {
		return nil, err
	}
	return &proxy.FileDescriptor{
		File:    file,
		Address: s.lsnCfg.Address,
	}, nil
}

func (s *T) String() string {
	return fmt.Sprintf("srv(%v, %v)", s.state, &s.lsnCfg)
}

// Start starts the server to handle a list of specified hosts.
func (s *T) Start(hostCfgs map[engine.HostKey]engine.Host) error {
	log.Infof("%s start", s)
	switch s.state {
	case srvStateInit:
		lsn, err := net.Listen(s.lsnCfg.Address.Network, s.lsnCfg.Address.Address)
		if err != nil {
			return err
		}

		lsn = &graceful.TCPKeepAliveListener{TCPListener: lsn.(*net.TCPListener)}

		if s.isProxyProto() {
			lsn = &proxyproto.Listener{
				Listener:           lsn,
				ProxyHeaderTimeout: s.options.ReadTimeout,
			}
		}

		if s.isTLS() {
			config, err := s.newTLSCfg(hostCfgs)
			if err != nil {
				return err
			}
			lsn = graceful.NewTLSListener(lsn, config)
		}
		s.srv = graceful.NewWithOptions(
			graceful.Options{
				Server:       s.newHTTPServer(),
				Listener:     lsn,
				StateHandler: s.connTck.RegisterStateChange,
			})
		s.state = srvStateActive
		s.serveWg.Add(1)
		go s.serve(s.srv)
		return nil
	case srvStateHijacked:
		s.state = srvStateActive
		s.serveWg.Add(1)
		go s.serve(s.srv)
		return nil
	}
	return errors.Errorf("%v Calling start in unsupported state", s)
}

// Shutdown gracefuly terminates the server if running.
func (s *T) Shutdown() {
	if s.srv != nil {
		s.srv.Close()
	}
}

func (s *T) Update(lsnCfg engine.Listener, hostCfgs map[engine.HostKey]engine.Host) error {
	// We can not listen for different protocols on the same socket
	if s.lsnCfg.Protocol != lsnCfg.Protocol {
		return errors.Errorf("conflicting protocol %s and %s", s.lsnCfg.Protocol, lsnCfg.Protocol)
	}
	if lsnCfg.Scope == s.lsnCfg.Scope && (&lsnCfg).SettingsEquals(&s.lsnCfg) {
		return nil
	}

	log.Infof("%v update %v", s, &lsnCfg)
	scopedRouter, err := newScopeRouter(lsnCfg.Scope, s.router)
	if err != nil {
		return errors.Wrap(err, "failed to create scoped handler")
	}
	s.scopedRouter = scopedRouter
	s.lsnCfg = lsnCfg
	if err := s.reloadTLSCfg(hostCfgs); err != nil {
		return errors.Wrap(err, "failed to reload TLS config")
	}
	return nil
}

func (s *T) TakeFile(fd *proxy.FileDescriptor, hostCfgs map[engine.HostKey]engine.Host) error {
	log.Infof("%s takeFile %v", s, fd)

	lsn, err := fd.ToListener()
	if err != nil {
		return errors.Wrapf(err, "failed to obtain listener for %v", fd)
	}

	tcpLsn, ok := lsn.(*net.TCPListener)
	if !ok {
		return errors.Errorf("bad listener type %T", lsn)
	}
	lsn = &graceful.TCPKeepAliveListener{TCPListener: tcpLsn}

	if s.isProxyProto() {
		lsn = &proxyproto.Listener{
			Listener:           lsn,
			ProxyHeaderTimeout: s.options.ReadTimeout,
		}
	}

	if s.isTLS() {
		config, err := s.newTLSCfg(hostCfgs)
		if err != nil {
			return errors.Wrap(err, "failed to create TLS config")
		}
		lsn = graceful.NewTLSListener(lsn, config)
	}

	s.srv = graceful.NewWithOptions(
		graceful.Options{
			Server:       s.newHTTPServer(),
			Listener:     lsn,
			StateHandler: s.connTck.RegisterStateChange,
		})
	s.state = srvStateHijacked
	return nil
}

// OnHostsUpdated is supposed to be called whenever a list of hosts is updated,
// or an OCSP notification for a host is received.
func (s *T) OnHostsUpdated(hostCfgs map[engine.HostKey]engine.Host) {
	if !s.isTLS() {
		return
	}
	if err := s.reloadTLSCfg(hostCfgs); err != nil {
		log.Errorf("Failed to reload TLS config: %v", err)
	}
}

func (s *T) reloadTLSCfg(hostCfgs map[engine.HostKey]engine.Host) error {
	if s.state != srvStateActive {
		return nil
	}

	gracefulServer, err := s.srv.HijackListener(
		s.newHTTPServer(),
		func(lsn net.Listener) (net.Listener, error) {
			lsn = &graceful.TCPKeepAliveListener{TCPListener: lsn.(*net.TCPListener)}

			if s.isProxyProto() {
				lsn = &proxyproto.Listener{
					Listener:           lsn,
					ProxyHeaderTimeout: s.options.ReadTimeout,
				}
			}

			if s.isTLS() {
				tlsCfg, err := s.newTLSCfg(hostCfgs)
				if err != nil {
					return nil, errors.Wrap(err, "failed to create TLS config")
				}
				lsn = graceful.NewTLSListener(lsn, tlsCfg)
			}
			return lsn, nil
		})
	if err != nil {
		return err
	}
	s.serveWg.Add(1)
	go s.serve(gracefulServer)

	s.srv.Close()
	s.srv = gracefulServer
	return nil
}

func (s *T) newHTTPServer() *http.Server {
	return &http.Server{
		Handler:        s.scopedRouter,
		ReadTimeout:    s.options.ReadTimeout,
		WriteTimeout:   s.options.WriteTimeout,
		MaxHeaderBytes: s.options.MaxHeaderBytes,
	}
}

func (s *T) isTLS() bool {
	return s.lsnCfg.Protocol == engine.HTTPS
}

func (s *T) isProxyProto() bool {
	return s.lsnCfg.ProxyProtocol == engine.PROXY_PROTO_V1
}

func (s *T) newTLSCfg(hostCfgs map[engine.HostKey]engine.Host) (*tls.Config, error) {
	config, err := s.lsnCfg.TLSConfig()
	if err != nil {
		return nil, err
	}

	if config.NextProtos == nil {
		// "h2" is required in order to enable HTTP 2: https://golang.org/src/net/http/server.go
		config.NextProtos = []string{"h2", "http/1.1"}
	}

	defaultHostName := ""
	pairs := map[string]tls.Certificate{}
	getCertFuncs := map[string]getCertificateFunc{}

	for _, hostCfg := range hostCfgs {
		// Capture default hostname for later use
		if hostCfg.Settings.Default {
			defaultHostName = hostCfg.Name
		}

		// If KeyPair-based cert
		if hostCfg.Settings.KeyPair != nil {

			//If autocert is also set, log a warning but proceed with non-autocert
			if hostCfg.Settings.AutoCert != nil {
				log.Warningf("Host %v has a KeyPair and AutoCert enabled. KeyPair takes precedence. Autocert generation disabled.", hostCfg.Name)
			}

			// Get the certificate for this host out of settings and remember it
			cert, err := certForHost(hostCfg)
			if err != nil {
				log.Errorf("Unable to get certificate from host %s due to error: %v.", hostCfg.Name, err)
				continue
			}

			//Staple the OCSP response to the cert
			ocspStapleToCert(s.stapler, hostCfg, &cert)

			pairs[hostCfg.Name] = cert

		} else if hostCfg.Settings.AutoCert != nil {

			// Generate a certificate-getting function for this host which will be called later as needed.
			// This function will staple OCSP response when cert is generated.
			getCertFunc, err := certFuncForHost(hostCfg, s.autoCertCache, s.stapler)
			if err != nil {
				log.Errorf("Unable to generate GetCertificate function for host %s due to error: %v.", hostCfg.Name, err)
				continue
			}
			getCertFuncs[hostCfg.Name] = getCertFunc
		}
	}

	// Convert the hostname->cert mappings into an array with defaultHostName's cert coming first
	config.Certificates, err = tlsCertArray(pairs, defaultHostName)
	if err != nil {
		return nil, err
	}

	config.BuildNameToCertificate()

	// Generate an aggergate GetCertificate that calls individual host's GetCertificate generated above.
	config.GetCertificate = getCertFuncAggregate(getCertFuncs)

	return config, nil
}

func (s *T) hasListeners() bool {
	return s.state == srvStateActive || s.state == srvStateHijacked
}

func (s *T) serve(srv *graceful.Server) {
	defer s.serveWg.Done()
	log.Infof("%s serve", s)
	srv.ListenAndServe()
	log.Infof("%v stop", s)
}

type srvState int

const (
	srvStateInit     = iota // server has been created
	srvStateActive   = iota // server is active and is serving requests
	srvStateHijacked = iota // server has hijacked listeners from other server
)

func (s srvState) String() string {
	switch s {
	case srvStateInit:
		return "init"
	case srvStateActive:
		return "active"
	case srvStateHijacked:
		return "hijacked"
	}
	return "undefined"
}

func newScopeRouter(scope string, router http.Handler) (http.Handler, error) {
	if scope == "" {
		if opentracing.IsGlobalTracerRegistered() {
			return tracing.NewMiddleware(router), nil
		}
		return router, nil
	}

	scopedRouter := route.NewMux()
	scopedRouter.SetNotFound(proxy.DefaultNotFound)
	if err := scopedRouter.Handle(scope, router); err != nil {
		return nil, err
	}

	if opentracing.IsGlobalTracerRegistered() {
		return tracing.NewMiddleware(scopedRouter), nil
	}
	return scopedRouter, nil
}

// Returns a GetCertificate function for this host based on AutoCert settings,
// by generating an autocert manager for the host.
// It optionally will staple the OCSP response to the cert when it is generated,
// if OCSP stapling is enabled for the host.
func certFuncForHost(hostCfg engine.Host, autoCertCache autocert.Cache, s stapler.Stapler) (getCertificateFunc, error) {
	ac := hostCfg.Settings.AutoCert

	// Each host gets its own Autocert Manager - this allows individual
	// certs to use different autocert authorities, as well as auth keys
	autoCertMgr := &autocert.Manager{
		Prompt:     autocert.AcceptTOS,
		Cache:      autoCertCache,
		HostPolicy: autocert.HostWhitelist(hostCfg.Name),
		Email:      ac.Email,
	}

	if ac.RenewBefore > 0 {
		autoCertMgr.RenewBefore = ac.RenewBefore
	}

	// if either directory or key are non-empty, we need to generate
	// a custom ACME client to override either.
	if ac.Key != "" || ac.DirectoryURL != "" {

		// If DirectoryURL is empty, the default Let's Encrypt URL will be picked.
		autoCertMgr.Client = &acme.Client{
			DirectoryURL: ac.DirectoryURL,
		}

		// If Key is non-empty, then decode it as RSA or EC which are the only two keys
		// we support. Go's crypto library doesn't support a generic function to provide back
		// a private key interface.
		if ac.Key != "" {
			block, _ := pem.Decode([]byte(ac.Key))
			if block == nil {
				return nil, fmt.Errorf("Autocert Key PEM Block for Host %s is invalid.", hostCfg.Name)
			} else if block.Type == "RSA PRIVATE KEY" {
				rsaPrivateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
				if err != nil {
					return nil, errors.Wrapf(err, "Error parsing Autocert Key block of type %s, for Host %s, as an RSA Private Key.", block.Type, hostCfg.Name)
				}
				autoCertMgr.Client.Key = rsaPrivateKey
			} else if block.Type == "EC PRIVATE KEY" {
				ecPrivateKey, err := x509.ParseECPrivateKey(block.Bytes)
				if err != nil {
					return nil, errors.Wrapf(err, "Error parsing Autocert Key block of type %s, for Host %s, as an ECDSA Private Key.", block.Type, hostCfg.Name)
				}
				autoCertMgr.Client.Key = ecPrivateKey
			} else {
				return nil, fmt.Errorf("AutoCert Private Key for Host %s is of unrecognized type: %s. Supported types"+
					"are RSA PRIVATE KEY and EC PRIVATE KEY.", hostCfg.Name, block.Type)
			}
		}
	}

	getCertFuncForStapling := stapler.WithGetCertFunc(hostCfg.Name, stapler.GetCertificateFunc(autoCertMgr.GetCertificate))

	// Wrap the GetCert for this host, so we can generate and staple
	// an optional OCSP response when requested.
	stapledGetCert := func(info *tls.ClientHelloInfo) (*tls.Certificate, error) {
		keyPair, err := autoCertMgr.GetCertificate(info)
		ocspStapleToCert(s, hostCfg, keyPair, getCertFuncForStapling)
		return keyPair, err
	}

	return stapledGetCert, nil
}

// Generate an aggregate GetCertificate function over GetCertificate functions for
// each host that we generated using the certFuncForHost function above.
// This allows all of those functions to masquerade as one uber function that can
// get a certificate (through autocert) for any host.
func getCertFuncAggregate(getCertFuncs map[string]getCertificateFunc) getCertificateFunc {
	return func(info *tls.ClientHelloInfo) (*tls.Certificate, error) {
		if getCertificateFunc, ok := getCertFuncs[info.ServerName]; ok {
			// We have a get certificate function for this host - allow AutoCertManager to
			// provide this one, in case there's expiry/renewal to be done.
			cert, err := getCertificateFunc(info)
			if err != nil {
				log.Errorf("Failed to generate Autocert for ServerName: %s. Error: %v.", info.ServerName, err)
			}
			return cert, err
		} else {
			return nil, nil
		}
	}
}

func tlsCertArray(pairs map[string]tls.Certificate, defaultHostName string) ([]tls.Certificate, error) {
	arr := make([]tls.Certificate, 0, len(pairs))
	if defaultHostName != "" {
		keyPair, ok := pairs[defaultHostName]
		if !ok {
			return nil, errors.Errorf("default host '%s' certificate is not passed", defaultHostName)
		}
		arr = append(arr, keyPair)
	}

	for hostName, keyPair := range pairs {
		if hostName != defaultHostName {
			arr = append(arr, keyPair)
		}
	}
	return arr, nil
}

func ocspStapleToCert(stapler stapler.Stapler, hostCfg engine.Host, keyPair *tls.Certificate, opts ...stapler.StapleHostOption) {
	if !hostCfg.Settings.OCSP.Enabled {
		return
	}

	log.Infof("OCSP is enabled for %v, resolvers: %v", hostCfg, hostCfg.Settings.OCSP.Responders)

	r, err := stapler.StapleHost(&hostCfg, opts...)

	if err != nil {
		log.Warningf("Failed to staple %v, error %v", hostCfg, err)
		return
	}

	if r.Response.Status != ocsp.Good && r.Response.Status != ocsp.Revoked {
		log.Warningf("Got undefined status from OCSP responder: %v", r.Response.Status)
		return
	}

	keyPair.OCSPStaple = r.Staple
}

// Returns a certificate based on a hosts KeyPair settings.
func certForHost(hostCfg engine.Host) (tls.Certificate, error) {
	c := hostCfg.Settings.KeyPair
	cert, err := tls.X509KeyPair(c.Cert, c.Key)
	if err != nil {
		return tls.Certificate{}, err
	}
	return cert, nil
}


text:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace UIAssistant.Plugin.HitaHint
{
    /// <summary>
    /// Usage.xaml の相互作用ロジック
    /// </summary>
    public partial class Usage : UserControl
    {
        public Usage()
        {
            InitializeComponent();
            DataContext = HitaHint.Settings;
        }
    }
}


text:
define(function(require){
  	var Vue = require("vue");
  	var tpl = require("text!component/vue/header.html!strip");
  	return Vue.extend({
		template: tpl,
		methods:{
		},
		props: ['data'],
		data: function(){
			return {
				pageTitle: ('string' == typeof this.data.pageTitle)?this.data.pageTitle:'宜生CMS',
				menuList: this.data.menuList
			};
		},
		components:{
		}
	});
});


text:
#include "analysisManager.h"

namespace PSim {

analysisManager::analysisManager(string tName, systemState* state) {
	trialName = tName;
	int nParticles = state->nParticles;
	xStart = new float [nParticles];
	yStart = new float [nParticles];
	zStart = new float [nParticles];
	xPBC = new int[nParticles];
	yPBC = new int[nParticles];
	zPBC = new int[nParticles];
	counter = 0;
	boxSize = state->boxSize;

	for (int i = 0; i < nParticles; i++)
	{
		xStart[i] = 0;
		yStart[i] = 0;
		zStart[i] = 0;
		xPBC[i] = 0;
		yPBC[i] = 0;
		zPBC[i] = 0;
	}
}

void analysisManager::writeInitialState(particle** particles, systemState* state) {
	int nParticles = state->nParticles;
	initDisplacementTracker(particles, nParticles);
	writeInitTemp(particles, nParticles);
	writeSystem(particles, nParticles, trialName + "/initialState");
	std::string movName = trialName + "/movie/system-initial";
	writeSystemXYZ(particles, nParticles, true, 0, movName);
}

void analysisManager::writeRunTimeState(particle** particles, systemState* state) {
	//Output a snapshot every second.
	if ((counter % state->outputFreq) == 0) {
		if (state->currentTime > 0) {
			PSim::util::clearLines(-1);
		}
		writeSystemState(particles, state->nParticles, state->currentTime);
	} else {
		updateTracker(particles, state->nParticles);
	}
	counter++;
}

void analysisManager::writeFinalState(particle** particles, systemState* state) {
	writeSystem(particles, state->nParticles, trialName + "/finalState");
}

}


text:
from abc import ABCMeta, abstractmethod

class StockCriteria:
    __metaclass__ = ABCMeta

    def __init__(self):
        pass

    @abstractmethod
    def meet(self, fin, log=None):
        """
        Whether meet the stock criteria or not
        :param fin: the morningstar financial object of the stock
        :type fin: financial.Financial
        :param log: the log information
        :type log: pyvalue.log_info.LogInfo
        :return: whether the criteria is meet or not
        """
        raise NotImplementedError()

class StockScoreCriteria(StockCriteria):
    def __init__(self, scorer, threshold=0):
        """

        :param scorer: the scorer used by this filter
        :type scorer: pyvalue.stock_scorer.StockScorer
        :param threshold: the threshold for the scorer
        :type threshold: float
        """
        self._scorer = scorer
        self._threshold = threshold

    def meet(self, fin, log=None):
        return self._scorer.score(fin, log) >= self._threshold


text:
<?php

namespace sheepguru\core\types;

use InvalidArgumentException;

/**
 * A class that contains an integer and a string
 */
class IntStr
{
  /**
   * Int value
   * @var int
   */
  protected $int = 0;

  /**
   * String value
   * @var string
   */
  protected $str = '';

  /**
   * Create a new IntStr instance
   * @param int $int Int
   * @param string $str String
   * @throws InvalidArgumentException if int is not an integer
   */
  public static function create( $int, $str )
  {
    if ( !EDataType::isInt( $int ) )
      throw new InvalidArgumentException( 'int must be an integer' );

    if ( empty( $str ) )
      $str = (string)$int;

    $c = __CLASS__;
    return new $c( $int, $str );
  }

  /**
   * Create a new IntStr instance
   * @param int $int Int
   * @param string $str String
   */
  protected function __construct( $int, $str )
  {
    $this->int = $int;
    $this->str = $str;
  }

  /**
   * Retrieve the integer value
   * @param mixed $x Arguments (unused)
   * @return int value
   */
  public function __invoke( $x = 0 )
  {
    return $this->int;
  }

  /**
   * Retrieve the string value
   * @return string string
   */
  public function __toString()
  {
    return $this->str;
  }

}


text:
include_recipe "nodejs"
include_recipe "git"
include_recipe "zeromq"

user node[:ce_delta_hub][:user] do
end

git "#{node[:ce_delta_hub][:destination]}" do
  user node[:ce_delta_hub][:user]
  repository node[:ce_delta_hub][:repository]
  destination node[:ce_delta_hub][:destination]
  not_if { ::FileTest.exists?(node[:ce_delta_hub][:destination]) }
end

file "#{node[:ce_delta_hub][:destination]}/config.json" do
  owner node[:ce_delta_hub][:user]
  content <<-EOH
{
  "commission": {
    "account": #{node[:ce_delta_hub][:commission][:account]}
  },
  "ce-front-end": {
    "stream": #{node[:ce_delta_hub][:ce_front_end][:stream]},
    "state": #{node[:ce_delta_hub][:ce_front_end][:state]}
  },
  "ce-engine": {
    "stream": #{node[:ce_delta_hub][:ce_engine][:stream]},
    "state": #{node[:ce_delta_hub][:ce_engine][:state]}
  }
}
  EOH
end

bash "start ce-delta-hub server" do
  code <<-EOH
    su -l #{node[:ce_delta_hub][:user]} -c "cd #{node[:ce_delta_hub][:destination]} && npm install && npm start"
  EOH
end


text:
import { HostStatus } from './../models/host-status';
import { TryFunctionsService } from './try-functions.service';
import { ApiProxy } from './../models/api-proxy';
import { AuthSettings } from './../models/auth-settings';
import { HostingEnvironment } from './../models/arm/hosting-environment';
import { SiteService } from './slots.service';
import { FunctionAppEditMode } from 'app/shared/models/function-app-edit-mode';
import { SiteConfig } from './../models/arm/site-config';
import { PortalResources } from './../models/portal-resources';
import { BroadcastEvent } from 'app/shared/models/broadcast-event';
import { TranslateService } from '@ngx-translate/core';
import { AiService } from 'app/shared/services/ai.service';
import { NoCorsHttpService } from './../no-cors-http-service';
import { BroadcastService } from './broadcast.service';
import { GlobalStateService } from './global-state.service';
import { FunctionAppContext } from './functions-service';
import { Observable } from 'rxjs/Observable';
import { UserService } from './user.service';
import { FunctionsResponse } from './../models/functions-response';
import { ArmUtil } from 'app/shared/Utilities/arm-utils';
import { ConfigService } from './config.service';
import { Site } from './../models/arm/site';
import { ArmObj } from './../models/arm/arm-obj';
import { FunctionInfo } from './../models/function-info';
import { CacheService } from 'app/shared/services/cache.service';
import { Injectable } from '@angular/core';
import { UrlTemplates } from 'app/shared/url-templates';
import { Http, Headers, Response } from '@angular/http';
import { ErrorIds } from '../models/error-ids';
import { ErrorEvent, ErrorType } from '../models/error-event';
import { Constants } from 'app/shared/models/constants';
import * as jsonschema from 'jsonschema';
import { FunctionsVersionInfoHelper} from '../models/functions-version-info';

export interface FunctionAppContext {
    site: ArmObj<Site>;
    scmUrl: string;
    mainSiteUrl: string;
    urlTemplates: UrlTemplates;
    tryFunctionsScmCreds?: string;
    masterKey?: string;
}

@Injectable()
export class FunctionsService {
    private _token: string;

    private _http: NoCorsHttpService;

    constructor(
        private _cacheService: CacheService,
        private _configService: ConfigService,
        private _userService: UserService,
        private _globalStateService: GlobalStateService,
        private _broadcastService: BroadcastService,
        private _ngHttp: Http,
        private _aiService: AiService,
        private _translateService: TranslateService,
        private _siteService: SiteService,
        private _tryFunctionsService: TryFunctionsService) {

        this._http = new NoCorsHttpService(this._cacheService, this._ngHttp, this._broadcastService, this._aiService, this._translateService, () => this._getPortalHeaders());

        this._userService.getStartupInfo()
            .subscribe(info => {
                this._token = info.token;
            });
    }

    getAppContext(siteResourceId: string): Observable<FunctionAppContext> {
        let context: FunctionAppContext;

        return this._cacheService.getArm(siteResourceId)
            .switchMap(r => {
                const site: ArmObj<Site> = r.json();
                const scmUrl = this.getScmUrl(site);
                const mainSiteUrl = this.getMainUrl(site);

                context = {
                    site: site,
                    scmUrl: scmUrl,
                    mainSiteUrl: mainSiteUrl,
                    urlTemplates: new UrlTemplates(scmUrl, mainSiteUrl, ArmUtil.isLinuxApp(site))
                };

                return this._initKeysAndWarmupMainSite(context);
            })
            .map(r => context);
    }

    getFunction(context: FunctionAppContext, functionName: string) {
        return this.getFunctions(context)
            .map(fcs => {
                return fcs && fcs.find(f => f.name.toLowerCase() === functionName.toLowerCase());
            });
    }

    private _initKeysAndWarmupMainSite(context: FunctionAppContext) {
        this._http.post(context.urlTemplates.pingUrl, '')
            .retryWhen(this.retryAntares)
            .subscribe(() => { });

        return this._getHostSecretsFromScm(context);
    }

    getFunctions(context: FunctionAppContext) {
        let fcs: FunctionInfo[];

        return Observable.zip(
            this._cacheService.get(context.urlTemplates.functionsUrl, false, this._getScmSiteHeaders(context))
            .catch(() => this._http.get(context.urlTemplates.functionsUrl, { headers: this._getScmSiteHeaders(context) }))
            .retryWhen(this.retryAntares)
            .map((r: Response) => {
                try {
                    fcs = r.json() as FunctionInfo[];
                    fcs.forEach(fc => fc.context = context);
                    return fcs;
                } catch (e) {
                    // We have seen this happen when kudu was returning JSON that contained
                    // comments because Json.NET is okay with comments in the JSON file.
                    // We can't parse that JSON in browser, so this is just to handle the error correctly.
                    this._broadcastService.broadcast<ErrorEvent>(BroadcastEvent.Error, {
                        message: this._translateService.instant(PortalResources.error_parsingFunctionListReturenedFromKudu),
                        errorId: ErrorIds.deserializingKudusFunctionList,
                        errorType: ErrorType.Fatal,
                        resourceId: context.site.id
                    });
                    this._trackEvent(context, ErrorIds.deserializingKudusFunctionList, {
                        error: e,
                        content: r.text(),
                    });
                    return <FunctionInfo[]>[];
                }
            }),
            this._cacheService.postArm(`${context.site.id}/config/appsettings/list`),
            (functions, appSettings) => ({functions: functions, appSettings: appSettings.json()}))
            .map(result => {
                // For runtime 2.0 we use settings for disabling functionsgit branch
                const appSettings = result.appSettings as ArmObj<any>;
                if (FunctionsVersionInfoHelper.getFuntionGeneration(appSettings.properties[Constants.runtimeVersionAppSettingName]) === 'V2') {
                    result.functions.forEach(f => {
                        const disabledSetting = appSettings.properties[`AzureWebJobs.${f.name}.Disabled`];
                        f.config.disabled = (disabledSetting && disabledSetting.toLocaleLowerCase() === 'true');
                    });
                }
                return result.functions;
            })
            .do(() => this._broadcastService.broadcast<string>(BroadcastEvent.ClearError, ErrorIds.unableToRetrieveFunctionsList),
                (error: FunctionsResponse) => {
                    if (!error.isHandled) {
                        this._broadcastService.broadcast<ErrorEvent>(BroadcastEvent.Error, {
                            message: this._translateService.instant(PortalResources.error_unableToRetrieveFunctionListFromKudu),
                            errorId: ErrorIds.unableToRetrieveFunctionsList,
                            errorType: ErrorType.RuntimeError,
                            resourceId: context.site.id
                        });
                        this._trackEvent(context, ErrorIds.unableToRetrieveFunctionsList, {
                            content: error.text(),
                            status: error.status.toString()
                        });
                    }
                });
    }

    getApiProxies(context: FunctionAppContext) {
        return Observable.zip(
            this._cacheService.get(context.urlTemplates.proxiesJsonUrl, false, this._getScmSiteHeaders(context))
                .catch(() => this._http.get(context.urlTemplates.proxiesJsonUrl, { headers: this._getScmSiteHeaders(context) }))
                .retryWhen(e => e.scan((errorCount: number, err: Response) => {
                    if (err.status === 404 || errorCount >= 10) {
                        throw err;
                    }
                    return errorCount + 1;
                }, 0).delay(200))
                .catch(_ => Observable.of({
                    json: () => { return {}; }
                })),
            this._cacheService.get('assets/schemas/proxies.json', false, this._getPortalHeaders()),
            (p, s) => ({ proxies: p, schema: s.json() })
        ).map(r => {
            let proxies = null;
            try {
                proxies = r.proxies.json();
            } catch (e) {
                this._broadcastService.broadcast<ErrorEvent>(BroadcastEvent.Error, {
                    message: `${this._translateService.instant(PortalResources.error_schemaValidationProxies)}. ${e}`,
                    errorId: ErrorIds.proxySchemaValidationFails,
                    errorType: ErrorType.Fatal,
                    resourceId: context.site.id
                });
                return ApiProxy.fromJson({});
            }

            if (proxies.proxies) {
                const validateResult = jsonschema.validate(proxies, r.schema).toString();

                if (validateResult) {
                    this._broadcastService.broadcast<ErrorEvent>(BroadcastEvent.Error, {
                        message: `${this._translateService.instant(PortalResources.error_schemaValidationProxies)}. ${validateResult}`,
                        errorId: ErrorIds.proxySchemaValidationFails,
                        errorType: ErrorType.Fatal,
                        resourceId: context.site.id
                    });
                    return ApiProxy.fromJson({});
                }
            }
            return ApiProxy.fromJson(proxies);
        });
    }

    getScmUrl(site: ArmObj<Site>) {
        if (this._configService.isStandalone()) {
            return this.getMainUrl(site);
        } else {
            return `https://${site.properties.hostNameSslStates.find(s => s.hostType === 1).name}`;
        }
    }

    getMainUrl(site: ArmObj<Site>) {
        if (this._configService.isStandalone()) {
            return `https://${site.properties.defaultHostName}/functions/${site.name}`;
        } else {
            return `https://${site.properties.defaultHostName}`;
        }
    }

    getFunctionAppEditMode(context: FunctionAppContext): Observable<FunctionAppEditMode> {
        // 4 settings to check here, SourceControl, Visual Studio generated, Slots, FUNCTION_APP_EDIT_MODE
        // editMode (true -> readWrite, false -> readOnly)
        // Table
        // | SourceControl | VS    | Slots | AppSettingValue | EditMode                  |
        // |---------------|-------|-------|-----------------|---------------------------|
        // | true          | true  | Yes   | readWrite       | ReadWriteSourceControlled |
        // | true          | true  | Yes   | readOnly        | ReadOnlySourceControlled  |
        // | true          | true  | Yes   | undefined       | ReadOnlySourceControlled  |
        // | true          | true  | No    | readWrite       | ReadWriteSourceControlled |
        // | true          | true  | No    | readOnly        | ReadOnlySourceControlled  |
        // | true          | true  | No    | undefined       | ReadOnlySourceControlled  |
        // | true          | false | Yes   | readWrite       | ReadWriteSourceControlled |
        // | true          | false | Yes   | readOnly        | ReadOnlySourceControlled  |
        // | true          | false | Yes   | undefined       | ReadOnlySourceControlled  |
        // | true          | false | No    | readWrite       | ReadWriteSourceControlled |
        // | true          | false | No    | readOnly        | ReadOnlySourceControlled  |
        // | true          | false | No    | undefined       | ReadOnlySourceControlled  |
        // | false         | true  | Yes   | readWrite       | ReadWriteVSGenerated      |
        // | false         | true  | Yes   | readOnly        | ReadOnlyVSGenerated       |
        // | false         | true  | Yes   | undefined       | ReadOnlyVSGenerated       |
        // | false         | true  | No    | readWrite       | ReadWriteVSGenerated      |
        // | false         | true  | No    | readOnly        | ReadOnlyVSGenerated       |
        // | false         | true  | No    | undefined       | ReadOnlyVSGenerated       |
        // | false         | false | Yes   | readWrite       | ReadWrite                 |
        // | false         | false | Yes   | readOnly        | ReadOnly                  |
        // | false         | false | Yes   | undefined       | ReadOnlySlots             |
        // | false         | false | No    | readWrite       | ReadWrite                 |
        // | false         | false | No    | readOnly        | ReadOnly                  |
        // | false         | false | No    | undefined       | ReadWrite                 |
        // |_______________|_______|_______|_________________|___________________________|

        return Observable.zip(
            this._checkIfSourceControlEnabled(context.site),
            this._cacheService.postArm(`${context.site.id}/config/appsettings/list`, true),
            SiteService.isSlot(context.site.id)
                ? Observable.of(true)
                : this._siteService.getSlotsList(context.site.id).map(r => r.length > 0),
            this.getFunctions(context),
            (a, b, s, f: FunctionInfo[]) => ({ sourceControlEnabled: a, appSettingsResponse: b, hasSlots: s, functions: f })
        )
            .map(result => {
                const appSettings: ArmObj<any> = result.appSettingsResponse.json();
                const sourceControlled = result.sourceControlEnabled;

                let editModeSettingString: string = appSettings.properties[Constants.functionAppEditModeSettingName] || '';
                editModeSettingString = editModeSettingString.toLocaleLowerCase();
                const vsCreatedFunc = result.functions.find((fc: any) => !!fc.config.generatedBy);
                const hasSlots = result.hasSlots;

                const resolveReadOnlyMode = () => {
                    if (sourceControlled) {
                        return FunctionAppEditMode.ReadOnlySourceControlled;
                    } else if (vsCreatedFunc) {
                        return FunctionAppEditMode.ReadOnlyVSGenerated;
                    } else if (hasSlots) {
                        return FunctionAppEditMode.ReadOnly;
                    } else {
                        return FunctionAppEditMode.ReadOnly;
                    };
                };

                const resolveReadWriteMode = () => {
                    if (sourceControlled) {
                        return FunctionAppEditMode.ReadWriteSourceControlled;
                    } else if (vsCreatedFunc) {
                        return FunctionAppEditMode.ReadWriteVSGenerated;
                    } else if (hasSlots) {
                        return FunctionAppEditMode.ReadWrite;
                    } else {
                        return FunctionAppEditMode.ReadWrite;
                    };
                };

                const resolveUndefined = () => {
                    if (sourceControlled) {
                        return FunctionAppEditMode.ReadOnlySourceControlled;
                    } else if (vsCreatedFunc) {
                        return FunctionAppEditMode.ReadOnlyVSGenerated;
                    } else if (hasSlots) {
                        return FunctionAppEditMode.ReadOnlySlots;
                    } else {
                        return FunctionAppEditMode.ReadWrite;
                    };
                };

                if (editModeSettingString === Constants.ReadWriteMode) {
                    return resolveReadWriteMode();
                } else if (editModeSettingString === Constants.ReadOnlyMode) {
                    return resolveReadOnlyMode();
                } else {
                    return resolveUndefined();
                }
            })
            .catch(() => Observable.of(FunctionAppEditMode.ReadWrite));
    }

    reachableInternalLoadBalancerApp(context: FunctionAppContext, http: CacheService): Observable<boolean> {
        if (context && context.site &&
            context.site.properties.hostingEnvironmentProfile &&
            context.site.properties.hostingEnvironmentProfile.id) {
            return http.getArm(context.site.properties.hostingEnvironmentProfile.id, false, '2016-09-01')
                .mergeMap(r => {
                    const ase: ArmObj<HostingEnvironment> = r.json();
                    if (ase.properties.internalLoadBalancingMode &&
                        ase.properties.internalLoadBalancingMode !== 'None') {
                        return this.pingScmSite(context);
                    } else {
                        return Observable.of(true);
                    }
                });
        } else {
            return Observable.of(true);
        }
    }

    private _isAppStopped(context: FunctionAppContext, http: CacheService): Observable<boolean> {
        return http.getArm(context.site.id)
            .map(s => s.json() as ArmObj<Site>)
            .map(s => s.properties.state !== 'Running');
    }

    /**
     * This method just pings the root of the SCM site. It doesn't care about the response in anyway or use it.
     */
    pingScmSite(context: FunctionAppContext): Observable<boolean> {
        return this._http.get(context.urlTemplates.pingScmSiteUrl, { headers: this._getScmSiteHeaders(context) })
            .map(_ => true)
            .catch(() => Observable.of(false));
    }

    public getAuthSettings(context: FunctionAppContext): Observable<AuthSettings> {
        if (this._tryFunctionsService.functionContainer) {
            return Observable.of({
                easyAuthEnabled: false,
                AADConfigured: false,
                AADNotConfigured: false,
                clientCertEnabled: false
            });
        }

        return this._cacheService.postArm(`${context.site.id}/config/authsettings/list`)
            .map(r => {
                const auth: ArmObj<any> = r.json();
                return {
                    easyAuthEnabled: auth.properties['enabled'] && auth.properties['unauthenticatedClientAction'] !== 1,
                    AADConfigured: auth.properties['clientId'] ? true : false,
                    AADNotConfigured: auth.properties['clientId'] ? false : true,
                    clientCertEnabled: context.site.properties.clientCertEnabled
                };
            });
    }

    private _getHostToken(context: FunctionAppContext) {
        return ArmUtil.isLinuxApp(context.site)
            ? this._http.get(Constants.serviceHost + `api/runtimetoken${context.site.id}`, { headers: this._getPortalHeaders() })
            : this._http.get(context.urlTemplates.scmTokenUrl, { headers: this._getScmSiteHeaders(context) });
    }

    private _getHostSecretsFromScm(context: FunctionAppContext) {
        return this.reachableInternalLoadBalancerApp(context, this._cacheService)
            .concatMap(r => this._isAppStopped(context, this._cacheService).map(s => !s && r))
            .concatMap(reachableAndNotStopped => !reachableAndNotStopped
                ? Observable.of(false)
                : this.getAuthSettings(context)
                    .mergeMap(authSettings => {
                        return authSettings.clientCertEnabled
                            ? Observable.of()
                            : this._getHostToken(context)
                                .retryWhen(this.retryAntares)
                                .map(r => r.json())
                                .mergeMap((token: string) => {
                                    // Call the main site to get the masterKey
                                    // build authorization header
                                    const authHeader = new Headers();
                                    authHeader.append('Authorization', `Bearer ${token}`);
                                    return this._http.get(context.urlTemplates.masterKeyUrl, { headers: authHeader })
                                        .retryWhen(error => error.scan((errorCount: number, err: FunctionsResponse) => {
                                            if (err.isHandled || (err.status < 500 && err.status !== 401) || errorCount >= 30) {
                                                throw err;
                                            } else if (err.status === 503 && errorCount >= 4) {
                                                throw err;
                                            } else {
                                                return errorCount + 1;
                                            }
                                        }, 0).delay(1000))
                                        .do((r: Response) => {
                                            // Since we fall back to kudu above, use a union of kudu and runtime types.
                                            const key: { name: string, value: string } & { masterKey: string } = r.json();
                                            if (key.masterKey) {
                                                context.masterKey = key.masterKey;
                                            } else {
                                                context.masterKey = key.value;
                                            }
                                        });
                                })
                                .map(_ => true)
                                .catch(e => this._checkRuntimeStatus(context).map(_ => null));
                    }));
    }

    fireSyncTrigger(context: FunctionAppContext) {
        const url = context.urlTemplates.syncTriggersUrl;
        this._http.post(url, '', { headers: this._getScmSiteHeaders(context) })
            .subscribe(success => console.log(success), error => console.log(error));
    }

    private _checkIfSourceControlEnabled(site: ArmObj<Site>): Observable<boolean> {
        return this._cacheService.getArm(`${site.id}/config/web`)
            .map(r => {
                const config: ArmObj<SiteConfig> = r.json();
                return !config.properties['scmType'] || config.properties['scmType'] !== 'None';
            })
            .catch(() => Observable.of(false));
    }

    // to talk to scm site
    private _getScmSiteHeaders(context: FunctionAppContext, contentType?: string, token?: string): Headers {
        contentType = contentType || 'application/json';

        const headers = new Headers();
        headers.append('Content-Type', contentType);
        headers.append('Accept', 'application/json,*/*');
        if (!this._globalStateService.showTryView && this._token && !token) {
            headers.append('Authorization', `Bearer ${this._token}`);
        }

        if (this._tryFunctionsService.functionContainer && this._tryFunctionsService.functionContainer.tryScmCred) {
            headers.append('Authorization', `Basic ${this._tryFunctionsService.functionContainer.tryScmCred}`);
        }

        if (context.masterKey) {
            headers.append('x-functions-key', context.masterKey);
        }

        if (token) {
            headers.append('Authorization', `Bearer ${token}`);
        }

        return headers;
    }

    // to talk to Functions Portal
    private _getPortalHeaders(contentType?: string): Headers {
        contentType = contentType || 'application/json';
        const headers = new Headers();
        headers.append('Content-Type', contentType);
        headers.append('Accept', 'application/json,*/*');

        if (this._token) {
            headers.append('client-token', this._token);
            headers.append('portal-token', this._token);
        }

        return headers;
    }

    /**
     * This function is just a wrapper around AiService.trackEvent. It injects default params expected from this class.
     * Currently that's only scmUrl
     * @param params any additional parameters to get added to the default parameters that this class reports to AppInsights
     */
    private _trackEvent(context: FunctionAppContext, name: string, params: { [name: string]: string }) {
        const standardParams = {
            scmUrl: context.urlTemplates.pingScmSiteUrl
        };

        for (const key in params) {
            if (params.hasOwnProperty(key)) {
                standardParams[key] = params[key];
            }
        }

        this._aiService.trackEvent(name, standardParams);
    }

    private _getFunctionHostStatus(context: FunctionAppContext): Observable<HostStatus> {
        return this.getAuthSettings(context)
            .mergeMap(authSettings => {
                if (authSettings.clientCertEnabled) {
                    return Observable.of(null);
                } else if (!context.masterKey) {
                    return this._getHostToken(context)
                        .map(r => r.json() as string)
                        .concatMap(t => this._http.get(context.urlTemplates.runtimeStatusUrl, { headers: this._getScmSiteHeaders(context, null, t) }).map(r => r.json()));
                } else {
                    return this._http.get(context.urlTemplates.runtimeStatusUrl, { headers: this._getScmSiteHeaders(context) })
                        .map(r => r.json());
                }
            });
    }

    private _checkRuntimeStatus(context: FunctionAppContext): Observable<HostStatus> {
        const hostStatus = this._getFunctionHostStatus(context);
        hostStatus
            .subscribe(status => {
                if (status && status.state !== 'Running') {
                    status.errors = status.errors || [];
                    this._broadcastService.broadcast<ErrorEvent>(BroadcastEvent.Error, {
                        message: this._translateService.instant(PortalResources.error_functionRuntimeIsUnableToStart)
                            + '\n'
                            + status.errors.reduce((a, b) => `${a}\n${b}`),
                        errorId: ErrorIds.functionRuntimeIsUnableToStart,
                        errorType: ErrorType.Fatal,
                        resourceId: context.site.id
                    });
                    this._trackEvent(context, ErrorIds.functionRuntimeIsUnableToStart, {
                        content: status.errors.reduce((a, b) => `${a}\n${b}`),
                        status: '200'
                    });
                } else if (status) {
                    status.errors = status.errors || [];
                    this._broadcastService.broadcast<ErrorEvent>(BroadcastEvent.Error, {
                        message: status.errors.reduce((a, b) => `${a}\n${b}`),
                        errorId: ErrorIds.generalHostErrorFromHost,
                        errorType: ErrorType.RuntimeError,
                        resourceId: context.site.id
                    });
                    this._trackEvent(context, ErrorIds.functionRuntimeIsUnableToStart, {
                        content: status.errors.reduce((a, b) => `${a}\n${b}`),
                        status: '200'
                    });
                }
            }, e => {
                let content = e;
                let resStatus = '0';
                try {
                    content = e.text ? e.text() : e;
                    resStatus = e.status ? e.status.toString() : '0';
                } catch (_) { }

                this._broadcastService.broadcast<ErrorEvent>(BroadcastEvent.Error, {
                    message: this._translateService.instant(PortalResources.error_functionRuntimeIsUnableToStart),
                    errorId: ErrorIds.functionRuntimeIsUnableToStart,
                    errorType: ErrorType.Fatal,
                    resourceId: context.site.id
                });
                this._trackEvent(context, ErrorIds.functionRuntimeIsUnableToStart, {
                    content: content,
                    status: resStatus
                });
            });
        return hostStatus;
    }

    private retryAntares(error: Observable<any>): Observable<any> {
        return error.scan((errorCount: number, err: FunctionsResponse) => {
            if (err.isHandled || err.status < 500 || errorCount >= 10) {
                throw err;
            } else {
                return errorCount + 1;
            }
        }, 0).delay(1000);
    }
}


text:
"""
Tests that the Ts() and Qm() builders accept and
process multiple sources correctly.
"""

import TestSCons

test = TestSCons.TestSCons()
test.dir_fixture("image")
test.file_fixture('../../qtenv.py')
test.file_fixture('../../../__init__.py','site_scons/site_tools/qt4/__init__.py')
test.run(stderr=None)

test.must_exist(test.workpath('my_en.ts'))
test.must_exist(test.workpath('a.ts'))
test.must_exist(test.workpath('b.ts'))
test.must_contain(test.workpath('my_en.ts'),'SCons rocks!')
test.must_contain(test.workpath('my_en.ts'),'And Qt4 too!')
test.must_contain(test.workpath('a.ts'),'SCons rocks!')
test.must_contain(test.workpath('b.ts'),'And Qt4 too!')
test.must_exist(test.workpath('my_en.qm'))

test.run(options = '-c')

test.must_exist(test.workpath('my_en.ts'))
test.must_exist(test.workpath('a.ts'))
test.must_exist(test.workpath('b.ts'))
test.must_not_exist(test.workpath('my_en.qm'))

test.pass_test()

# Local Variables:
# tab-width:4
# indent-tabs-mode:nil
# End:
# vim: set expandtab tabstop=4 shiftwidth=4:


text:
<md-card>
    <md-data-table-toolbar class="alternate" ng-show="vm.selected.length">

{{vm.selected.length}} {{vm.selected.length > 1 ? 'items selecionados' : 'item selecionado'}}

    </md-data-table-toolbar>

    <md-data-table-container>

                {{categoria.descricao}}

                    <md-button class="md-icon-button md-sheet">

                            <md-tooltip>Editar</md-tooltip>
                            <md-icon md-font-icon="mdi mdi-pencil md-18"></md-icon>

                    </md-button>

                    <md-button ng-click="vm.remove(categoria.id)" class="md-icon-button md-sheet">
                        <md-tooltip>Remover</md-tooltip>
                        <md-icon md-font-icon="mdi mdi-delete md-18"></md-icon>
                    </md-button>

    </md-data-table-container>

    <md-data-table-pagination md-limit="query.limit"
                              md-page="query.page"
                              md-total="{{vm.total}}"
                              md-trigger="onpagechange"></md-data-table-pagination>
</md-card>


text:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans         http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<!--

    Copyright 2005-2014 The Kuali Foundation

    Licensed under the Educational Community License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

    http://www.opensource.org/licenses/ecl2.php

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
  <bean id="Namespace" parent="Namespace-parentBean"/>

  <bean id="Namespace-parentBean" abstract="true" parent="BusinessObjectEntry">
    <property name="dataObjectClass" value="org.kuali.rice.coreservice.impl.namespace.NamespaceBo"/>
    <property name="inquiryDefinition">
      <ref bean="Namespace-inquiryDefinition"/>
    </property>
    <property name="lookupDefinition">
      <ref bean="Namespace-lookupDefinition"/>
    </property>
    <property name="titleAttribute" value="name"/>
    <property name="objectLabel" value="Namespace"/>
    <property name="attributes">
      <list>
        <ref bean="Namespace-code"/>
        <ref bean="Namespace-name"/>
        <ref bean="Namespace-applicationId"/>
        <ref bean="Namespace-active"/>
      </list>
    </property>
  </bean>

<!-- Attribute Definitions -->

  <bean id="Namespace-code" parent="Namespace-code-parentBean"/>

  <bean id="Namespace-code-parentBean" abstract="true" parent="AttributeDefinition">
    <property name="name" value="code"/>
    <property name="forceUppercase" value="true"/>
    <property name="label" value="Namespace Code"/>
    <property name="shortLabel" value="Namespace Cd"/>
    <property name="maxLength" value="20"/>
    <property name="validationPattern">
      <bean parent="AnyCharacterValidationPattern"/>
    </property>
    <property name="required" value="true"/>
    <property name="control">
      <bean parent="TextControlDefinition" p:size="10"/>
    </property>
    <property name="summary" value="Code identifying the namespace"/>
    <property name="description" value="Code identifying the namespace"/>
  </bean>
  <bean id="Namespace-name" parent="Namespace-name-parentBean"/>

  <bean id="Namespace-name-parentBean" abstract="true" parent="AttributeDefinition">
    <property name="name" value="name"/>
    <property name="forceUppercase" value="false"/>
    <property name="label" value="Namespace Name"/>
    <property name="shortLabel" value="Namespace Nm"/>
    <property name="maxLength" value="40"/>
    <property name="validationPattern">
      <bean parent="AnyCharacterValidationPattern" p:allowWhitespace="true"/>
    </property>
    <property name="required" value="true"/>
    <property name="control">
      <bean parent="TextControlDefinition" p:size="40"/>
    </property>
    <property name="summary" value="The name of the namespace"/>
    <property name="description" value="The name of the namespace."/>
  </bean>
  <bean id="Namespace-active" parent="Namespace-active-parentBean"/>

  <bean id="Namespace-active-parentBean" abstract="true" parent="AttributeReferenceDummy-activeIndicator">
    <property name="name" value="active"/>
  </bean>
  <bean id="Namespace-applicationId" parent="Namespace-applicationId-parentBean"/>

  <bean id="Namespace-applicationId-parentBean" abstract="true" parent="AttributeDefinition">
    <property name="forceUppercase" value="false"/>
    <property name="shortLabel" value="Appl Nmspc"/>
    <property name="maxLength" value="20"/>
    <property name="validationPattern">
      <bean parent="AnyCharacterValidationPattern"/>
    </property>
    <property name="required" value="false"/>
    <property name="control">
      <bean parent="TextControlDefinition" p:size="10"/>
    </property>
    <property name="summary" value="The code of the application namespace."/>
    <property name="name" value="applicationId"/>
    <property name="label" value="Application ID"/>
    <property name="description" value="The id of the application which owns this namespace."/>
  </bean>

<!-- Business Object Inquiry Definition -->

  <bean id="Namespace-inquiryDefinition" parent="Namespace-inquiryDefinition-parentBean"/>

  <bean id="Namespace-inquiryDefinition-parentBean" abstract="true" parent="InquiryDefinition">
    <property name="title" value="Namespace Inquiry"/>
    <property name="inquirySections">
      <list>
        <bean parent="InquirySectionDefinition">
          <property name="title" value="Namespace Information"/>
          <property name="numberOfColumns" value="1"/>
          <property name="inquiryFields">
            <list>
              <bean parent="FieldDefinition" p:attributeName="code"/>
              <bean parent="FieldDefinition" p:attributeName="name" p:noInquiry="true"/>
              <bean parent="FieldDefinition" p:attributeName="applicationId"/>
              <bean parent="FieldDefinition" p:attributeName="active"/>
            </list>
          </property>
        </bean>
      </list>
    </property>
  </bean>

<!-- Business Object Lookup Definition -->

  <bean id="Namespace-lookupDefinition" parent="Namespace-lookupDefinition-parentBean"/>

  <bean id="Namespace-lookupDefinition-parentBean" abstract="true" parent="LookupDefinition">
    <property name="title" value="Namespace Lookup"/>

    <property name="defaultSort">
      <bean parent="SortDefinition">
        <property name="attributeNames">
          <list>
            <value>name</value>
          </list>
        </property>
      </bean>
    </property>
    <property name="lookupFields">
      <list>
        <bean parent="FieldDefinition" p:attributeName="code" p:noLookup="true"/>
        <bean parent="FieldDefinition" p:attributeName="name"/>
        <bean parent="FieldDefinition" p:attributeName="applicationId"/>
        <bean parent="FieldDefinition" p:defaultValue="Y" p:attributeName="active"/>
      </list>
    </property>
    <property name="resultFields">
      <list>
        <bean parent="FieldDefinition" p:attributeName="code" p:forceInquiry="true"/>
        <bean parent="FieldDefinition" p:attributeName="name"/>
        <bean parent="FieldDefinition" p:attributeName="applicationId"/>
        <bean parent="FieldDefinition" p:attributeName="active"/>
      </list>
    </property>
  </bean>
</beans>


text:
package cn.iflyapi.blog.controller;

import org.apdplat.word.WordFrequencyStatistics;
import org.apdplat.word.WordSegmenter;
import org.apdplat.word.segmentation.Word;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author flyhero
 * @date 2018-12-22 3:24 PM
 */
@RestController
public class TestController {

    public static void main(String[] args) {
        List<Word> words = WordSegmenter.seg("不得不知的Spring设计模式");
        System.out.println(words.stream().map(word -> word.getText()).collect(Collectors.joining(",")));
        System.out.println(words.toString());

        long t = System.currentTimeMillis();
        String s = System.getProperty("java.io.tmpdir");
        //词频统计设置
        WordFrequencyStatistics wordFrequencyStatistics = new WordFrequencyStatistics();
        wordFrequencyStatistics.setRemoveStopWord(false);
        wordFrequencyStatistics.setResultPath(s + "word-frequency-statistics.txt");
        //开始分词
        wordFrequencyStatistics.seg("明天下雨,明天吧,明天好,结合成分子,明天有关于分子和原子的课程,下雨了也要去听课");
        //输出词频统计结果
        wordFrequencyStatistics.dump();
        System.out.println((System.currentTimeMillis() - t) / 1000);

        try {
            List<String> wrs= new ArrayList<>();
            File file = new File(s + "word-frequency-statistics.txt");
            BufferedReader br = new BufferedReader(new FileReader(file));
            String s1;
            int count = 0;
            while ((s1 = br.readLine()) != null) {
                count++;
                if (count <=3) {
                    wrs.add(s1.split(" ")[0]);
                }
            }
            br.close();
            System.out.println(wrs.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}