text:
"""Extremely random forest graph builder. go/brain-tree."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import math
import random

from tensorflow.contrib.tensor_forest.python import constants
from tensorflow.contrib.tensor_forest.python.ops import inference_ops
from tensorflow.contrib.tensor_forest.python.ops import training_ops

from tensorflow.python.framework import constant_op
from tensorflow.python.framework import dtypes
from tensorflow.python.framework import ops
from tensorflow.python.ops import array_ops
from tensorflow.python.ops import control_flow_ops
from tensorflow.python.ops import init_ops
from tensorflow.python.ops import math_ops
from tensorflow.python.ops import random_ops
from tensorflow.python.ops import state_ops
from tensorflow.python.ops import variable_scope
from tensorflow.python.ops import variables as tf_variables
from tensorflow.python.platform import tf_logging as logging

# A convenience class for holding random forest hyperparameters.
#
# To just get some good default parameters, use:
#   hparams = ForestHParams(num_classes=2, num_features=40).fill()
#
# Note that num_classes can not be inferred and so must always be specified.
# Also, either num_splits_to_consider or num_features should be set.
#
# To override specific values, pass them to the constructor:
#   hparams = ForestHParams(num_classes=5, num_trees=10, num_features=5).fill()
#
# TODO(thomaswc): Inherit from tf.HParams when that is publicly available.
class ForestHParams(object):
  """A base class for holding hyperparameters and calculating good defaults."""

  def __init__(self,
               num_trees=100,
               max_nodes=10000,
               bagging_fraction=1.0,
               num_splits_to_consider=0,
               feature_bagging_fraction=1.0,
               max_fertile_nodes=0,
               split_after_samples=250,
               min_split_samples=5,
               valid_leaf_threshold=1,
               **kwargs):
    self.num_trees = num_trees
    self.max_nodes = max_nodes
    self.bagging_fraction = bagging_fraction
    self.feature_bagging_fraction = feature_bagging_fraction
    self.num_splits_to_consider = num_splits_to_consider
    self.max_fertile_nodes = max_fertile_nodes
    self.split_after_samples = split_after_samples
    self.min_split_samples = min_split_samples
    self.valid_leaf_threshold = valid_leaf_threshold

    for name, value in kwargs.items():
      setattr(self, name, value)

  def values(self):
    return self.__dict__

  def fill(self):
    """Intelligently sets any non-specific parameters."""
    # Fail fast if num_classes or num_features isn't set.
    _ = getattr(self, 'num_classes')
    _ = getattr(self, 'num_features')

    self.bagged_num_features = int(self.feature_bagging_fraction *
                                   self.num_features)

    self.bagged_features = None
    if self.feature_bagging_fraction < 1.0:
      self.bagged_features = [random.sample(
          range(self.num_features),
          self.bagged_num_features) for _ in range(self.num_trees)]

    self.regression = getattr(self, 'regression', False)

    # Num_outputs is the actual number of outputs (a single prediction for
    # classification, a N-dimenensional point for regression).
    self.num_outputs = self.num_classes if self.regression else 1

    # Add an extra column to classes for storing counts, which is needed for
    # regression and avoids having to recompute sums for classification.
    self.num_output_columns = self.num_classes + 1

    # The Random Forest literature recommends sqrt(# features) for
    # classification problems, and p/3 for regression problems.
    # TODO(thomaswc): Consider capping this for large number of features.
    self.num_splits_to_consider = (
        self.num_splits_to_consider or
        max(10, int(math.ceil(math.sqrt(self.num_features)))))

    # max_fertile_nodes doesn't effect performance, only training speed.
    # We therefore set it primarily based upon space considerations.
    # Each fertile node takes up num_splits_to_consider times as much
    # as space as a non-fertile node.  We want the fertile nodes to in
    # total only take up as much space as the non-fertile nodes, so
    num_fertile = int(math.ceil(self.max_nodes / self.num_splits_to_consider))
    # But always use at least 1000 accumulate slots.
    num_fertile = max(num_fertile, 1000)
    self.max_fertile_nodes = self.max_fertile_nodes or num_fertile
    # But it also never needs to be larger than the number of leaves,
    # which is max_nodes / 2.
    self.max_fertile_nodes = min(self.max_fertile_nodes,
                                 int(math.ceil(self.max_nodes / 2.0)))

    # We have num_splits_to_consider slots to fill, and we want to spend
    # approximately split_after_samples samples initializing them.
    num_split_initializiations_per_input = max(1, int(math.floor(
        self.num_splits_to_consider / self.split_after_samples)))
    self.split_initializations_per_input = getattr(
        self, 'split_initializations_per_input',
        num_split_initializiations_per_input)

    # If base_random_seed is 0, the current time will be used to seed the
    # random number generators for each tree.  If non-zero, the i-th tree
    # will be seeded with base_random_seed + i.
    self.base_random_seed = getattr(self, 'base_random_seed', 0)

    return self

# A simple container to hold the training variables for a single tree.
class TreeTrainingVariables(object):
  """Stores tf.Variables for training a single random tree.

  Uses tf.get_variable to get tree-specific names so that this can be used
  with a tf.learn-style implementation (one that trains a model, saves it,
  then relies on restoring that model to evaluate).
  """

  def __init__(self, params, tree_num, training):
    self.tree = variable_scope.get_variable(
        name=self.get_tree_name('tree', tree_num), dtype=dtypes.int32,
        shape=[params.max_nodes, 2],
        initializer=init_ops.constant_initializer(-2))
    self.tree_thresholds = variable_scope.get_variable(
        name=self.get_tree_name('tree_thresholds', tree_num),
        shape=[params.max_nodes],
        initializer=init_ops.constant_initializer(-1.0))
    self.end_of_tree = variable_scope.get_variable(
        name=self.get_tree_name('end_of_tree', tree_num),
        dtype=dtypes.int32,
        initializer=constant_op.constant([1]))
    self.start_epoch = tf_variables.Variable(
        [0] * (params.max_nodes), name='start_epoch')

    if training:
      self.node_to_accumulator_map = variable_scope.get_variable(
          name=self.get_tree_name('node_to_accumulator_map', tree_num),
          shape=[params.max_nodes],
          dtype=dtypes.int32,
          initializer=init_ops.constant_initializer(-1))
      self.accumulator_to_node_map = variable_scope.get_variable(
          name=self.get_tree_name('accumulator_to_node_map', tree_num),
          shape=[params.max_fertile_nodes],
          dtype=dtypes.int32,
          initializer=init_ops.constant_initializer(-1))

      self.candidate_split_features = variable_scope.get_variable(
          name=self.get_tree_name('candidate_split_features', tree_num),
          shape=[params.max_fertile_nodes, params.num_splits_to_consider],
          dtype=dtypes.int32,
          initializer=init_ops.constant_initializer(-1))
      self.candidate_split_thresholds = variable_scope.get_variable(
          name=self.get_tree_name('candidate_split_thresholds', tree_num),
          shape=[params.max_fertile_nodes, params.num_splits_to_consider],
          initializer=init_ops.constant_initializer(0.0))

    # Statistics shared by classification and regression.
    self.node_sums = variable_scope.get_variable(
        name=self.get_tree_name('node_sums', tree_num),
        shape=[params.max_nodes, params.num_output_columns],
        initializer=init_ops.constant_initializer(0.0))

    if training:
      self.candidate_split_sums = variable_scope.get_variable(
          name=self.get_tree_name('candidate_split_sums', tree_num),
          shape=[params.max_fertile_nodes, params.num_splits_to_consider,
                 params.num_output_columns],
          initializer=init_ops.constant_initializer(0.0))
      self.accumulator_sums = variable_scope.get_variable(
          name=self.get_tree_name('accumulator_sums', tree_num),
          shape=[params.max_fertile_nodes, params.num_output_columns],
          initializer=init_ops.constant_initializer(-1.0))

      # Regression also tracks second order stats.
      if params.regression:
        self.node_squares = variable_scope.get_variable(
            name=self.get_tree_name('node_squares', tree_num),
            shape=[params.max_nodes, params.num_output_columns],
            initializer=init_ops.constant_initializer(0.0))

        self.candidate_split_squares = variable_scope.get_variable(
            name=self.get_tree_name('candidate_split_squares', tree_num),
            shape=[params.max_fertile_nodes, params.num_splits_to_consider,
                   params.num_output_columns],
            initializer=init_ops.constant_initializer(0.0))

        self.accumulator_squares = variable_scope.get_variable(
            name=self.get_tree_name('accumulator_squares', tree_num),
            shape=[params.max_fertile_nodes, params.num_output_columns],
            initializer=init_ops.constant_initializer(-1.0))

      else:
        self.node_squares = constant_op.constant(
            0.0, name=self.get_tree_name('node_squares', tree_num))

        self.candidate_split_squares = constant_op.constant(
            0.0, name=self.get_tree_name('candidate_split_squares', tree_num))

        self.accumulator_squares = constant_op.constant(
            0.0, name=self.get_tree_name('accumulator_squares', tree_num))

  def get_tree_name(self, name, num):
    return '{0}-{1}'.format(name, num)

class ForestStats(object):

  def __init__(self, tree_stats, params):
    """A simple container for stats about a forest."""
    self.tree_stats = tree_stats
    self.params = params

  def get_average(self, thing):
    val = 0.0
    for i in range(self.params.num_trees):
      val += getattr(self.tree_stats[i], thing)

    return val / self.params.num_trees

class TreeStats(object):

  def __init__(self, num_nodes, num_leaves):
    self.num_nodes = num_nodes
    self.num_leaves = num_leaves

class ForestTrainingVariables(object):
  """A container for a forests training data, consisting of multiple trees.

  Instantiates a TreeTrainingVariables object for each tree. We override the
  __getitem__ and __setitem__ function so that usage looks like this:

    forest_variables = ForestTrainingVariables(params)

    ... forest_variables.tree ...
  """

  def __init__(self, params, device_assigner, training=True,
               tree_variables_class=TreeTrainingVariables):
    self.variables = []
    for i in range(params.num_trees):
      with ops.device(device_assigner.get_device(i)):
        self.variables.append(tree_variables_class(params, i, training))

  def __setitem__(self, t, val):
    self.variables[t] = val

  def __getitem__(self, t):
    return self.variables[t]

class RandomForestDeviceAssigner(object):
  """A device assigner that uses the default device.

  Write subclasses that implement get_device for control over how trees
  get assigned to devices.  This assumes that whole trees are assigned
  to a device.
  """

  def __init__(self):
    self.cached = None

  def get_device(self, unused_tree_num):
    if not self.cached:
      dummy = constant_op.constant(0)
      self.cached = dummy.device

    return self.cached

class RandomForestGraphs(object):
  """Builds TF graphs for random forest training and inference."""

  def __init__(self, params, device_assigner=None,
               variables=None, tree_variables_class=TreeTrainingVariables,
               tree_graphs=None, training=True,
               t_ops=training_ops,
               i_ops=inference_ops):
    self.params = params
    self.device_assigner = device_assigner or RandomForestDeviceAssigner()
    logging.info('Constructing forest with params = ')
    logging.info(self.params.__dict__)
    self.variables = variables or ForestTrainingVariables(
        self.params, device_assigner=self.device_assigner, training=training,
        tree_variables_class=tree_variables_class)
    tree_graph_class = tree_graphs or RandomTreeGraphs
    self.trees = [
        tree_graph_class(
            self.variables[i], self.params,
            t_ops.Load(), i_ops.Load(), i)
        for i in range(self.params.num_trees)]

  def _bag_features(self, tree_num, input_data):
    split_data = array_ops.split(1, self.params.num_features, input_data)
    return array_ops.concat(
        1, [split_data[ind] for ind in self.params.bagged_features[tree_num]])

  def training_graph(self, input_data, input_labels, data_spec=None,
                     epoch=None, **tree_kwargs):
    """Constructs a TF graph for training a random forest.

    Args:
      input_data: A tensor or SparseTensor or placeholder for input data.
      input_labels: A tensor or placeholder for labels associated with
        input_data.
      data_spec: A list of tf.dtype values specifying the original types of
        each column.
      epoch: A tensor or placeholder for the epoch the training data comes from.
      **tree_kwargs: Keyword arguments passed to each tree's training_graph.

    Returns:
      The last op in the random forest training graph.
    """
    data_spec = [constants.DATA_FLOAT] if data_spec is None else data_spec
    tree_graphs = []
    for i in range(self.params.num_trees):
      with ops.device(self.device_assigner.get_device(i)):
        seed = self.params.base_random_seed
        if seed != 0:
          seed += i
        # If using bagging, randomly select some of the input.
        tree_data = input_data
        tree_labels = input_labels
        if self.params.bagging_fraction < 1.0:
          # TODO(thomaswc): This does sampling without replacment.  Consider
          # also allowing sampling with replacement as an option.
          batch_size = array_ops.slice(array_ops.shape(input_data), [0], [1])
          r = random_ops.random_uniform(batch_size, seed=seed)
          mask = math_ops.less(
              r, array_ops.ones_like(r) * self.params.bagging_fraction)
          gather_indices = array_ops.squeeze(
              array_ops.where(mask), squeeze_dims=[1])
          # TODO(thomaswc): Calculate out-of-bag data and labels, and store
          # them for use in calculating statistics later.
          tree_data = array_ops.gather(input_data, gather_indices)
          tree_labels = array_ops.gather(input_labels, gather_indices)
        if self.params.bagged_features:
          tree_data = self._bag_features(i, tree_data)

        initialization = self.trees[i].tree_initialization()

        with ops.control_dependencies([initialization]):
          tree_graphs.append(
              self.trees[i].training_graph(
                  tree_data, tree_labels, seed, data_spec=data_spec,
                  epoch=([0] if epoch is None else epoch),
                  **tree_kwargs))

    return control_flow_ops.group(*tree_graphs, name='train')

  def inference_graph(self, input_data, data_spec=None, **inference_args):
    """Constructs a TF graph for evaluating a random forest.

    Args:
      input_data: A tensor or SparseTensor or placeholder for input data.
      data_spec: A list of tf.dtype values specifying the original types of
        each column.
      **inference_args: Keyword arguments to pass through to each tree.

    Returns:
      The last op in the random forest inference graph.
    """
    data_spec = [constants.DATA_FLOAT] if data_spec is None else data_spec
    probabilities = []
    for i in range(self.params.num_trees):
      with ops.device(self.device_assigner.get_device(i)):
        tree_data = input_data
        if self.params.bagged_features:
          tree_data = self._bag_features(i, input_data)
        probabilities.append(self.trees[i].inference_graph(
            tree_data, data_spec, **inference_args))
    with ops.device(self.device_assigner.get_device(0)):
      all_predict = array_ops.pack(probabilities)
      return math_ops.div(
          math_ops.reduce_sum(all_predict, 0), self.params.num_trees,
          name='probabilities')

  def average_size(self):
    """Constructs a TF graph for evaluating the average size of a forest.

    Returns:
      The average number of nodes over the trees.
    """
    sizes = []
    for i in range(self.params.num_trees):
      with ops.device(self.device_assigner.get_device(i)):
        sizes.append(self.trees[i].size())
    return math_ops.reduce_mean(math_ops.to_float(array_ops.pack(sizes)))

  # pylint: disable=unused-argument
  def training_loss(self, features, labels):
    return math_ops.neg(self.average_size())

  # pylint: disable=unused-argument
  def validation_loss(self, features, labels):
    return math_ops.neg(self.average_size())

  def average_impurity(self):
    """Constructs a TF graph for evaluating the leaf impurity of a forest.

    Returns:
      The last op in the graph.
    """
    impurities = []
    for i in range(self.params.num_trees):
      with ops.device(self.device_assigner.get_device(i)):
        impurities.append(self.trees[i].average_impurity())
    return math_ops.reduce_mean(array_ops.pack(impurities))

  def get_stats(self, session):
    tree_stats = []
    for i in range(self.params.num_trees):
      with ops.device(self.device_assigner.get_device(i)):
        tree_stats.append(self.trees[i].get_stats(session))
    return ForestStats(tree_stats, self.params)

class RandomTreeGraphs(object):
  """Builds TF graphs for random tree training and inference."""

  def __init__(self, variables, params, t_ops, i_ops, tree_num):
    self.training_ops = t_ops
    self.inference_ops = i_ops
    self.variables = variables
    self.params = params
    self.tree_num = tree_num

  def tree_initialization(self):
    def _init_tree():
      return state_ops.scatter_update(self.variables.tree, [0], [[-1, -1]]).op

    def _nothing():
      return control_flow_ops.no_op()

    return control_flow_ops.cond(
        math_ops.equal(array_ops.squeeze(array_ops.slice(
            self.variables.tree, [0, 0], [1, 1])), -2),
        _init_tree, _nothing)

  def _gini(self, class_counts):
    """Calculate the Gini impurity.

    If c(i) denotes the i-th class count and c = sum_i c(i) then
      score = 1 - sum_i ( c(i) / c )^2

    Args:
      class_counts: A 2-D tensor of per-class counts, usually a slice or
        gather from variables.node_sums.

    Returns:
      A 1-D tensor of the Gini impurities for each row in the input.
    """
    smoothed = 1.0 + array_ops.slice(class_counts, [0, 1], [-1, -1])
    sums = math_ops.reduce_sum(smoothed, 1)
    sum_squares = math_ops.reduce_sum(math_ops.square(smoothed), 1)

    return 1.0 - sum_squares / (sums * sums)

  def _weighted_gini(self, class_counts):
    """Our split score is the Gini impurity times the number of examples.

    If c(i) denotes the i-th class count and c = sum_i c(i) then
      score = c * (1 - sum_i ( c(i) / c )^2 )
            = c - sum_i c(i)^2 / c
    Args:
      class_counts: A 2-D tensor of per-class counts, usually a slice or
        gather from variables.node_sums.

    Returns:
      A 1-D tensor of the Gini impurities for each row in the input.
    """
    smoothed = 1.0 + array_ops.slice(class_counts, [0, 1], [-1, -1])
    sums = math_ops.reduce_sum(smoothed, 1)
    sum_squares = math_ops.reduce_sum(math_ops.square(smoothed), 1)

    return sums - sum_squares / sums

  def _variance(self, sums, squares):
    """Calculate the variance for each row of the input tensors.

    Variance is V = E[x^2] - (E[x])^2.

    Args:
      sums: A tensor containing output sums, usually a slice from
        variables.node_sums.  Should contain the number of examples seen
        in index 0 so we can calculate expected value.
      squares: Same as sums, but sums of squares.

    Returns:
      A 1-D tensor of the variances for each row in the input.
    """
    total_count = array_ops.slice(sums, [0, 0], [-1, 1])
    e_x = sums / total_count
    e_x2 = squares / total_count

    return math_ops.reduce_sum(e_x2 - math_ops.square(e_x), 1)

  def training_graph(self,
                     input_data,
                     input_labels,
                     random_seed,
                     data_spec,
                     epoch=None,
                     input_weights=None):

    """Constructs a TF graph for training a random tree.

    Args:
      input_data: A tensor or SparseTensor or placeholder for input data.
      input_labels: A tensor or placeholder for labels associated with
        input_data.
      random_seed: The random number generator seed to use for this tree.  0
        means use the current time as the seed.
      data_spec: A list of tf.dtype values specifying the original types of
        each column.
      epoch: A tensor or placeholder for the epoch the training data comes from.
      input_weights: A float tensor or placeholder holding per-input weights,
        or None if all inputs are to be weighted equally.

    Returns:
      The last op in the random tree training graph.
    """
    epoch = [0] if epoch is None else epoch

    if input_weights is None:
      input_weights = []

    sparse_indices = []
    sparse_values = []
    sparse_shape = []
    if isinstance(input_data, ops.SparseTensor):
      sparse_indices = input_data.indices
      sparse_values = input_data.values
      sparse_shape = input_data.shape
      input_data = []

    # Count extremely random stats.
    (node_sums, node_squares, splits_indices, splits_sums, splits_squares,
     totals_indices, totals_sums, totals_squares,
     input_leaves) = (self.training_ops.count_extremely_random_stats(
         input_data,
         sparse_indices,
         sparse_values,
         sparse_shape,
         data_spec,
         input_labels,
         input_weights,
         self.variables.tree,
         self.variables.tree_thresholds,
         self.variables.node_to_accumulator_map,
         self.variables.candidate_split_features,
         self.variables.candidate_split_thresholds,
         self.variables.start_epoch,
         epoch,
         num_classes=self.params.num_output_columns,
         regression=self.params.regression))
    node_update_ops = []
    node_update_ops.append(
        state_ops.assign_add(self.variables.node_sums, node_sums))

    splits_update_ops = []
    splits_update_ops.append(self.training_ops.scatter_add_ndim(
        self.variables.candidate_split_sums,
        splits_indices, splits_sums))
    splits_update_ops.append(self.training_ops.scatter_add_ndim(
        self.variables.accumulator_sums, totals_indices,
        totals_sums))

    if self.params.regression:
      node_update_ops.append(state_ops.assign_add(self.variables.node_squares,
                                                  node_squares))
      splits_update_ops.append(self.training_ops.scatter_add_ndim(
          self.variables.candidate_split_squares,
          splits_indices, splits_squares))
      splits_update_ops.append(self.training_ops.scatter_add_ndim(
          self.variables.accumulator_squares, totals_indices,
          totals_squares))

    # Sample inputs.
    update_indices, feature_updates, threshold_updates = (
        self.training_ops.sample_inputs(
            input_data,
            sparse_indices,
            sparse_values,
            sparse_shape,
            input_weights,
            self.variables.node_to_accumulator_map,
            input_leaves,
            self.variables.candidate_split_features,
            self.variables.candidate_split_thresholds,
            split_initializations_per_input=(
                self.params.split_initializations_per_input),
            split_sampling_random_seed=random_seed))
    update_features_op = state_ops.scatter_update(
        self.variables.candidate_split_features, update_indices,
        feature_updates)
    update_thresholds_op = state_ops.scatter_update(
        self.variables.candidate_split_thresholds, update_indices,
        threshold_updates)

    # Calculate finished nodes.
    with ops.control_dependencies(splits_update_ops):
      finished, stale = self.training_ops.finished_nodes(
          self.variables.accumulator_to_node_map,
          self.variables.node_to_accumulator_map,
          self.variables.candidate_split_sums,
          self.variables.candidate_split_squares,
          self.variables.accumulator_sums,
          self.variables.accumulator_squares,
          self.variables.start_epoch,
          epoch,
          num_split_after_samples=self.params.split_after_samples,
          min_split_samples=self.params.min_split_samples)

    # Update leaf scores.
    # TODO(thomaswc): Store the leaf scores in a TopN and only update the
    # scores of the leaves that were touched by this batch of input.
    children = array_ops.squeeze(
        array_ops.slice(self.variables.tree, [0, 0], [-1, 1]), squeeze_dims=[1])
    is_leaf = math_ops.equal(constants.LEAF_NODE, children)
    leaves = math_ops.to_int32(
        array_ops.squeeze(
            array_ops.where(is_leaf), squeeze_dims=[1]))
    non_fertile_leaves = array_ops.boolean_mask(
        leaves, math_ops.less(array_ops.gather(
            self.variables.node_to_accumulator_map, leaves), 0))

    # TODO(gilberth): It should be possible to limit the number of non
    # fertile leaves we calculate scores for, especially since we can only take
    # at most array_ops.shape(finished)[0] of them.
    with ops.control_dependencies(node_update_ops):
      sums = array_ops.gather(self.variables.node_sums, non_fertile_leaves)
      if self.params.regression:
        squares = array_ops.gather(self.variables.node_squares,
                                   non_fertile_leaves)
        non_fertile_leaf_scores = self._variance(sums, squares)
      else:
        non_fertile_leaf_scores = self._weighted_gini(sums)

    # Calculate best splits.
    with ops.control_dependencies(splits_update_ops):
      split_indices = self.training_ops.best_splits(
          finished, self.variables.node_to_accumulator_map,
          self.variables.candidate_split_sums,
          self.variables.candidate_split_squares,
          self.variables.accumulator_sums,
          self.variables.accumulator_squares,
          regression=self.params.regression)

    # Grow tree.
    with ops.control_dependencies([update_features_op, update_thresholds_op]):
      (tree_update_indices, tree_children_updates, tree_threshold_updates,
       new_eot) = (self.training_ops.grow_tree(
           self.variables.end_of_tree, self.variables.node_to_accumulator_map,
           finished, split_indices, self.variables.candidate_split_features,
           self.variables.candidate_split_thresholds))
      tree_update_op = state_ops.scatter_update(
          self.variables.tree, tree_update_indices, tree_children_updates)
      thresholds_update_op = state_ops.scatter_update(
          self.variables.tree_thresholds, tree_update_indices,
          tree_threshold_updates)
      # TODO(thomaswc): Only update the epoch on the new leaves.
      new_epoch_updates = epoch * array_ops.ones_like(tree_threshold_updates,
                                                      dtype=dtypes.int32)
      epoch_update_op = state_ops.scatter_update(
          self.variables.start_epoch, tree_update_indices,
          new_epoch_updates)

    # Update fertile slots.
    with ops.control_dependencies([tree_update_op]):
      (n2a_map_updates, a2n_map_updates, accumulators_cleared,
       accumulators_allocated) = (self.training_ops.update_fertile_slots(
           finished,
           non_fertile_leaves,
           non_fertile_leaf_scores,
           self.variables.end_of_tree,
           self.variables.accumulator_sums,
           self.variables.node_to_accumulator_map,
           stale,
           regression=self.params.regression))

    # Ensure end_of_tree doesn't get updated until UpdateFertileSlots has
    # used it to calculate new leaves.
    gated_new_eot, = control_flow_ops.tuple(
        [new_eot], control_inputs=[n2a_map_updates])
    eot_update_op = state_ops.assign(self.variables.end_of_tree, gated_new_eot)

    updates = []
    updates.append(eot_update_op)
    updates.append(tree_update_op)
    updates.append(thresholds_update_op)
    updates.append(epoch_update_op)

    updates.append(
        state_ops.scatter_update(self.variables.node_to_accumulator_map,
                                 n2a_map_updates[0], n2a_map_updates[1]))

    updates.append(
        state_ops.scatter_update(self.variables.accumulator_to_node_map,
                                 a2n_map_updates[0], a2n_map_updates[1]))

    cleared_and_allocated_accumulators = array_ops.concat(
        0, [accumulators_cleared, accumulators_allocated])

    # Calculate values to put into scatter update for candidate counts.
    # Candidate split counts are always reset back to 0 for both cleared
    # and allocated accumulators. This means some accumulators might be doubly
    # reset to 0 if the were released and not allocated, then later allocated.
    split_values = array_ops.tile(
        array_ops.expand_dims(array_ops.expand_dims(
            array_ops.zeros_like(cleared_and_allocated_accumulators,
                                 dtype=dtypes.float32), 1), 2),
        [1, self.params.num_splits_to_consider, self.params.num_output_columns])
    updates.append(state_ops.scatter_update(
        self.variables.candidate_split_sums,
        cleared_and_allocated_accumulators, split_values))
    if self.params.regression:
      updates.append(state_ops.scatter_update(
          self.variables.candidate_split_squares,
          cleared_and_allocated_accumulators, split_values))

    # Calculate values to put into scatter update for total counts.
    total_cleared = array_ops.tile(
        array_ops.expand_dims(
            math_ops.neg(array_ops.ones_like(accumulators_cleared,
                                             dtype=dtypes.float32)), 1),
        [1, self.params.num_output_columns])
    total_reset = array_ops.tile(
        array_ops.expand_dims(
            array_ops.zeros_like(accumulators_allocated,
                                 dtype=dtypes.float32), 1),
        [1, self.params.num_output_columns])
    accumulator_updates = array_ops.concat(0, [total_cleared, total_reset])
    updates.append(state_ops.scatter_update(
        self.variables.accumulator_sums,
        cleared_and_allocated_accumulators, accumulator_updates))
    if self.params.regression:
      updates.append(state_ops.scatter_update(
          self.variables.accumulator_squares,
          cleared_and_allocated_accumulators, accumulator_updates))

    # Calculate values to put into scatter update for candidate splits.
    split_features_updates = array_ops.tile(
        array_ops.expand_dims(
            math_ops.neg(array_ops.ones_like(
                cleared_and_allocated_accumulators)), 1),
        [1, self.params.num_splits_to_consider])
    updates.append(state_ops.scatter_update(
        self.variables.candidate_split_features,
        cleared_and_allocated_accumulators, split_features_updates))

    updates += self.finish_iteration()

    return control_flow_ops.group(*updates)

  def finish_iteration(self):
    """Perform any operations that should be done at the end of an iteration.

    This is mostly useful for subclasses that need to reset variables after
    an iteration, such as ones that are used to finish nodes.

    Returns:
      A list of operations.
    """
    return []

  def inference_graph(self, input_data, data_spec):
    """Constructs a TF graph for evaluating a random tree.

    Args:
      input_data: A tensor or SparseTensor or placeholder for input data.
      data_spec: A list of tf.dtype values specifying the original types of
        each column.

    Returns:
      The last op in the random tree inference graph.
    """
    sparse_indices = []
    sparse_values = []
    sparse_shape = []
    if isinstance(input_data, ops.SparseTensor):
      sparse_indices = input_data.indices
      sparse_values = input_data.values
      sparse_shape = input_data.shape
      input_data = []
    return self.inference_ops.tree_predictions(
        input_data, sparse_indices, sparse_values, sparse_shape, data_spec,
        self.variables.tree,
        self.variables.tree_thresholds,
        self.variables.node_sums,
        valid_leaf_threshold=self.params.valid_leaf_threshold)

  def average_impurity(self):
    """Constructs a TF graph for evaluating the average leaf impurity of a tree.

    If in regression mode, this is the leaf variance. If in classification mode,
    this is the gini impurity.

    Returns:
      The last op in the graph.
    """
    children = array_ops.squeeze(array_ops.slice(
        self.variables.tree, [0, 0], [-1, 1]), squeeze_dims=[1])
    is_leaf = math_ops.equal(constants.LEAF_NODE, children)
    leaves = math_ops.to_int32(array_ops.squeeze(array_ops.where(is_leaf),
                                                 squeeze_dims=[1]))
    counts = array_ops.gather(self.variables.node_sums, leaves)
    gini = self._weighted_gini(counts)
    # Guard against step 1, when there often are no leaves yet.
    def impurity():
      return gini
    # Since average impurity can be used for loss, when there's no data just
    # return a big number so that loss always decreases.
    def big():
      return array_ops.ones_like(gini, dtype=dtypes.float32) * 10000000.
    return control_flow_ops.cond(math_ops.greater(
        array_ops.shape(leaves)[0], 0), impurity, big)

  def size(self):
    """Constructs a TF graph for evaluating the current number of nodes.

    Returns:
      The current number of nodes in the tree.
    """
    return self.variables.end_of_tree - 1

  def get_stats(self, session):
    num_nodes = self.variables.end_of_tree.eval(session=session) - 1
    num_leaves = array_ops.where(
        math_ops.equal(array_ops.squeeze(array_ops.slice(
            self.variables.tree, [0, 0], [-1, 1])), constants.LEAF_NODE)
        ).eval(session=session).shape[0]
    return TreeStats(num_nodes, num_leaves)


text:
from base import CppBase

class DynamicLib(CppBase):
    pass

redpajama_set_name: RedPajamaGithub


text:
<?php

/**
 * This file is part of the Spryker Demoshop.
 * For full license information, please view the LICENSE file that was distributed with this source code.
 */

namespace Pyz\Client\ProductNew;

use Pyz\Client\ProductNew\Plugin\Elasticsearch\Query\NewProductsQueryPlugin;
use Spryker\Client\Catalog\Plugin\Elasticsearch\ResultFormatter\RawCatalogSearchResultFormatterPlugin;
use Spryker\Client\CatalogPriceProductConnector\Plugin\CurrencyAwareCatalogSearchResultFormatterPlugin;
use Spryker\Client\CatalogPriceProductConnector\Plugin\ProductPriceQueryExpanderPlugin;
use Spryker\Client\Kernel\AbstractDependencyProvider;
use Spryker\Client\Kernel\Container;
use Spryker\Client\ProductLabel\ProductLabelClient;
use Spryker\Client\Search\Plugin\Elasticsearch\QueryExpander\FacetQueryExpanderPlugin;
use Spryker\Client\Search\Plugin\Elasticsearch\QueryExpander\LocalizedQueryExpanderPlugin;
use Spryker\Client\Search\Plugin\Elasticsearch\QueryExpander\PaginatedQueryExpanderPlugin;
use Spryker\Client\Search\Plugin\Elasticsearch\QueryExpander\SortedQueryExpanderPlugin;
use Spryker\Client\Search\Plugin\Elasticsearch\QueryExpander\StoreQueryExpanderPlugin;
use Spryker\Client\Search\Plugin\Elasticsearch\ResultFormatter\FacetResultFormatterPlugin;
use Spryker\Client\Search\Plugin\Elasticsearch\ResultFormatter\PaginatedResultFormatterPlugin;
use Spryker\Client\Search\Plugin\Elasticsearch\ResultFormatter\SortedResultFormatterPlugin;
use Spryker\Client\Search\SearchClient;
use Spryker\Shared\Kernel\Store;

class ProductNewDependencyProvider extends AbstractDependencyProvider
{
    public const CLIENT_SEARCH = 'CLIENT_SEARCH';
    public const CLIENT_PRODUCT_LABEL = 'CLIENT_PRODUCT_LABEL';
    public const NEW_PRODUCTS_QUERY_PLUGIN = 'NEW_PRODUCTS_QUERY_PLUGIN';
    public const NEW_PRODUCTS_QUERY_EXPANDER_PLUGINS = 'NEW_PRODUCTS_QUERY_EXPANDER_PLUGINS';
    public const NEW_PRODUCTS_RESULT_FORMATTER_PLUGINS = 'NEW_PRODUCTS_RESULT_FORMATTER_PLUGINS';
    public const STORE = 'STORE';

    /**
     * @param \Spryker\Client\Kernel\Container $container
     *
     * @return \Spryker\Client\Kernel\Container
     */
    public function provideServiceLayerDependencies(Container $container)
    {
        $container = $this->addSearchClient($container);
        $container = $this->addProductLabelClient($container);
        $container = $this->addNewProductsQueryPlugin($container);
        $container = $this->addNewProductsQueryExpanderPlugins($container);
        $container = $this->addNewProductsResultFormatterPlugins($container);
        $container = $this->addStore($container);

        return $container;
    }

    /**
     * @param \Spryker\Client\Kernel\Container $container
     *
     * @return \Spryker\Client\Kernel\Container
     */
    protected function addSearchClient(Container $container)
    {
        $container[self::CLIENT_SEARCH] = function () {
            return new SearchClient();
        };

        return $container;
    }

    /**
     * @param \Spryker\Client\Kernel\Container $container
     *
     * @return \Spryker\Client\Kernel\Container
     */
    protected function addProductLabelClient(Container $container)
    {
        $container[self::CLIENT_PRODUCT_LABEL] = function () {
            return new ProductLabelClient();
        };

        return $container;
    }

    /**
     * @param \Spryker\Client\Kernel\Container $container
     *
     * @return \Spryker\Client\Kernel\Container
     */
    protected function addNewProductsQueryPlugin(Container $container)
    {
        $container[self::NEW_PRODUCTS_QUERY_PLUGIN] = function () {
            return new NewProductsQueryPlugin();
        };

        return $container;
    }

    /**
     * @param \Spryker\Client\Kernel\Container $container
     *
     * @return \Spryker\Client\Kernel\Container
     */
    protected function addNewProductsQueryExpanderPlugins(Container $container)
    {
        $container[self::NEW_PRODUCTS_QUERY_EXPANDER_PLUGINS] = function () {
            return [
                new StoreQueryExpanderPlugin(),
                new LocalizedQueryExpanderPlugin(),
                new ProductPriceQueryExpanderPlugin(),
                new FacetQueryExpanderPlugin(),
                new SortedQueryExpanderPlugin(),
                new PaginatedQueryExpanderPlugin(),
            ];
        };

        return $container;
    }

    /**
     * @param \Spryker\Client\Kernel\Container $container
     *
     * @return \Spryker\Client\Kernel\Container
     */
    protected function addNewProductsResultFormatterPlugins(Container $container)
    {
        $container[self::NEW_PRODUCTS_RESULT_FORMATTER_PLUGINS] = function () {
            return [
                new FacetResultFormatterPlugin(),
                new SortedResultFormatterPlugin(),
                new PaginatedResultFormatterPlugin(),
                new CurrencyAwareCatalogSearchResultFormatterPlugin(
                    new RawCatalogSearchResultFormatterPlugin()
                ),
            ];
        };

        return $container;
    }

    /**
     * @param \Spryker\Client\Kernel\Container $container
     *
     * @return \Spryker\Client\Kernel\Container
     */
    protected function addStore(Container $container)
    {
        $container[self::STORE] = function () {
            return Store::getInstance();
        };

        return $container;
    }
}


text:
/*
* This is the landing index page.
* */
module.exports = function InitRoute(Route) {

  Route.namespace('landing').root('/')

  Route
    .get('/', 'Landing page with angular.js mini app')
    .query({
      name: Route.type.STRING.default('John')
    })
    .then(function() {
      this.render('landing', {
        name: this.query('name')
      });
    });

};


text:
#include "CCApplication.h"
#include "CCEGLView.h"
#include <unistd.h>
#include <sys/time.h>
#include <string>
#include "CCDirector.h"

NS_CC_BEGIN

Application* Application::sm_pSharedApplication = 0;
bool Application::s_running = false;

/*
static long getCurrentMillSecond()
{
    long lLastTime;
    struct timeval stCurrentTime;
    gettimeofday(&stCurrentTime,NULL);
    // millseconds
    lLastTime = stCurrentTime.tv_sec * 1000 + stCurrentTime.tv_usec * 0.001;
    return lLastTime;
}
*/

Application::Application()
{
    CC_ASSERT(!sm_pSharedApplication);
    sm_pSharedApplication = this;
    setAnimationInterval(1.0f/60.0f);
}

Application::~Application()
{
    CC_ASSERT(this == sm_pSharedApplication);
    sm_pSharedApplication = NULL;
}

int Application::run()
{
    // Initialize instance and cocos2d.
    if (!applicationDidFinishLaunching())
        return 0;

    s_running = true;
    for (;;)
    {
        //long iLastTime = getCurrentMillSecond();
        Director::getInstance()->mainLoop();
        EGLView::getInstance()->ProcessEventQueue();

        //long iCurTime = getCurrentMillSecond();
        /*
        if (iCurTime-iLastTime<_animationInterval)
        {
            usleep((_animationInterval - iCurTime+iLastTime)*1000);
        }
        */
    }

    s_running = false;
    return -1;
}

void Application::setAnimationInterval(double interval)
{
    _animationInterval = interval * 1000.0f;
}

Application::Platform Application::getTargetPlatform()
{
    return Platform::OS_NACL;
}

Application* Application::getInstance()
{
    CC_ASSERT(sm_pSharedApplication);
    return sm_pSharedApplication;
}

// @deprecated Use getInstance() instead
Application* Application::sharedApplication()
{
    return Application::getInstance();
}

LanguageType Application::getCurrentLanguage()
{
    LanguageType ret = LanguageType::ENGLISH;
    return ret;
}

NS_CC_END


text:
// Copyright (c) 2013 Intel Corporation. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.xwalk.core.internal.extension.api.presentation;

import java.lang.ref.WeakReference;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;

import org.xwalk.core.internal.XWalkClient;
import org.xwalk.core.internal.XWalkUIClientInternal;
import org.xwalk.core.internal.XWalkViewInternal;

/**
 * Represents the content to be presented on the secondary display.
 */
public class XWalkPresentationContent {
    public final int INVALID_PRESENTATION_ID = -1;

    private int mPresentationId = INVALID_PRESENTATION_ID;
    private XWalkViewInternal mContentView;
    private Context mContext;
    private WeakReference<Activity> mActivity;
    private PresentationDelegate mDelegate;

    public XWalkPresentationContent(
            Context context, WeakReference<Activity> activity, PresentationDelegate delegate) {
        mContext = context;
        mActivity = activity;
        mDelegate = delegate;
    }

    public void load(final String url) {
        Activity activity = mActivity.get();
        if (activity == null) return;

        if (mContentView == null) {
            mContentView = new XWalkViewInternal(mContext, activity);
            final XWalkUIClientInternal xWalkUIClient = new XWalkUIClientInternal(mContentView) {
                @Override
                public void onJavascriptCloseWindow(XWalkViewInternal view) {
                    // The content was closed already. Web need to invalidate the
                    // presentation id now.
                    mPresentationId = INVALID_PRESENTATION_ID;
                    onContentClosed();
                }

                @Override
                public void onPageLoadStarted(XWalkViewInternal view, String url) {
                    mPresentationId = mContentView.getContentID();
                    String script = "navigator.presentation.session = new navigator.presentation.PresentationSession(";
                    script += mPresentationId;
                    script += ");";
                    view.evaluateJavascript(script, null);
                }

                @Override
                public void onPageLoadStopped(
                        XWalkViewInternal view, String url, LoadStatusInternal status) {
                    if (status == LoadStatusInternal.FINISHED) {
                        onContentLoaded();
                    }
                }
            };
            mContentView.setUIClient(xWalkUIClient);
        }
        mContentView.loadUrl(url);
    }

    public int getPresentationId() {
        return mPresentationId;
    }

    public View getContentView() {
        return mContentView;
    }

    public void close() {
        mContentView.onDestroy();
        mPresentationId = INVALID_PRESENTATION_ID;
        mContentView = null;
    }

    public void onPause() {
        mContentView.pauseTimers();
        mContentView.onHide();
    }

    public void onResume() {
        mContentView.resumeTimers();
        mContentView.onShow();
    }

    private void onContentLoaded() {
        if (mDelegate != null) mDelegate.onContentLoaded(this);
    }

    private void onContentClosed() {
        if (mDelegate != null) mDelegate.onContentClosed(this);
    }

    /**
     * Interface to hook into XWalkPresentationContent instance.
     */
    public interface PresentationDelegate {
        /**
         * Called when the presentation content is loaded.
         */
        public void onContentLoaded(XWalkPresentationContent content);

        /**
         * Called when the presentation content is closed.
         */
        public void onContentClosed(XWalkPresentationContent content);
    }
}


text:
using System;
using System.Text;

public static partial class Extensions
{
    /// <summary>
    ///     A string extension method that encode the string to Base64.
    /// </summary>
    /// <param name="this">The @this to act on.</param>
    /// <returns>The encoded string to Base64.</returns>
    public static string EncodeBase64(this string @this)
    {
        return Convert.ToBase64String(Activator.CreateInstance<ASCIIEncoding>().GetBytes(@this));
    }
}


text:
package org.jbehave.examples.trader.i18n;

import java.util.Locale;

import org.jbehave.core.junit.JUnit4StoryRunner;
import org.jbehave.examples.trader.i18n.steps.FrSteps;
import org.junit.runner.RunWith;

@RunWith(JUnit4StoryRunner.class)
public class FrStories extends LocalizedStories {

    @Override
    protected Locale locale() {
        return new Locale("fr");
    }

    @Override
    protected String storyPattern() {
        return "**/*.histoire";
    }

    @Override
    protected Object localizedSteps() {
        return new FrSteps();
    }

}


text:
	({ \
		__weak id target_ = (TARGET); \
		[target_ rac_valuesForKeyPath:@keypath(TARGET, KEYPATH) observer:self]; \
	})

@class RACDisposable;
@class RACSignal;

@interface NSObject (RACPropertySubscribing)

/// Creates a signal to observe the value at the given key path.
///
/// The initial value is sent on subscription, the subsequent values are sent
/// from whichever thread the change occured on, even if it doesn't have a valid
/// scheduler.
///
/// Returns a signal that immediately sends the receiver's current value at the
/// given keypath, then any changes thereafter.
- (RACSignal *)rac_valuesForKeyPath:(NSString *)keyPath observer:(__weak NSObject *)observer;

/// Creates a signal to observe the changes of the given key path.
///
/// The initial value is sent on subscription, the subsequent values are sent
/// from whichever thread the change occured on, even if it doesn't have a valid
/// scheduler.
///
/// Returns a signal that sends tuples containing the current value at the key
/// path and the change dictionary for each KVO callback.
- (RACSignal *)rac_valuesAndChangesForKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options observer:(__weak NSObject *)observer;

@end

#define RACAble(...) \
	metamacro_if_eq(1, metamacro_argcount(__VA_ARGS__)) \
		(_RACAbleObject(self, __VA_ARGS__)) \
		(_RACAbleObject(__VA_ARGS__))

#define _RACAbleObject(object, property) [object rac_signalForKeyPath:@keypath(object, property) observer:self]

#define RACAbleWithStart(...) \
	metamacro_if_eq(1, metamacro_argcount(__VA_ARGS__)) \
		(_RACAbleWithStartObject(self, __VA_ARGS__)) \
		(_RACAbleWithStartObject(__VA_ARGS__))

#define _RACAbleWithStartObject(object, property) [object rac_signalWithStartingValueForKeyPath:@keypath(object, property) observer:self]

@interface NSObject (RACPropertySubscribingDeprecated)

+ (RACSignal *)rac_signalFor:(NSObject *)object keyPath:(NSString *)keyPath observer:(NSObject *)observer __attribute__((deprecated("Use -rac_valuesForKeyPath:observer: or RACObserve() instead.")));
+ (RACSignal *)rac_signalWithStartingValueFor:(NSObject *)object keyPath:(NSString *)keyPath observer:(NSObject *)observer __attribute__((deprecated("Use -rac_valuesForKeyPath:observer: or RACObserve() instead.")));
+ (RACSignal *)rac_signalWithChangesFor:(NSObject *)object keyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options observer:(NSObject *)observer __attribute__((deprecated("Use -rac_valuesAndChangesForKeyPath:options:observer: instead.")));
- (RACSignal *)rac_signalForKeyPath:(NSString *)keyPath observer:(NSObject *)observer __attribute__((deprecated("Use -rac_valuesForKeyPath:observer: or RACObserve() instead.")));
- (RACSignal *)rac_signalWithStartingValueForKeyPath:(NSString *)keyPath observer:(NSObject *)observer __attribute__((deprecated("Use -rac_valuesForKeyPath:observer: or RACObserve() instead.")));
- (RACDisposable *)rac_deriveProperty:(NSString *)keyPath from:(RACSignal *)signal __attribute__((deprecated("Use -[RACSignal setKeyPath:onObject:] instead")));

@end


text:
/**
 * Created by fr0609 on 6/28/15.
 */

var gulp  = require('gulp');
var args = require('yargs');
var browserSync = require('browser-sync');
var config = require('./gulp.config')();
var del = require('del');
var $ = require('gulp-load-plugins')({lazy:true});
var port = process.env.PORT || config.defaultPort;

gulp.task('vet', function() {
    log('Analyzing source gwith JSHint and JSCS')
    return gulp
        .src(config.alljs)
        .pipe($.if(args.verbose, $.print()))
        .pipe($.jscs())
        .pipe($.jshint())
        .pipe($.jshint.reporter('jshint-stylish', {verbose:true}))
        .pipe($.jshint.reporter('fail'));
});

gulp.task('styles', ['clean-styles'] , function(){
    log('compiling Less -> css');

    return gulp
        .src(config.less)
        .pipe($.plumber())
        .pipe($.less())
        .pipe($.autoprefixer({browser:['last 2 version', '> 5']}))
        .pipe(gulp.dest(config.temp));

});

gulp.task('clean-styles', function(done){
   var files = config.temp + '**/*.css';
   clean(files, done);
});

function clean(path, done){
    log('Cleaning: ' + $.util.colors.blue(path));
    del(path, done);
}

gulp.task('less-watcher', function(){
    gulp.watch([config.less], ['styles']);
});

gulp.task('wiredep', function(){
    log('Wire up the bower css js and our app js into html');
    var options = config.getWiredepDefaultOptions();
    var wiredep = require('wiredep').stream;
    return gulp
        .src(config.index)
        .pipe(wiredep(options))
        .pipe($.inject(gulp.src(config.js)))
        .pipe(gulp.dest(config.client));
});

gulp.task('inject',['wiredep', 'styles'], function(){
    log('Wire up the bower css js and our app js into html');
    return gulp
        .src(config.index)
        .pipe($.inject(gulp.src(config.css)))
        .pipe(gulp.dest(config.client));
});

gulp.task('serve-dev',['inject'], function(){
    var isDev = true;
    var nodeOptions = {
        script: config.nodeServer,
        delayTime : 1,
        env : {
            'PORT' : port,
            'NODE_ENV': isDev ? 'dev' : 'build'
        },
        watch : [config.server]
    };
    return $.nodemon(nodeOptions)
        .on('restart', function(ev){
            log('/** nodemon restarted ***/');
            log('/** files changed on restart:\n' + ev);
            setTimeout(function(){
                browserSync.notify('reloading now ...');
                browserSync.reload({stream : false});
            }, config.browserReloadDelay)
        })
        .on('start', function(){
            log('/** nodemon started ***/');
            startBrowserSync();
        })
        .on('crash', function(ev){
             log('/** nodemon crashed :\n'+ev);
        })
        .on('exit', function(){
            log('/** nodemon exit ***/');
    });
});

function changeEvent(event){
    var srcPattern  = new RegExp('/.*(?=/' + config.source + ')/');
    log('File' + event.path.replace(srcPattern, '') + ' ' + event.type);
}
function startBrowserSync(){
    if(browserSync.active){
        return;
    }
    log('Starting browser-sync on port ' + port);
    gulp.watch([config.less],['styles'])
    .on('change', function(event){changeEvent(event); });

    var options = {
        proxy : 'localhost:'+port,
        port:3000,
        files : [
            config.client + '**/*.*',
            '!' + config.less,
            config.temp + '**/*.css',
        ],
        ghostMode:{
           clicks : true,
            location: false,
            forms :true,
            scroll : true
        },
        injectChanges :true,
        logFileChanges :true,
        logLevel : 'debug',
        logPrefix: 'gulp-patterns',
        notify : true,
        reloadDelay: 0
    }
    browserSync(options);
}
///////

function log(msg) {
    if (typeof(msg) == 'object') {
        for (var item in msg) {
            if (msg.hasOwnProperty(item)) {
                $.util.log($.util.blue(msg[item]));
            }
        }
    }
    else {
        $.util.log($.util.colors.blue(msg));
    }
}


text:
package cn.felord.wepay.ali.sdk.api.response;

import java.util.List;
import cn.felord.wepay.ali.sdk.api.internal.mapping.ApiField;
import cn.felord.wepay.ali.sdk.api.internal.mapping.ApiListField;
import cn.felord.wepay.ali.sdk.api.domain.PaymentSchedule;

import cn.felord.wepay.ali.sdk.api.AlipayResponse;

/**
 * ALIPAY API: alipay.ecapiprod.drawndn.paymentschedule.get response.
 *
 * @author auto create
 * @version $Id: $Id
 */
public class AlipayEcapiprodDrawndnPaymentscheduleGetResponse extends AlipayResponse {

	private static final long serialVersionUID = 2892671874988325651L;

	/**
	 * 返回的支用还款计划集合
	 */
	@ApiListField("payment_schedules")
	@ApiField("payment_schedule")
	private List<PaymentSchedule> paymentSchedules;

	/**
	 * 唯一标识这次请求
	 */
	@ApiField("request_id")
	private String requestId;

	/**
	 *
Setter for the field paymentSchedules.

	 *
	 * @param paymentSchedules a {@link java.util.List} object.
	 */
	public void setPaymentSchedules(List<PaymentSchedule> paymentSchedules) {
		this.paymentSchedules = paymentSchedules;
	}
	/**
	 *
Getter for the field paymentSchedules.

	 *
	 * @return a {@link java.util.List} object.
	 */
	public List<PaymentSchedule> getPaymentSchedules( ) {
		return this.paymentSchedules;
	}

	/**
	 *
Setter for the field requestId.

	 *
	 * @param requestId a {@link java.lang.String} object.
	 */
	public void setRequestId(String requestId) {
		this.requestId = requestId;
	}
	/**
	 *
Getter for the field requestId.

	 *
	 * @return a {@link java.lang.String} object.
	 */
	public String getRequestId( ) {
		return this.requestId;
	}

}


text:
require 'redis'
require 'json'
require 'logger'

require_relative('beehive/version')
require_relative('beehive/client')
require_relative('beehive/worker')

##
# Beehive is a lightweight queue system that uses Redis to store all jobs. It's inspired
# by existing systems such as Minion and Stalker. Beehive does not offer a plugin system
# or any other fancy features, it merely allows you to queue a job and have it processed
# in the background.
#
# ## Features
#
# * Uses Redis as it's storage mechanism.
# * Lightweight: workers don't waste resources on loading large amounts of libraries.
# * Easy to use
#
# Fore more information see the README file.
#
# @author Yorick Peterse
# @since  0.1
#
module Beehive
  ##
  # Hash containing all the defined jobs.
  #
  # @author Yorick Peterse
  # @since  0.1
  #
  Jobs = {}

  ##
  # Adds a new job to the list.
  #
  # @example
  #  Beehive.job('video') do |params|
  #    puts "Hello!"
  #  end
  #
  # @author Yorick Peterse
  # @since  0.1
  # @param  [String] job The name of the job.
  # @param  [Proc] block The block to call whenever the job has to be processed.
  #
  def self.job(job, &block)
    Jobs[job.to_s] = block
  end
end


text:
<?php

/**
 * Converts changesets between different formats.
 */
final class ArcanistBundle {

  private $changes;
  private $conduit;
  private $blobs = array();
  private $diskPath;
  private $projectID;
  private $baseRevision;
  private $revisionID;
  private $encoding;
  private $loadFileDataCallback;
  private $authorName;
  private $authorEmail;

  public function setAuthorEmail($author_email) {
    $this->authorEmail = $author_email;
    return $this;
  }

  public function getAuthorEmail() {
    return $this->authorEmail;
  }

  public function setAuthorName($author_name) {
    $this->authorName = $author_name;
    return $this;
  }

  public function getAuthorName() {
    return $this->authorName;
  }

  public function getFullAuthor() {
    $author_name = $this->getAuthorName();
    if ($author_name === null) {
      return null;
    }

    $author_email = $this->getAuthorEmail();
    if ($author_email === null) {
      return null;
    }

    $full_author = sprintf('%s <%s>', $author_name, $author_email);

    // Because git is very picky about the author being in a valid format,
    // verify that we can parse it.
    $address = new PhutilEmailAddress($full_author);
    if (!$address->getDisplayName() || !$address->getAddress()) {
      return null;
    }

    return $full_author;
  }

  public function setConduit(ConduitClient $conduit) {
    $this->conduit = $conduit;
    return $this;
  }

  public function setProjectID($project_id) {
    $this->projectID = $project_id;
    return $this;
  }

  public function getProjectID() {
    return $this->projectID;
  }

  public function setBaseRevision($base_revision) {
    $this->baseRevision = $base_revision;
    return $this;
  }

  public function setEncoding($encoding) {
    $this->encoding = $encoding;
    return $this;
  }

  public function getEncoding() {
    return $this->encoding;
  }

  public function getBaseRevision() {
    return $this->baseRevision;
  }

  public function setRevisionID($revision_id) {
    $this->revisionID = $revision_id;
    return $this;
  }

  public function getRevisionID() {
    return $this->revisionID;
  }

  public static function newFromChanges(array $changes) {
    $obj = new ArcanistBundle();
    $obj->changes = $changes;
    return $obj;
  }

  private function getEOL($patch_type) {
    // NOTE: Git always generates "\n" line endings, even under Windows, and
    // can not parse certain patches with "\r\n" line endings. SVN generates
    // patches with "\n" line endings on Mac or Linux and "\r\n" line endings
    // on Windows. (This EOL style is used only for patch metadata lines, not
    // for the actual patch content.)

    // (On Windows, Mercurial generates \n newlines for `--git` diffs, as it
    // must, but also \n newlines for unified diffs. We never need to deal with
    // these as we use Git format for Mercurial, so this case is currently
    // ignored.)

    switch ($patch_type) {
      case 'git':
        return "\n";
      case 'unified':
        return phutil_is_windows() ? "\r\n" : "\n";
      default:
        throw new Exception(
          "Unknown patch type '{$patch_type}'!");
    }
  }

  public static function newFromArcBundle($path) {
    $path = Filesystem::resolvePath($path);

    $future = new ExecFuture(
      'tar tfO %s',
      $path);
    list($stdout, $file_list) = $future->resolvex();
    $file_list = explode("\n", trim($file_list));

    if (in_array('meta.json', $file_list)) {
      $future = new ExecFuture(
        'tar xfO %s meta.json',
        $path);
      $meta_info = $future->resolveJSON();
      $version       = idx($meta_info, 'version', 0);
      $project_name  = idx($meta_info, 'projectName');
      $base_revision = idx($meta_info, 'baseRevision');
      $revision_id   = idx($meta_info, 'revisionID');
      $encoding      = idx($meta_info, 'encoding');
      $author_name   = idx($meta_info, 'authorName');
      $author_email  = idx($meta_info, 'authorEmail');
    } else {
      // this arc bundle was probably made before we started storing meta info
      $version       = 0;
      $project_name  = null;
      $base_revision = null;
      $revision_id   = null;
      $encoding      = null;
      $author        = null;
    }

    $future = new ExecFuture(
      'tar xfO %s changes.json',
      $path);
    $changes = $future->resolveJSON();

    foreach ($changes as $change_key => $change) {
      foreach ($change['hunks'] as $key => $hunk) {
        list($hunk_data) = execx('tar xfO %s hunks/%s', $path, $hunk['corpus']);
        $changes[$change_key]['hunks'][$key]['corpus'] = $hunk_data;
      }
    }

    foreach ($changes as $change_key => $change) {
      $changes[$change_key] = ArcanistDiffChange::newFromDictionary($change);
    }

    $obj = new ArcanistBundle();
    $obj->changes = $changes;
    $obj->diskPath = $path;
    $obj->setProjectID($project_name);
    $obj->setBaseRevision($base_revision);
    $obj->setRevisionID($revision_id);
    $obj->setEncoding($encoding);

    return $obj;
  }

  public static function newFromDiff($data) {
    $obj = new ArcanistBundle();

    $parser = new ArcanistDiffParser();
    $obj->changes = $parser->parseDiff($data);

    return $obj;
  }

  private function __construct() {}

  public function writeToDisk($path) {
    $changes = $this->getChanges();

    $change_list = array();
    foreach ($changes as $change) {
      $change_list[] = $change->toDictionary();
    }

    $hunks = array();
    foreach ($change_list as $change_key => $change) {
      foreach ($change['hunks'] as $key => $hunk) {
        $hunks[] = $hunk['corpus'];
        $change_list[$change_key]['hunks'][$key]['corpus'] = count($hunks) - 1;
      }
    }

    $blobs = array();
    foreach ($change_list as $change) {
      if (!empty($change['metadata']['old:binary-phid'])) {
        $blobs[$change['metadata']['old:binary-phid']] = null;
      }
      if (!empty($change['metadata']['new:binary-phid'])) {
        $blobs[$change['metadata']['new:binary-phid']] = null;
      }
    }
    foreach ($blobs as $phid => $null) {
      $blobs[$phid] = $this->getBlob($phid);
    }

    $meta_info = array(
      'version'      => 5,
      'projectName'  => $this->getProjectID(),
      'baseRevision' => $this->getBaseRevision(),
      'revisionID'   => $this->getRevisionID(),
      'encoding'     => $this->getEncoding(),
      'authorName'   => $this->getAuthorName(),
      'authorEmail'  => $this->getAuthorEmail(),
    );

    $dir = Filesystem::createTemporaryDirectory();
    Filesystem::createDirectory($dir.'/hunks');
    Filesystem::createDirectory($dir.'/blobs');
    Filesystem::writeFile($dir.'/changes.json', json_encode($change_list));
    Filesystem::writeFile($dir.'/meta.json', json_encode($meta_info));
    foreach ($hunks as $key => $hunk) {
      Filesystem::writeFile($dir.'/hunks/'.$key, $hunk);
    }
    foreach ($blobs as $key => $blob) {
      Filesystem::writeFile($dir.'/blobs/'.$key, $blob);
    }
    execx(
      '(cd %s; tar -czf %s *)',
      $dir,
      Filesystem::resolvePath($path));
    Filesystem::remove($dir);
  }

  public function toUnifiedDiff() {
    $eol = $this->getEOL('unified');

    $result = array();
    $changes = $this->getChanges();
    foreach ($changes as $change) {

      $hunk_changes = $this->buildHunkChanges($change->getHunks(), $eol);
      if (!$hunk_changes) {
        continue;
      }

      $old_path = $this->getOldPath($change);
      $cur_path = $this->getCurrentPath($change);

      $index_path = $cur_path;
      if ($index_path === null) {
        $index_path = $old_path;
      }

      $result[] = 'Index: '.$index_path;
      $result[] = $eol;
      $result[] = str_repeat('=', 67);
      $result[] = $eol;

      if ($old_path === null) {
        $old_path = '/dev/null';
      }

      if ($cur_path === null) {
        $cur_path = '/dev/null';
      }

      // When the diff is used by `patch`, `patch` ignores what is listed as the
      // current path and just makes changes to the file at the old path (unless
      // the current path is '/dev/null'.
      // If the old path and the current path aren't the same (and neither is
      // /dev/null), this indicates the file was moved or copied. By listing
      // both paths as the new file, `patch` will apply the diff to the new
      // file.
      if ($cur_path !== '/dev/null' && $old_path !== '/dev/null') {
        $old_path = $cur_path;
      }

      $result[] = '--- '.$old_path.$eol;
      $result[] = '+++ '.$cur_path.$eol;

      $result[] = $hunk_changes;
    }

    if (!$result) {
      return '';
    }

    $diff = implode('', $result);
    return $this->convertNonUTF8Diff($diff);
  }

  public function toGitPatch() {
    $eol = $this->getEOL('git');

    $result = array();
    $changes = $this->getChanges();

    $binary_sources = array();
    foreach ($changes as $change) {
      if (!$this->isGitBinaryChange($change)) {
        continue;
      }

      $type = $change->getType();
      if ($type == ArcanistDiffChangeType::TYPE_MOVE_AWAY ||
          $type == ArcanistDiffChangeType::TYPE_COPY_AWAY ||
          $type == ArcanistDiffChangeType::TYPE_MULTICOPY) {
        foreach ($change->getAwayPaths() as $path) {
          $binary_sources[$path] = $change;
        }
      }
    }

    foreach (array_keys($changes) as $multicopy_key) {
      $multicopy_change = $changes[$multicopy_key];

      $type = $multicopy_change->getType();
      if ($type != ArcanistDiffChangeType::TYPE_MULTICOPY) {
        continue;
      }

      // Decompose MULTICOPY into one MOVE_HERE and several COPY_HERE because
      // we need more information than we have in order to build a delete patch
      // and represent it as a bunch of COPY_HERE plus a delete. For details,
      // see T419.

      // Basically, MULTICOPY means there are 2 or more corresponding COPY_HERE
      // changes, so find one of them arbitrarily and turn it into a MOVE_HERE.

      // TODO: We might be able to do this more cleanly after T230 is resolved.

      $decompose_okay = false;
      foreach ($changes as $change_key => $change) {
        if ($change->getType() != ArcanistDiffChangeType::TYPE_COPY_HERE) {
          continue;
        }
        if ($change->getOldPath() != $multicopy_change->getCurrentPath()) {
          continue;
        }
        $decompose_okay = true;
        $change = clone $change;
        $change->setType(ArcanistDiffChangeType::TYPE_MOVE_HERE);
        $changes[$change_key] = $change;

        // The multicopy is now fully represented by MOVE_HERE plus one or more
        // COPY_HERE, so throw it away.
        unset($changes[$multicopy_key]);
        break;
      }

      if (!$decompose_okay) {
        throw new Exception(
          'Failed to decompose multicopy changeset in order to generate diff.');
      }
    }

    foreach ($changes as $change) {
      $type = $change->getType();
      $file_type = $change->getFileType();

      if ($file_type == ArcanistDiffChangeType::FILE_DIRECTORY) {
        // TODO: We should raise a FYI about this, so the user is aware
        // that we omitted it, if the directory is empty or has permissions
        // which git can't represent.

        // Git doesn't support empty directories, so we simply ignore them. If
        // the directory is nonempty, 'git apply' will create it when processing
        // the changesets for files inside it.
        continue;
      }

      if ($type == ArcanistDiffChangeType::TYPE_MOVE_AWAY) {
        // Git will apply this in the corresponding MOVE_HERE.
        continue;
      }

      $old_mode = idx($change->getOldProperties(), 'unix:filemode', '100644');
      $new_mode = idx($change->getNewProperties(), 'unix:filemode', '100644');

      $is_binary = $this->isGitBinaryChange($change);

      if ($is_binary) {
        $old_binary = idx($binary_sources, $this->getCurrentPath($change));
        $change_body = $this->buildBinaryChange($change, $old_binary);
      } else {
        $change_body = $this->buildHunkChanges($change->getHunks(), $eol);
      }
      if ($type == ArcanistDiffChangeType::TYPE_COPY_AWAY) {
        // TODO: This is only relevant when patching old Differential diffs
        // which were created prior to arc pruning TYPE_COPY_AWAY for files
        // with no modifications.
        if (!strlen($change_body) && ($old_mode == $new_mode)) {
          continue;
        }
      }

      $old_path = $this->getOldPath($change);
      $cur_path = $this->getCurrentPath($change);

      if ($old_path === null) {
        $old_index = 'a/'.$cur_path;
        $old_target  = '/dev/null';
      } else {
        $old_index = 'a/'.$old_path;
        $old_target  = 'a/'.$old_path;
      }

      if ($cur_path === null) {
        $cur_index = 'b/'.$old_path;
        $cur_target  = '/dev/null';
      } else {
        $cur_index = 'b/'.$cur_path;
        $cur_target  = 'b/'.$cur_path;
      }

      $result[] = "diff --git {$old_index} {$cur_index}".$eol;

      if ($type == ArcanistDiffChangeType::TYPE_ADD) {
        $result[] = "new file mode {$new_mode}".$eol;
      }

      if ($type == ArcanistDiffChangeType::TYPE_COPY_HERE ||
          $type == ArcanistDiffChangeType::TYPE_MOVE_HERE ||
          $type == ArcanistDiffChangeType::TYPE_COPY_AWAY ||
          $type == ArcanistDiffChangeType::TYPE_CHANGE) {
        if ($old_mode !== $new_mode) {
          $result[] = "old mode {$old_mode}".$eol;
          $result[] = "new mode {$new_mode}".$eol;
        }
      }

      if ($type == ArcanistDiffChangeType::TYPE_COPY_HERE) {
        $result[] = "copy from {$old_path}".$eol;
        $result[] = "copy to {$cur_path}".$eol;
      } else if ($type == ArcanistDiffChangeType::TYPE_MOVE_HERE) {
        $result[] = "rename from {$old_path}".$eol;
        $result[] = "rename to {$cur_path}".$eol;
      } else if ($type == ArcanistDiffChangeType::TYPE_DELETE ||
                 $type == ArcanistDiffChangeType::TYPE_MULTICOPY) {
        $old_mode = idx($change->getOldProperties(), 'unix:filemode');
        if ($old_mode) {
          $result[] = "deleted file mode {$old_mode}".$eol;
        }
      }

      if ($change_body) {
        if (!$is_binary) {
          $result[] = "--- {$old_target}".$eol;
          $result[] = "+++ {$cur_target}".$eol;
        }
        $result[] = $change_body;
      }
    }

    $diff = implode('', $result).$eol;
    return $this->convertNonUTF8Diff($diff);
  }

  private function isGitBinaryChange(ArcanistDiffChange $change) {
    $file_type = $change->getFileType();
    return ($file_type == ArcanistDiffChangeType::FILE_BINARY ||
            $file_type == ArcanistDiffChangeType::FILE_IMAGE);
  }

  private function convertNonUTF8Diff($diff) {
    if ($this->encoding) {
      $diff = phutil_utf8_convert($diff, $this->encoding, 'UTF-8');
    }
    return $diff;
  }

  public function getChanges() {
    return $this->changes;
  }

  private function breakHunkIntoSmallHunks(ArcanistDiffHunk $base_hunk) {
    $context = 3;

    $results = array();
    $lines = phutil_split_lines($base_hunk->getCorpus());
    $n = count($lines);

    $old_offset = $base_hunk->getOldOffset();
    $new_offset = $base_hunk->getNewOffset();

    $ii = 0;
    $jj = 0;
    while ($ii < $n) {
      // Skip lines until we find the next line with changes. Note: this skips
      // both ' ' (no changes) and '\' (no newline at end of file) lines. If we
      // don't skip the latter, we may incorrectly generate a terminal hunk
      // that has no actual change information when a file doesn't have a
      // terminal newline and not changed near the end of the file. 'patch' will
      // fail to apply the diff if we generate a hunk that does not actually
      // contain changes.
      for ($jj = $ii; $jj < $n; ++$jj) {
        $char = $lines[$jj][0];
        if ($char == '-' || $char == '+') {
          break;
        }
      }
      if ($jj >= $n) {
        break;
      }

      $hunk_start = max($jj - $context, 0);

      // NOTE: There are two tricky considerations here.
      // We can not generate a patch with overlapping hunks, or 'git apply'
      // rejects it after 1.7.3.4.
      // We can not generate a patch with too much trailing context, or
      // 'patch' rejects it.
      // So we need to ensure that we generate disjoint hunks, but don't
      // generate any hunks with too much context.

      $old_lines = 0;
      $new_lines = 0;
      $hunk_adjust = 0;
      $last_change = $jj;
      $break_here = null;
      for (; $jj < $n; ++$jj) {
        if ($lines[$jj][0] == ' ') {

          if ($jj - $last_change > $context) {
            if ($break_here === null) {
              // We haven't seen a change in $context lines, so this is a
              // potential place to break the hunk. However, we need to keep
              // looking in case there is another change fewer than $context
              // lines away, in which case we have to merge the hunks.
              $break_here = $jj;
            }
          }

          if ($jj - $last_change > (($context + 1) * 2)) {
            // We definitely aren't going to merge this with the next hunk, so
            // break out of the loop. We'll end the hunk at $break_here.
            break;
          }
        } else {
          $break_here = null;
          $last_change = $jj;

          if ($lines[$jj][0] == '\\') {
            // When we have a "\ No newline at end of file" line, it does not
            // contribute to either hunk length.
            ++$hunk_adjust;
          } else if ($lines[$jj][0] == '-') {
            ++$old_lines;
          } else if ($lines[$jj][0] == '+') {
            ++$new_lines;
          }
        }
      }

      if ($break_here !== null) {
        $jj = $break_here;
      }

      $hunk_length = min($jj, $n) - $hunk_start;
      $count_length = ($hunk_length - $hunk_adjust);

      $hunk = new ArcanistDiffHunk();
      $hunk->setOldOffset($old_offset + $hunk_start - $ii);
      $hunk->setNewOffset($new_offset + $hunk_start - $ii);
      $hunk->setOldLength($count_length - $new_lines);
      $hunk->setNewLength($count_length - $old_lines);

      $corpus = array_slice($lines, $hunk_start, $hunk_length);
      $corpus = implode('', $corpus);
      $hunk->setCorpus($corpus);

      $results[] = $hunk;

      $old_offset += ($jj - $ii) - $new_lines;
      $new_offset += ($jj - $ii) - $old_lines;
      $ii = $jj;
    }

    return $results;
  }

  private function getOldPath(ArcanistDiffChange $change) {
    $old_path = $change->getOldPath();
    $type = $change->getType();

    if (!strlen($old_path) ||
        $type == ArcanistDiffChangeType::TYPE_ADD) {
      $old_path = null;
    }

    return $old_path;
  }

  private function getCurrentPath(ArcanistDiffChange $change) {
    $cur_path = $change->getCurrentPath();
    $type = $change->getType();

    if (!strlen($cur_path) ||
        $type == ArcanistDiffChangeType::TYPE_DELETE ||
        $type == ArcanistDiffChangeType::TYPE_MULTICOPY) {
      $cur_path = null;
    }

    return $cur_path;
  }

  private function buildHunkChanges(array $hunks, $eol) {
    assert_instances_of($hunks, 'ArcanistDiffHunk');
    $result = array();
    foreach ($hunks as $hunk) {
      $small_hunks = $this->breakHunkIntoSmallHunks($hunk);
      foreach ($small_hunks as $small_hunk) {
        $o_off = $small_hunk->getOldOffset();
        $o_len = $small_hunk->getOldLength();
        $n_off = $small_hunk->getNewOffset();
        $n_len = $small_hunk->getNewLength();
        $corpus = $small_hunk->getCorpus();

        // NOTE: If the length is 1 it can be omitted. Since git does this,
        // we also do it so that "arc export --git" diffs are as similar to
        // real git diffs as possible, which helps debug issues.

        if ($o_len == 1) {
          $o_head = "{$o_off}";
        } else {
          $o_head = "{$o_off},{$o_len}";
        }

        if ($n_len == 1) {
          $n_head = "{$n_off}";
        } else {
          $n_head = "{$n_off},{$n_len}";
        }

        $result[] = "@@ -{$o_head} +{$n_head} @@".$eol;
        $result[] = $corpus;

        $last = substr($corpus, -1);
        if ($last !== false && $last != "\r" && $last != "\n") {
          $result[] = $eol;
        }
      }
    }
    return implode('', $result);
  }

  public function setLoadFileDataCallback($callback) {
    $this->loadFileDataCallback = $callback;
    return $this;
  }

  private function getBlob($phid, $name = null) {
    if ($this->loadFileDataCallback) {
      return call_user_func($this->loadFileDataCallback, $phid);
    }

    if ($this->diskPath) {
      list($blob_data) = execx('tar xfO %s blobs/%s', $this->diskPath, $phid);
      return $blob_data;
    }

    $console = PhutilConsole::getConsole();

    if ($this->conduit) {
      if ($name) {
        $console->writeErr("Downloading binary data for '%s'...\n", $name);
      } else {
        $console->writeErr("Downloading binary data...\n");
      }
      $data_base64 = $this->conduit->callMethodSynchronous(
        'file.download',
        array(
          'phid' => $phid,
        ));
      return base64_decode($data_base64);
    }

    throw new Exception("Nowhere to load blob '{$phid}' from!");
  }

  private function buildBinaryChange(ArcanistDiffChange $change, $old_binary) {
    $eol = $this->getEOL('git');

    // In Git, when we write out a binary file move or copy, we need the
    // original binary for the source and the current binary for the
    // destination.

    if ($old_binary) {
      if ($old_binary->getOriginalFileData() !== null) {
        $old_data = $old_binary->getOriginalFileData();
        $old_phid = null;
      } else {
        $old_data = null;
        $old_phid = $old_binary->getMetadata('old:binary-phid');
      }
    } else {
      $old_data = $change->getOriginalFileData();
      $old_phid = $change->getMetadata('old:binary-phid');
    }

    if ($old_data === null && $old_phid) {
      $name = basename($change->getOldPath());
      $old_data = $this->getBlob($old_phid, $name);
    }

    $old_length = strlen($old_data);

    if ($old_data === null) {
      $old_data = '';
      $old_sha1 = str_repeat('0', 40);
    } else {
      $old_sha1 = sha1("blob {$old_length}\0{$old_data}");
    }

    $new_phid = $change->getMetadata('new:binary-phid');

    $new_data = null;
    if ($change->getCurrentFileData() !== null) {
      $new_data = $change->getCurrentFileData();
    } else if ($new_phid) {
      $name = basename($change->getCurrentPath());
      $new_data = $this->getBlob($new_phid, $name);
    }

    $new_length = strlen($new_data);

    if ($new_data === null) {
      $new_data = '';
      $new_sha1 = str_repeat('0', 40);
    } else {
      $new_sha1 = sha1("blob {$new_length}\0{$new_data}");
    }

    $content = array();
    $content[] = "index {$old_sha1}..{$new_sha1}".$eol;
    $content[] = 'GIT binary patch'.$eol;

    $content[] = "literal {$new_length}".$eol;
    $content[] = $this->emitBinaryDiffBody($new_data).$eol;

    $content[] = "literal {$old_length}".$eol;
    $content[] = $this->emitBinaryDiffBody($old_data).$eol;

    return implode('', $content);
  }

  private function emitBinaryDiffBody($data) {
    $eol = $this->getEOL('git');

    if (!function_exists('gzcompress')) {
      throw new Exception(
        'This patch has binary data. The PHP zlib extension is required to '.
        'apply patches with binary data to git. Install the PHP zlib '.
        'extension to continue.');
    }

    // See emit_binary_diff_body() in diff.c for git's implementation.

    $buf = '';

    $deflated = gzcompress($data);
    $lines = str_split($deflated, 52);
    foreach ($lines as $line) {
      $len = strlen($line);
      // The first character encodes the line length.
      if ($len <= 26) {
        $buf .= chr($len + ord('A') - 1);
      } else {
        $buf .= chr($len - 26 + ord('a') - 1);
      }
      $buf .= self::encodeBase85($line);
      $buf .= $eol;
    }

    return $buf;
  }

  public static function encodeBase85($data) {
    // This is implemented awkwardly in order to closely mirror git's
    // implementation in base85.c

    // It is also implemented awkwardly to work correctly on 32-bit machines.
    // Broadly, this algorithm converts the binary input to printable output
    // by transforming each 4 binary bytes of input to 5 printable bytes of
    // output, one piece at a time.
    //
    // To do this, we convert the 4 bytes into a 32-bit integer, then use
    // modulus and division by 85 to pick out printable bytes (85^5 is slightly
    // larger than 2^32). In C, this algorithm is fairly easy to implement
    // because the accumulator can be made unsigned.
    //
    // In PHP, there are no unsigned integers, so values larger than 2^31 break
    // on 32-bit systems under modulus:
    //
    //   $ php -r 'print (1 << 31) % 13;' # On a 32-bit machine.
    //   -11
    //
    // However, PHP's float type is an IEEE 754 64-bit double precision float,
    // so we can safely store integers up to around 2^53 without loss of
    // precision. To work around the lack of an unsigned type, we just use a
    // double and perform the modulus with fmod().
    //
    // (Since PHP overflows integer operations into floats, we don't need much
    // additional casting.)

    static $map = array(
      '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
      'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
      'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
      'U', 'V', 'W', 'X', 'Y', 'Z',
      'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
      'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
      'u', 'v', 'w', 'x', 'y', 'z',
      '!', '#', '$', '%', '&', '(', ')', '*', '+', '-',
      ';', '<', '=', '>', '?', '@', '^', '_', '`', '{',
      '|', '}', '~',
    );

    $buf = '';

    $pos = 0;
    $bytes = strlen($data);
    while ($bytes) {
      $accum = 0;
      for ($count = 24; $count >= 0; $count -= 8) {
        $val = ord($data[$pos++]);
        $val = $val * (1 << $count);
        $accum = $accum + $val;
        if (--$bytes == 0) {
          break;
        }
      }
      $slice = '';
      for ($count = 4; $count >= 0; $count--) {
        $val = (int)fmod($accum, 85.0);
        $accum = floor($accum / 85.0);
        $slice .= $map[$val];
      }
      $buf .= strrev($slice);
    }

    return $buf;
  }

}


text: include_recipe "../cookbooks/gitolite/default.rb"


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

package com.google.api.ads.admanager.jaxws.v202208;

import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;

/**
 *
 *             A custom field that has the drop-down data type.
 *
 *
 *
Java class for DropDownCustomField complex type.
 *
 *
The following schema fragment specifies the expected content contained within this class.
 *
 *

 * <complexType name="DropDownCustomField">
 *   <complexContent>
 *     <extension base="{https://www.google.com/apis/ads/publisher/v202208}CustomField">
 *       <sequence>
 *         <element name="options" type="{https://www.google.com/apis/ads/publisher/v202208}CustomFieldOption" maxOccurs="unbounded" minOccurs="0"/>
 *       </sequence>
 *     </extension>
 *   </complexContent>
 * </complexType>
 *

 *
 *
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "DropDownCustomField", propOrder = {
    "options"
})
public class DropDownCustomField
    extends CustomField
{

    protected List<CustomFieldOption> options;

    /**
     * Gets the value of the options property.
     *
     *

     * This accessor method returns a reference to the live list,
     * not a snapshot. Therefore any modification you make to the
     * returned list will be present inside the JAXB object.
     * This is why there is not a set method for the options property.
     *
     *

     * For example, to add a new item, do as follows:
     *

     *    getOptions().add(newItem);
     *

     *
     *
     *

     * Objects of the following type(s) are allowed in the list
     * {@link CustomFieldOption }
     *
     *
     */
    public List<CustomFieldOption> getOptions() {
        if (options == null) {
            options = new ArrayList<CustomFieldOption>();
        }
        return this.options;
    }

}


text:
define(function(require, exports, module) {
    main.consumes = [
        "Plugin", "Menu", "MenuItem", "Divider", "settings", "ui", "c9",
        "watcher", "panels", "util", "save", "preferences", "commands", "Tree",
        "Datagrid", "tabManager", "layout", "preferences.experimental"
    ];
    main.provides = ["scm"];
    return main;

    /*
        LEGEND:
            * = done (item should be removed)
            / = half done
            - = not done

        # LOW HANGING FRUIT
            - conflicts
                - save
            - tree
                - add watcher to .git/HEAD
            - fix errors with added/removed files

        # TODO
            - commit
                - do dry-run
                    - add status message for ammend
                    - display branch to commit to
                - amend doesnt work
            - pull
                - pull --rebase
            - detail
                - afterChoose should stage/unstage files instead of opening diff view
                - drag to staged doesnt work sometimes (rowheight issue?)
            - conflicts
                - add commands? detect, next, prev, use 1/ 2
            - branches
                - Harutyun: Resize properly when expanding/collapsing
                - Harutyun: scrollMargin for left, right and bottom doesn't work (same for log, detail)
                - Use icon
            - log
                - Setting indent to 0 doesn't work
                - Remove arrow in title
            - add discard changes to context menu
            - resize issues
            - hotkeys should show bars in dialog
            - conflicts
                - dark theme (Ruben)
            - toolbar
                / pull (or fetch - split button
                    / add fetch dialog (Ruben)
                        / dropdown for remotes
                        - output
                    - Merge doesn't work (no UI for merge message)
                    - Handle error states
                    - Change to splitbutton with form only shown on arrow
                    - reload of log doesn't work after pull
                / push button - split button
                    / add push dialog (Ruben)
                        / dropdown for remotes/branches
                        - output
                    - Handle error states
                    - Change to splitbutton with form only shown on arrow
                    - reload of log doesn't work after push

        # LATER
            # Ruben
                - Choose Git Path - use file dialog
                    - support multiple git roots
                - Add setting to collapse tree to only see roots
            - tree
                - solve edge line cases
            - Compare view
                - save the right file (use ace session clone)
                - git add the left file
                - undo doesn't work
                - scrolling doesn't work well. It should scroll the sides as
                    slowly as the side with most lines would go when scrolling there
            - conflicts
                - automatically add to staging on save
                - dialog for one deleted and one saved file
                - undo
            - branches
                - When updating, only do a partial update (maintain selection, expanded state - see test/data/data.js)
                - make a datagrid?
    */

    function main(options, imports, register) {
        var Plugin = imports.Plugin;
        var Tree = imports.Tree;
        var Menu = imports.Menu;
        var Datagrid = imports.Datagrid;
        var MenuItem = imports.MenuItem;
        var Divider = imports.Divider;
        var settings = imports.settings;
        var ui = imports.ui;
        var c9 = imports.c9;
        var tabManager = imports.tabManager;
        var watcher = imports.watcher;
        var panels = imports.panels;
        var util = imports.util;
        var save = imports.save;
        var layout = imports.layout;
        var prefs = imports.preferences;
        var commands = imports.commands;
        var experimental = imports["preferences.experimental"];

        // var Tooltip = require("ace_tree/tooltip");
        var DataProvider = require("ace_tree/data_provider");
        var escapeHTML = require("ace/lib/lang").escapeHTML;

        /***** Initialization *****/

        var ENABLED = experimental.addExperiment("git", !c9.hosted, "Panels/Source Control Management");
        if (!ENABLED)
            return register(null, { "scm": { on: function() {} }});

        var plugin = new Plugin("Ajax.org", main.consumes);
        var emit = plugin.getEmitter();

        var scms = {};
        var scm;

        var workspaceDir = c9.workspaceDir; // + "/plugins/c9.ide.scm/mock/git";

        var loaded = false;
        function load() {
            if (loaded) return false;
            loaded = true;

            commands.addCommand({
                name: "blame",
                group: "scm",
                exec: function() {
                    var tab = tabManager.focussedTab || tabManager.getPanes()[0].activeTab;
                    if (!tab || !tab.path || tab.editorType != "ace")
                        return;
                    var blameAnnotation, err, data;
                    var ace = tab.editor.ace;
                    var session = ace.session;
                    require(["./blame"], function(blameModule) {
                        if (ace.session != session)
                            return;
                        blameAnnotation = blameModule.annotate(ace);
                        done();
                    });

                    var path = tab.path;
                    scm.getBlame(path, function(err, blameInfo) {
                        if (err) return console.error(err);
                        data = blameInfo;
                        done();
                    });

                    function done() {
                        if (!blameAnnotation) return;
                        if (data === null) return;

                        blameAnnotation.setData(data);
                    }
                },
                isAvailable: function() {
                    var tab = tabManager.focussedTab || tabManager.getPanes()[0].activeTab;
                    if (!tab || !tab.path || tab.editorType != "ace")
                        return false;
                    return true;
                }
            }, plugin);

            // commands.addCommand({
            //     name: "addall",
            //     group: "scm",
            //     exec: function(){ addAll(); }
            // }, plugin);

            // commands.addCommand({
            //     name: "unstageall",
            //     group: "scm",
            //     exec: function(){ unstageAll(); }
            // }, plugin);

            // commands.addCommand({
            //     name: "fetch",
            //     group: "scm",
            //     exec: function(){ fetch(); }
            // }, plugin);

            // commands.addCommand({
            //     name: "push",
            //     group: "scm",
            //     exec: function(){ push({}); }
            // }, plugin);

            // commands.addCommand({
            //     name: "pull",
            //     group: "scm",
            //     exec: function(){ pull({}); }
            // }, plugin);

            c9.on("ready", function _() {
                if (scm) return;

                if (!isDetecting)
                    emit.sticky("scm", null);
                else
                    setTimeout(_, 100);
            }, plugin);
        }

        /***** Methods *****/

        var isDetecting = 0;
        function registerSCM(name, scmPlugin) {
            scms[name] = scmPlugin;
            if (!scm) scm = scmPlugin;

            emit("register", { plugin: scmPlugin });

            isDetecting++;
            scmPlugin.detect("/", function(err, active) {
                isDetecting--;
                if (err || !active) return;
                emit.sticky("scm", scmPlugin);
            });
        }

        function unregisterSCM(name, scmPlugin) {
            delete scms[name];

            emit("unregister", { plugin: scmPlugin });
        }

        function openDiff(options, callback) {
            var found;
            if (tabManager.getTabs().some(function(tab) {
                if (tab.editorType == "diff.unified") {
                    if (tab.document.getSession().isEqual(options)) {
                        found = tab;
                        return true;
                    }
                }
                return false;
            })) return tabManager.focusTab(found);

            tabManager[options.preview ? "preview" : "open"]({
                newfile: true,
                editorType: "diff.unified",
                focus: true,
                document: {
                    "title": "Compare View",
                    "diff.unified": options
                }
                // path: "/compare.diff"
            }, function(err, tab) {
                callback && callback(err, tab);
            });
        }

        /***** Lifecycle *****/

        plugin.on("load", function() {
            load();
        });
        plugin.on("unload", function() {
            loaded = false;
        });

        /***** Register and define API *****/

        plugin.freezePublicAPI({
            /**
             *
             */
            register: registerSCM,

            /**
             *
             */
            unregister: unregisterSCM,

            /**
             *
             */
            openDiff: openDiff
        });

        register(null, {
            "scm": plugin
        });
    }
});


text:
package modal;

import java.util.List;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "data")
public class SearchData {

	@XmlElement(name = "task")
	private List<SearchTask> searchList;

	public List<SearchTask> getTasks() {

		return searchList;
	}

	public void setSearch(List<SearchTask> tasks) {
		this.searchList = tasks;
	}
}


text:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<title> Class Hierarchy</title>
<meta name="date" content="2012-09-01">
<link rel="stylesheet" type="text/css" href="stylesheet.css" title="Style">

JavaScript is disabled on your browser.

Package

Class

Use

Tree

Deprecated

Index

Help

Prev

Next

Frames

No Frames

All Classes

Hierarchy For Package <Unnamed>

Class Hierarchy

java.lang.Object

CloudMadeDistanceCalculator (implements DistanceCalculator)

CloudMadeGeocoder (implements Geocoder)

java.awt.Component (implements java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable)

java.awt.Container

java.awt.Window (implements javax.accessibility.Accessible)

java.awt.Frame (implements java.awt.MenuContainer)

javax.swing.JFrame (implements javax.accessibility.Accessible, javax.swing.RootPaneContainer, javax.swing.WindowConstants)

PlannerView (implements java.util.Observer)

PropertiesView (implements java.util.Observer)

CSVHandler

DistancePlanner

EuklidDistanceCalculator (implements DistanceCalculator)

GeoAddress

GoogleGeocoder (implements Geocoder)

Input

Letter

MapViewer

java.util.Observable

PlannerModel

Osm2poDistanceCalculator (implements DistanceCalculator)

Team

TeamRoutConstructor

java.awt.event.WindowAdapter (implements java.awt.event.WindowFocusListener, java.awt.event.WindowListener, java.awt.event.WindowStateListener)

Console (implements java.awt.event.ActionListener, java.lang.Runnable, java.awt.event.WindowListener)

PlannerController (implements java.awt.event.ActionListener, javax.swing.event.ListSelectionListener)

PropertiesController (implements java.awt.event.ActionListener)

Interface Hierarchy

DistanceCalculator

Geocoder

Enum Hierarchy

java.lang.Object

java.lang.Enum<E> (implements java.lang.Comparable<T>, java.io.Serializable)

Course

Modus

Package

Class

Use

Tree

Deprecated

Index

Help

Prev

Next

Frames

No Frames

All Classes