text:
"""Version 2 of class Optimizer."""
# pylint: disable=g-bad-name

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import abc
import functools

import six

from tensorflow.python.distribute import distribute_lib
from tensorflow.python.distribute import distribution_strategy_context as distribute_ctx
from tensorflow.python.distribute import reduce_util as ds_reduce_util
from tensorflow.python.distribute import values as distributed_values
from tensorflow.python.eager import backprop
from tensorflow.python.eager import context
from tensorflow.python.framework import dtypes
from tensorflow.python.framework import ops
from tensorflow.python.keras import backend
from tensorflow.python.keras import initializers
from tensorflow.python.keras.engine import base_layer_utils
from tensorflow.python.ops import array_ops
from tensorflow.python.ops import clip_ops
from tensorflow.python.ops import gradients
from tensorflow.python.ops import math_ops
from tensorflow.python.ops import resource_variable_ops
from tensorflow.python.ops import variables as tf_variables
from tensorflow.python.platform import tf_logging as logging
from tensorflow.python.training.checkpointable import base as checkpointable
from tensorflow.python.util import nest
from tensorflow.python.util.tf_export import keras_export

def _deduplicate_indexed_slices(values, indices):
  """Sums `values` associated with any non-unique `indices`.

  Args:
    values: A `Tensor` with rank >= 1.
    indices: A one-dimensional integer `Tensor`, indexing into the first
      dimension of `values` (as in an IndexedSlices object).

  Returns:
    A tuple of (`summed_values`, `unique_indices`) where `unique_indices` is a
    de-duplicated version of `indices` and `summed_values` contains the sum of
    `values` slices associated with each unique index.
  """
  unique_indices, new_index_positions = array_ops.unique(indices)
  summed_values = math_ops.unsorted_segment_sum(
      values, new_index_positions,
      array_ops.shape(unique_indices)[0])
  return (summed_values, unique_indices)

@six.add_metaclass(abc.ABCMeta)
@keras_export("keras.optimizers.Optimizer", v1=[])
class OptimizerV2(checkpointable.CheckpointableBase):
  """Updated base class for optimizers.

  This class defines the API to add Ops to train a model.  You never use this
  class directly, but instead instantiate one of its subclasses such as
  `GradientDescentOptimizer`, `AdagradOptimizer`, or `MomentumOptimizer`.

  ### Usage

  ```python
  # Create an optimizer with the desired parameters.
  opt = GradientDescentOptimizer(learning_rate=0.1)
  # Add Ops to the graph to minimize a cost by updating a list of variables.
  # "cost" is a Tensor, and the list of variables contains tf.Variable
  # objects.
  opt_op = opt.minimize(cost, var_list=<list of variables>)
  ```

  In the training program you will just have to run the returned Op.

  ```python
  # Execute opt_op to do one step of training:
  opt_op.run()
  ```

  ### Processing gradients before applying them.

  Calling `minimize()` takes care of both computing the gradients and
  applying them to the variables.  If you want to process the gradients
  before applying them you can instead use the optimizer in three steps:

  1.  Compute the gradients with `compute_gradients()`.
  2.  Process the gradients as you wish.
  3.  Apply the processed gradients with `apply_gradients()`.

  Example:

  ```python
  # Create an optimizer.
  opt = GradientDescentOptimizer(learning_rate=0.1)

  # Compute the gradients for a list of variables.
  grads_and_vars = opt.compute_gradients(loss, <list of variables>)

  # grads_and_vars is a list of tuples (gradient, variable).  Do whatever you
  # need to the 'gradient' part, for example cap them, etc.
  capped_grads_and_vars = [(MyCapper(gv[0]), gv[1]) for gv in grads_and_vars]

  # Ask the optimizer to apply the capped gradients.
  opt.apply_gradients(capped_grads_and_vars)
  ```

  ### Slots

  Some optimizer subclasses, such as `MomentumOptimizer` and `AdagradOptimizer`
  allocate and manage additional variables associated with the variables to
  train.  These are called <i>Slots</i>.  Slots have names and you can ask the
  optimizer for the names of the slots that it uses.  Once you have a slot name
  you can ask the optimizer for the variable it created to hold the slot value.

  This can be useful if you want to log debug a training algorithm, report stats
  about the slots, etc.

  ### Hyper parameters

  These are arguments passed to the optimizer subclass constructor
  (the `__init__` method), and then passed to `self._set_hyper()`.
  They can be either regular Python values (like 1.0), tensors, or
  callables. If they are callable, the callable will be called during
  `apply_gradients()` to get the value for the hyper parameter.

  """

  def __init__(self, name, **kwargs):
    """Create a new Optimizer.

    This must be called by the constructors of subclasses.
    Note that Optimizer instances should not bind to a single graph,
    and so shouldn't keep Tensors as member variables. Generally
    you should be able to use the _set_hyper()/state.get_hyper()
    facility instead.

    This class in stateful and thread-compatible.

    Args:
      name: A non-empty string.  The name to use for accumulators created
        for the optimizer.
      **kwargs: keyword arguments. Allowed to be {`clipnorm`, `clipvalue`, `lr`,
        `decay`}. `clipnorm` is clip gradients by norm; `clipvalue` is clip
        gradients by value, `decay` is included for backward compatibility to
        allow time inverse decay of learning rate. `lr` is included for backward
        compatibility, recommended to use `learning_rate` instead.

    Raises:
      ValueError: If name is malformed.
      RuntimeError: If _create_slots has been overridden instead of
          _create_vars.
    """
    allowed_kwargs = {"clipnorm", "clipvalue", "lr", "decay"}
    for k in kwargs:
      if k not in allowed_kwargs:
        raise TypeError("Unexpected keyword argument "
                        "passed to optimizer: " + str(k))
      # checks that all keyword arguments are non-negative.
      if kwargs[k] < 0:
        raise ValueError("Expected {} >= 0, received: {}".format(k, kwargs[k]))

    self._use_locking = True
    self._name = name
    self._hyper = {}
    # dict: {variable name : {slot name : variable}}
    self._slots = {}
    self._slot_names = []
    self._weights = []
    self._iterations = None

    # For implementing Checkpointable. Stores information about how to restore
    # slot variables which have not yet been created
    # (checkpointable._CheckpointPosition objects).
    #  {slot_name :
    #      {_var_key(variable_to_train): [checkpoint_position, ... ], ... },
    #   ... }
    self._deferred_slot_restorations = {}

    decay = kwargs.pop("decay", 0.0)
    if decay < 0.:
      raise ValueError("decay cannot be less than 0: {}".format(decay))
    self._initial_decay = decay
    if "clipnorm" in kwargs:
      self.clipnorm = kwargs.pop("clipnorm")
    if "clipvalue" in kwargs:
      self.clipvalue = kwargs.pop("clipvalue")

    self._hypers_created = False

  def minimize(self, loss, var_list, grad_loss=None, name=None):
    """Add operations to minimize `loss` by updating `var_list`.

    This method simply combines calls `compute_gradients()` and
    `apply_gradients()`. If you want to process the gradient before applying
    them call `compute_gradients()` and `apply_gradients()` explicitly instead
    of using this function.

    Args:
      loss: A callable taking no arguments which returns the value to minimize.
      var_list: list or tuple of `Variable` objects to update to minimize
        `loss`.
      grad_loss: Optional. A `Tensor` holding the gradient computed for `loss`.
      name: Optional name for the returned operation.

    Returns:
      An Operation that updates the variables in `var_list`.  If `global_step`
      was not `None`, that operation also increments `global_step`.

    Raises:
      ValueError: If some of the variables are not `Variable` objects.

    @compatibility(eager)
    When eager execution is enabled, `loss` should be a Python function that
    takes no arguments and computes the value to be minimized. Minimization (and
    gradient computation) is done with respect to the elements of `var_list`.
    `grad_loss` is ignored when eager execution is enabled.
    @end_compatibility
    """
    grads_and_vars = self._compute_gradients(
        loss, var_list=var_list, grad_loss=grad_loss)

    return self.apply_gradients(grads_and_vars, name=name)

  def _compute_gradients(self, loss, var_list, grad_loss=None):
    """Compute gradients of `loss` for the variables in `var_list`.

    This is the first part of `minimize()`.  It returns a list
    of (gradient, variable) pairs where "gradient" is the gradient
    for "variable".  Note that "gradient" can be a `Tensor`, an
    `IndexedSlices`, or `None` if there is no gradient for the
    given variable.

    Args:
      loss: A callable taking no arguments which returns the value to minimize.
      var_list: List or tuple of `tf.Variable` to update to minimize
        `loss`.  Defaults to the list of variables collected in the graph under
        the key `GraphKeys.TRAINABLE_VARIABLES`.
      grad_loss: Optional. A `Tensor` holding the gradient computed for `loss`.

    Returns:
      A list of (gradient, variable) pairs. Variable is always present, but
      gradient can be `None`.

    Raises:
      TypeError: If `var_list` contains anything else than `Variable` objects.
      ValueError: If some arguments are invalid, or var_list is None.
    """
    var_list = nest.flatten(var_list)
    # TODO(josh11b): Test that we handle weight decay in a reasonable way.
    with backprop.GradientTape() as tape:
      tape.watch(var_list)
      loss_value = loss()
      loss_value = self._scale_loss(loss_value)
    grads = tape.gradient(loss_value, var_list, grad_loss)

    if hasattr(self, "clipnorm"):
      grads = [clip_ops.clip_by_norm(g, self.clipnorm) for g in grads]
    if hasattr(self, "clipvalue"):
      grads = [
          clip_ops.clip_by_value(g, -self.clipvalue, self.clipvalue)
          for g in grads
      ]

    grads_and_vars = list(zip(grads, var_list))
    self._assert_valid_dtypes([
        v for g, v in grads_and_vars
        if g is not None and v.dtype != dtypes.resource
    ])

    return grads_and_vars

  @staticmethod
  def _scale_loss(loss_value):
    if distribute_lib.get_loss_reduction() == ds_reduce_util.ReduceOp.MEAN:
      num_replicas = \
        distribute_ctx.get_distribution_strategy().num_replicas_in_sync
      if num_replicas > 1:
        loss_value *= (1. / num_replicas)
    return loss_value

  def get_gradients(self, loss, params):
    """Returns gradients of `loss` with respect to `params`.

    Arguments:
      loss: Loss tensor.
      params: List of variables.

    Returns:
      List of gradient tensors.

    Raises:
      ValueError: In case any gradient cannot be computed (e.g. if gradient
        function not implemented).
    """
    loss = self._scale_loss(loss)
    grads = gradients.gradients(loss, params)
    if None in grads:
      raise ValueError("An operation has `None` for gradient. "
                       "Please make sure that all of your ops have a "
                       "gradient defined (i.e. are differentiable). "
                       "Common ops without gradient: "
                       "K.argmax, K.round, K.eval.")
    if hasattr(self, "clipnorm"):
      grads = [clip_ops.clip_by_norm(g, self.clipnorm) for g in grads]
    if hasattr(self, "clipvalue"):
      grads = [
          clip_ops.clip_by_value(g, -self.clipvalue, self.clipvalue)
          for g in grads
      ]
    return grads

  def apply_gradients(self, grads_and_vars, name=None):
    """Apply gradients to variables.

    This is the second part of `minimize()`. It returns an `Operation` that
    applies gradients.

    Args:
      grads_and_vars: List of (gradient, variable) pairs as returned by
        `compute_gradients()`.
      name: Optional name for the returned operation.  Default to the name
        passed to the `Optimizer` constructor.

    Returns:
      An `Operation` that applies the specified gradients. If `global_step`
      was not None, that operation also increments `global_step`.

    Raises:
      TypeError: If `grads_and_vars` is malformed.
      ValueError: If none of the variables have gradients.
    """
    grads_and_vars = _filter_grads(grads_and_vars)
    var_list = [v for (_, v) in grads_and_vars]
    if distribute_ctx.has_distribution_strategy():
      reduced_grads = merge_grads(grads_and_vars)
      grads_and_vars = zip(reduced_grads, var_list)

    self._create_hypers()
    with ops.init_scope():
      self._create_slots(var_list)
    update_ops = []

    self._prepare(var_list)

    def update_grad_to_var(grad, var):
      """Apply gradient to variable."""
      if isinstance(var, ops.Tensor):
        raise NotImplementedError("Trying to update a Tensor ", var)
      if isinstance(grad, ops.IndexedSlices):
        if var.constraint is not None:
          raise RuntimeError(
              "Cannot use a constraint function on a sparse variable.")
        return self._resource_apply_sparse_duplicate_indices(
            grad.values, var, grad.indices)
      update_op = self._resource_apply_dense(grad, var)
      if var.constraint is not None:
        with ops.control_dependencies([update_op]):
          return var.assign(var.constraint(var))
      else:
        return update_op

    with ops.name_scope(name, self._name) as name:
      for grad, var in grads_and_vars:
        scope_name = ("" if ops.executing_eagerly_outside_functions() else
                      "_" + var.op.name)
        with ops.name_scope("update" + scope_name):
          update_ops.append(update_grad_to_var(grad, var))
      # control dependencies does not work in per replica mode, please change
      # this once b/118841692 is fixed.
      # with ops.control_dependencies(update_ops):
      #   apply_updates = self._iterations.assign_add(1).op
      apply_updates = merge_update_step(update_ops, self.iterations)
      return apply_updates

  def get_updates(self, loss, params):
    grads = self.get_gradients(loss, params)
    grads_and_vars = list(zip(grads, params))
    self._assert_valid_dtypes([
        v for g, v in grads_and_vars
        if g is not None and v.dtype != dtypes.resource
    ])
    return [self.apply_gradients(grads_and_vars)]

  def _set_hyper(self, name, value):
    """set hyper `name` to value. value can be callable, tensor, numeric."""
    if name not in self._hyper:
      self._hyper[name] = value
    else:
      prev_value = self._hyper[name]
      if callable(prev_value) or isinstance(prev_value,
                                            (ops.Tensor, int, float)):
        self._hyper[name] = value
      else:
        backend.set_value(self._hyper[name], value)

  def _get_hyper(self, name, dtype=None):
    value = self._hyper[name]
    if callable(value):
      value = value()
    if dtype:
      return math_ops.cast(value, dtype)
    else:
      return value

  def __getattribute__(self, name):
    """Overridden to support hyperparameter access."""
    try:
      return super(OptimizerV2, self).__getattribute__(name)
    except AttributeError as e:
      # Needed to avoid infinite recursion with __setattr__.
      if name == "_hyper":
        raise e
      # Backwards compatibility with Keras optimizers.
      if name == "lr":
        name = "learning_rate"
      if name in self._hyper:
        return self._hyper[name]
      raise e

  def __setattr__(self, name, value):
    """Override setattr to support dynamic hyperparameter setting."""
    # Backwards compatibility with Keras optimizers.
    if name == "lr":
      name = "learning_rate"
    if hasattr(self, "_hyper") and name in self._hyper:
      self._set_hyper(name, value)
    else:
      super(OptimizerV2, self).__setattr__(name, value)

  def get_slot_names(self):
    """A list of names for this optimizer's slots."""
    return self._slot_names

  def add_slot(self, var, slot_name, initializer="zeros"):
    """Add a new slot variable for `var`."""
    if slot_name not in self._slot_names:
      self._slot_names.append(slot_name)
    var_key = _var_key(var)
    slot_dict = self._slots.setdefault(var_key, {})
    weight = slot_dict.get(slot_name, None)
    if weight is None:
      if isinstance(initializer, six.string_types) or callable(initializer):
        initializer = initializers.get(initializer)
        initial_value = functools.partial(
            initializer, shape=var.shape, dtype=var.dtype)
      else:
        initial_value = initializer
      weight = tf_variables.Variable(
          name="%s/%s" % (var._shared_name, slot_name),  # pylint: disable=protected-access
          dtype=var.dtype,
          trainable=False,
          initial_value=initial_value)
      backend.track_variable(weight)
      slot_dict[slot_name] = weight
      self._restore_slot_variable(
          slot_name=slot_name, variable=var,
          slot_variable=weight)
      self._weights.append(weight)
    return weight

  def get_slot(self, var, slot_name):
    var_key = _var_key(var)
    slot_dict = self._slots[var_key]
    return slot_dict[slot_name]

  def _prepare(self, var_list):
    pass

  def _create_hypers(self):
    if self._hypers_created:
      return
    if self._iterations is None:
      with ops.device("cpu:0"):
        self._iterations = self.add_weight(
            "iter",
            shape=[],
            dtype=dtypes.int64,
            trainable=False,
            aggregation=tf_variables.VariableAggregation.ONLY_FIRST_REPLICA)
        self._weights.append(self._iterations)
    for name, value in self._hyper.items():
      if isinstance(value, ops.Tensor) or callable(value):
        pass
      else:
        self._hyper[name] = self.add_weight(
            name,
            shape=[],
            trainable=False,
            initializer=value,
            aggregation=tf_variables.VariableAggregation.ONLY_FIRST_REPLICA)
    self._hypers_created = True

  @property
  def iterations(self):
    """Variable. The number of training steps this Optimizer has run."""
    if not self._hypers_created:
      self._create_hypers()
    return self._iterations

  @iterations.setter
  def iterations(self, variable):
    if self._hypers_created:
      raise RuntimeError("Cannot set `iterations` to a new Variable after"
                         "the Optimizer weights have been created")
    self._iterations = variable
    self._weights.append(self._iterations)

  def _decayed_lr(self, var_dtype):
    """Get decayed learning rate as a Tensor with dtype=var_dtype."""
    lr_t = self._get_hyper("learning_rate", var_dtype)
    if self._initial_decay > 0.:
      local_step = math_ops.cast(self.iterations, var_dtype)
      decay_t = self._get_hyper("decay", var_dtype)
      lr_t = lr_t / (1. + decay_t * local_step)
    return lr_t

  @abc.abstractmethod
  def get_config(self):
    """Returns the config of the optimimizer.

    An optimizer config is a Python dictionary (serializable)
    containing the configuration of an optimizer.
    The same optimizer can be reinstantiated later
    (without any saved state) from this configuration.

    Returns:
        Python dictionary.
    """
    config = {"name": self._name}
    if hasattr(self, "clipnorm"):
      config["clipnorm"] = self.clipnorm
    if hasattr(self, "clipvalue"):
      config["clipvalue"] = self.clipvalue
    return config

  @classmethod
  def from_config(cls, config, custom_objects=None):
    """Creates an optimizer from its config.

    This method is the reverse of `get_config`,
    capable of instantiating the same optimizer from the config
    dictionary.

    Arguments:
        config: A Python dictionary, typically the output of get_config.
        custom_objects: A Python dictionary mapping names to additional Python
          objects used to create this optimizer, such as a function used for a
          hyperparameter.

    Returns:
        An optimizer instance.
    """
    if "lr" in config:
      config["learning_rate"] = config.pop("lr")
    return cls(**config)

  def _serialize_hyperparameter(self, hyperparameter_name):
    """Serialize a hyperparameter that can be a float, callable, or Tensor."""
    value = self._get_hyper(hyperparameter_name)
    if callable(value):
      return value()
    if isinstance(value, (ops.Tensor, tf_variables.Variable,
                          distributed_values.TPUMirroredVariable)):
      return backend.get_value(value)
    return value

  def variables(self):
    """Returns variables of this Optimizer based on the order created."""
    return self._weights

  @property
  def weights(self):
    """Returns variables of this Optimizer based on the order created."""
    return self._weights

  def get_weights(self):
    params = self.weights
    return backend.batch_get_value(params)

  # TODO(tanzheny): Maybe share this logic with base_layer.
  def set_weights(self, weights):
    params = self.weights
    if len(params) != len(weights):
      raise ValueError(
          "You called `set_weights(weights)` on optimizer " + self._name +
          " with a  weight list of length " + str(len(weights)) +
          ", but the optimizer was expecting " + str(len(params)) +
          " weights. Provided weights: " + str(weights)[:50] + "...")
    if not params:
      return
    weight_value_tuples = []
    param_values = backend.batch_get_value(params)
    for pv, p, w in zip(param_values, params, weights):
      if pv.shape != w.shape:
        raise ValueError("Optimizer weight shape " + str(pv.shape) +
                         " not compatible with "
                         "provided weight shape " + str(w.shape))
      weight_value_tuples.append((p, w))
    backend.batch_set_value(weight_value_tuples)

  def add_weight(self,
                 name,
                 shape,
                 dtype=None,
                 initializer="zeros",
                 trainable=None,
                 synchronization=tf_variables.VariableSynchronization.AUTO,
                 aggregation=tf_variables.VariableAggregation.NONE):

    if dtype is None:
      dtype = dtypes.float32
    if isinstance(initializer, six.string_types) or callable(initializer):
      initializer = initializers.get(initializer)

    if synchronization == tf_variables.VariableSynchronization.ON_READ:
      if trainable:
        raise ValueError(
            "Synchronization value can be set to "
            "VariableSynchronization.ON_READ only for non-trainable variables. "
            "You have specified trainable=True and "
            "synchronization=VariableSynchronization.ON_READ.")
      else:
        # Set trainable to be false when variable is to be synced on read.
        trainable = False
    elif trainable is None:
      trainable = True

    variable = self._add_variable_with_custom_getter(
        name=name,
        shape=shape,
        getter=base_layer_utils.make_variable,
        overwrite=True,
        initializer=initializer,
        dtype=dtype,
        trainable=trainable,
        use_resource=True,
        synchronization=synchronization,
        aggregation=aggregation)
    backend.track_variable(variable)

    return variable

  def _assert_valid_dtypes(self, tensors):
    """Asserts tensors are all valid types (see `_valid_dtypes`).

    Args:
      tensors: Tensors to check.

    Raises:
      ValueError: If any tensor is not a valid type.
    """
    valid_dtypes = self._valid_dtypes()
    for t in tensors:
      dtype = t.dtype.base_dtype
      if dtype not in valid_dtypes:
        raise ValueError("Invalid type %r for %s, expected: %s." %
                         (dtype, t.name, [v for v in valid_dtypes]))

  def _valid_dtypes(self):
    """Valid types for loss, variables and gradients.

    Subclasses should override to allow other float types.

    Returns:
      Valid types for loss, variables and gradients.
    """
    return set(
        [dtypes.float16, dtypes.bfloat16, dtypes.float32, dtypes.float64])

  def _call_if_callable(self, param):
    """Call the function if param is callable."""
    return param() if callable(param) else param

  def _resource_apply_dense(self, grad, handle):
    """Add ops to apply dense gradients to the variable `handle`.

    Args:
      grad: a `Tensor` representing the gradient.
      handle: a `Tensor` of dtype `resource` which points to the variable to be
        updated.

    Returns:
      An `Operation` which updates the value of the variable.
    """
    raise NotImplementedError()

  def _resource_apply_sparse_duplicate_indices(self, grad, handle, indices):
    """Add ops to apply sparse gradients to `handle`, with repeated indices.

    Optimizers which override this method must deal with repeated indices. See
    the docstring of `_apply_sparse_duplicate_indices` for details. By default
    the correct behavior, to sum non-unique indices and their associated
    gradients, is enforced by first pre-processing `grad` and `indices` and
    passing them on to `_resource_apply_sparse`. Optimizers which deal correctly
    with duplicate indices may instead override this method to avoid the
    overhead of summing.

    Args:
      grad: a `Tensor` representing the gradient for the affected indices.
      handle: a `Tensor` of dtype `resource` which points to the variable to be
        updated.
      indices: a `Tensor` of integral type representing the indices for which
        the gradient is nonzero. Indices may be repeated.

    Returns:
      An `Operation` which updates the value of the variable.
    """
    summed_grad, unique_indices = _deduplicate_indexed_slices(
        values=grad, indices=indices)
    return self._resource_apply_sparse(summed_grad, handle, unique_indices)

  def _resource_apply_sparse(self, grad, handle, indices):
    """Add ops to apply sparse gradients to the variable `handle`.

    Similar to `_apply_sparse`, the `indices` argument to this method has been
    de-duplicated. Optimizers which deal correctly with non-unique indices may
    instead override `_resource_apply_sparse_duplicate_indices` to avoid this
    overhead.

    Args:
      grad: a `Tensor` representing the gradient for the affected indices.
      handle: a `Tensor` of dtype `resource` which points to the variable to be
        updated.
      indices: a `Tensor` of integral type representing the indices for which
        the gradient is nonzero. Indices are unique.

    Returns:
      An `Operation` which updates the value of the variable.
    """
    raise NotImplementedError()

  def _resource_scatter_add(self, x, i, v):
    with ops.control_dependencies(
        [resource_variable_ops.resource_scatter_add(x.handle, i, v)]):
      return x.value()

  def _resource_scatter_update(self, x, i, v):
    with ops.control_dependencies(
        [resource_variable_ops.resource_scatter_update(x.handle, i, v)]):
      return x.value()

  # ---------------
  # For implementing the checkpointable interface
  # ---------------

  def _restore_slot_variable(self, slot_name, variable, slot_variable):
    """Restore a newly created slot variable's value."""
    variable_key = _var_key(variable)
    deferred_restorations = self._deferred_slot_restorations.get(
        slot_name, {}).pop(variable_key, [])
    # Iterate over restores, highest restore UID first to minimize the number
    # of assignments.
    deferred_restorations.sort(key=lambda position: position.restore_uid,
                               reverse=True)
    for checkpoint_position in deferred_restorations:
      checkpoint_position.restore(slot_variable)

  def _create_or_restore_slot_variable(
      self, slot_variable_position, slot_name, variable):
    """Restore a slot variable's value, possibly creating it.

    Called when a variable which has an associated slot variable is created or
    restored. When executing eagerly, we create the slot variable with a
    restoring initializer.

    No new variables are created when graph building. Instead,
    _restore_slot_variable catches these after normal creation and adds restore
    ops to the graph. This method is nonetheless important when graph building
    for the case when a slot variable has already been created but `variable`
    has just been added to a dependency graph (causing us to realize that the
    slot variable needs to be restored).

    Args:
      slot_variable_position: A `checkpointable._CheckpointPosition` object
        indicating the slot variable `Checkpointable` object to be restored.
      slot_name: The name of this `Optimizer`'s slot to restore into.
      variable: The variable object this slot is being created for.
    """
    variable_key = _var_key(variable)
    slot_dict = self._slots.get(variable_key, {})
    slot_variable = slot_dict.get(slot_name, None)
    if (slot_variable is None and context.executing_eagerly() and
        slot_variable_position.is_simple_variable()
        # Defer slot variable creation if there is an active variable creator
        # scope. Generally we'd like to eagerly create/restore slot variables
        # when possible, but this may mean that scopes intended to catch
        # `variable` also catch its eagerly created slot variable
        # unintentionally (specifically make_template would add a dependency on
        # a slot variable if not for this case). Deferring is mostly harmless
        # (aside from double initialization), and makes variable creator scopes
        # behave the same way they do when graph building.
        and not ops.get_default_graph()._variable_creator_stack):  # pylint: disable=protected-access
      initializer = checkpointable.CheckpointInitialValue(
          checkpoint_position=slot_variable_position)
      slot_variable = self.add_slot(
          var=variable,
          initializer=initializer,
          slot_name=slot_name)
      # Slot variables are not owned by any one object (because we don't want to
      # save the slot variable if the optimizer is saved without the non-slot
      # variable, or if the non-slot variable is saved without the optimizer;
      # it's a dependency hypergraph with edges of the form (optimizer, non-slot
      # variable, variable)). So we don't _track_ slot variables anywhere, and
      # instead special-case this dependency and otherwise pretend it's a normal
      # graph.
    if slot_variable is not None:
      # If we've either made this slot variable, or if we've pulled out an
      # existing slot variable, we should restore it.
      slot_variable_position.restore(slot_variable)
    else:
      # We didn't make the slot variable. Defer restoring until it gets created
      # normally. We keep a list rather than the one with the highest restore
      # UID in case slot variables have their own dependencies, in which case
      # those could differ between restores.
      self._deferred_slot_restorations.setdefault(
          slot_name, {}).setdefault(variable_key, []).append(
              slot_variable_position)

def _filter_grads(grads_and_vars):
  """Filter out iterable with grad equal to None."""
  grads_and_vars = tuple(grads_and_vars)
  if not grads_and_vars:
    return grads_and_vars
  filtered = []
  vars_with_empty_grads = []
  for grad, var in grads_and_vars:
    if grad is None:
      vars_with_empty_grads.append(var)
    else:
      filtered.append((grad, var))
  filtered = tuple(filtered)
  if not filtered:
    raise ValueError("No gradients provided for any variable: %s." %
                     ([v.name for _, v in grads_and_vars],))
  if vars_with_empty_grads:
    logging.warning(
        ("Gradients does not exist for variables %s when minimizing the loss."),
        ([v.name for v in vars_with_empty_grads]))
  return filtered

def merge_update_step(update_ops, local_step):
  """Merge local step counter update from different replicas."""

  def merge_update_step_fn(strategy, update_ops, local_step):
    merged_ops = []
    for update_op in update_ops:
      merged_ops.append(strategy.group(update_op))
    with ops.control_dependencies(merged_ops):
      incre_op = local_step.assign_add(1).op
    return incre_op

  return distribute_ctx.get_replica_context().merge_call(
      merge_update_step_fn, args=(update_ops, local_step))

def merge_grads(grads_and_vars):
  """Merge gradients from different replicas."""

  def merge_grad_fn(strategy, grads_and_vars):
    reduced_grads = strategy.batch_reduce(ds_reduce_util.ReduceOp.SUM,
                                          grads_and_vars)
    return reduced_grads

  return distribute_ctx.get_replica_context().merge_call(
      merge_grad_fn, args=(grads_and_vars,))

def _var_key(var):
  """Key for representing a primary variable, for looking up slots.

  In graph mode the name is derived from the var shared name.
  In eager mode the name is derived from the var unique id.
  If distribution strategy exists, get the primary variable first.

  Args:
    var: the variable.

  Returns:
    the unique name of the variable.
  """

  # pylint: disable=protected-access
  if distribute_ctx.has_distribution_strategy() and hasattr(
      var, "_primary_var"):
    var = var._primary_var
  if hasattr(var, "op"):
    return var._shared_name
  return var._unique_id

def _get_slot_key_from_var(var, slot_name):
  """Get the slot key for the variable: var_name/slot_name."""

  name = _var_key(var)
  return name + "/" + slot_name


text:
/* Javadoc style sheet */
/*
Overall document style
*/
body {
    background-color:#f4f4f4;
    color:#111111;
    font-family:Arial, Helvetica, sans-serif;
    font-size:76%;
    padding:0;
    margin:0;
    margin-top:20px;
}
a:link, a:visited {
    text-decoration:none;
    color:#0074D9; /* OK */
}
a:hover, a:focus {
    text-decoration:none;
    color:#2ECC40; /* OK */
}
a:active {
    text-decoration:none;
    color:#2ECC40; /* OK */
}
a[name] {
    color:#ffff00;
}
a[name]:hover {
    text-decoration:none;
    color:#2ECC40;
}
pre {
    font-size:1.3em;
}
h1 {
    font-size:1.8em;
}
h2 {
    font-size:1.5em;
}
h3 {
    font-size:1.4em;
}
h4 {
    font-size:1.3em;
}
h5 {
    font-size:1.2em;
}
h6 {
    font-size:1.1em;
}
ul {
    list-style-type:disc;
}
code, tt {
    font-size:1.2em;
}
dt code {
    font-size:1.2em;
}
table tr td dt code {
    font-size:1.2em;
    vertical-align:top;
}
sup {
    font-size:.6em;
}
/*
Document title and Copyright styles
*/
.clear {
    clear:both;
    height:0px;
    overflow:hidden;
}
.aboutLanguage {
    float:right;
    padding:0px 21px;
    font-size:.8em;
    z-index:200;
    margin-top:-7px;
}
.legalCopy {
    margin-left:20px;
    margin-bottom:100px;
    width:200px;
}
p.legalCopy small {
    font-size:1em;

}
.bar a, .bar a:link, .bar a:visited, .bar a:active {
    color:#ff0000;
    text-decoration:none;
}
.bar a:hover, .bar a:focus {
    color:#ff0000;
}
.tab {
    background-color:#ff0000;
    background-position:left top;
    background-repeat:no-repeat;
    color:#ffff00;
    padding:8px;
    width:5em;
    font-weight:bold;
}
/*
Navigation bar styles
*/
.bar {
    background-repeat:repeat-x;
    color:#0074D9; /* OK */
    padding:.8em .5em .4em .8em;
    height:auto;/*height:1.8em;*/
    font-size:1em;
    margin:0;
}
.topNav {
    color:#2ECC40;
    float:left;
    padding:0;
    clear:right;
    height:2.8em;
    padding-top:10px;
    padding-left:10px;
    overflow:hidden;
}
.bottomNav {
    margin-top:10px;
    color:#2ECC40;
    float:left;
    padding:0;
    clear:right;
    height:2.8em;
    padding-top:10px;
    padding-left:10px;
    overflow:hidden;
}
.subNav {
    background-color:#f4f4f4;
    padding:20px 10px;
    margin:10px 0px;
    clear:left;
    float:left;
    overflow:hidden;
}
.subNav div {
    clear:left;
    float:left;
}

ul.navList, ul.subNavList {
    float:left;
    margin:0 0 0 0px;
    padding:0 0 0 0px;
}

ul.subNavList {
    border-bottom:1px solid black;
    margin-left:10px;
    padding-top:10px;
}
ul.navList li{
    list-style:none;
    float:left;
    padding:6px 10px;
}
ul.subNavList li{
    list-style:none;
    float:left;
    font-size:90%;
}
.topNav a:link, .topNav a:active, .topNav a:visited, .bottomNav a:link, .bottomNav a:active, .bottomNav a:visited {
    color:#0074D9; /* OK */
    text-decoration:none;
}
.topNav a:hover, .bottomNav a:hover {
    text-decoration:none;
    color:#2ECC40; /* OK */
}

/*
Page header and footer styles
*/
.header, .footer {
    clear:both;
    margin:0 20px;
    padding:5px 0 0 0;
}
.indexHeader {
    margin:10px;
    position:relative;
}
.indexHeader h1 {
    font-size:1.3em;
}
.title {
    color:#001F3F;
    margin:10px 0;
    font-weight: normal;
}
.subTitle {
    margin:5px 0 0 0;
}
.header ul {
    margin:0 0 25px 0;
    padding:0;
}
.footer ul {
    margin:20px 0 5px 0;
}
.header ul li, .footer ul li {
    list-style:none;
    font-size:1.2em;
}
/*
Heading styles
*/
div.details ul.blockList ul.blockList ul.blockList li.blockList h4, div.details ul.blockList ul.blockList ul.blockListLast li.blockList h4 {
    background-color:#DDDDDD; /* OK, method-details */
    margin:0 0 6px -8px;
    padding:10px 5px;
    border-top:1px solid #0074D9; /* OK */
}
ul.blockList ul.blockList ul.blockList li.blockList h3 {
    background-color:#DDDDDD; /* OK, method summary */
    margin:0 0 6px -8px;
    padding:12px 5px;
    border-top:1px solid #0074D9; /* OK */
}
ul.blockList ul.blockList li.blockList h3 {
    padding:0;
    margin:15px 0;
}
ul.blockList li.blockList h2 {
    padding:0px 0 20px 0;
}
/*
Page layout container styles
*/
.contentContainer, .sourceContainer, .classUseContainer, .serializedFormContainer, .constantValuesContainer {
    clear:both;
    padding:50px 20px;
    position:relative;
}
.indexContainer {
    margin:10px;
    position:relative;
    font-size:1.0em;
}
.indexContainer h2 {
    font-size:1.1em;
    padding:0 0 3px 0;
}
.indexContainer ul {
    margin:0;
    padding:0;
}
.indexContainer ul li {
    list-style:none;
}
.contentContainer dl dt {
    border-top:1px solid #0074D9;
    padding-top:5px;
}

.contentContainer dl dd {
    margin-bottom:10px;
}

.contentContainer .description dl dt, .contentContainer .details dl dt, .serializedFormContainer dl dt {
    font-size:1.1em;
    font-weight:bold;
    margin:10px 0 0 0;
    color:#001F3F; /* OK */
}
.contentContainer .description dl dd, .contentContainer .details dl dd, .serializedFormContainer dl dd {
    margin:10px 0 10px 20px;
}
.serializedFormContainer dl.nameValue dt {
    margin-left:1px;
    font-size:1.1em;
    display:inline;
    font-weight:bold;
}
.serializedFormContainer dl.nameValue dd {
    margin:0 0 0 1px;
    font-size:1.1em;
    display:inline;
}
/*
List styles
*/
ul.horizontal li {
    display:inline;
    font-size:0.9em;
}
ul.inheritance {
    margin:0;
    padding:0;
}
ul.inheritance li {
    display:inline;
    list-style:none;
}
ul.inheritance li ul.inheritance {
    margin-left:15px;
    padding-left:15px;
    padding-top:1px;
}
ul.blockList, ul.blockListLast {
    margin:10px 0 10px 0;
    padding:0;

}
ul.blockList li.blockList, ul.blockListLast li.blockList {
    list-style:none;
    margin-bottom:25px;

}
ul.blockList ul.blockList li.blockList, ul.blockList ul.blockListLast li.blockList {
    padding:0px 20px 5px 10px;
    border:1px solid #0074D9; /* OK */
    background-color:#ffffff;
}

ul.blockList ul.blockList ul.blockList li.blockList, ul.blockList ul.blockList ul.blockListLast li.blockList {
    padding:0 0 5px 8px;
    border:1px solid #0074D9; /* OK */
    background-color:#ffffff;
    border-top:none;
}
ul.blockList ul.blockList ul.blockList ul.blockList li.blockList {
    margin-left:0;
    padding-left:0;
    padding-bottom:15px;
    border:none;
    border-bottom:1px solid #0074D9;
}
ul.blockList ul.blockList ul.blockList ul.blockList li.blockListLast {
    list-style:none;
    border-bottom:none;
    padding-bottom:0;
}
table tr td dl, table tr td dl dt, table tr td dl dd {
    margin-top:0px;
    margin-bottom:1px;
}
/*
Table styles
*/
.contentContainer table, .classUseContainer table, .constantValuesContainer table {
    border-bottom:1px solid #0074D9;
    width:100%;
}
.contentContainer ul li table, .classUseContainer ul li table, .constantValuesContainer ul li table {
    width:100%;
}
.contentContainer .description table, .contentContainer .details table {
    border-bottom:none;
}
.contentContainer ul li table th.colOne, .contentContainer ul li table th.colFirst, .contentContainer ul li table th.colLast, .classUseContainer ul li table th, .constantValuesContainer ul li table th, .contentContainer ul li table td.colOne, .contentContainer ul li table td.colFirst, .contentContainer ul li table td.colLast, .classUseContainer ul li table td, .constantValuesContainer ul li table td{
    vertical-align:top;
    padding-right:20px;
}
.contentContainer ul li table th.colLast, .classUseContainer ul li table th.colLast,.constantValuesContainer ul li table th.colLast,
.contentContainer ul li table td.colLast, .classUseContainer ul li table td.colLast,.constantValuesContainer ul li table td.colLast,
.contentContainer ul li table th.colOne, .classUseContainer ul li table th.colOne,
.contentContainer ul li table td.colOne, .classUseContainer ul li table td.colOne {
    padding-right:3px;
}
.overviewSummary caption, .packageSummary caption, .contentContainer ul.blockList li.blockList caption, .summary caption, .classUseContainer caption, .constantValuesContainer caption {
    position:relative;
    text-align:left;
    background-repeat:no-repeat;
    color:#ff0000;
    font-weight:bold;
    clear:none;
    overflow:hidden;
    padding:0px;
    margin:0px;
    padding:20px 20px;
}
caption a:link, caption a:hover, caption a:active, caption a:visited {
    color:#ff0000;
}
.overviewSummary caption span, .packageSummary caption span, .contentContainer ul.blockList li.blockList caption span, .summary caption span, .classUseContainer caption span, .constantValuesContainer caption span {
    white-space:nowrap;
    padding-top:8px;
    padding-left:0px;
    display:block;
    float:left;
    height:18px;
    color:#001F3F;
    font-size:1.4em;
    font-weight:normal;
}
.overviewSummary .tabEnd, .packageSummary .tabEnd, .contentContainer ul.blockList li.blockList .tabEnd, .summary .tabEnd, .classUseContainer .tabEnd, .constantValuesContainer .tabEnd {
    width:10px;
    position:relative;
    float:left;
}
ul.blockList ul.blockList li.blockList table {
    margin:0 0 12px 0px;
    width:100%;
}
.tableSubHeadingColor {
    background-color: #ffff00;
}

.altColor {
    background-color:#ffffff;
}
.rowColor {
    background-color:#f0f0f0;
}
.overviewSummary td, .packageSummary td, .contentContainer ul.blockList li.blockList td, .summary td, .classUseContainer td, .constantValuesContainer td {
    text-align:left;
    padding:3px 3px 3px 7px;
}
th.colFirst, th.colLast, th.colOne, .constantValuesContainer th {
    background:#01FF70;
    border-top:1px solid #0074D9;
    border-bottom:1px solid #0074D9;
    text-align:left;
    padding:12px 3px 12px 7px;
}
td.colOne a:link, td.colOne a:active, td.colOne a:visited, td.colOne a:hover, td.colFirst a:link, td.colFirst a:active, td.colFirst a:visited, td.colFirst a:hover, td.colLast a:link, td.colLast a:active, td.colLast a:visited, td.colLast a:hover, .constantValuesContainer td a:link, .constantValuesContainer td a:active, .constantValuesContainer td a:visited, .constantValuesContainer td a:hover {
    font-weight:normal;
}
td.colFirst, th.colFirst {
    border-left:1px solid #0074D9;
    white-space:nowrap;
}
td.colLast, th.colLast {
    border-right:1px solid #0074D9;
}
td.colOne, th.colOne {
    border-right:1px solid #0074D9;
    border-left:1px solid #0074D9;
}
table.overviewSummary  {
    padding:0px;
    margin-left:0px;
}
table.overviewSummary td.colFirst, table.overviewSummary th.colFirst,
table.overviewSummary td.colOne, table.overviewSummary th.colOne {
    width:25%;
    vertical-align:middle;
}
table.packageSummary td.colFirst, table.overviewSummary th.colFirst {
    width:25%;
    vertical-align:middle;
}
/*
Content styles
*/
.description pre {
    margin-top:0;
}
.deprecatedContent {
    margin:0;
    padding:10px 0;
}
.docSummary {
    padding:0;
}
/*
Formatting effect styles
*/
.sourceLineNo {
    color:green;
    padding:0 30px 0 0;
}
h1.hidden {
    visibility:hidden;
    overflow:hidden;
    font-size:.9em;
}
.block {
    display:block;
    margin:3px 0 0 0;
}
.strong {
    font-weight:bold;
}

.toggleList {
    padding:0;
    margin:0;
    margin-top:12px;
    border:0;
}

.toggleList dt {
    background:#01FF70;
    color:#333;
    display:inline-block;
    padding:8px 16px;
    margin:10px 0px 10px 0px;
    -moz-border-radius: 20px; /* Firefox */
    -webkit-border-radius: 20px; /* Safari, Chrome */
    border-radius: 20px; /* CSS3 */
    border-top:0;
}

.toggleList dt:hover {
    cursor:pointer;
}

.toggleList dt span {
    border-top:0;
    font-family: monospace;
    padding:0;
    margin:0;
}

.toggleList dd {
    margin:0;
    padding:0;
    border:0;
}

html.isjs .toggleList dd {
    border:0;
    display: none;
}

.toggleList pre {
    padding: 4px 4px 4px 4px;
}

.navBarCell1Rev {
    background-color:#0074D9;
    color:#ffffff;
    -moz-border-radius: 20px; /* Firefox */
    -webkit-border-radius: 20px; /* Safari, Chrome */
    border-radius: 20px; /* CSS3 */
    display:inline-block;
    margin:0px 10px 0px 10px;
}

pre {

    font-size:1em;
}

p {
    width:80%;
}

redpajama_set_name: RedPajamaGithub


text:
package proftest

import (
	"encoding/json"
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"os/exec"
	"regexp"
	"testing"
)

// Diff compares two byte arrays using the diff tool to highlight the
// differences. It is meant for testing purposes to display the
// differences between expected and actual output.
func Diff(b1, b2 []byte) (data []byte, err error) {
	f1, err := ioutil.TempFile("", "proto_test")
	if err != nil {
		return nil, err
	}
	defer os.Remove(f1.Name())
	defer f1.Close()

	f2, err := ioutil.TempFile("", "proto_test")
	if err != nil {
		return nil, err
	}
	defer os.Remove(f2.Name())
	defer f2.Close()

	f1.Write(b1)
	f2.Write(b2)

	data, err = exec.Command("diff", "-u", f1.Name(), f2.Name()).CombinedOutput()
	if len(data) > 0 {
		// diff exits with a non-zero status when the files don't match.
		// Ignore that failure as long as we get output.
		err = nil
	}
	if err != nil {
		data = []byte(fmt.Sprintf("diff failed: %v\nb1: %q\nb2: %q\n", err, b1, b2))
		err = nil
	}
	return
}

// EncodeJSON encodes a value into a byte array. This is intended for
// testing purposes.
func EncodeJSON(x interface{}) []byte {
	data, err := json.MarshalIndent(x, "", "    ")
	if err != nil {
		panic(err)
	}
	data = append(data, '\n')
	return data
}

// TestUI implements the plugin.UI interface, triggering test failures
// if more than Ignore errors not matching AllowRx are printed.
// Also tracks the number of times the error matches AllowRx in
// NumAllowRxMatches.
type TestUI struct {
	T                 *testing.T
	Ignore            int
	AllowRx           string
	NumAllowRxMatches int
	Input             []string
	index             int
}

// ReadLine returns no input, as no input is expected during testing.
func (ui *TestUI) ReadLine(_ string) (string, error) {
	if ui.index >= len(ui.Input) {
		return "", io.EOF
	}
	input := ui.Input[ui.index]
	ui.index++
	if input == "**error**" {
		return "", fmt.Errorf("Error: %s", input)
	}
	return input, nil
}

// Print messages are discarded by the test UI.
func (ui *TestUI) Print(args ...interface{}) {
}

// PrintErr messages may trigger an error failure. A fixed number of
// error messages are permitted when appropriate.
func (ui *TestUI) PrintErr(args ...interface{}) {
	if ui.AllowRx != "" {
		if matched, err := regexp.MatchString(ui.AllowRx, fmt.Sprint(args...)); matched || err != nil {
			if err != nil {
				ui.T.Errorf("failed to match against regex %q: %v", ui.AllowRx, err)
			}
			ui.NumAllowRxMatches++
			return
		}
	}
	if ui.Ignore > 0 {
		ui.Ignore--
		return
	}
	// Stringify arguments with fmt.Sprint() to match what default UI
	// implementation does. Without this Error() calls fmt.Sprintln() which
	// _always_ adds spaces between arguments, unlike fmt.Sprint() which only
	// adds them between arguments if neither is string.
	ui.T.Error("unexpected error: " + fmt.Sprint(args...))
}

// IsTerminal indicates if the UI is an interactive terminal.
func (ui *TestUI) IsTerminal() bool {
	return false
}

// WantBrowser indicates whether a browser should be opened with the -http option.
func (ui *TestUI) WantBrowser() bool {
	return false
}

// SetAutoComplete is not supported by the test UI.
func (ui *TestUI) SetAutoComplete(_ func(string) string) {
}


text:
class KeyMessageHandler : public MessageHandler {
 public:

  // Constructor takes as input the result log stream.
  KeyMessageHandler(std::ostream* log);

  // This method implements the actual communication protocol. It takes as input
  // the security parameter and wait for the expected response. The method
  // transmits the public key from the client to the server.
  virtual void Send(std::istream* is);

};
#endif


text:
/* TEMPLATE GENERATED TESTCASE FILE
Filename: CWE195_Signed_to_Unsigned_Conversion_Error__fscanf_memcpy_84.h
Label Definition File: CWE195_Signed_to_Unsigned_Conversion_Error.label.xml
Template File: sources-sink-84.tmpl.h
*/
/*
 * @description
 * CWE: 195 Signed to Unsigned Conversion Error
 * BadSource: fscanf Read data from the console using fscanf()
 * GoodSource: Positive integer
 * Sinks: memcpy
 *    BadSink : Copy strings using memcpy() with the length of data
 * Flow Variant: 84 Data flow: data passed to class constructor and destructor by declaring the class object on the heap and deleting it after use
 *
 * */

#include "std_testcase.h"

namespace CWE195_Signed_to_Unsigned_Conversion_Error__fscanf_memcpy_84
{

#ifndef OMITBAD

class CWE195_Signed_to_Unsigned_Conversion_Error__fscanf_memcpy_84_bad
{
public:
    CWE195_Signed_to_Unsigned_Conversion_Error__fscanf_memcpy_84_bad(int dataCopy);
    ~CWE195_Signed_to_Unsigned_Conversion_Error__fscanf_memcpy_84_bad();

private:
    int data;
};

#endif /* OMITBAD */

#ifndef OMITGOOD

class CWE195_Signed_to_Unsigned_Conversion_Error__fscanf_memcpy_84_goodG2B
{
public:
    CWE195_Signed_to_Unsigned_Conversion_Error__fscanf_memcpy_84_goodG2B(int dataCopy);
    ~CWE195_Signed_to_Unsigned_Conversion_Error__fscanf_memcpy_84_goodG2B();

private:
    int data;
};

#endif /* OMITGOOD */

}


text:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/et_name"
            android:layout_width="0dp"
            android:layout_height="48dp"
            android:layout_gravity="center|bottom"
            android:layout_weight="1"
            android:hint="@string/name"
            android:inputType="text" />

        <ImageView
            android:id="@+id/iv_avatar"
            android:layout_width="96dp"
            android:layout_height="96dp"
            android:contentDescription="@string/app_name"
            android:src="@android:drawable/sym_def_app_icon" />
    </LinearLayout>

    <EditText
        android:id="@+id/et_last_name"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:hint="@string/last_name"
        android:inputType="text" />

    <EditText
        android:id="@+id/et_address"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:hint="@string/address"
        android:inputType="text" />

    <EditText
        android:id="@+id/et_phone"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:hint="@string/phone"
        android:inputType="phone" />

    <EditText
        android:id="@+id/et_notes"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:hint="@string/notes"
        android:inputType="textMultiLine" />

</LinearLayout>


text:
<?php

final class Lisphp_Runtime_Let implements Lisphp_Applicable
{
    public function apply(Lisphp_Scope $scope, Lisphp_List $arguments)
    {
        $vars = $arguments->car();
        $scope = new Lisphp_Scope($scope);
        foreach ($vars as $var) {
            list($var, $value) = $var;
            $scope->let($var, $value->evaluate($scope->superscope));
        }
        foreach ($arguments->cdr() as $form) {
            $retval = $form->evaluate($scope);
        }

        return $retval;
    }
}


text:
title: "Flame Graphs"
weight: 3
type: docs
aliases:
  - /ops/debugging/flame_graphs.html
  - /ops/debugging/flame_graphs
---
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you 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.
-->

# Flame Graphs

[Flame Graphs](http://www.brendangregg.com/flamegraphs.html) are a visualization that effectively surfaces answers to questions like:
- Which methods are currently consuming CPU resources?
- How does consumption by one method compare to the others?
- Which series of calls on the stack led to executing a particular method?

{{< img src="/fig/flame_graph_on_cpu.png" class="img-fluid" width="90%" >}}
{{% center %}}
Flame Graph
{{% /center %}}

Flame Graphs are constructed by sampling stack traces a number of times. Each method call is presented by a bar, where the length of the bar is proportional to the number of times it is present in the samples.

Starting with Flink 1.13, Flame Graphs are natively supported in Flink. In order to produce a Flame Graph, navigate to the job graph of a running job, select an operator of interest and in the menu to the right click on the Flame Graph tab:

{{< img src="/fig/flame_graph_operator.png" class="img-fluid" width="90%" >}}
{{% center %}}
Operator's On-CPU Flame Graph
{{% /center %}}

{{< hint warning >}}

Any measurement process in and of itself inevitably affects the subject of measurement (see the [double-split experiment](https://en.wikipedia.org/wiki/Double-slit_experiment#Relational_interpretation)). Sampling CPU stack traces is no exception. In order to prevent unintended impacts on production environments, Flame Graphs are currently available as an opt-in feature. To enable it, you'll need to set [`rest.flamegraph.enabled: true`]({{< ref "docs/deployment/config">}}#rest-flamegraph-enabled) in `conf/flink-conf.yaml`. We recommend enabling it in development and pre-production environments, but you should treat it as an experimental feature in production.

{{< /hint >}}

Apart from the On-CPU Flame Graphs, [Off-CPU](http://www.brendangregg.com/FlameGraphs/offcpuflamegraphs.html) and Mixed visualizations are available and can be switched between by using the selector at the top of the pane:

{{< img src="/fig/flame_graph_selector.png" class="img-fluid" width="30%" >}}

The Off-CPU Flame Graph visualizes blocking calls found in the samples. A distinction is made as follows:
- On-CPU: `Thread.State` in **[RUNNABLE, NEW]**
- Off-CPU: `Thread.State` in **[TIMED_WAITING, WAITING, BLOCKED]**

{{< img src="/fig/flame_graph_off_cpu.png" class="img-fluid" width="90%" >}}
{{% center %}}
Off-CPU Flame Graph
{{% /center %}}

Mixed mode Flame Graphs are constructed from stack traces of threads in all possible states.

{{< img src="/fig/flame_graph_mixed.png" class="img-fluid" width="90%" >}}
{{% center %}}
Flame Graph in Mixed Mode
{{% /center %}}

##  Sampling process

The collection of stack traces is done purely within the JVM, so only method calls within the Java runtime are visible (no system calls).

Flame Graph construction is performed at the level of an individual [operator]({{< ref "docs/concepts/glossary" >}}#operator), i.e. all [task]({{< ref "docs/concepts/glossary" >}}#task) threads of that operator are sampled in parallel and their stack traces are combined.

{{< hint info >}}
**Note:**
Stack trace samples from all threads of an operator are combined together. If a method call consumes 100% of the resources in one of the parallel tasks but none in the others, the bottleneck might be obscured by being averaged out.

There are plans to address this limitation in the future by providing "drill down" visualizations to the task level.
{{< /hint >}}


text:
class CreateStockSettings < ActiveRecord::Migration
  def change
    create_table :stock_settings do |t|
      t.string :symbol

      t.timestamps null: false
    end
  end
end


text:
function setupMutatorTest(arrayMutator, nodeMutator, initialData) {
    return {
        requiredHtmlResources:"eachChild",
        becauseOf:function () {
            this.initialData_ = initialData || this.defaultInitialData;
            this.bind(this.initialData_);
            this.itemNodes_ = this.extractItemNodes();
            this.rebind(this.initialData_, arrayMutator);
        },
        "Should clean unused nodes" : function() {
            var currentNodes = this.extractItemNodes();

            var nodesToClean = photon.array.filter(this.itemNodes_, function(node) {
                return photon.array.indexOf(currentNodes, node) === -1;
            }, this);

            if (this.itemNodes_.length > currentNodes.length) {
                assertEquals(nodesToClean.length, this.itemNodes_.length - currentNodes.length);
            }

            photon.array.forEach(nodesToClean, function(node) {
                assertNull(photon.binding
                    .NodeBindingInfo.getForElement(node));
                assertNull(photon.binding
                    .DataContext.getLocalForElement(node));
            })
        },
        "Should render correct items":function () {
            assertEquals(this.dataContexts_[1],
                this.extractItemValues());
        },
        "Should re-use nodes":function () {
            var actual = this.extractItemNodes();

            // mutate current nodes
            var expected = this.itemNodes_.slice(0);
            (nodeMutator || arrayMutator).call(this, expected);

            // assert lengths
            assertEquals("Node array lengths differ", expected.length, actual.length);
            for (var i = 0, n = expected.length; i < n; i++) {
                assertSame(photon.string.format("Array differ at index {0}, expectedHTML:{1}, actualHTML:{2}", i, expected[i].innerHTML, actual[i].innerHTML), expected[i], actual[i]);
            }
        },
        "Should maintain correct data context hierarchy" : function() {
            var dataContexts = photon.array.map(this.extractItemNodes(),
                function(node) {
                    return photon.binding.DataContext.getForElement(node);
                });

            function getDataContextHierarchy(dataContext) {
                var result = [];
                while (dataContext) {
                    result.push(dataContext);
                    dataContext = dataContext.getParent();
                }
                return result;
            }

            photon.array.forEach(dataContexts, function(dataContext) {
                var hierarchy = getDataContextHierarchy(dataContext);
                assertEquals(2, hierarchy.length);

                // verify we haven't been cloned (this was happening!!, BUG)
                assertNotSame(hierarchy[0], hierarchy[1]);

                // verify we are parented correctly
                assertSame(photon.binding.DataContext.getForElement(
                    this.flowElement_), hierarchy[1]);

            }, this);
        }
    };
}

var itemsRendererTests = {
    "When binding for first time":{
        requiredHtmlResources:"eachChild",
        becauseOf:function () {
            this.bind(this.defaultInitialData);
        },
        "Should render correct items":function () {
            assertEquals([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], this.extractItemValues());
        }
    },
    "When an item is deleted from middle":setupMutatorTest(
        function (data) {
            data.splice(1, 1);
        }),
    "When multiple consecutive items are deleted from middle":setupMutatorTest(
        function (data) {
            data.splice(1, 2);
        }),
    "When an item is inserted at the start":setupMutatorTest(
        function (data) {
            data.shift(20);
        },
        function (nodes) {
            nodes.shift(this.extractItemNodeByValue(20));
        }),
    "When an item is inserted in the middle":setupMutatorTest(
        function (data) {
            data.splice(2, 0, 20);
        },
        function (nodes) {
            nodes.splice(2, 0, this.extractItemNodeByValue(20));
        }),
    "When an item is inserted at the end":setupMutatorTest(
        function (data) {
            data.push(20);
        },
        function (nodes) {
            nodes.push(this.extractItemNodeByValue(20));
        }),
    "When multiple consecutive items are inserted in the middle":setupMutatorTest(
        function (data) {
            data.splice(2, 0, 20, 21);
        },
        function (nodes) {
            nodes.splice(2, 0, this.extractItemNodeByValue(20),
                this.extractItemNodeByValue(21));
        }),
    "When an item is set in the middle":setupMutatorTest(
        function (data) {
            data[2] = 20;
        },
        function () {
        }),
    "When more items are replaced by less items":setupMutatorTest(
        function (data) {
            data.splice(2, 2, 20);
        },
        function (nodes) {
            nodes.splice(2, 2, nodes[3]); // should re-use 1 of the 2 deleted slots
        }),
    "When an item is set after a delete":setupMutatorTest(
        function (data) {
            data.splice(1, 1);
            data[3] = 21;
        },
        function (nodes) {
            nodes.splice(1, 1);
            nodes[3] = this.extractItemNodeByValue(21);
        }
    ),
    "When an item is deleted after an insert":setupMutatorTest(
        function (data) {
            data.push(20);
            data.splice(3, 1);
        },
        function (nodes) {
            var copy = nodes.slice(0);
            nodes.push(copy[3]);        // should re-use deleted
            nodes.splice(3, 1);
        }
    ),
    "When an item is inserted after a delete":setupMutatorTest(
        function (data) {
            data.splice(0, 2);
            data.splice(3, 0, 20);
        },
        function (nodes) {
            var copy = nodes.slice(0);
            nodes.splice(0, 2);
            nodes.splice(3, 0, copy[0]); // should re-use deleted
        }
    ),
    /**
     * BUG: this case showed up when testing with a random mutator
     */
    "When recycling nodes from the front onto the end":setupMutatorTest(
        function (data) {
            data.splice(0, 3, 0, 9, 6, 6);
        },
        function (nodes) {
            var originalNodes = nodes.slice(0);
            nodes[0] = originalNodes[1];
            nodes[1] = originalNodes[2];
            nodes[2] = originalNodes[0];
            nodes[3] = this.extractItemNodeByValue(6, 1)
        },
        [8, 0, 0]
    ),
    "When inserting after a set":setupMutatorTest(
        function (data) {
            data[2] = 20;
            data.push(21);
            data.push(22);
        },
        function (nodes) {
            nodes.push(this.extractItemNodeByValue(21));
            nodes.push(this.extractItemNodeByValue(22));
        }),
    "When less items are replaced by more items":setupMutatorTest(
        function (data) {
            data.splice(6, 2, 20, 21, 22, 23);
        },
        function (nodes) {
            nodes.splice(6, 2, nodes[6], nodes[7],
                this.extractItemNodeByValue(22), this.extractItemNodeByValue(23));
        }),
    "When an item is inserted after less items are replaced by more":setupMutatorTest(
        function (data) {
            data.splice(6, 2, 20, 21, 22, 23, 24);
            data.push(25);
            data.push(26);
        },
        function (nodes) {
            nodes.splice(6, 2, nodes[6], nodes[7],
                this.extractItemNodeByValue(22),
                this.extractItemNodeByValue(23),
                this.extractItemNodeByValue(24));
            nodes.push(
                this.extractItemNodeByValue(25),
                this.extractItemNodeByValue(26));
        }),
    "When multiple changes occur":setupMutatorTest(
        function (data) {
            data.splice(0, 0, 20);        // insert at front (should re-use 1 node
            data.splice(2, 2);            // delete
            data[5] = 21;                 // set
            data.splice(6, 3, 21, 22);    // set 2, delete 1
            data.push(23);                // push onto end
            data.push(24);
        },
        function (nodes) {
            var copy = nodes.slice(0);
            nodes.splice(0, 0, copy[1]);          // should re-use 1 of the 2 deleted slots
            nodes.splice(2, 2);
            nodes[5] = copy[6];                   // No 5, used to be 4 (I1, D2), taking into account the 2 items
            // deleted use 6 (easier to code than to explain!!)

            nodes.splice(6, 3, copy[7], copy[8]); // No 6, 7 used to be 7, 8 and represent 2 sets out of a (D3, I2)
            nodes.push(copy[9]);                  // push onto end
            nodes.push(copy[2]);                  // push onto end
        }),
    "When multiple consecutive items are set in the middle":setupMutatorTest(
        function (data) {
            data[2] = 20;
            data[3] = 21;
        },
        function () {
        }),
    "When benchmarking changes to large data set":{
        requiredHtmlResources:"eachChild",
        becauseOf:function () {
            var data = [];
            for (var i = 0; i < 1000; i++) {
                data[i] = i;
            }
            this.bindMs = timeCall(this.bind, this, [data]);
            data = data.slice(0);
            for (i = 0; i < 250; i++) {
                switch (this.randomInt(3)) {
                    case 0:
                        data.splice(this.randomInt(data.length), 0, this.randomInt(10000));
                        break;
                    case 1:
                        data.splice(this.randomInt(data.length), 1);
                        break;
                    case 2:
                        data[this.randomInt(data.length)] = this.randomInt(10000);
                        break;
                }
            }

            this.updateMs = timeCall(function() {
                photon.binding.DataContext.getForElement(this.flowElement_)
                    .setSource(data);
                this.dataContexts_.push(data);
            }, this);
        },
        "Should perform":function () {
            jstestdriver.console.log(this.bindMs);
            jstestdriver.console.log(this.updateMs);
        }
    }
}

var itemsRendererTestPrototype = {
    defaultInitialData:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
    createSystemUnderTest:function () {
        this.flowElement_ = $("#flow")[0];
    },
    tearDown:function () {
        photon.dom.cleanNode(document);
    },
    bind:function (data) {
        this.dataContexts_ = this.dataContexts_ || [];
        this.dataContexts_.push(data);
        photon.binding.applyBindings(data)
    },
    rebind:function (data, mutator) {
        data = data.slice(0);
        mutator.call(this, data);
        photon.binding.DataContext.getForElement(this.flowElement_)
            .setSource(data);
        this.dataContexts_.push(data);
    },
    randomInt:function (max) {
        return Math.floor(Math.random() * max);
    },
    extractItemNodeByValue:function (value, occurance) {
        return $("span.item").filter(function () {
            return Number(this.innerText) === value;
        })[occurance || 0];
    },
    extractItemNodes:function () {
        return $("span.item").toArray();
    },
    extractItemValues:function () {
        return photon.array.map(this.extractItemNodes(), function (item) {
            return Number(item.innerText);
        });
    },
    htmlResources:{
        eachChild:function () {
            /*:DOC +=

             */
        }
    }
};

DefineTestSuite("FlowBinding.Each.Child",
    itemsRendererTests,
    photon.extend(
        itemsRendererTestPrototype,
        {
            renderTarget_:photon.templating.RenderTarget.Child
        }
    )
);

DefineTestSuite("FlowBinding.Each.NextSibling",
    photon.extend(itemsRendererTests,
        {
            "When there is a static next sibling" : {
                becauseOf : function() {
                    /*:DOC +=

                     Some content

                     */

                    var items = [1, 2, 3];
                    photon.binding.applyBindings(items);
                },
                "Should render in the correct position" : function() {
                    var lastItem = $(".item")[2];
                    assertNotNullOrUndefined(
                        lastItem.nextSibling);
                    assertSame(lastItem.nextSibling,
                        $("#nextSibling")[0]);
                }
            }
        }),
    photon.extend(
        itemsRendererTestPrototype,
        {
            renderTarget_:photon.templating.RenderTarget.NextSibling
        }
    )
);


text:
package client

import (
	"errors"
	"fmt"
	"net/url"

	"golang.org/x/net/context"

	distreference "github.com/docker/distribution/reference"
	"github.com/docker/engine-api/types/reference"
)

// ImageTag tags an image in the docker host
func (cli *Client) ImageTag(ctx context.Context, imageID, ref string) error {
	distributionRef, err := distreference.ParseNamed(ref)
	if err != nil {
		return fmt.Errorf("Error parsing reference: %q is not a valid repository/tag", ref)
	}

	if _, isCanonical := distributionRef.(distreference.Canonical); isCanonical {
		return errors.New("refusing to create a tag with a digest reference")
	}

	tag := reference.GetTagFromNamedRef(distributionRef)

	query := url.Values{}
	query.Set("repo", distributionRef.Name())
	query.Set("tag", tag)

	resp, err := cli.post(ctx, "/images/"+imageID+"/tag", query, nil, nil)
	ensureReaderClosed(resp)
	return err
}


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

<title>Uses of Interface org.apache.poi.ss.formula.EvaluationName (POI API Documentation)</title>
<link rel="stylesheet" type="text/css" href="../../../../../../stylesheet.css" title="Style">

JavaScript is disabled on your browser.

Overview

Package

Class

Use

Tree

Deprecated

Index

Help

PREV

NEXT

FRAMES

NO FRAMES

All Classes

Uses of Interface
org.apache.poi.ss.formula.EvaluationName

Packages that use EvaluationName

Package
Description

org.apache.poi.hssf.usermodel

usermodel package maps HSSF low level strutures to familiar workbook/sheet model

org.apache.poi.ss.formula

This package contains common internal POI code for manipulating formulas.

org.apache.poi.ss.formula.eval

org.apache.poi.xssf.usermodel

Uses of EvaluationName in org.apache.poi.hssf.usermodel

Methods in org.apache.poi.hssf.usermodel that return EvaluationName

Modifier and Type
Method and Description

EvaluationName
HSSFEvaluationWorkbook.getName(NamePtg namePtg)

EvaluationName
HSSFEvaluationWorkbook.getName(java.lang.String name,
       int sheetIndex)

Lookup a named range by its name.

Uses of EvaluationName in org.apache.poi.ss.formula

Methods in org.apache.poi.ss.formula that return EvaluationName

Modifier and Type
Method and Description

EvaluationName
EvaluationWorkbook.getName(NamePtg namePtg)

EvaluationName
FormulaParsingWorkbook.getName(java.lang.String name,
       int sheetIndex)

named range name matching is case insensitive

EvaluationName
EvaluationWorkbook.getName(java.lang.String name,
       int sheetIndex)

Uses of EvaluationName in org.apache.poi.ss.formula.eval

Methods in org.apache.poi.ss.formula.eval that return EvaluationName

Modifier and Type
Method and Description

EvaluationName
ExternalNameEval.getName()

Constructors in org.apache.poi.ss.formula.eval with parameters of type EvaluationName

Constructor and Description

ExternalNameEval(EvaluationName name)

Uses of EvaluationName in org.apache.poi.xssf.usermodel

Methods in org.apache.poi.xssf.usermodel that return EvaluationName

Modifier and Type
Method and Description

EvaluationName
BaseXSSFEvaluationWorkbook.getName(NamePtg namePtg)

EvaluationName
BaseXSSFEvaluationWorkbook.getName(java.lang.String name,
       int sheetIndex)

Return EvaluationName wrapper around the matching XSSFName (named range)

Overview

Package

Class

Use

Tree

Deprecated

Index

Help

PREV

NEXT

FRAMES

NO FRAMES

All Classes

                Copyright 2016 The Apache Software Foundation or
        its licensors, as applicable.


text:
A simple function to send a notification message to a Microsoft Teams channel using the channels connector "Incoming Webhook".

`Send-TeamsNotification -Uri $Uri -Text 'Example notification message' -Title 'IMPORTANT!' -ThemeColor 'FF8000' -ButtonType ViewAction -ButtonName 'Click Here' -ButtonTarget 'https://contoso.com' -Verbose`
![](https://raw.githubusercontent.com/PhilipHaglund/PowerShell/master/Send-TeamsNotification/pics/Advanced_Notification_PS.png)

The result after running the cmdlet above:
![](https://raw.githubusercontent.com/PhilipHaglund/PowerShell/master/Send-TeamsNotification/pics/Advanced_Notification.png)

## Enable a Incomming Webhook ##

- Right click on a channel and choose 'Connectors'
 ![](https://raw.githubusercontent.com/PhilipHaglund/PowerShell/master/Send-TeamsNotification/pics/Connector.png)

- Search for 'Webhook' and click 'Add'
 ![](https://raw.githubusercontent.com/PhilipHaglund/PowerShell/master/Send-TeamsNotification/pics/Webhook.png)

- Enter a name for the Webhook.
 ![](https://raw.githubusercontent.com/PhilipHaglund/PowerShell/master/Send-TeamsNotification/pics/Notification.png)

- Copy the genereted HTTP URL and save it, for example to a variable.
 ![](https://raw.githubusercontent.com/PhilipHaglund/PowerShell/master/Send-TeamsNotification/pics/Copy.png)


text:
require 'spec_helper'

describe Chewy::Runtime do
  describe '.version' do
    specify { expect(described_class.version).to be_a(described_class::Version) }
    specify { expect(described_class.version).to be >= '2.4' }
    specify { expect(described_class.version).to be < '6.0' }
  end
end


text:
// This file is part of Eigen, a lightweight C++ template library
// for linear algebra.
//
// Copyright (C) 2008-2010 Gael Guennebaud <gael.guennebaud@inria.fr>
// Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
//
// Eigen is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 3 of the License, or (at your option) any later version.
//
// Alternatively, you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of
// the License, or (at your option) any later version.
//
// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License and a copy of the GNU General Public License along with
// Eigen. If not, see <http://www.gnu.org/licenses/>.

#ifndef EIGEN_VECTORBLOCK_H
#define EIGEN_VECTORBLOCK_H

/** \class VectorBlock
  * \ingroup Core_Module
  *
  * \brief Expression of a fixed-size or dynamic-size sub-vector
  *
  * \param VectorType the type of the object in which we are taking a sub-vector
  * \param Size size of the sub-vector we are taking at compile time (optional)
  *
  * This class represents an expression of either a fixed-size or dynamic-size sub-vector.
  * It is the return type of DenseBase::segment(Index,Index) and DenseBase::segment<int>(Index) and
  * most of the time this is the only way it is used.
  *
  * However, if you want to directly maniputate sub-vector expressions,
  * for instance if you want to write a function returning such an expression, you
  * will need to use this class.
  *
  * Here is an example illustrating the dynamic case:
  * \include class_VectorBlock.cpp
  * Output: \verbinclude class_VectorBlock.out
  *
  * \note Even though this expression has dynamic size, in the case where \a VectorType
  * has fixed size, this expression inherits a fixed maximal size which means that evaluating
  * it does not cause a dynamic memory allocation.
  *
  * Here is an example illustrating the fixed-size case:
  * \include class_FixedVectorBlock.cpp
  * Output: \verbinclude class_FixedVectorBlock.out
  *
  * \sa class Block, DenseBase::segment(Index,Index,Index,Index), DenseBase::segment(Index,Index)
  */
template<typename VectorType, int Size>
struct ei_traits<VectorBlock<VectorType, Size> >
  : public ei_traits<Block<VectorType,
                     ei_traits<VectorType>::Flags & RowMajorBit ? 1 : Size,
                     ei_traits<VectorType>::Flags & RowMajorBit ? Size : 1> >
{
};

template<typename VectorType, int Size> class VectorBlock
  : public Block<VectorType,
                     ei_traits<VectorType>::Flags & RowMajorBit ? 1 : Size,
                     ei_traits<VectorType>::Flags & RowMajorBit ? Size : 1>
{
    typedef Block<VectorType,
                     ei_traits<VectorType>::Flags & RowMajorBit ? 1 : Size,
                     ei_traits<VectorType>::Flags & RowMajorBit ? Size : 1> Base;
    enum {
      IsColVector = !(ei_traits<VectorType>::Flags & RowMajorBit)
    };
  public:
    EIGEN_DENSE_PUBLIC_INTERFACE(VectorBlock)

    using Base::operator=;

    /** Dynamic-size constructor
      */
    inline VectorBlock(const VectorType& vector, Index start, Index size)
      : Base(vector,
             IsColVector ? start : 0, IsColVector ? 0 : start,
             IsColVector ? size  : 1, IsColVector ? 1 : size)
    {
      EIGEN_STATIC_ASSERT_VECTOR_ONLY(VectorBlock);
    }

    /** Fixed-size constructor
      */
    inline VectorBlock(const VectorType& vector, Index start)
      : Base(vector, IsColVector ? start : 0, IsColVector ? 0 : start)
    {
      EIGEN_STATIC_ASSERT_VECTOR_ONLY(VectorBlock);
    }
};

/** \returns a dynamic-size expression of a segment (i.e. a vector block) in *this.
  *
  * \only_for_vectors
  *
  * \param start the first coefficient in the segment
  * \param size the number of coefficients in the segment
  *
  * Example: \include MatrixBase_segment_int_int.cpp
  * Output: \verbinclude MatrixBase_segment_int_int.out
  *
  * \note Even though the returned expression has dynamic size, in the case
  * when it is applied to a fixed-size vector, it inherits a fixed maximal size,
  * which means that evaluating it does not cause a dynamic memory allocation.
  *
  * \sa class Block, segment(Index)
  */
template<typename Derived>
inline VectorBlock<Derived> DenseBase<Derived>
  ::segment(Index start, Index size)
{
  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
  return VectorBlock<Derived>(derived(), start, size);
}

/** This is the const version of segment(Index,Index).*/
template<typename Derived>
inline const VectorBlock<Derived>
DenseBase<Derived>::segment(Index start, Index size) const
{
  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
  return VectorBlock<Derived>(derived(), start, size);
}

/** \returns a dynamic-size expression of the first coefficients of *this.
  *
  * \only_for_vectors
  *
  * \param size the number of coefficients in the block
  *
  * Example: \include MatrixBase_start_int.cpp
  * Output: \verbinclude MatrixBase_start_int.out
  *
  * \note Even though the returned expression has dynamic size, in the case
  * when it is applied to a fixed-size vector, it inherits a fixed maximal size,
  * which means that evaluating it does not cause a dynamic memory allocation.
  *
  * \sa class Block, block(Index,Index)
  */
template<typename Derived>
inline VectorBlock<Derived>
DenseBase<Derived>::head(Index size)
{
  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
  return VectorBlock<Derived>(derived(), 0, size);
}

/** This is the const version of head(Index).*/
template<typename Derived>
inline const VectorBlock<Derived>
DenseBase<Derived>::head(Index size) const
{
  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
  return VectorBlock<Derived>(derived(), 0, size);
}

/** \returns a dynamic-size expression of the last coefficients of *this.
  *
  * \only_for_vectors
  *
  * \param size the number of coefficients in the block
  *
  * Example: \include MatrixBase_end_int.cpp
  * Output: \verbinclude MatrixBase_end_int.out
  *
  * \note Even though the returned expression has dynamic size, in the case
  * when it is applied to a fixed-size vector, it inherits a fixed maximal size,
  * which means that evaluating it does not cause a dynamic memory allocation.
  *
  * \sa class Block, block(Index,Index)
  */
template<typename Derived>
inline VectorBlock<Derived>
DenseBase<Derived>::tail(Index size)
{
  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
  return VectorBlock<Derived>(derived(), this->size() - size, size);
}

/** This is the const version of tail(Index).*/
template<typename Derived>
inline const VectorBlock<Derived>
DenseBase<Derived>::tail(Index size) const
{
  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
  return VectorBlock<Derived>(derived(), this->size() - size, size);
}

/** \returns a fixed-size expression of a segment (i.e. a vector block) in \c *this
  *
  * \only_for_vectors
  *
  * The template parameter \a Size is the number of coefficients in the block
  *
  * \param start the index of the first element of the sub-vector
  *
  * Example: \include MatrixBase_template_int_segment.cpp
  * Output: \verbinclude MatrixBase_template_int_segment.out
  *
  * \sa class Block
  */
template<typename Derived>
template<int Size>
inline VectorBlock<Derived,Size>
DenseBase<Derived>::segment(Index start)
{
  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
  return VectorBlock<Derived,Size>(derived(), start);
}

/** This is the const version of segment<int>(Index).*/
template<typename Derived>
template<int Size>
inline const VectorBlock<Derived,Size>
DenseBase<Derived>::segment(Index start) const
{
  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
  return VectorBlock<Derived,Size>(derived(), start);
}

/** \returns a fixed-size expression of the first coefficients of *this.
  *
  * \only_for_vectors
  *
  * The template parameter \a Size is the number of coefficients in the block
  *
  * Example: \include MatrixBase_template_int_start.cpp
  * Output: \verbinclude MatrixBase_template_int_start.out
  *
  * \sa class Block
  */
template<typename Derived>
template<int Size>
inline VectorBlock<Derived,Size>
DenseBase<Derived>::head()
{
  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
  return VectorBlock<Derived,Size>(derived(), 0);
}

/** This is the const version of head<int>().*/
template<typename Derived>
template<int Size>
inline const VectorBlock<Derived,Size>
DenseBase<Derived>::head() const
{
  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
  return VectorBlock<Derived,Size>(derived(), 0);
}

/** \returns a fixed-size expression of the last coefficients of *this.
  *
  * \only_for_vectors
  *
  * The template parameter \a Size is the number of coefficients in the block
  *
  * Example: \include MatrixBase_template_int_end.cpp
  * Output: \verbinclude MatrixBase_template_int_end.out
  *
  * \sa class Block
  */
template<typename Derived>
template<int Size>
inline VectorBlock<Derived,Size>
DenseBase<Derived>::tail()
{
  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
  return VectorBlock<Derived, Size>(derived(), size() - Size);
}

/** This is the const version of tail<int>.*/
template<typename Derived>
template<int Size>
inline const VectorBlock<Derived,Size>
DenseBase<Derived>::tail() const
{
  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
  return VectorBlock<Derived, Size>(derived(), size() - Size);
}

#endif // EIGEN_VECTORBLOCK_H


text:
<?php

namespace spec\Sylius\Bundle\ArchetypeBundle\Form\EventListener;

use PhpSpec\ObjectBehavior;
use Sylius\Component\Archetype\Model\ArchetypeInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;

/**
 * @author Magdalena Banasiak <magdalena.banasiak@lakion.com>
 */
final class ParentArchetypeListenerSpec extends ObjectBehavior
{
    function let()
    {
        $this->beConstructedWith('product');
    }

    function it_is_initializable()
    {
        $this->shouldHaveType('Sylius\Bundle\ArchetypeBundle\Form\EventListener\ParentArchetypeListener');
    }

    function it_implements_event_subscriber_interface()
    {
        $this->shouldImplement(EventSubscriberInterface::class);
    }

    function it_is_subscribed_to_pre_set_data_form_event()
    {
        $this->getSubscribedEvents()->shouldReturn([FormEvents::PRE_SET_DATA => 'preSetData']);
    }

    function it_throws_exception_if_add_event_subscriber_parameter_is_not_an_instance_of_archetype_interface(FormEvent $event)
    {
        $event->getData()->willReturn('badObject');
        $this->shouldThrow(new UnexpectedTypeException('badObject', ArchetypeInterface::class))
             ->during('preSetData', [$event])
        ;
    }
}


text:
// Copyright (c) Alexandre Mutel. All rights reserved.
// This file is licensed under the BSD-Clause 2 license.
// See the license.txt file in the project root for more information.

namespace Lunet.Core;

public abstract class ContentProcessor<TPlugin> : ProcessorBase<TPlugin>, IContentProcessor where TPlugin : ISitePlugin
{
    protected ContentProcessor(TPlugin plugin) : base(plugin)
    {
    }

    public abstract ContentResult TryProcessContent(ContentObject file, ContentProcessingStage stage);
}


text:
var User = require('../models/User');

module.exports = function(passport) {

	return {
		loginUser : function(req, res, next) {

			passport.authenticate('local', function(err, user, info) {

				if(req.xhr) {

					if (err) { return res.json({ error: err.message }); }
					if (!user) { return res.json({error : "Invalid Login"}); }

					login(req, res, user);

				} else {
					//non ajax request response would go here
				}

			})(req, res, next);
		},

		logoutUser : function(req, res, next) {

			//remove logged in cookie
			res.clearCookie('rememberme');

			req.logout();
			return res.json({logout:'success'});
		},

		registerUser : function(req, res) {

			var email = req.param('email'),
				pass  = req.param('password'),
				fname = req.param('fristName'),
				lname = req.param('lastName');

			User.getByEmail(email, function(err, user) {

				if(!user) {

					User.signup(email, pass, fname, lname, function(err, user){

						if (err) { return res.json({ error: err.message }); }

						login(req, res, user);
					});

				} else {

					return res.json({ error: "User with this email already exists" });
				}
			});

		},

		getUser : function(req, res) {

			var id 		     = req.param('id') || -1,
				loggedInUser = req.isAuthenticated() ? req.user : false;

			User.getById(id, function(err, user) {

				var userIsLoggedIn = loggedInUser ? (loggedInUser.id == user.id) : false;

				var response = user ? {
					firstName: user.firstName,
					lastName: user.lastName,
					id: user.id,
					email: user.email,
					loggedIn: userIsLoggedIn
				} : { error: 'user not found.' };

				res.json(response);
			});
		}
	}
};

function login(req, res, user) {

	req.login(user, function(err) {

		if (err) { return res.json({error:err}); }

		var user = {
			firstName: req.user.firstName,
			lastName: req.user.lastName,
			id: req.user.id,
			email: req.user.email,
			loggedIn: true
		};

		//set cookie to remember logged in user
		res.cookie('rememberme', req.user.id, { /*maxAge: 900000,*/ httpOnly: false });

		return res.json(user);
	});
}


text:
typedef unsigned long int time_t;
#endif

//!
//! Initialize timer module.
//!
void timer_init(void);

//!
//! Returns elapsed time since timer initialization.
//!
//! \return 1x10-4 s
//!
time_t timer_get_time(void);

//!
//! Loops for given period of time.
//!
//! \param ms Number of miliseconds to loop.
//!
void timer_sleep(u32 ms);

#endif // _COLLECT_TIMER_H_


text:
import {expect} from '../test-setup';
import * as cli from '../../src/app/nested-yargs';

import {CLI} from '../../src/app/app';

describe('command line parsing', () => {

	describe('global flags', () => {
		describe('verbosity', () => {
			const rootCategory = new CLI().rootCategory;

			function assertVerbosity(argv, expected) {
				if (argv.clierror) {
					throw argv.clierror;
				}
				if (expected!==undefined) {
					expect(global).to.have.property('verboseLevel').equal(expected);
				} else {
					expect(global).to.not.have.property('verboseLevel');
				}
			}

			it('is 1 by default', () => {
				const argv = cli.parse(rootCategory, []);
				assertVerbosity(argv, 1);
			});

			it('is 2 for a single v flag', () => {
				const argv = cli.parse(rootCategory, ['-v']);
				assertVerbosity(argv, 2);
			});

			it('is 3 for a double v flag', () => {
				const argv = cli.parse(rootCategory, ['-vv']);
				assertVerbosity(argv, 3);
			});

			it('is 0 with one quiet', () => {
				const argv = cli.parse(rootCategory, ['-q']);
				assertVerbosity(argv, 0);
			});

		});
	});

});


text:
#include "ctkCmdLineModuleReference.h"
#include "ctkCmdLineModuleReference_p.h"
#include "ctkCmdLineModuleXmlParser_p.h"
#include "ctkCmdLineModuleXmlException.h"

#include <QBuffer>

//----------------------------------------------------------------------------
ctkCmdLineModuleReferencePrivate::ctkCmdLineModuleReferencePrivate()
  : Backend(NULL), XmlException(NULL)
{
}

//----------------------------------------------------------------------------
ctkCmdLineModuleReferencePrivate::~ctkCmdLineModuleReferencePrivate()
{
  delete XmlException;
}

//----------------------------------------------------------------------------
ctkCmdLineModuleDescription ctkCmdLineModuleReferencePrivate::description() const
{
  // If we already got an XML exception just throw it immediately, since
  // the XML description cannot change for this module reference.
  if (XmlException)
  {
    throw *XmlException;
  }

  // Lazy creation. The title is a required XML element.
  if (Description.title().isNull())
  {
    QByteArray xml(RawXmlDescription);
    QBuffer xmlInput(&xml);
    ctkCmdLineModuleXmlParser parser(&xmlInput, &Description);
    try
    {
      parser.doParse();
    }
    catch (const ctkCmdLineModuleXmlException& e)
    {
      XmlException = e.clone();
    }
  }
  return Description;
}

//----------------------------------------------------------------------------
ctkCmdLineModuleReference::ctkCmdLineModuleReference()
  : d(new ctkCmdLineModuleReferencePrivate())
{}

//----------------------------------------------------------------------------
ctkCmdLineModuleReference::~ctkCmdLineModuleReference()
{
}

//----------------------------------------------------------------------------
ctkCmdLineModuleReference::ctkCmdLineModuleReference(const ctkCmdLineModuleReference &ref)
  : d(ref.d)
{
}

//----------------------------------------------------------------------------
ctkCmdLineModuleReference &ctkCmdLineModuleReference::operator =(const ctkCmdLineModuleReference &ref)
{
  d = ref.d;
  return *this;
}

//----------------------------------------------------------------------------
ctkCmdLineModuleReference::operator bool() const
{
  return !d->RawXmlDescription.isEmpty();
}

//----------------------------------------------------------------------------
ctkCmdLineModuleDescription ctkCmdLineModuleReference::description() const
{
  return d->description();
}

//----------------------------------------------------------------------------
QByteArray ctkCmdLineModuleReference::rawXmlDescription() const
{
  return d->RawXmlDescription;
}

//----------------------------------------------------------------------------
QString ctkCmdLineModuleReference::xmlValidationErrorString() const
{
  return d->XmlValidationErrorString;
}

//----------------------------------------------------------------------------
QUrl ctkCmdLineModuleReference::location() const
{
  return d->Location;
}

//----------------------------------------------------------------------------
ctkCmdLineModuleBackend *ctkCmdLineModuleReference::backend() const
{
  return d->Backend;
}

//----------------------------------------------------------------------------
uint qHash(const ctkCmdLineModuleReference& moduleRef)
{
  return qHash(moduleRef.d.data());
}


text:
var exec = require("child_process").exec,
	path = require("path"),
	fs = require("fs"),
	prefix = path.resolve(__dirname, "../alltests/");

describe("mixed ts with pure es5", function(){
	it("multiple classes parsed from es5 and typescript should parse without error",function(done){

		var testFolder = "mixed_multiple_classes_in_file";
			inputDir = prefix + "/"+testFolder+"/input",
			actualFile = prefix + "/"+testFolder+"/actualOutput/parsed.txt",
			expectedFile = prefix + "/"+testFolder+"/expectedOutput/parsed.txt"

		exec("node transpiled_ts_parser.js " + inputDir + " " + actualFile, function (err) {

			var expectedContent = fs.readFileSync(expectedFile, "utf-8");
			var actualContent = fs.readFileSync(actualFile, "utf-8");

			expect(err).toBe(null)
			expect(expectedContent).toBe(actualContent);

			done();
		})
	})
});


text:
namespace Microsoft.Azure.Management.Monitor
{
    using Microsoft.Azure;
    using Microsoft.Azure.Management;
    using Microsoft.Rest;
    using Microsoft.Rest.Azure;
    using Microsoft.Rest.Azure.OData;
    using Models;
    using System.Collections;
    using System.Collections.Generic;
    using System.Threading;
    using System.Threading.Tasks;

    ///
    /// MetricDefinitionsOperations operations.
    ///
    public partial interface IMetricDefinitionsOperations
    {
        ///
        /// Lists the metric definitions for the resource.
The
        /// **$filter** parameter is optional, and can be used to only retrieve
        /// certain metric definitions.
For example, get just the
        /// definition for the CPU percentage counter: $filter=name.value eq
        /// '\Processor(_Total)\% Processor Time'.
This **$filter** is
        /// very restricted and allows only clauses of the form **'name eq
        /// <value>'** separated by **or** logical
        /// operators.
**NOTE**: No other syntax is allowed.
        ///
        /// <param name='resourceUri'>
        /// The identifier of the resource.
        /// </param>
        /// <param name='odataQuery'>
        /// OData parameters to apply to the operation.
        /// </param>
        /// <param name='customHeaders'>
        /// The headers that will be added to request.
        /// </param>
        /// <param name='cancellationToken'>
        /// The cancellation token.
        /// </param>
        /// <exception cref="ErrorResponseException">
        /// Thrown when the operation returned an invalid status code
        /// </exception>
        /// <exception cref="Microsoft.Rest.SerializationException">
        /// Thrown when unable to deserialize the response
        /// </exception>
        /// <exception cref="Microsoft.Rest.ValidationException">
        /// Thrown when a required parameter is null
        /// </exception>
        Task<AzureOperationResponse<IEnumerable<MetricDefinition>>> ListWithHttpMessagesAsync(string resourceUri, ODataQuery<MetricDefinition> odataQuery = default(ODataQuery<MetricDefinition>), Dictionary<string, List<string>> customHeaders = null, CancellationToken cancellationToken = default(CancellationToken));
    }
}