text:
#include "tensorflow/core/common_runtime/graph_execution_state.h"

#include <memory>
#include <set>
#include <string>
#include <unordered_set>
#include <utility>
#include <vector>

#include "tensorflow/core/common_runtime/device.h"
#include "tensorflow/core/common_runtime/optimization_registry.h"
#include "tensorflow/core/common_runtime/placer.h"
#include "tensorflow/core/framework/graph.pb_text.h"
#include "tensorflow/core/framework/graph_def_util.h"
#include "tensorflow/core/framework/node_def.pb.h"
#include "tensorflow/core/framework/versions.pb.h"
#include "tensorflow/core/graph/algorithm.h"
#include "tensorflow/core/graph/graph.h"
#include "tensorflow/core/graph/graph_constructor.h"
#include "tensorflow/core/graph/subgraph.h"
#include "tensorflow/core/graph/tensor_id.h"
#include "tensorflow/core/graph/validate.h"
#include "tensorflow/core/lib/core/errors.h"
#include "tensorflow/core/lib/core/status.h"
#include "tensorflow/core/lib/strings/stringprintf.h"
#include "tensorflow/core/platform/logging.h"
#include "tensorflow/core/platform/types.h"
#include "tensorflow/core/util/device_name_utils.h"
#include "tensorflow/core/util/util.h"

#ifndef IS_MOBILE_PLATFORM
#include "tensorflow/core/grappler/clusters/virtual_cluster.h"
#include "tensorflow/core/grappler/grappler_item.h"
#include "tensorflow/core/grappler/optimizers/meta_optimizer.h"
#endif  // IS_MOBILE_PLATFORM

namespace tensorflow {

GraphExecutionState::GraphExecutionState(
    GraphDef* graph_def, const GraphExecutionStateOptions& options)
    : stateful_placements_(options.stateful_placements),
      device_set_(options.device_set),
      session_options_(options.session_options),
      flib_def_(new FunctionLibraryDefinition(OpRegistry::Global(),
                                              graph_def->library())),
      graph_(nullptr) {
  // NOTE(mrry): GraphDef does not have a move constructor, so we pass
  // a non-const pointer and use `Swap()` to transfer the contents
  // without copying.
  original_graph_def_.Swap(graph_def);
  // TODO(mrry): Publish placement visualizations or handle the log
  // placement option.
}

GraphExecutionState::~GraphExecutionState() {
  node_name_to_cost_id_map_.clear();
  delete graph_;
}

/* static */ Status GraphExecutionState::MakeForBaseGraph(
    GraphDef* graph_def, const GraphExecutionStateOptions& options,
    std::unique_ptr<GraphExecutionState>* out_state) {
#ifndef __ANDROID__
  VLOG(4) << "Graph proto is " << graph_def->DebugString();
#endif  // __ANDROID__

  std::unique_ptr<GraphExecutionState> ret(
      new GraphExecutionState(graph_def, options));

  TF_RETURN_IF_ERROR(
      AddDefaultAttrsToGraphDef(&ret->original_graph_def_, *ret->flib_def_, 0));
  // TODO(mrry): Refactor InitBaseGraph() so that we don't have to
  // pass an empty BuildGraphOptions (that isn't going to be used when
  // place_pruned_graph is false).
  if (!ret->session_options_->config.graph_options().place_pruned_graph()) {
    TF_RETURN_IF_ERROR(ret->InitBaseGraph(BuildGraphOptions()));
  }
  *out_state = std::move(ret);
  return Status::OK();
}

/* static */ Status GraphExecutionState::MakeForPrunedGraph(
    const FunctionDefLibrary& func_def_lib,
    const GraphExecutionStateOptions& options, const GraphDef& graph_def,
    const BuildGraphOptions& subgraph_options,
    std::unique_ptr<GraphExecutionState>* out_state,
    std::unique_ptr<ClientGraph>* out_client_graph) {
  DCHECK(options.session_options->config.graph_options().place_pruned_graph());
  // NOTE(mrry): This makes a copy of `graph_def`, which is
  // regrettable. We could make `GraphDef` objects sharable between
  // execution states to optimize pruned graph execution, but since
  // this case is primarily used for interactive sessions, we make the
  // bet that graph construction is not performance-critical. (Note
  // also that the previous version used `Extend()`, which is strictly
  // more expensive than copying a `GraphDef`.)
  GraphDef temp(graph_def);
  std::unique_ptr<GraphExecutionState> ret(
      new GraphExecutionState(&temp, options));
  TF_RETURN_IF_ERROR(
      AddDefaultAttrsToGraphDef(&ret->original_graph_def_, *ret->flib_def_, 0));
  TF_RETURN_IF_ERROR(ret->InitBaseGraph(subgraph_options));
  TF_RETURN_IF_ERROR(ret->BuildGraph(subgraph_options, out_client_graph));
  *out_state = std::move(ret);
  return Status::OK();
}

Status GraphExecutionState::Extend(
    const GraphDef& extension_def,
    std::unique_ptr<GraphExecutionState>* out) const {
  GraphDef gdef;

  // 1. Copy the function library.
  TF_RETURN_IF_ERROR(flib_def_->AddLibrary(extension_def.library()));
  *gdef.mutable_library() = flib_def_->ToProto();

  // 2. Build an index of the new node names.
  std::unordered_set<string> new_names;
  for (const NodeDef& node : extension_def.node()) {
    new_names.insert(node.name());
  }

  // 3. Add the non-duplicates from the old graph to the new graph.
  //    Return an error if the same node name appears in both the
  //    old graph and the extension.
  for (const NodeDef& node : original_graph_def_.node()) {
    if (new_names.count(node.name()) == 0) {
      *gdef.add_node() = node;
    } else {
      return errors::InvalidArgument(tensorflow::strings::Printf(
          "GraphDef argument to Extend includes node '%s', which was created "
          "by a previous call to Create or Extend in this session.",
          node.name().c_str()));
    }
  }

  // 4. Merge the versions field.
  int old_node_size = gdef.node_size();
  gdef.mutable_node()->MergeFrom(extension_def.node());
  TF_RETURN_IF_ERROR(
      AddDefaultAttrsToGraphDef(&gdef, *flib_def_, old_node_size));
  // Merge versions
  if (gdef.has_versions()) {
    if (gdef.versions().producer() != extension_def.versions().producer()) {
      return errors::InvalidArgument(
          "Can't extend GraphDef at version ", gdef.versions().producer(),
          " with graph at version ", extension_def.versions().producer());
    }
    VersionDef* versions = gdef.mutable_versions();
    versions->set_min_consumer(std::max(
        versions->min_consumer(), extension_def.versions().min_consumer()));
    if (extension_def.versions().bad_consumers_size()) {
      // Add new bad_consumers that aren't already marked bad.
      //
      // Note: This implementation is quadratic time if there are many calls to
      // ExtendLocked with many bad consumers.  Since this is unlikely, and
      // fixing it would require data structures outside of this routine,
      // quadratic time it is.
      auto* bad_consumers = versions->mutable_bad_consumers();
      const std::unordered_set<int> existing(bad_consumers->begin(),
                                             bad_consumers->end());
      for (const int v : extension_def.versions().bad_consumers()) {
        if (existing.find(v) == existing.end()) {
          bad_consumers->Add(v);
        }
      }
    }

  } else {
    gdef.mutable_versions()->CopyFrom(extension_def.versions());
  }

  // 5. Validate that the final graphdef is valid.
  if (gdef.versions().producer() >= 5) {
    // Validate the graph: we assume that merging two valid graphs
    // should maintain graph validity.
    TF_RETURN_IF_ERROR(graph::ValidateGraphDef(gdef, *flib_def_));
  }

  // 6. Add the extension.
  GraphExecutionStateOptions combined_options;
  combined_options.device_set = device_set_;
  combined_options.session_options = session_options_;
  combined_options.stateful_placements = stateful_placements_;

  // NOTE(mrry): `gdef` is no longer valid after the constructor
  // executes.
  std::unique_ptr<GraphExecutionState> new_execution_state(
      new GraphExecutionState(&gdef, combined_options));

  TF_RETURN_IF_ERROR(AddDefaultAttrsToGraphDef(
      &new_execution_state->original_graph_def_, *flib_def_, 0));
  if (!session_options_->config.graph_options().place_pruned_graph()) {
    // TODO(mrry): Refactor InitBaseGraph() so that we don't have to
    // pass an empty BuildGraphOptions (that isn't going to be used
    // when place_pruned_graph is false).
    TF_RETURN_IF_ERROR(new_execution_state->InitBaseGraph(BuildGraphOptions()));
  }
  *out = std::move(new_execution_state);

  // TODO(mrry): This is likely to be used for non-throughput-sensitive
  // interactive workloads, but in future we may want to transfer other
  // parts of the placement and/or cost model.
  return Status::OK();
}

void GraphExecutionState::SaveStatefulNodes(Graph* graph) {
  for (Node* n : graph->nodes()) {
    if (n->op_def().is_stateful()) {
      VLOG(2) << "Saving " << n->DebugString();
      stateful_placements_[n->name()] = n->assigned_device_name();
    }
  }
}

void GraphExecutionState::RestoreStatefulNodes(Graph* graph) {
  for (Node* n : graph->nodes()) {
    if (n->op_def().is_stateful()) {
      auto iter = stateful_placements_.find(n->name());
      if (iter != stateful_placements_.end()) {
        n->set_assigned_device_name(iter->second);
        VLOG(2) << "Restored " << n->DebugString();
      }
    }
  }
}

namespace {

class TensorConnectionPruneRewrite : public subgraph::PruneRewrite {
 public:
  TensorConnectionPruneRewrite(const string* endpoint_name,
                               NodeBuilder::NodeOut from_tensor)
      : subgraph::PruneRewrite(endpoint_name, nullptr /* device_info */),
        from_tensor_(std::move(from_tensor)) {}

  Status AddNode(Graph* g, NodeBuilder::NodeOut feed_tensor,
                 Node** out_node) override {
    Status s;
    auto check_no_cycle_fn = [this, feed_tensor, &s](Node* n) {
      if (n == feed_tensor.node) {
        s.Update(errors::InvalidArgument(
            "Requested Tensor connection between nodes \"",
            feed_tensor.node->name(), "\" and \"", from_tensor_.node->name(),
            "\" would create a cycle."));
      }
    };
    ReverseDFSFrom(*g, {from_tensor_.node}, std::move(check_no_cycle_fn),
                   nullptr);
    TF_RETURN_IF_ERROR(s);

    TF_RETURN_IF_ERROR(
        NodeBuilder(strings::StrCat("_identity_", feed_tensor.node->name(), "_",
                                    feed_tensor.index),
                    "Identity")
            .Input(from_tensor_)
            .Attr("T",
                  BaseType(from_tensor_.node->output_type(from_tensor_.index)))
            .Finalize(g, out_node));

    (*out_node)->set_assigned_device_name(
        feed_tensor.node->assigned_device_name());
    return Status::OK();
  }

 private:
  NodeBuilder::NodeOut from_tensor_;
};

template <class Map>
Status LookupDevice(const DeviceSet& device_set, const string& tensor_name,
                    const Map& tensor2device,
                    const tensorflow::DeviceAttributes** out_device_attrs) {
  *out_device_attrs = nullptr;
  if (tensor2device.empty()) {
    *out_device_attrs = &device_set.client_device()->attributes();
    return Status::OK();
  }
  const auto it = tensor2device.find(tensor_name);
  if (it == tensor2device.end()) {
    *out_device_attrs = &device_set.client_device()->attributes();
    return Status::OK();
  }
  DeviceNameUtils::ParsedName parsed_name;
  if (!DeviceNameUtils::ParseFullName(it->second, &parsed_name)) {
    return errors::InvalidArgument("Invalid device name ('", it->second,
                                   "') provided for the tensor '", tensor_name,
                                   "' in CallableOptions");
  }
  Device* device = device_set.FindDeviceByName(
      DeviceNameUtils::ParsedNameToString(parsed_name));
  if (device == nullptr) {
    return errors::InvalidArgument("Device '", it->second,
                                   "' specified for tensor '", tensor_name,
                                   "' in CallableOptions does not exist");
  }
  *out_device_attrs = &device->attributes();
  return Status::OK();
}

struct TensorAndDevice {
  // WARNING: backing memory for the 'tensor' field is NOT owend.
  const TensorId tensor;
  // WARNING: device pointer is not owned, so must outlive TensorAndDevice.
  const DeviceAttributes* device;
};

// Tensors of some DataTypes cannot placed in device memory as feeds or
// fetches. Validate against a whitelist of those known to work.
bool IsFeedAndFetchSupported(DataType dtype, const string& device_type) {
  // The mechanism for supporting feeds of device-backed Tensors requires
  // the _Arg kernel to be registered for the corresponding type (and that
  // the input to the kernel be in device and not host memory).
  //
  // The mechanism for supporting fetches of device-backed Tensors requires
  // the _Retval kernel to be registered for the corresponding type (and
  // that the output is produced in device and not host memory).
  //
  // For now, we return true iff there are _Arg AND _Retval kernels for dtype on
  // the device. False negatives are okay, false positives would be bad.
  //
  // TODO(ashankar): Instead of a whitelist here, perhaps we could query
  // the kernel registry for _Arg and _Retval kernels instead.
  if (device_type == DEVICE_CPU) return true;
  if (device_type != DEVICE_GPU) return false;
  switch (dtype) {
    case DT_BFLOAT16:
    case DT_BOOL:
    case DT_COMPLEX128:
    case DT_COMPLEX64:
    case DT_DOUBLE:
    case DT_FLOAT:
    case DT_HALF:
    case DT_INT16:
    case DT_INT64:
    case DT_INT8:
    case DT_UINT16:
    case DT_UINT8:
      return true;
    default:
      return false;
  }
}

Status ValidateFeedAndFetchDevices(
    const Graph& graph,
    const std::vector<TensorAndDevice>& tensors_and_devices) {
  if (tensors_and_devices.empty()) return Status::OK();
  std::vector<bool> found(tensors_and_devices.size(), false);
  for (const Node* node : graph.nodes()) {
    // Linearly looping through all nodes and then all feed+fetch tensors isn't
    // quite efficient. At the time of this writing, the expectation was that
    // tensors_and_devices.size() is really small in practice, so this won't be
    // problematic.
    // Revist and make a more efficient lookup possible if needed (e.g., perhaps
    // Graph can maintain a map from node name to Node*).
    for (int i = 0; i < tensors_and_devices.size(); ++i) {
      const TensorAndDevice& td = tensors_and_devices[i];
      if (td.tensor.first != node->name()) continue;
      found[i] = true;
      TF_RETURN_IF_ERROR(graph.IsValidOutputTensor(node, td.tensor.second));
      const DataType dtype = node->output_type(td.tensor.second);
      if (!IsFeedAndFetchSupported(dtype, td.device->device_type())) {
        return errors::Unimplemented(
            "Cannot feed or fetch tensor '", td.tensor.ToString(),
            "' from device ", td.device->name(), " as feeding/fetching from ",
            td.device->device_type(), " devices is not yet supported for ",
            DataTypeString(dtype), " tensors");
      }
    }
  }
  for (int i = 0; i < found.size(); ++i) {
    if (!found[i]) {
      return errors::InvalidArgument(
          "Tensor ", tensors_and_devices[i].tensor.ToString(),
          ", specified in either feed_devices or fetch_devices was not found "
          "in the Graph");
    }
  }
  return Status::OK();
}
}  // namespace

Status GraphExecutionState::PruneGraph(
    const BuildGraphOptions& options, Graph* graph,
    subgraph::RewriteGraphMetadata* out_rewrite_metadata) {
  std::vector<std::unique_ptr<subgraph::PruneRewrite>> feed_rewrites;
  feed_rewrites.reserve(options.callable_options.feed_size());
  std::vector<std::unique_ptr<subgraph::PruneRewrite>> fetch_rewrites;
  fetch_rewrites.reserve(options.callable_options.fetch_size());
  if (options.use_function_convention) {
    std::vector<TensorAndDevice> tensors_and_devices;
    for (int i = 0; i < options.callable_options.feed_size(); ++i) {
      // WARNING: feed MUST be a reference, since ArgFeedRewrite and
      // tensors_and_devices holds on to its address.
      const string& feed = options.callable_options.feed(i);
      const DeviceAttributes* device_info;
      TF_RETURN_IF_ERROR(LookupDevice(*device_set_, feed,
                                      options.callable_options.feed_devices(),
                                      &device_info));
      feed_rewrites.emplace_back(
          new subgraph::ArgFeedRewrite(&feed, device_info, i));
      tensors_and_devices.push_back({ParseTensorName(feed), device_info});
    }
    if (!options.callable_options.fetch_devices().empty() &&
        !options.callable_options.fetch_skip_sync()) {
      return errors::Unimplemented(
          "CallableOptions.fetch_skip_sync = false is not yet implemented. You "
          "can set it to true instead, but MUST ensure that Device::Sync() is "
          "invoked on the Device corresponding to the fetched tensor before "
          "dereferencing the Tensor's memory.");
    }
    for (int i = 0; i < options.callable_options.fetch_size(); ++i) {
      // WARNING: fetch MUST be a reference, since RetvalFetchRewrite and
      // tensors_and_devices holds on to its address.
      const string& fetch = options.callable_options.fetch(i);
      const DeviceAttributes* device_info;
      TF_RETURN_IF_ERROR(LookupDevice(*device_set_, fetch,
                                      options.callable_options.fetch_devices(),
                                      &device_info));
      fetch_rewrites.emplace_back(
          new subgraph::RetvalFetchRewrite(&fetch, device_info, i));
      tensors_and_devices.push_back({ParseTensorName(fetch), device_info});
    }
    TF_RETURN_IF_ERROR(
        ValidateFeedAndFetchDevices(*graph, tensors_and_devices));
  } else {
    if (!options.callable_options.feed_devices().empty() ||
        !options.callable_options.fetch_devices().empty()) {
      return errors::Unimplemented(
          "CallableOptions::feed_devices and CallableOptions::fetch_devices "
          "to configure feeding/fetching tensors to/from device memory is not "
          "yet supported when using a remote session.");
    }
    const DeviceAttributes* device_info =
        &device_set_->client_device()->attributes();
    for (const string& feed : options.callable_options.feed()) {
      feed_rewrites.emplace_back(
          new subgraph::RecvFeedRewrite(&feed, device_info));
    }
    for (const string& fetch : options.callable_options.fetch()) {
      fetch_rewrites.emplace_back(
          new subgraph::SendFetchRewrite(&fetch, device_info));
    }
  }

  for (const TensorConnection& tensor_connection :
       options.callable_options.tensor_connection()) {
    Node* from_node = nullptr;
    TensorId from_id(ParseTensorName(tensor_connection.from_tensor()));

    for (Node* n : graph->nodes()) {
      if (n->name() == from_id.first) {
        from_node = n;
        break;
      }
    }
    if (from_node == nullptr) {
      return errors::InvalidArgument(
          "Requested tensor connection from unknown node: \"",
          tensor_connection.to_tensor(), "\".");
    }
    if (from_id.second >= from_node->num_outputs()) {
      return errors::InvalidArgument(
          "Requested tensor connection from unknown edge: \"",
          tensor_connection.to_tensor(),
          "\" (actual number of outputs = ", from_node->num_outputs(), ").");
    }

    feed_rewrites.emplace_back(new TensorConnectionPruneRewrite(
        &tensor_connection.to_tensor(), {from_node, from_id.second}));
  }

  std::vector<string> target_node_names(
      options.callable_options.target().begin(),
      options.callable_options.target().end());
  TF_RETURN_IF_ERROR(subgraph::RewriteGraphForExecution(
      graph, feed_rewrites, fetch_rewrites, target_node_names,
      out_rewrite_metadata));

  CHECK_EQ(out_rewrite_metadata->feed_types.size(),
           options.callable_options.feed_size() +
               options.callable_options.tensor_connection_size());
  for (int i = 0; i < options.callable_options.tensor_connection_size(); ++i) {
    out_rewrite_metadata->feed_types.pop_back();
  }
  return Status::OK();
}

Status GraphExecutionState::InitBaseGraph(const BuildGraphOptions& options) {
  const GraphDef* graph_def = &original_graph_def_;

  std::unique_ptr<Graph> new_graph(new Graph(OpRegistry::Global()));
  GraphConstructorOptions opts;
  TF_RETURN_IF_ERROR(ConvertGraphDefToGraph(opts, *graph_def, new_graph.get()));
  for (const Node* n : new_graph->nodes()) {
    VLOG(2) << "Mapping " << n->name() << " to " << n->cost_id();
    node_name_to_cost_id_map_[n->name()] = n->cost_id();
  }
  if (session_options_ &&
      session_options_->config.graph_options().place_pruned_graph()) {
    // Rewrite the graph before placement.
    rewrite_metadata_.reset(new subgraph::RewriteGraphMetadata);
    TF_RETURN_IF_ERROR(
        PruneGraph(options, new_graph.get(), rewrite_metadata_.get()));
  }

  // Save stateful placements before placing.
  RestoreStatefulNodes(new_graph.get());

  GraphOptimizationPassOptions optimization_options;
  optimization_options.session_options = session_options_;
  optimization_options.graph = &new_graph;
  optimization_options.flib_def = flib_def_.get();
  optimization_options.device_set = device_set_;

  TF_RETURN_IF_ERROR(OptimizationPassRegistry::Global()->RunGrouping(
      OptimizationPassRegistry::PRE_PLACEMENT, optimization_options));

  Placer placer(new_graph.get(), device_set_, session_options_);
  // TODO(mrry): Consider making the Placer cancelable.
  TF_RETURN_IF_ERROR(placer.Run());

  TF_RETURN_IF_ERROR(OptimizationPassRegistry::Global()->RunGrouping(
      OptimizationPassRegistry::POST_PLACEMENT, optimization_options));

  SaveStatefulNodes(new_graph.get());
  graph_ = new_graph.release();
  return Status::OK();
}

Status GraphExecutionState::OptimizeGraph(
    const BuildGraphOptions& options, std::unique_ptr<Graph>* optimized_graph,
    std::unique_ptr<FunctionLibraryDefinition>* optimized_flib) {
#ifndef IS_MOBILE_PLATFORM
  if (session_options_->config.graph_options().place_pruned_graph()) {
    return errors::InvalidArgument("Can't optimize a pruned graph");
  }

  const RewriterConfig& rewrite_options =
      session_options_->config.graph_options().rewrite_options();

  if (grappler::MetaOptimizerEnabled(rewrite_options)) {
    // Adding this functionality in steps. The first step is to make sure
    // we don't break dependencies. The second step will be to turn the
    // functionality on by default.
    grappler::GrapplerItem item;
    item.id = "tf_graph";
    graph_->ToGraphDef(&item.graph);
    // TODO(b/114748242): Add a unit test to test this bug fix.
    if (flib_def_) {
      *item.graph.mutable_library() = flib_def_->ToProto();
    }

    item.fetch.insert(item.fetch.end(),
                      options.callable_options.fetch().begin(),
                      options.callable_options.fetch().end());
    item.fetch.insert(item.fetch.end(),
                      options.callable_options.target().begin(),
                      options.callable_options.target().end());

    for (const TensorConnection& tensor_connection :
         options.callable_options.tensor_connection()) {
      item.fetch.push_back(tensor_connection.from_tensor());
    }

    if (!(options.callable_options.feed().empty() &&
          options.callable_options.tensor_connection().empty())) {
      std::unordered_set<string> feeds;
      for (const string& feed : options.callable_options.feed()) {
        TensorId id = ParseTensorName(feed);
        if (id.second != 0) {
          return errors::InvalidArgument("Unsupported feed: ", feed);
        }
        feeds.emplace(id.first);
      }
      for (const TensorConnection& tensor_connection :
           options.callable_options.tensor_connection()) {
        TensorId id = ParseTensorName(tensor_connection.to_tensor());
        if (id.second != 0) {
          return errors::InvalidArgument("Unsupported feed: ",
                                         tensor_connection.to_tensor());
        }
        feeds.emplace(id.first);
      }
      for (const NodeDef& node : original_graph_def_.node()) {
        if (feeds.find(node.name()) == feeds.end()) {
          continue;
        }
        if (node.attr().count("dtype") == 0 ||
            node.attr().count("shape") == 0) {
          return errors::InvalidArgument("Missing node shape or type");
        }
        TensorShapeProto shape_proto(node.attr().at("shape").shape());
        // If the shape of the placeholder value is only partially known,
        // we're free to use any dimension we want to feed the placeholder. We
        // choose 1 to minimize the memory impact. Note that this only matters
        // if an optimizer choose to run the graph to build its cost model,
        // which doesn't happen (yet)
        if (shape_proto.unknown_rank()) {
          shape_proto.set_unknown_rank(false);
        }
        for (auto& dim : *shape_proto.mutable_dim()) {
          if (dim.size() < 0) {
            dim.set_size(1);
          }
        }
        TensorShape shape(shape_proto);
        DataType type = node.attr().at("dtype").type();
        Tensor fake_input(type, shape);
        item.feed.emplace_back(node.name(), fake_input);
      }
    }

    Device* cpu_device = nullptr;
    for (const auto& device : device_set_->devices()) {
      if (device->parsed_name().id == 0 &&
          StringPiece(device->parsed_name().type) == "CPU" &&
          device->GetAllocator(AllocatorAttributes()) != nullptr) {
        cpu_device = device;
      }
    }
    grappler::VirtualCluster cluster(device_set_);
    GraphDef new_graph;
    TF_RETURN_IF_ERROR(grappler::RunMetaOptimizer(
        item, rewrite_options, cpu_device, &cluster, &new_graph));

    // Merge optimized graph function library with an original library.
    // Optimized graph might have new functions specialized for it's
    // instantiation context (see Grappler function optimizer), and modified
    // function body for the existing functions.
    optimized_flib->reset(new FunctionLibraryDefinition(*flib_def_));

    for (const FunctionDef& fdef : new_graph.library().function()) {
      const string& func_name = fdef.signature().name();

      if ((*optimized_flib)->Contains(func_name)) {
        VLOG(3) << "Replace function: name=" << func_name;
        TF_RETURN_IF_ERROR((*optimized_flib)->ReplaceFunction(func_name, fdef));
      } else {
        VLOG(3) << "Add new function: name=" << func_name;
        TF_RETURN_IF_ERROR((*optimized_flib)->AddFunctionDef(fdef));
      }
    }

    optimized_graph->reset(new Graph(OpRegistry::Global()));

    GraphConstructorOptions opts;
    opts.allow_internal_ops = true;
    TF_RETURN_IF_ERROR(
        ConvertGraphDefToGraph(opts, new_graph, optimized_graph->get()));
    // The graph conversion sets the requested device names but not the
    // assigned device names. However, since at this point the graph is placed
    // TF expects an assigned device name for every node. Therefore we copy
    // the requested device into the assigned device field.
    for (Node* node : optimized_graph->get()->nodes()) {
      node->set_assigned_device_name(node->requested_device());
    }
    return Status::OK();
  } else {
    return errors::InvalidArgument("Meta Optimizer disabled");
  }
#else
  return errors::InvalidArgument("Mobile platforms not supported");
#endif  // IS_MOBILE_PLATFORM
}

Status GraphExecutionState::BuildGraph(const BuildGraphOptions& options,
                                       std::unique_ptr<ClientGraph>* out) {
  VLOG(1) << "BuildGraph";
  if (!graph_) {
    // It is only valid to call this method directly when the original graph
    // was created with the option `place_pruned_graph == false`.
    return errors::Internal(
        "Attempted to prune a graph that has not been fully initialized.");
  }

  // Grappler optimization might change the structure of a graph itself, and
  // also it can add/prune functions to/from the library.
  std::unique_ptr<Graph> optimized_graph;
  std::unique_ptr<FunctionLibraryDefinition> optimized_flib;

  Status s = OptimizeGraph(options, &optimized_graph, &optimized_flib);
  if (!s.ok()) {
    VLOG(2) << "Grappler optimization failed. Error: " << s.error_message();
    // Simply copy the original graph and the function library if we couldn't
    // optimize it.
    optimized_graph.reset(new Graph(flib_def_.get()));
    CopyGraph(*graph_, optimized_graph.get());
    optimized_flib.reset(new FunctionLibraryDefinition(*flib_def_));
  }

  subgraph::RewriteGraphMetadata rewrite_metadata;
  if (session_options_ == nullptr ||
      !session_options_->config.graph_options().place_pruned_graph()) {
    TF_RETURN_IF_ERROR(
        PruneGraph(options, optimized_graph.get(), &rewrite_metadata));
  } else {
    // This GraphExecutionState represents a graph that was
    // pruned when this was constructed, so we copy the metadata from
    // a member variable.
    CHECK(rewrite_metadata_);
    rewrite_metadata = *rewrite_metadata_;
  }

  CHECK_EQ(options.callable_options.feed_size(),
           rewrite_metadata.feed_types.size());
  CHECK_EQ(options.callable_options.fetch_size(),
           rewrite_metadata.fetch_types.size());

  // TODO(andydavis): Clarify optimization pass requirements around CostModel.
  GraphOptimizationPassOptions optimization_options;
  optimization_options.session_options = session_options_;
  optimization_options.graph = &optimized_graph;
  optimization_options.flib_def = optimized_flib.get();
  optimization_options.device_set = device_set_;

  TF_RETURN_IF_ERROR(OptimizationPassRegistry::Global()->RunGrouping(
      OptimizationPassRegistry::POST_REWRITE_FOR_EXEC, optimization_options));

  int64 collective_graph_key = options.collective_graph_key;
  if (collective_graph_key == BuildGraphOptions::kNoCollectiveGraphKey) {
    // BuildGraphOptions does not specify a collective_graph_key.  Check all
    // nodes in the Graph and FunctionLibraryDefinition for collective ops and
    // if found, initialize a collective_graph_key as a hash of the ordered set
    // of instance keys.
    std::set<int32> instance_key_set;
    for (Node* node : optimized_graph->nodes()) {
      if (node->IsCollective()) {
        int32 instance_key;
        TF_RETURN_IF_ERROR(
            GetNodeAttr(node->attrs(), "instance_key", &instance_key));
        instance_key_set.emplace(instance_key);
      } else {
        const FunctionDef* fdef = optimized_flib->Find(node->def().op());
        if (fdef != nullptr) {
          for (const NodeDef& ndef : fdef->node_def()) {
            if (ndef.op() == "CollectiveReduce" ||
                ndef.op() == "CollectiveBcastSend" ||
                ndef.op() == "CollectiveBcastRecv") {
              int32 instance_key;
              TF_RETURN_IF_ERROR(
                  GetNodeAttr(ndef, "instance_key", &instance_key));
              instance_key_set.emplace(instance_key);
            }
          }
        }
      }
    }
    if (!instance_key_set.empty()) {
      uint64 hash = 0x8774aa605c729c72ULL;
      for (int32 instance_key : instance_key_set) {
        hash = Hash64Combine(instance_key, hash);
      }
      collective_graph_key = hash;
    }
  }

  // Copy the extracted graph in order to make its node ids dense,
  // since the local CostModel used to record its stats is sized by
  // the largest node id.
  std::unique_ptr<ClientGraph> dense_copy(
      new ClientGraph(std::move(optimized_flib), rewrite_metadata.feed_types,
                      rewrite_metadata.fetch_types, collective_graph_key));
  CopyGraph(*optimized_graph, &dense_copy->graph);

  // TODO(vrv): We should check invariants of the graph here.

  *out = std::move(dense_copy);
  return Status::OK();
}

}  // namespace tensorflow


text:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <title>Python API for Applications — SIP 4.16.3 Reference Guide</title>

    <link rel="stylesheet" href="_static/default.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />

    <link rel="shortcut icon" href="_static/logo_tn.ico"/>
    <link rel="top" title="SIP 4.16.3 Reference Guide" href="index.html" />
    <link rel="next" title="The Build System" href="build_system.html" />
    <link rel="prev" title="Using the C API when Embedding" href="embedding.html" />

Navigation

          index

          modules |

          next |

          previous |

SIP 4.16.3 Reference Guide »

Python API for Applications¶

The main purpose of the <tt class="xref py py-mod docutils literal">sip</tt> module is to provide functionality common to
all SIP generated bindings.  It is loaded automatically and most of the time
you will completely ignore it.  However, it does expose some functionality that
can be used by applications.

<tt class="descclassname">sip.</tt><tt class="descname">cast</tt><big>(</big>obj, type<big>)</big> → object¶

This does the Python equivalent of casting a C++ instance to one of its
sub or super-class types.

<col class="field-name" />
<col class="field-body" />

Parameters:

obj – the Python object.

type – the type.

Returns:
a new Python object is that wraps the same C++ instance as obj, but
has the type type.

<tt class="descclassname">sip.</tt><tt class="descname">delete</tt><big>(</big>obj<big>)</big>¶

For C++ instances this calls the C++ destructor.  For C structures it
returns the structure’s memory to the heap.

<col class="field-name" />
<col class="field-body" />

Parameters:	obj – the Python object.

<tt class="descclassname">sip.</tt><tt class="descname">dump</tt><big>(</big>obj<big>)</big>¶

This displays various bits of useful information about the internal state
of the Python object that wraps a C++ instance or C structure.

<col class="field-name" />
<col class="field-body" />

Parameters:	obj – the Python object.

<tt class="descclassname">sip.</tt><tt class="descname">enableautoconversion</tt><big>(</big>type, enable<big>)</big> → bool¶

New in version 4.14.7.

Instances of some classes may be automatically converted to other Python
objects even though the class has been wrapped.  This allows that behaviour
to be suppressed so that an instances of the wrapped class is returned
instead.

<col class="field-name" />
<col class="field-body" />

Parameters:

type – the Python type object.

enable – is <tt class="docutils literal">True</tt> if auto-conversion should be enabled for the type.  This is
the default behaviour.

Returns:
<tt class="docutils literal">True</tt> or <tt class="docutils literal">False</tt> depending on whether or not auto-conversion was
previously enabled for the type.  This allows the previous state to be
restored later on.

<tt class="descclassname">sip.</tt><tt class="descname">getapi</tt><big>(</big>name<big>)</big> → version¶

New in version 4.9.

This returns the version number that has been set for an API.  The version
number is either set explicitly by a call to <tt class="xref py py-func docutils literal">sip.setapi()</tt> or
implicitly by importing the module that defines it.

<col class="field-name" />
<col class="field-body" />

Parameters:	name – the name of the API.

Returns:	The version number that has been set for the API.  An exception will
be raised if the API is unknown.

<tt class="descclassname">sip.</tt><tt class="descname">isdeleted</tt><big>(</big>obj<big>)</big> → bool¶

This checks if the C++ instance or C structure has been deleted and
returned to the heap.

<col class="field-name" />
<col class="field-body" />

Parameters:	obj – the Python object.

Returns:	<tt class="docutils literal">True</tt> if the C/C++ instance has been deleted.

<tt class="descclassname">sip.</tt><tt class="descname">ispycreated</tt><big>(</big>obj<big>)</big> → bool¶

New in version 4.12.1.

This checks if the C++ instance or C structure was created by Python.  If
it was then it is possible to call a C++ instance’s protected methods.

<col class="field-name" />
<col class="field-body" />

Parameters:	obj – the Python object.

Returns:	<tt class="docutils literal">True</tt> if the C/C++ instance was created by Python.

<tt class="descclassname">sip.</tt><tt class="descname">ispyowned</tt><big>(</big>obj<big>)</big> → bool¶

This checks if the C++ instance or C structure is owned by Python.

<col class="field-name" />
<col class="field-body" />

Parameters:	obj – the Python object.

Returns:	<tt class="docutils literal">True</tt> if the C/C++ instance is owned by Python.

<tt class="descclassname">sip.</tt><tt class="descname">setapi</tt><big>(</big>name, version<big>)</big>¶

New in version 4.9.

This sets the version number of an API.  An exception is raised if a
different version number has already been set, either explicitly by a
previous call, or implicitly by importing the module that defines it.

<col class="field-name" />
<col class="field-body" />

Parameters:

name – the name of the API.

version – The version number to set for the API.  Version numbers must be
greater than or equal to 1.

<tt class="descclassname">sip.</tt><tt class="descname">setdeleted</tt><big>(</big>obj<big>)</big>¶

This marks the C++ instance or C structure as having been deleted and
returned to the heap so that future references to it raise an exception
rather than cause a program crash.  Normally SIP handles such things
automatically, but there may be circumstances where this isn’t possible.

<col class="field-name" />
<col class="field-body" />

Parameters:	obj – the Python object.

<tt class="descclassname">sip.</tt><tt class="descname">setdestroyonexit</tt><big>(</big>destroy<big>)</big>¶

New in version 4.14.2.

When the Python interpreter exits it garbage collects those objects that it
can.  This means that any corresponding C++ instances and C structures
owned by Python are destroyed.  Unfortunately this happens in an
unpredictable order and so can cause memory faults within the wrapped
library.  Calling this function with a value of <tt class="docutils literal">False</tt> disables the
automatic destruction of C++ instances and C structures.

<col class="field-name" />
<col class="field-body" />

Parameters:	destroy – <tt class="docutils literal">True</tt> if all C++ instances and C structures owned by Python should
be destroyed when the interpreter exits.  This is the default.

<tt class="descclassname">sip.</tt><tt class="descname">settracemask</tt><big>(</big>mask<big>)</big>¶

If the bindings have been created with SIP’s -r command
line option then the generated code will include debugging statements that
trace the execution of the code.  (It is particularly useful when trying to
understand the operation of a C++ library’s virtual function calls.)

<col class="field-name" />
<col class="field-body" />

Parameters:	mask – the mask that determines which debugging statements are enabled.

Debugging statements are generated at the following points:

in a C++ virtual function (mask is <tt class="docutils literal">0x0001</tt>)

in a C++ constructor (mask is <tt class="docutils literal">0x0002</tt>)

in a C++ destructor (mask is <tt class="docutils literal">0x0004</tt>)

in a Python type’s __init__ method (mask is <tt class="docutils literal">0x0008</tt>)

in a Python type’s __del__ method (mask is <tt class="docutils literal">0x0010</tt>)

in a Python type’s ordinary method (mask is <tt class="docutils literal">0x0020</tt>).

By default the trace mask is zero and all debugging statements are
disabled.

class <tt class="descclassname">sip.</tt><tt class="descname">simplewrapper</tt>¶

This is an alternative type object than can be used as the base type of an
instance wrapped by SIP.  Objects using this are smaller than those that
use the default <tt class="xref py py-class docutils literal">sip.wrapper</tt> type but do not support the concept of
object ownership.

<tt class="descclassname">sip.</tt><tt class="descname">SIP_VERSION</tt>¶

This is a Python integer object that represents the SIP version number as
a 3 part hexadecimal number (e.g. v4.0.0 is represented as <tt class="docutils literal">0x040000</tt>).
It was first implemented in SIP v4.2.

<tt class="descclassname">sip.</tt><tt class="descname">SIP_VERSION_STR</tt>¶

This is a Python string object that defines the SIP version number as
represented as a string.  For development snapshots it will start with
<tt class="docutils literal">snapshot-</tt>.  It was first implemented in SIP v4.3.

<tt class="descclassname">sip.</tt><tt class="descname">transferback</tt><big>(</big>obj<big>)</big>¶

This function is a wrapper around <tt class="xref c c-func docutils literal">sipTransferBack()</tt>.

<tt class="descclassname">sip.</tt><tt class="descname">transferto</tt><big>(</big>obj, owner<big>)</big>¶

This function is a wrapper around <tt class="xref c c-func docutils literal">sipTransferTo()</tt>.

<tt class="descclassname">sip.</tt><tt class="descname">unwrapinstance</tt><big>(</big>obj<big>)</big> → integer¶

This returns the address, as an integer, of a wrapped C/C++ structure or
class instance.

<col class="field-name" />
<col class="field-body" />

Parameters:	obj – the Python object.

Returns:	an integer that is the address of the C/C++ instance.

class <tt class="descclassname">sip.</tt><tt class="descname">voidptr</tt>¶

This is the type object for the type SIP uses to represent a C/C++
<tt class="docutils literal">void *</tt>.  It may have a size associated with the address in which case
the Python buffer interface is supported.  The type has the following
methods.

<tt class="descname">__init__</tt><big>(</big>address[, size=-1[, writeable=True]]<big>)</big>¶

<col class="field-name" />
<col class="field-body" />

Parameters:

address – the address, either another <tt class="xref py py-class docutils literal">sip.voidptr</tt>, <tt class="docutils literal">None</tt>, a
Python Capsule, a Python CObject, an object that implements the
buffer protocol or an integer.

size – the optional associated size of the block of memory and is negative
if the size is not known.

writeable – set if the memory is writeable.  If it is not specified, and
address is a <tt class="xref py py-class docutils literal">sip.voidptr</tt> instance then its value will be
used.

<tt class="descname">__int__</tt><big>(</big><big>)</big> → integer¶

This returns the address as an integer.

<col class="field-name" />
<col class="field-body" />

Returns:	the integer address.

<tt class="descname">__getitem__</tt><big>(</big>idx<big>)</big> → item¶

New in version 4.12.

This returns the item at a given index.  An exception will be raised if
the address does not have an associated size.  In this way it behaves
like a Python <tt class="docutils literal">memoryview</tt> object.

<col class="field-name" />
<col class="field-body" />

Parameters:	idx – is the index which may either be an integer, an object that
implements <tt class="docutils literal">__index__()</tt> or a slice object.

Returns:	the item.  If the index is an integer then the item will be a
Python v2 string object or a Python v3 bytes object containing the
single byte at that index.  If the index is a slice object then the
item will be a new <tt class="xref py py-class docutils literal">voidptr</tt> object defining the subset of
the memory corresponding to the slice.

<tt class="descname">__hex__</tt><big>(</big><big>)</big> → string¶

This returns the address as a hexadecimal string.

<col class="field-name" />
<col class="field-body" />

Returns:	the hexadecimal string address.

<tt class="descname">__len__</tt><big>(</big><big>)</big> → integer¶

New in version 4.12.

This returns the size associated with the address.

<col class="field-name" />
<col class="field-body" />

Returns:	the associated size.  An exception will be raised if there is none.

<tt class="descname">__setitem__</tt><big>(</big>idx, item<big>)</big>¶

New in version 4.12.

This updates the memory at a given index.  An exception will be raised
if the address does not have an associated size or is not writable.  In
this way it behaves like a Python <tt class="docutils literal">memoryview</tt> object.

<col class="field-name" />
<col class="field-body" />

Parameters:

idx – is the index which may either be an integer, an object that
implements <tt class="docutils literal">__index__()</tt> or a slice object.

item – is the data that will update the memory defined by the index.  It
must implement the buffer interface and be the same size as the
data that is being updated.

<tt class="descname">ascapsule</tt><big>(</big><big>)</big> → capsule¶

New in version 4.10.

This returns the address as an unnamed Python Capsule.  This requires
Python v3.1 or later or Python v2.7 or later.

<col class="field-name" />
<col class="field-body" />

Returns:	the Capsule.

<tt class="descname">ascobject</tt><big>(</big><big>)</big> → cObject¶

This returns the address as a Python CObject.  This is deprecated with
Python v3.1 and is not supported with Python v3.2 and later.

<col class="field-name" />
<col class="field-body" />

Returns:	the CObject.

<tt class="descname">asstring</tt><big>(</big>[size=-1]<big>)</big> → string/bytes¶

This returns a copy of the block of memory as a Python v2 string object
or a Python v3 bytes object.

<col class="field-name" />
<col class="field-body" />

Parameters:	size – the number of bytes to copy.  If it is negative then the size
associated with the address is used.  If there is no associated
size then an exception is raised.

Returns:	the string or bytes object.

<tt class="descname">getsize</tt><big>(</big><big>)</big> → integer¶

This returns the size associated with the address.

<col class="field-name" />
<col class="field-body" />

Returns:	the associated size which will be negative if there is none.

<tt class="descname">setsize</tt><big>(</big>size<big>)</big>¶

This sets the size associated with the address.

<col class="field-name" />
<col class="field-body" />

Parameters:	size – the size to associate.  If it is negative then no size is
associated.

<tt class="descname">getwriteable</tt><big>(</big><big>)</big> → bool¶

This returns the writeable state of the memory.

<col class="field-name" />
<col class="field-body" />

Returns:	<tt class="docutils literal">True</tt> if the memory is writeable.

<tt class="descname">setwriteable</tt><big>(</big>writeable<big>)</big>¶

This sets the writeable state of the memory.

<col class="field-name" />
<col class="field-body" />

Parameters:	writeable – the writeable state to set.

<tt class="descclassname">sip.</tt><tt class="descname">wrapinstance</tt><big>(</big>addr, type<big>)</big> → object¶

This wraps a C structure or C++ class instance in a Python object.  If the
instance has already been wrapped then a new reference to the existing
object is returned.

<col class="field-name" />
<col class="field-body" />

Parameters:

addr – the address of the instance as a number.

type – the Python type of the instance.

Returns:
the Python object that wraps the instance.

class <tt class="descclassname">sip.</tt><tt class="descname">wrapper</tt>¶

This is the type object of the default base type of all instances wrapped
by SIP.  The <tt class="xref std std-canno docutils literal">Supertype</tt> class annotation can be used to specify a
different base type for a class.

class <tt class="descclassname">sip.</tt><tt class="descname">wrappertype</tt>¶

This is the type object of the metatype of the <tt class="xref py py-class docutils literal">sip.wrapper</tt> type.

Previous topic

Using the C API when Embedding

Next topic

The Build System

Quick search

    <form class="search" action="search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>

    Enter search terms or a module, class or function name.

Navigation

          index

          modules |

          next |

          previous |

SIP 4.16.3 Reference Guide »

        © Copyright 2014 Riverbank Computing Limited.
      Created using Sphinx 1.2.2.

redpajama_set_name: RedPajamaGithub


text:
using Newtonsoft.Json;

namespace xAPI.Client.Resources
{
    /// <summary>
    /// An optional property that represents the outcome of a graded
    /// Activity achieved by an Agent.
    /// </summary>
    public class Score
    {
        /// <summary>
        /// The score related to the experience as modified by scaling
        /// and/or normalization.
        /// </summary>
        [JsonProperty("scaled")]
        public decimal? Scaled { get; set; }

        /// <summary>
        /// The score achieved by the Actor in the experience described
        /// by the Statement. This is not modified by any scaling or
        /// normalization.
        /// </summary>
        [JsonProperty("raw")]
        public decimal? Raw { get; set; }

        /// <summary>
        /// The lowest possible score for the experience described by
        /// the Statement.
        /// </summary>
        [JsonProperty("min")]
        public decimal? Min { get; set; }

        /// <summary>
        /// The highest possible score for the experience described by
        /// the Statement.
        /// </summary>
        [JsonProperty("max")]
        public decimal? Max { get; set; }
    }
}


text:
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
import { SettingsComponent } from './settings.component';
import { StoreModule } from '@ngrx/store';
import { settingsFeatureKey, SettingsReducer } from '../reducers/settings.reducer';
import { SliderCheckboxComponent } from '../../../core/forms/slider-checkbox/slider-checkbox.component';
import { TranslateModule } from '@ngx-translate/core';

describe('SettingsComponent', () => {
	let component: SettingsComponent;
	let fixture: ComponentFixture<SettingsComponent>;

	beforeEach(waitForAsync(() => {
		TestBed.configureTestingModule({
			imports: [
				StoreModule.forRoot({
					[settingsFeatureKey]: SettingsReducer
				}),
				TranslateModule.forRoot()
			],
			declarations: [SettingsComponent, SliderCheckboxComponent]
		})
			.compileComponents();
	}));

	beforeEach(() => {
		fixture = TestBed.createComponent(SettingsComponent);
		component = fixture.componentInstance;
		fixture.detectChanges();
	});

	it('should create', () => {
		expect(component).toBeTruthy();
	});
});


text:
//---------------------------------------------------------------------------

#ifndef MainFormH
#define MainFormH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include "JvColorCombo.hpp"
#include "JvCombobox.hpp"
#include "JvExStdCtrls.hpp"
#include "JvCharMap.hpp"
#include <ComCtrls.hpp>
#include <Dialogs.hpp>
#include <ExtCtrls.hpp>
#include <Menus.hpp>
//---------------------------------------------------------------------------
class TfrmMain : public TForm
{
__published:	// IDE-managed Components
  TPopupMenu *PopupMenu1;
  TMenuItem *Copy1;
  TPanel *Panel1;
  TLabel *Label1;
  TLabel *Label2;
  TLabel *Label3;
  TLabel *lblFilter;
  TLabel *Label4;
  TButton *btnFont;
  TCheckBox *chkZoomPanel;
  TEdit *edStart;
  TUpDown *udStart;
  TEdit *edEnd;
  TUpDown *udEnd;
  TEdit *edCols;
  TUpDown *udColumns;
  TJvColorComboBox *cbColor;
  TJvFontComboBox *cbFont;
  TCheckBox *chkUnicode;
  TRichEdit *reInfo;
  TButton *btnSelect;
  TComboBox *cbFilter;
  TComboBox *cbLocales;
  TCheckBox *chkShadow;
  TCheckBox *chkDisplayAll;
  TLabel *lblChars;
  TFontDialog *FontDialog1;
  void __fastcall FormCreate(TObject *Sender);
  void __fastcall btnFontClick(TObject *Sender);
  void __fastcall chkZoomPanelClick(TObject *Sender);
  void __fastcall chkUnicodeClick(TObject *Sender);
  void __fastcall Copy1Click(TObject *Sender);
  void __fastcall btnSelectClick(TObject *Sender);
  void __fastcall cbFilterClick(TObject *Sender);
  void __fastcall cbLocalesClick(TObject *Sender);
  void __fastcall chkShadowClick(TObject *Sender);
  void __fastcall chkDisplayAllClick(TObject *Sender);
  void __fastcall edColsChange(TObject *Sender);
  void __fastcall edEndChange(TObject *Sender);
  void __fastcall edStartChange(TObject *Sender);
private:	// User declarations
  TEdit *edCharacter;

  void __fastcall cbColorChange(TObject *Sender);
  void __fastcall cbFontChange(TObject *Sender);

  void __fastcall FillFilter();
  void __fastcall FillLocales();
  void __fastcall DoJMSelectChar(TObject *Sender, WideChar AChar);
  void __fastcall DoJMResize(TObject *Sender);
  void __fastcall DoJMValidateChar(TObject *Sender, WideChar AChar, bool &Valid);
  void __fastcall DisplayInfo(WideChar AChar);
public:		// User declarations
  __fastcall TfrmMain(TComponent* Owner);
  TJvCharMap *JM;
  bool Changing;
};
//---------------------------------------------------------------------------
extern PACKAGE TfrmMain *frmMain;
//---------------------------------------------------------------------------
#endif


text:
{-# LANGUAGE BangPatterns #-}
module Lookup(lookupTiles,withTemplate,withTemplates,Score,Val,Count,Hist(..),HandTiles(..)) where

import qualified Data.IntMap as M
import Data.Array.IArray((!))
--import Data.List(group,sort)
import Data.Monoid(Monoid(..))
--import Data.Ix(inRange)
import Data.Char (toLower)
import Data.List(group)

import Dict(Dict(Dict))
import Tiles(valArray)
import Template

-- imports for testing
import Dict(buildInts,Dictionary(..))
import Test.HUnit
import Test.QuickCheck
import Test.QuickCheck.Batch
import Data.List(nub,sort,nubBy,unfoldr)
import Data.Maybe(listToMaybe)
import Control.Monad(replicateM,foldM,liftM2)
--
import Common
import Debug.Trace

type IsWild = Bool
type Val = Int
type Count = Int
type Score = Int
newtype Hist = Hist {unHist :: [(Val,Count)]} deriving Show

data HandTiles = HandTiles Count Hist Count -- # wilds, Hist of A..Z, # of tiles including wilds

data SM = SM !Int !Int -- First is running total of tiles, the second is multipler

hist :: [Val] -> Hist
hist = Hist . map hist' . group . sort
  where hist' xs = (head xs, length xs)

fromHist :: Hist -> [Val]
fromHist = concatMap (uncurry (flip replicate)) . unHist

pullTile :: Hist -> [(Val,Hist)]
pullTile = helper id . unHist
  where helper _ [] = []
        helper f (pair@(x,1):xs) = (x,Hist $ f             xs)  : helper (f . (pair:)) xs
        helper f (pair@(x,n):xs) = (x,Hist $ f ((x,pred n):xs)) : helper (f . (pair:)) xs

addBonus :: (Score,String) -> (Score,String)
addBonus (score,str) = (score+50,str)

lookupTiles :: Dict -> HandTiles -> [(Score,String)]
lookupTiles !t (HandTiles wilds s n) =
  let scoreBonus | n/=7 = id
                 | otherwise = map scoreBonus'
      scoreBonus' x@(_,str) | length str == 7 = addBonus x
                            | otherwise = x
  in scoreBonus $ lookupAll t 0 wilds s

withTemplate :: Dict -> HandTiles -> String  -> [(Score,String)]
withTemplate t (HandTiles wilds s n) sTemplate =
  let template = parseTemplate sTemplate
      m = length (filter isBlank template)
      --msg = show(wilds,s,template,(m,n,m<=n))
      scoreBonus | m == 7 = map addBonus
                 | otherwise = id
  in if m <= n then scoreBonus . mapFst total . lookupTemplate t mempty wilds s $ template
       else []

withTemplates :: Dict -> HandTiles -> String  -> [(Score,String)]
withTemplates t (HandTiles wilds s n) sTemplate =
  let templates = allTemplates (parseTemplate sTemplate)
      templateLens = map (\template -> (template,length (filter isBlank template))) templates
      validTemplates = filter (\(_,m)-> 1<=m && m<=n) templateLens
      oneTemplateLen (template,m) | m==7 = map addBonus . oneTemplate $ template
                                  | otherwise = oneTemplate template
      oneTemplate template = mapFst total . lookupTemplate t mempty wilds s $ template
  in concatMap oneTemplateLen $ validTemplates

lookupAll :: Dict -> Score -> Count -> Hist -> [(Score,String)]
lookupAll !(Dict here m) !score !wilds !s =
    if here then (score,[]) : rest else rest
  where rest :: [(Score,String)]
        rest = concatMap choose (pullTile s) ++ wild
        choose :: (Val,Hist) -> [(Score,String)]
        choose (y,s') = case M.lookup y m of
                          Nothing -> []
                          Just t -> let c = toEnum y
                                        score' = score + valArray!y
                                    in mapSnd (c:) (lookupAll t score' wilds s')
        wild :: [(Score,String)]
        wild = if wilds>0
                 then let wilds' = pred wilds
                          f (y,t) = let c = toLower (toEnum y)
                                    in mapSnd (c:) (lookupAll t score wilds' s)
                      in concatMap f (M.assocs m)
                 else []

mapFst :: (a1 -> a2) -> [(a1,b)] -> [(a2,b)]
mapFst f = go where
  go [] = []
  go ((a,b):rest) = (f a,b) : go rest

mapSnd :: (b1 -> b2) -> [(a,b1)] -> [(a,b2)]
mapSnd f = go where
  go [] = []
  go ((a,b):rest) = (a,f b) : go rest

scoreOf :: Square -> IsWild -> Val -> SM
scoreOf square isWild val =
  case square of
    Normal       -> SM base 1
    DoubleLetter -> SM (2*base) 1
    TripleLetter -> SM (3*base) 1
    DoubleWord   -> SM base 2
    TripleWord   -> SM base 3
 where base | isWild = 0
            | otherwise = valArray!val

instance Monoid SM where
  mempty = SM 0 1
  mappend (SM score1 mult1) (SM score2 mult2) = SM (score1+score2) (mult1 * mult2)

addTile :: SM -> Square -> IsWild -> Val -> SM
addTile sm s i v = mappend sm (scoreOf s i v)

total :: SM -> Score
total (SM t m) = m*t

lookupTemplate :: Dict -> SM -> Count -> Hist  -> Template -> [(SM,String)]
lookupTemplate !(Dict here _m) !score !_wilds !_s [] =
  if here then [(score,[])] else []

lookupTemplate !(Dict _here m) !score !wilds !s (Filled isWild val : template') =
   case M.lookup val m of
     Nothing -> []
     Just t -> let c = (if isWild then toLower else id) (toEnum val)
                   score' = addTile score Normal isWild val
               in mapSnd (c:) (lookupTemplate t score' wilds s template')

lookupTemplate !(Dict _here m) !score !wilds !s (Blank square:template') =
  concatMap choose (pullTile s) ++ useWild
 where
  choose :: (Val,Hist) -> [(SM,String)]
  choose (val,s') =
    case M.lookup val m of
      Nothing -> []
      Just t -> let c = toEnum val
                    score' = addTile score square False val
                in mapSnd (c:) (lookupTemplate t score' wilds s' template')
  useWild ::[(SM,String)]
  useWild | 0 == wilds = []
          | otherwise =
    let wilds' = pred wilds
        useAs (val,t) = let c = toLower (toEnum val)
                            score' = addTile score square True val
                        in mapSnd (c:) (lookupTemplate t score' wilds' s template')
    in concatMap useAs (M.assocs m)

-- testing

instance Arbitrary Hist where
  arbitrary = do
    n <- choose (1,7)
    vals <- replicateM n (choose (fromEnum 'A',fromEnum 'E'))
    return (hist vals)

test_hist_fromHist :: [Val] -> Bool
test_hist_fromHist x = sort x == (fromHist (hist x))

test_pullTile :: Hist -> Bool
test_pullTile h@(Hist x) = let each = map fst x == nub (sort (map fst (pullTile h)))
                               y0 = (fromHist h)
                               y1 = (sort $ unfoldr (listToMaybe . pullTile) h)
                               y2 = (sort $ unfoldr (listToMaybe . reverse . pullTile) h)
                           in each && (y0==y1) && (y0==y2)

instance Arbitrary HandTiles where
  arbitrary = do
    w <- choose (0,2)
    n <- choose (0,7-w)
    vals <- replicateM n (choose (1,26))
    return (HandTiles w (hist vals) (w+n))

test_lookup :: Dictionary -> Bool
test_lookup (Dictionary ds) =
  let dict = buildInts ds
      isOk :: [Int] -> Bool
      isOk h = (score==score')&&(sort str==str')
        where score' = (sum $ map (valArray!) h) + (if length h == 7 then 50 else 0)
              str' = sort $ map toEnum h
              (score,str) = last . sort $ lookupTiles dict $ HandTiles 0 (hist h) (length h)
  in all isOk ds


text:
#import <UIKit/UIKit.h>
#import "MPUAbstractMainController.h"
#import "MPUMposUi.h"

@interface MPUTransactionMainController : MPUAbstractMainController

@property (nonatomic, strong) MPTransactionParameters *parameters;
@property (nonatomic, strong) MPTransactionProcessParameters *processParameters;
@property (nonatomic, copy) NSString *sessionIdentifier;
@property (nonatomic, copy) MPUTransactionCompleted completed;

@end


text:
$(function () {
    var app = diagnostics.app;

    var Provider = diagnostics.module("provider");
    var Method = diagnostics.module("method");
    var Results = diagnostics.module("results");

    var Router = Backbone.Router.extend({
        routes: {
            "": "index",
            ":providerName": "methods"
        },

        index: function () {
            var providers = new Provider.Collection();
            providers.fetch().success(function () {
                var list = new Provider.Views.List({ model: providers });
                list.render();
            });
        },

        methods: function (providerName) {
            var methods = new Method.Collection({ providerName: providerName });
            methods.fetch().success(function () {
                var list = new Method.Views.List({ model: methods });
                list.render();
            });
        }
    });

    // Start router and trigger first route
    app.router = new Router();
    Backbone.history.start();
});


text:
<resources>
    <string name="app_name">MateusAugusto</string>
</resources>


text:
import logging

from scrapy.spider import BaseSpider
from scrapy.selector import HtmlXPathSelector
from scrapy.http import Request, HtmlResponse
from scrapy.utils.response import get_base_url
from scrapy.utils.url import urljoin_rfc

from scrapy.xlib.pydispatch import dispatcher
from scrapy import signals

from product_spiders.items import Product, ProductLoader

class caldaiemurali_spider(BaseSpider):
    name = 'caldaiemurali-it.ebay'
    allowed_domains = ['www.ebay.it', 'stores.ebay.it', 'ebay.it', 'ebay.com']
    start_urls = ('http://stores.ebay.it/caldaiemurali-it',)

    scraped_urls = []
    items_count = 0
    items_scraped = 0
    pages_count = 0
    tries = 0

    def __init__(self, *a, **kw):
        super(caldaiemurali_spider, self).__init__(*a, **kw)
        dispatcher.connect(self.spider_idle, signals.spider_idle)
        dispatcher.connect(self.item_scraped, signals.item_scraped)

    def spider_idle(self, spider):
        logging.error("Total count: %d" % self.items_count)
        logging.error("Items scraped: %d" % self.items_scraped)
        if (self.items_count > self.items_scraped) and (self.tries < 5):
            logging.error("Not all scraped: found %d of %d" % (self.items_scraped, self.items_count))
            request = Request(self.start_urls[0], dont_filter=True)
            self._self._crawler.engine.crawl(request, self)
        else:
            logging.error("Scraped %d of %d. The rest are duplicates" % (self.items_scraped, self.items_count))
            logging.error("Finished on %d try" % self.tries)

    def item_scraped(self, item, response, spider):
        if spider == self:
            self.items_scraped += 1

    def parse_product(self, response):
        hxs = HtmlXPathSelector(response)

        products = hxs.select('//td[@id="CentralArea"]//td[@class="details"]')
        logging.error("Debug. %s. Found %d products" % (response.url, len(products)))
        for p in products:
            try:
                name = p.select('./div[1]/a/@title')[0].extract()
            except IndexError:
                continue

            try:
                url = p.select('./div[1]/a/@href')[0].extract()
            except IndexError:
                continue

            price = p.select('./div[3]/table/tr[1]/td[2]/span/text()').re(r'([0-9\.\, ]+)')
            if not price:
                price = p.select('./div[2]/table/tr[1]/td[2]/span/text()').re(r'([0-9\.\, ]+)')

            self.scraped_urls.append(url)

            product_loader = ProductLoader(item=Product(), response=response)
            product_loader.add_value('name', name.strip())
            product_loader.add_value('price', price[0].replace(".", "").replace(",", "."))
            product_loader.add_value('url', url)
            yield product_loader.load_item()

    def parse(self, response):
        self.tries += 1
        logging.error("Try %d" % self.tries)

        if not isinstance(response, HtmlResponse):
            return
        hxs = HtmlXPathSelector(response)
        base_url = get_base_url(response)

        items_count = hxs.select("//span[@class='smuy']/span[@class='countClass']/text()").extract()
        if not items_count:
            logging.error("Items count not found!")
            return
        self.items_count = int(items_count[0].replace(".", ""))

        self.pages_count = self.items_count / 30 + 1

        #pages
        for i in range(1, self.pages_count + 1):
            url = "http://stores.ebay.it/caldaiemurali-it/_i.html?_pgn=" + str(i)
            yield Request(url, dont_filter=True, callback=self.parse_product)


text:
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Reflection.Metadata;
using System.Reflection.PortableExecutable;
using System.Threading;
using System.Diagnostics;
using Roslyn.Utilities;
using Microsoft.CodeAnalysis.Symbols;

namespace Microsoft.CodeAnalysis
{
    /// <summary>
    /// Represents an immutable snapshot of assembly CLI metadata.
    /// </summary>
    public sealed class AssemblyMetadata : Metadata
    {
        private sealed class Data
        {
            public static readonly Data Disposed = new Data();

            public readonly ImmutableArray<ModuleMetadata> Modules;
            public readonly PEAssembly? Assembly;

            private Data()
            {
            }

            public Data(ImmutableArray<ModuleMetadata> modules, PEAssembly assembly)
            {
                Debug.Assert(!modules.IsDefaultOrEmpty && assembly != null);

                this.Modules = modules;
                this.Assembly = assembly;
            }

            public bool IsDisposed
            {
                get { return Assembly == null; }
            }
        }

        /// <summary>
        /// Factory that provides the <see cref="ModuleMetadata"/> for additional modules (other than <see cref="_initialModules"/>) of the assembly.
        /// Shall only throw <see cref="BadImageFormatException"/> or <see cref="IOException"/>.
        /// Null of all modules were specified at construction time.
        /// </summary>
        private readonly Func<string, ModuleMetadata>? _moduleFactoryOpt;

        /// <summary>
        /// Modules the <see cref="AssemblyMetadata"/> was created with, in case they are eagerly allocated.
        /// </summary>
        private readonly ImmutableArray<ModuleMetadata> _initialModules;

        // Encapsulates the modules and the corresponding PEAssembly produced by the modules factory.
        private Data? _lazyData;

        // The actual array of modules exposed via Modules property.
        // The same modules as the ones produced by the factory or their copies.
        private ImmutableArray<ModuleMetadata> _lazyPublishedModules;

        /// <summary>
        /// Cached assembly symbols.
        /// </summary>
        /// <remarks>
        /// Guarded by <see cref="CommonReferenceManager.SymbolCacheAndReferenceManagerStateGuard"/>.
        /// </remarks>
        internal readonly WeakList<IAssemblySymbolInternal> CachedSymbols = new WeakList<IAssemblySymbolInternal>();

        // creates a copy
        private AssemblyMetadata(AssemblyMetadata other, bool shareCachedSymbols)
            : base(isImageOwner: false, id: other.Id)
        {
            if (shareCachedSymbols)
            {
                this.CachedSymbols = other.CachedSymbols;
            }

            _lazyData = other._lazyData;
            _moduleFactoryOpt = other._moduleFactoryOpt;
            _initialModules = other._initialModules;
            // Leave lazyPublishedModules unset. Published modules will be set and copied as needed.
        }

        internal AssemblyMetadata(ImmutableArray<ModuleMetadata> modules)
            : base(isImageOwner: true, id: MetadataId.CreateNewId())
        {
            Debug.Assert(!modules.IsDefaultOrEmpty);
            _initialModules = modules;
        }

        internal AssemblyMetadata(ModuleMetadata manifestModule, Func<string, ModuleMetadata> moduleFactory)
            : base(isImageOwner: true, id: MetadataId.CreateNewId())
        {
            Debug.Assert(manifestModule != null);
            Debug.Assert(moduleFactory != null);

            _initialModules = ImmutableArray.Create(manifestModule);
            _moduleFactoryOpt = moduleFactory;
        }

        /// <summary>
        /// Creates a single-module assembly.
        /// </summary>
        /// <param name="peImage">
        /// Manifest module image.
        /// </param>
        /// <exception cref="ArgumentNullException"><paramref name="peImage"/> is null.</exception>
        public static AssemblyMetadata CreateFromImage(ImmutableArray<byte> peImage)
        {
            return Create(ModuleMetadata.CreateFromImage(peImage));
        }

        /// <summary>
        /// Creates a single-module assembly.
        /// </summary>
        /// <param name="peImage">
        /// Manifest module image.
        /// </param>
        /// <exception cref="ArgumentNullException"><paramref name="peImage"/> is null.</exception>
        /// <exception cref="BadImageFormatException">The PE image format is invalid.</exception>
        public static AssemblyMetadata CreateFromImage(IEnumerable<byte> peImage)
        {
            return Create(ModuleMetadata.CreateFromImage(peImage));
        }

        /// <summary>
        /// Creates a single-module assembly.
        /// </summary>
        /// <param name="peStream">Manifest module PE image stream.</param>
        /// <param name="leaveOpen">False to close the stream upon disposal of the metadata.</param>
        /// <exception cref="BadImageFormatException">The PE image format is invalid.</exception>
        public static AssemblyMetadata CreateFromStream(Stream peStream, bool leaveOpen = false)
        {
            return Create(ModuleMetadata.CreateFromStream(peStream, leaveOpen));
        }

        /// <summary>
        /// Creates a single-module assembly.
        /// </summary>
        /// <param name="peStream">Manifest module PE image stream.</param>
        /// <param name="options">False to close the stream upon disposal of the metadata.</param>
        /// <exception cref="BadImageFormatException">The PE image format is invalid.</exception>
        public static AssemblyMetadata CreateFromStream(Stream peStream, PEStreamOptions options)
        {
            return Create(ModuleMetadata.CreateFromStream(peStream, options));
        }

        /// <summary>
        /// Finds all modules of an assembly on a specified path and builds an instance of <see cref="AssemblyMetadata"/> that represents them.
        /// </summary>
        /// <param name="path">The full path to the assembly on disk.</param>
        /// <exception cref="ArgumentNullException"><paramref name="path"/> is null.</exception>
        /// <exception cref="ArgumentException"><paramref name="path"/> is invalid.</exception>
        /// <exception cref="IOException">Error reading file <paramref name="path"/>. See <see cref="Exception.InnerException"/> for details.</exception>
        /// <exception cref="NotSupportedException">Reading from a file path is not supported by the platform.</exception>
        public static AssemblyMetadata CreateFromFile(string path)
        {
            return CreateFromFile(ModuleMetadata.CreateFromFile(path), path);
        }

        internal static AssemblyMetadata CreateFromFile(ModuleMetadata manifestModule, string path)
        {
            return new AssemblyMetadata(manifestModule, moduleName => ModuleMetadata.CreateFromFile(Path.Combine(Path.GetDirectoryName(path) ?? "", moduleName)));
        }

        /// <summary>
        /// Creates a single-module assembly.
        /// </summary>
        /// <param name="module">
        /// Manifest module.
        /// </param>
        /// <remarks>This object disposes <paramref name="module"/> it when it is itself disposed.</remarks>
        public static AssemblyMetadata Create(ModuleMetadata module)
        {
            if (module == null)
            {
                throw new ArgumentNullException(nameof(module));
            }

            return new AssemblyMetadata(ImmutableArray.Create(module));
        }

        /// <summary>
        /// Creates a multi-module assembly.
        /// </summary>
        /// <param name="modules">
        /// Modules comprising the assembly. The first module is the manifest module of the assembly.</param>
        /// <remarks>This object disposes the elements of <paramref name="modules"/> it when it is itself <see cref="Dispose"/>.</remarks>
        /// <exception cref="ArgumentException"><paramref name="modules"/> is default value.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="modules"/> contains null elements.</exception>
        /// <exception cref="ArgumentException"><paramref name="modules"/> is empty or contains a module that doesn't own its image (was created via <see cref="Metadata.Copy"/>).</exception>
        public static AssemblyMetadata Create(ImmutableArray<ModuleMetadata> modules)
        {
            if (modules.IsDefaultOrEmpty)
            {
                throw new ArgumentException(CodeAnalysisResources.AssemblyMustHaveAtLeastOneModule, nameof(modules));
            }

            for (int i = 0; i < modules.Length; i++)
            {
                if (modules[i] == null)
                {
                    throw new ArgumentNullException(nameof(modules) + "[" + i + "]");
                }

                if (!modules[i].IsImageOwner)
                {
                    throw new ArgumentException(CodeAnalysisResources.ModuleCopyCannotBeUsedToCreateAssemblyMetadata, nameof(modules) + "[" + i + "]");
                }
            }

            return new AssemblyMetadata(modules);
        }

        /// <summary>
        /// Creates a multi-module assembly.
        /// </summary>
        /// <param name="modules">
        /// Modules comprising the assembly. The first module is the manifest module of the assembly.</param>
        /// <remarks>This object disposes the elements of <paramref name="modules"/> it when it is itself <see cref="Dispose"/>.</remarks>
        /// <exception cref="ArgumentException"><paramref name="modules"/> is default value.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="modules"/> contains null elements.</exception>
        /// <exception cref="ArgumentException"><paramref name="modules"/> is empty or contains a module that doesn't own its image (was created via <see cref="Metadata.Copy"/>).</exception>
        public static AssemblyMetadata Create(IEnumerable<ModuleMetadata> modules)
        {
            return Create(modules.AsImmutableOrNull());
        }

        /// <summary>
        /// Creates a multi-module assembly.
        /// </summary>
        /// <param name="modules">Modules comprising the assembly. The first module is the manifest module of the assembly.</param>
        /// <remarks>This object disposes the elements of <paramref name="modules"/> it when it is itself <see cref="Dispose"/>.</remarks>
        /// <exception cref="ArgumentException"><paramref name="modules"/> is default value.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="modules"/> contains null elements.</exception>
        /// <exception cref="ArgumentException"><paramref name="modules"/> is empty or contains a module that doesn't own its image (was created via <see cref="Metadata.Copy"/>).</exception>
        public static AssemblyMetadata Create(params ModuleMetadata[] modules)
        {
            return Create(ImmutableArray.CreateRange(modules));
        }

        /// <summary>
        /// Creates a shallow copy of contained modules and wraps them into a new instance of <see cref="AssemblyMetadata"/>.
        /// </summary>
        /// <remarks>
        /// The resulting copy shares the metadata images and metadata information read from them with the original.
        /// It doesn't own the underlying metadata images and is not responsible for its disposal.
        ///
        /// This is used, for example, when a metadata cache needs to return the cached metadata to its users
        /// while keeping the ownership of the cached metadata object.
        /// </remarks>
        internal new AssemblyMetadata Copy()
        {
            return new AssemblyMetadata(this, shareCachedSymbols: true);
        }

        internal AssemblyMetadata CopyWithoutSharingCachedSymbols()
        {
            return new AssemblyMetadata(this, shareCachedSymbols: false);
        }

        protected override Metadata CommonCopy()
        {
            return Copy();
        }

        /// <summary>
        /// Modules comprising this assembly. The first module is the manifest module.
        /// </summary>
        /// <exception cref="BadImageFormatException">The PE image format is invalid.</exception>
        /// <exception cref="IOException">IO error reading the metadata. See <see cref="Exception.InnerException"/> for details.</exception>
        /// <exception cref="ObjectDisposedException">The object has been disposed.</exception>
        public ImmutableArray<ModuleMetadata> GetModules()
        {
            if (_lazyPublishedModules.IsDefault)
            {
                var data = GetOrCreateData();
                var newModules = data.Modules;

                if (!IsImageOwner)
                {
                    newModules = newModules.SelectAsArray(module => module.Copy());
                }

                ImmutableInterlocked.InterlockedInitialize(ref _lazyPublishedModules, newModules);
            }

            if (_lazyData == Data.Disposed)
            {
                throw new ObjectDisposedException(nameof(AssemblyMetadata));
            }

            return _lazyPublishedModules;
        }

        /// <exception cref="BadImageFormatException">The PE image format is invalid.</exception>
        /// <exception cref="IOException">IO error while reading the metadata. See <see cref="Exception.InnerException"/> for details.</exception>
        /// <exception cref="ObjectDisposedException">The object has been disposed.</exception>
        internal PEAssembly? GetAssembly()
        {
            return GetOrCreateData().Assembly;
        }

        /// <exception cref="BadImageFormatException">The PE image format is invalid.</exception>
        /// <exception cref="IOException">IO error while reading the metadata. See <see cref="Exception.InnerException"/> for details.</exception>
        /// <exception cref="ObjectDisposedException">The object has been disposed.</exception>
        private Data GetOrCreateData()
        {
            if (_lazyData == null)
            {
                ImmutableArray<ModuleMetadata> modules = _initialModules;
                ImmutableArray<ModuleMetadata>.Builder? moduleBuilder = null;

                bool createdModulesUsed = false;
                try
                {
                    if (_moduleFactoryOpt != null)
                    {
                        Debug.Assert(_initialModules.Length == 1);

                        var additionalModuleNames = _initialModules[0].GetModuleNames();
                        if (additionalModuleNames.Length > 0)
                        {
                            moduleBuilder = ImmutableArray.CreateBuilder<ModuleMetadata>(1 + additionalModuleNames.Length);
                            moduleBuilder.Add(_initialModules[0]);

                            foreach (string moduleName in additionalModuleNames)
                            {
                                moduleBuilder.Add(_moduleFactoryOpt(moduleName));
                            }

                            modules = moduleBuilder.ToImmutable();
                        }
                    }

                    var assembly = new PEAssembly(this, modules.SelectAsArray(m => m.Module));
                    var newData = new Data(modules, assembly);

                    createdModulesUsed = Interlocked.CompareExchange(ref _lazyData, newData, null) == null;
                }
                finally
                {
                    if (moduleBuilder != null && !createdModulesUsed)
                    {
                        // dispose unused modules created above:
                        for (int i = _initialModules.Length; i < moduleBuilder.Count; i++)
                        {
                            moduleBuilder[i].Dispose();
                        }
                    }
                }
            }

            if (_lazyData.IsDisposed)
            {
                throw new ObjectDisposedException(nameof(AssemblyMetadata));
            }

            return _lazyData;
        }

        /// <summary>
        /// Disposes all modules contained in the assembly.
        /// </summary>
        public override void Dispose()
        {
            var previousData = Interlocked.Exchange(ref _lazyData, Data.Disposed);

            if (previousData == Data.Disposed || !this.IsImageOwner)
            {
                // already disposed, or not an owner
                return;
            }

            // AssemblyMetadata assumes their ownership of all modules passed to the constructor.
            foreach (var module in _initialModules)
            {
                module.Dispose();
            }

            if (previousData == null)
            {
                // no additional modules were created yet => nothing to dispose
                return;
            }

            Debug.Assert(_initialModules.Length == 1 || _initialModules.Length == previousData.Modules.Length);

            // dispose additional modules created lazily:
            for (int i = _initialModules.Length; i < previousData.Modules.Length; i++)
            {
                previousData.Modules[i].Dispose();
            }
        }

        /// <summary>
        /// Checks if the first module has a single row in Assembly table and that all other modules have none.
        /// </summary>
        /// <exception cref="BadImageFormatException">The PE image format is invalid.</exception>
        /// <exception cref="IOException">IO error reading the metadata. See <see cref="Exception.InnerException"/> for details.</exception>
        /// <exception cref="ObjectDisposedException">The object has been disposed.</exception>
        internal bool IsValidAssembly()
        {
            var modules = GetModules();

            if (!modules[0].Module.IsManifestModule)
            {
                return false;
            }

            for (int i = 1; i < modules.Length; i++)
            {
                // Ignore winmd modules since runtime winmd modules may be loaded as non-primary modules.
                var module = modules[i].Module;
                if (!module.IsLinkedModule && module.MetadataReader.MetadataKind != MetadataKind.WindowsMetadata)
                {
                    return false;
                }
            }

            return true;
        }

        /// <summary>
        /// Returns the metadata kind. <seealso cref="MetadataImageKind"/>
        /// </summary>
        public override MetadataImageKind Kind
        {
            get { return MetadataImageKind.Assembly; }
        }

        /// <summary>
        /// Creates a reference to the assembly metadata.
        /// </summary>
        /// <param name="documentation">Provider of XML documentation comments for the metadata symbols contained in the module.</param>
        /// <param name="aliases">Aliases that can be used to refer to the assembly from source code (see "extern alias" directive in C#).</param>
        /// <param name="embedInteropTypes">True to embed interop types from the referenced assembly to the referencing compilation. Must be false for a module.</param>
        /// <param name="filePath">Path describing the location of the metadata, or null if the metadata have no location.</param>
        /// <param name="display">Display string used in error messages to identity the reference.</param>
        /// <returns>A reference to the assembly metadata.</returns>
        public PortableExecutableReference GetReference(
            DocumentationProvider? documentation = null,
            ImmutableArray<string> aliases = default,
            bool embedInteropTypes = false,
            string? filePath = null,
            string? display = null)
        {
            return new MetadataImageReference(this, new MetadataReferenceProperties(MetadataImageKind.Assembly, aliases, embedInteropTypes), documentation, filePath, display);
        }
    }
}


text:
namespace pythonic
{

  namespace operator_
  {
    FPROXY_DECL(pythonic::operator_, __inv__, invert);
  }
}

#endif


text:
"""
legit.core
~~~~~~~~~~

This module provides the basic functionality of legit.
"""

import bootstrap
del bootstrap

__version__ = '0.1.0'
__author__ = 'Kenneth Reitz'
__license__ = 'BSD'


text:
import { curry } from 'ramda';
import Promise from 'bluebird';

import { artists } from './data';
import { Artist } from '../api/models/Artist';

const createOrMerge = curry((schema, query, objectOrUpdates) => {
  return schema.findOneAndUpdate(
    query,
    objectOrUpdates,
    {upsert: true, runValidators: true},
  );
});

const createArtist = createOrMerge(Artist);

const createArtists = (artistList) => (
  Promise.map(
    artistList,
    (a) => createArtist({name: a.name}, a)
      .then((doc) => {
        console.log('Created or merged:');
        console.log(doc);
        return doc;
      })
      .catch((err) => {
        console.log(err);
      })
  )
);

createArtists(artists)
.then(process.exit.bind(this, 0), process.exit.bind(this, 1));


text:
export default (sequelize, DataTypes) => {
  const Membership = sequelize.define('Membership', {
    id: {
      type: DataTypes.UUID,
      defaultValue: DataTypes.UUIDV4,
      primaryKey: true
    },
    userRole: {
      type: DataTypes.STRING,
      allowNull: true,
      defaultValue: 'member'
    },
    groupId: {
      type: DataTypes.UUID,
      allowNull: false
    },
    memberId: {
      type: DataTypes.UUID,
      allowNull: false
    },
  });

  Membership.associate = (models) => {
    Membership.belongsTo(models.User, {
      foreignKey: 'memberId',
    });

    Membership.belongsTo(models.Group, {
      foreignKey: 'groupId',
    });
  };

  return Membership;
};