text:
/*eslint no-console: off */
/*
 * ector-command
 * https://github.com/parmentf/node-ector-command
 *
 * Copyright (c) 2013 François Parmentier
 * Licensed under the MIT license.
 */
"use strict";

var config = require('../package');
var debug = require('debug')('ector:command');
var sugar = require('sugar');
var program = require('commander');
var FileConceptNetwork = require('file-concept-network').FileConceptNetwork;
var Ector = require('ector');

program
  .version(config.version)
  .option('-p, --person [name]', 'set the name of the utterer', 'User')
  .option('-b, --botname [botname]', 'set the name of the bot', 'ECTOR')
  .option('-q, --quiet', 'shut up!')
  .option('-l, --log [filepath]', 'log the dialogue in log file', 'ector.log')
  .parse(process.argv);

var ector = new Ector(program.botname, program.person);

ector.injectConceptNetwork(FileConceptNetwork);

var help = function (command) {
  switch (command) {
  case "bye":
  case "quit":
    console.log("Quit the program.");
    break;
  case "usage":
    console.log("Print the options of ector command.");
    break;
  case "person":
    console.log("Change the utterer's name (like -p)");
    console.log("Without parameter, display his name.");
    break;
  case "botname":
    console.log("change the bot's name (like -b)");
    console.log("Without parameter, display the bot's name.");
    break;
  case "version":
    console.log("give the current version");
    break;
  case "shownodes":
    console.log("show the nodes of the Concept Network");
    break;
  case "showlinks":
    console.log("show the links of the Concept Network");
    break;
  case "showstate":
    console.log("show the state of the Concept Network");
    break;
  case "debug":
    console.log("show the debug state (on or off)");
    break;
  case "save":
    console.log("save Ector's Concept Network and state");
    break;
  default:
    console.log("You can just start typing phrases.\n" +
                "But there are some commands you can use:\n" +
                "- @usage     : print the options of the ector command\n" +
                "- @quit      : quit\n" +
                "- @exit      : quit\n" +
                "- @bye       : quit\n" +
                "- @person    : change the utterer name (like -p)\n" +
                "- @botname   : change the bot's name (like -b)\n" +
                "- @version   : give the current version\n" +
                "- @save      : save Ector's Concept Network\n" +
                "- @shownodes : show the nodes of the Concept Network\n" +
                "- @showlinks : show the links of the Concept Network\n" +
                "- @showstate : show the state of the nodes\n" +
                "- @load      : load Ector's Concept Network\n" +
                // "- @cleanstate: clean the state from the non-activated nodes\n" +
                // "- @log [file]: log the entries in the file (no file turns off the logging)\n" +
                // "- @status    : show the status of Ector (Concept Network, states)\n" +
                // "- @sentence [on|off]: set the sentence reply mode\n" +
                // "- @generate [on|off]: set the generate reply mode\n" +
                "- @debug [on|off]: show the debug state (on or off)"
    );
  }
};

var previousResponseNodes = null;
var loop;
var treatPhrase = function (phrase) {
  debug('phrase', '"' + phrase + '"');
  if (phrase === null || phrase === '@bye' || phrase === "@quit" || phrase === "@exit") {
    process.exit(0);
  }
  if (phrase.startsWith('@help')) {
    var command = phrase.split(' ')[1];
    help(command);
  }
  else if (phrase === '@usage') {
    program.outputHelp();
  }
  else if (phrase.startsWith('@person')) {
    var person = phrase.split(' ').slice(1).join(' ');
    debug('person', person);
    if (person.length > 0) {
      program.person = person;
      ector.setUser(person);
    }
    else {
      console.log('person: "' + ector.username + '"');
    }
  }
  else if (phrase.startsWith('@botname')) {
    var botname = phrase.split(' ').slice(1).join(' ');
    debug('botname', botname);
    if (botname.length > 0) {
      program.botname = botname;
      ector.setName(botname);
    }
    else {
      console.log('botname: "' + ector.name + '"');
    }
  }
  else if (phrase === '@version') {
    console.log('version: ' + program.version());
  }
  else if (phrase === '@shownodes') {
    console.log('nodes:', ector.cn.node);
  }
  else if (phrase === '@showlinks') {
    console.log('links:', ector.cn.link);
  }
  else if (phrase === '@showstate') {
    console.log('state:', ector.cns[ector.username].nodeState);
  }
  else if (phrase.startsWith('@debug')) {
    var toggle = phrase.split(' ').slice(1).join(' ');
    debug('debug toggle', toggle);
    switch (toggle) {
    case 'on':
      process.env.NODE_DEBUG = 'ector:command';
      break;
    case 'off':
      delete process.env.NODE_DEBUG;
      break;
    }
    console.log('DEBUG ' + (process.env.DEBUG ? 'ON' : 'OFF'));
  }
  else if (phrase === '@save') {
    ector.cn.save('cn.json', function (err) {
      if (err) {
        console.error('Error while saving to file cn.json!\n%s', err);
      }
    });
  }
  else if (phrase === '@load') {
    debug('@load');
    ector.cn.load('cn.json', function (err) {
      debug(' end of cn.load', err);
      if (err) {
        console.error('Error while loading from file cn.json!\n%s', err);
      }
    });
  }
  else if (phrase.startsWith('@')) {
    console.log('This command does not exist.');
  }
  // When it is not a command (beginning with @)
  else {
    var nodes = ector.addEntry(phrase);
    // link the nodes of the last response to the node of this entry
    ector.linkNodesToLastSentence(previousResponseNodes);
    var response = ector.generateResponse();
    console.log('%s: %s', program.botname, response.sentence);
    previousResponseNodes = response.nodes;
    debug('response', response);
  }
  loop();
};

loop = function () {
  program.prompt(program.person + ': ', treatPhrase);
};
loop();

process.on('exit', function () {
  console.log('Bye');
});


text:
class AddCorrectAnswerToQuestions < ActiveRecord::Migration
  def change
    add_column :questions, :correct_answer, :integer, :default => nil
  end
end

redpajama_set_name: RedPajamaGithub


text:
namespace HockeyApp.Common.Enums
{
    public enum TournamentCostType
    {
        PerPerson = 1,
        PerTeam = 2
    }
}


text:
=begin
#Meltwater Streaming API v2

#The Meltwater Streaming API provides the needed resources for Meltwater clients to create & delete REST Hooks and stream Meltwater search results to your specified destination.

OpenAPI spec version: 2.0.0
Contact: support@api.meltwater.com
Generated by: https://github.com/swagger-api/swagger-codegen.git

=end

require 'date'

module SwaggerClient
  # Create an OAuth2 access token based on the provided `client_id` and `client_secret`.  #### Appendix    The Base64-encoded `client_id`:`client_secret` string can be generated in a  terminal with following command:        $ echo -n \"your_client_id:your_client_secret\" | base64    <i>You will need `base64` installed.</i>
  class OAuth2Token
    # The oauth2 access token to use for subsequent API calls
    attr_accessor :access_token

    # Time until the token expires in seconds
    attr_accessor :expires_in

    # The type of oauth2 scope the token is valid for
    attr_accessor :scope

    # The type/realm of the access token
    attr_accessor :token_type

    # Attribute mapping from ruby-style variable name to JSON key.
    def self.attribute_map
      {
        :'access_token' => :'access_token',
        :'expires_in' => :'expires_in',
        :'scope' => :'scope',
        :'token_type' => :'token_type'
      }
    end

    # Attribute type mapping.
    def self.swagger_types
      {
        :'access_token' => :'String',
        :'expires_in' => :'Integer',
        :'scope' => :'String',
        :'token_type' => :'String'
      }
    end

    # Initializes the object
    # @param [Hash] attributes Model attributes in the form of hash
    def initialize(attributes = {})
      return unless attributes.is_a?(Hash)

      # convert string to symbol for hash key
      attributes = attributes.each_with_object({}){|(k,v), h| h[k.to_sym] = v}

      if attributes.has_key?(:'access_token')
        self.access_token = attributes[:'access_token']
      end

      if attributes.has_key?(:'expires_in')
        self.expires_in = attributes[:'expires_in']
      end

      if attributes.has_key?(:'scope')
        self.scope = attributes[:'scope']
      end

      if attributes.has_key?(:'token_type')
        self.token_type = attributes[:'token_type']
      end

    end

    # Show invalid properties with the reasons. Usually used together with valid?
    # @return Array for valid properies with the reasons
    def list_invalid_properties
      invalid_properties = Array.new
      return invalid_properties
    end

    # Check to see if the all the properties in the model are valid
    # @return true if the model is valid
    def valid?
      return true
    end

    # Checks equality by comparing each attribute.
    # @param [Object] Object to be compared
    def ==(o)
      return true if self.equal?(o)
      self.class == o.class &&
          access_token == o.access_token &&
          expires_in == o.expires_in &&
          scope == o.scope &&
          token_type == o.token_type
    end

    # @see the `==` method
    # @param [Object] Object to be compared
    def eql?(o)
      self == o
    end

    # Calculates hash code according to all attributes.
    # @return [Fixnum] Hash code
    def hash
      [access_token, expires_in, scope, token_type].hash
    end

    # Builds the object from hash
    # @param [Hash] attributes Model attributes in the form of hash
    # @return [Object] Returns the model itself
    def build_from_hash(attributes)
      return nil unless attributes.is_a?(Hash)
      self.class.swagger_types.each_pair do |key, type|
        if type =~ /\AArray<(.*)>/i
          # check to ensure the input is an array given that the the attribute
          # is documented as an array but the input is not
          if attributes[self.class.attribute_map[key]].is_a?(Array)
            self.send("#{key}=", attributes[self.class.attribute_map[key]].map{ |v| _deserialize($1, v) } )
          end
        elsif !attributes[self.class.attribute_map[key]].nil?
          self.send("#{key}=", _deserialize(type, attributes[self.class.attribute_map[key]]))
        end # or else data not found in attributes(hash), not an issue as the data can be optional
      end

      self
    end

    # Deserializes the data based on type
    # @param string type Data type
    # @param string value Value to be deserialized
    # @return [Object] Deserialized data
    def _deserialize(type, value)
      case type.to_sym
      when :DateTime
        DateTime.parse(value)
      when :Date
        Date.parse(value)
      when :String
        value.to_s
      when :Integer
        value.to_i
      when :Float
        value.to_f
      when :BOOLEAN
        if value.to_s =~ /\A(true|t|yes|y|1)\z/i
          true
        else
          false
        end
      when :Object
        # generic object (usually a Hash), return directly
        value
      when /\AArray<(?<inner_type>.+)>\z/
        inner_type = Regexp.last_match[:inner_type]
        value.map { |v| _deserialize(inner_type, v) }
      when /\AHash<(?<k_type>.+?), (?<v_type>.+)>\z/
        k_type = Regexp.last_match[:k_type]
        v_type = Regexp.last_match[:v_type]
        {}.tap do |hash|
          value.each do |k, v|
            hash[_deserialize(k_type, k)] = _deserialize(v_type, v)
          end
        end
      else # model
        temp_model = SwaggerClient.const_get(type).new
        temp_model.build_from_hash(value)
      end
    end

    # Returns the string representation of the object
    # @return [String] String presentation of the object
    def to_s
      to_hash.to_s
    end

    # to_body is an alias to to_hash (backward compatibility)
    # @return [Hash] Returns the object in the form of hash
    def to_body
      to_hash
    end

    # Returns the object in the form of hash
    # @return [Hash] Returns the object in the form of hash
    def to_hash
      hash = {}
      self.class.attribute_map.each_pair do |attr, param|
        value = self.send(attr)
        next if value.nil?
        hash[param] = _to_hash(value)
      end
      hash
    end

    # Outputs non-array value in the form of hash
    # For object, use to_hash. Otherwise, just return the value
    # @param [Object] value Any valid value
    # @return [Hash] Returns the value in the form of hash
    def _to_hash(value)
      if value.is_a?(Array)
        value.compact.map{ |v| _to_hash(v) }
      elsif value.is_a?(Hash)
        {}.tap do |hash|
          value.each { |k, v| hash[k] = _to_hash(v) }
        end
      elsif value.respond_to? :to_hash
        value.to_hash
      else
        value
      end
    end

  end

end


text:
package io.crate.jobs;

import com.google.common.collect.Lists;
import io.crate.exceptions.Exceptions;
import io.crate.operation.collect.StatsTables;
import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.logging.Loggers;
import org.elasticsearch.threadpool.ThreadPool;

import javax.annotation.Nullable;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

public class JobExecutionContext {

    private static final ESLogger LOGGER = Loggers.getLogger(JobExecutionContext.class);

    private final UUID jobId;
    private final long keepAlive;
    private final LinkedHashMap<Integer, ExecutionSubContext> subContexts;
    private final AtomicBoolean closed = new AtomicBoolean(false);
    private ThreadPool threadPool;
    private StatsTables statsTables;

    volatile ContextCallback contextCallback;

    private final AtomicInteger activeSubContexts = new AtomicInteger(0);

    private volatile long lastAccessTime = -1;

    public static class Builder {

        private final UUID jobId;
        private final ThreadPool threadPool;
        private final StatsTables statsTables;
        private final long keepAlive = JobContextService.DEFAULT_KEEP_ALIVE;
        private final LinkedHashMap<Integer, ExecutionSubContext> subContexts = new LinkedHashMap<>();

        Builder(UUID jobId, ThreadPool threadPool, StatsTables statsTables) {
            this.jobId = jobId;
            this.threadPool = threadPool;
            this.statsTables = statsTables;
        }

        public void addSubContext(int executionNodeId, ExecutionSubContext subContext) {
            ExecutionSubContext collectContext = subContexts.put(executionNodeId, subContext);
            if (collectContext != null) {
                throw new IllegalArgumentException(String.format(Locale.ENGLISH,
                        "ExecutionSubContext for %d already added", executionNodeId));
            }
        }

        boolean isEmpty() {
            return subContexts.isEmpty();
        }

        public UUID jobId() {
            return jobId;
        }

        public JobExecutionContext build() {
            return new JobExecutionContext(jobId, keepAlive, threadPool, statsTables, subContexts);
        }
    }

    private JobExecutionContext(UUID jobId,
                                long keepAlive,
                                ThreadPool threadPool,
                                StatsTables statsTables,
                                LinkedHashMap<Integer, ExecutionSubContext> subContexts) {
        this.subContexts = new LinkedHashMap<>(subContexts.size());
        this.jobId = jobId;
        this.keepAlive = keepAlive;
        this.threadPool = threadPool;
        this.statsTables = statsTables;

        for (Map.Entry<Integer, ExecutionSubContext> entry : subContexts.entrySet()) {
            addContext(entry.getKey(), entry.getValue());
        }
    }

    void contextCallback(ContextCallback contextCallback) {
        this.contextCallback = contextCallback;
    }

    private void addContext(int subContextId, ExecutionSubContext subContext) {
        int numActive = activeSubContexts.incrementAndGet();
        ExecutionSubContext existing = subContexts.put(subContextId, subContext);
        if (existing == null) {
            subContext.addCallback(new RemoveContextCallback(subContextId));
            LOGGER.trace("adding subContext {}, now there are {} subContexts", subContextId, numActive);
            return;
        }

        activeSubContexts.decrementAndGet();
        throw new IllegalArgumentException(String.format(Locale.ENGLISH, "subContext %d is already present", subContextId));
    }

    public UUID jobId() {
        return jobId;
    }

    public void start() {
        List<Integer> reverseContextIds;
        synchronized (subContexts) {
             reverseContextIds = Lists.reverse(Lists.newArrayList(subContexts.keySet()));
        }
        for (Integer id : reverseContextIds) {
            ExecutionSubContext subContext = subContexts.get(id);
            if (subContext == null) {
                continue; // got killed before start was called
            }
            statsTables.operationStarted(id, jobId, subContext.name());
            subContext.start();
        }
    }

    @Nullable
    public <T extends ExecutionSubContext> T getSubContextOrNull(int executionNodeId) {
        lastAccessTime = threadPool.estimatedTimeInMillis();
        //noinspection unchecked
        return (T) subContexts.get(executionNodeId);
    }

    public <T extends ExecutionSubContext> T getSubContext(int executionNodeId) {
        lastAccessTime = threadPool.estimatedTimeInMillis();
        ExecutionSubContext subContext = subContexts.get(executionNodeId);
        if (subContext == null) {
            throw new IllegalArgumentException(String.format(Locale.ENGLISH,
                    "ExecutionSubContext for %s/%d doesn't exist", jobId(), executionNodeId));
        }
        //noinspection unchecked
        return (T)subContext;
    }

    public long lastAccessTime() {
        return this.lastAccessTime;
    }

    public long keepAlive() {
        return this.keepAlive;
    }

    public long kill() {
        long numKilled = 0L;
        if (!closed.getAndSet(true)) {
            if (activeSubContexts.get() == 0) {
                callContextCallback();
            } else {
                List<ExecutionSubContext> contexts;
                synchronized (subContexts) {
                    contexts = Lists.newArrayList(subContexts.values());
                }
                for (ExecutionSubContext executionSubContext : contexts) {
                    // kill will trigger the ContextCallback onClose too
                    // so it is not necessary to remove the executionSubContext from the map here as it will be done in the callback
                    executionSubContext.kill();
                    numKilled++;
                }
            }
        }
        return numKilled;
    }

    public void close() {
        if (!closed.getAndSet(true)) {
            LOGGER.trace("close called on JobExecutionContext {}", jobId);
            if (activeSubContexts.get() == 0) {
                callContextCallback();
            } else {
                for (ExecutionSubContext executionSubContext : subContexts.values()) {
                    executionSubContext.close();
                }
            }
        }
    }

    private void callContextCallback() {
        if (contextCallback == null) {
            return;
        }
        if (activeSubContexts.get() == 0) {
            contextCallback.onClose(null, -1L);
        }
    }

    private class RemoveContextCallback implements ContextCallback {

        private final int executionNodeId;

        public RemoveContextCallback(int executionNodeId) {
            this.executionNodeId = executionNodeId;
        }

        @Override
        public void onClose(@Nullable Throwable error, long bytesUsed) {
            if (LOGGER.isTraceEnabled()) {
                LOGGER.trace("[{}] Closing subContext {}",
                        System.identityHashCode(subContexts), executionNodeId);
            }

            Object remove;
            synchronized (subContexts) {
                remove = subContexts.remove(executionNodeId);
            }
            int remaining;
            if (remove == null) {
                LOGGER.trace("Closed context {} which was already closed.", executionNodeId);
                remaining = activeSubContexts.get();
            } else {
                statsTables.operationFinished(executionNodeId, Exceptions.messageOf(error), bytesUsed);
                remaining = activeSubContexts.decrementAndGet();
            }
            if (remaining == 0) {
                callContextCallback();
            } else {
                lastAccessTime = threadPool.estimatedTimeInMillis();
            }
        }
    }
}


text:
using json = nlohmann::json;

using namespace atomicpp;

json atomicpp::retrieveFromJSON(std::string path_to_file){
	// Do not pass path_to_file by reference - results in error!
	// Reads a .json file given at path_to_file
	// Uses the json module at https://github.com/nlohmann/json/
	// This relies upon the "json.hpp" header which must be included in the same folder as the source

	// Open a file-stream at path_to_file
	if (atomicpp::test_file_exists(path_to_file)){
		std::ifstream json_file(path_to_file);
		// Initialise a json file object at j_object
		json j_object;
		json_file >> j_object;
		return j_object;
	} else {
		std::stringstream errMsg;
		errMsg << "atomicpp::sharedFunctions::retrieveFromJSON error: file (" << path_to_file << ") not found";
		throw std::runtime_error(errMsg.str());
	}
};
bool atomicpp::test_file_exists (const std::string& name) {
    if (FILE *file = fopen(name.c_str(), "r")) {
        fclose(file);
        return true;
    } else {
        return false;
    }
};


text:
using boost::shared_ptr;
namespace po = boost::program_options;
using namespace std;
using namespace std::tr1;

// Define the following macro if you want to see lots of debugging output
// when you run the chart parser
#undef DEBUG_CHART_PARSER

// A few constants used by the chart parser ///////////////
static const int kMAX_NODES = 2000000;
static const string kPHRASE_STRING = "X";
static bool constants_need_init = true;
static WordID kUNIQUE_START;
static WordID kPHRASE;
static TRulePtr kX1X2;
static TRulePtr kX1;
static WordID kEPS;
static TRulePtr kEPSRule;

static void InitializeConstants() {
  if (constants_need_init) {
    kPHRASE = TD::Convert(kPHRASE_STRING) * -1;
    kUNIQUE_START = TD::Convert("S") * -1;
    kX1X2.reset(new TRule("[X] ||| [X,1] [X,2] ||| [X,1] [X,2]"));
    kX1.reset(new TRule("[X] ||| [X,1] ||| [X,1]"));
    kEPSRule.reset(new TRule("[X] ||| <eps> ||| <eps>"));
    kEPS = TD::Convert("<eps>");
    constants_need_init = false;
  }
}
////////////////////////////////////////////////////////////

class EGrammarNode {
  friend bool EarleyComposer::Compose(const Hypergraph& src_forest, Hypergraph* trg_forest);
  friend void AddGrammarRule(const string& r, map<WordID, EGrammarNode>* g);
 public:
#ifdef DEBUG_CHART_PARSER
  string hint;
#endif
  EGrammarNode() : is_some_rule_complete(false), is_root(false) {}
  const map<WordID, EGrammarNode>& GetTerminals() const { return tptr; }
  const map<WordID, EGrammarNode>& GetNonTerminals() const { return ntptr; }
  bool HasNonTerminals() const { return (!ntptr.empty()); }
  bool HasTerminals() const { return (!tptr.empty()); }
  bool RuleCompletes() const {
    return (is_some_rule_complete || (ntptr.empty() && tptr.empty()));
  }
  bool GrammarContinues() const {
    return !(ntptr.empty() && tptr.empty());
  }
  bool IsRoot() const {
    return is_root;
  }
  // these are the features associated with the rule from the start
  // node up to this point.  If you use these features, you must
  // not Extend() this rule.
  const SparseVector<double>& GetCFGProductionFeatures() const {
    return input_features;
  }

  const EGrammarNode* Extend(const WordID& t) const {
    if (t < 0) {
      map<WordID, EGrammarNode>::const_iterator it = ntptr.find(t);
      if (it == ntptr.end()) return NULL;
      return &it->second;
    } else {
      map<WordID, EGrammarNode>::const_iterator it = tptr.find(t);
      if (it == tptr.end()) return NULL;
      return &it->second;
    }
  }

 private:
  map<WordID, EGrammarNode> tptr;
  map<WordID, EGrammarNode> ntptr;
  SparseVector<double> input_features;
  bool is_some_rule_complete;
  bool is_root;
};
typedef map<WordID, EGrammarNode> EGrammar;    // indexed by the rule LHS

// edges are immutable once created
struct Edge {
#ifdef DEBUG_CHART_PARSER
  static int id_count;
  const int id;
#endif
  const WordID cat;                   // lhs side of rule proved/being proved
  const EGrammarNode* const dot;      // dot position
  const FSTNode* const q;             // start of span
  const FSTNode* const r;             // end of span
  const Edge* const active_parent;    // back pointer, NULL for PREDICT items
  const Edge* const passive_parent;   // back pointer, NULL for SCAN and PREDICT items
  const TargetPhraseSet* const tps;   // translations
  shared_ptr<SparseVector<double> > features; // features from CFG rule

  bool IsPassive() const {
    // when a rule is completed, this value will be set
    return static_cast<bool>(features);
  }
  bool IsActive() const { return !IsPassive(); }
  bool IsInitial() const {
    return !(active_parent || passive_parent);
  }
  bool IsCreatedByScan() const {
    return active_parent && !passive_parent && !dot->IsRoot();
  }
  bool IsCreatedByPredict() const {
    return dot->IsRoot();
  }
  bool IsCreatedByComplete() const {
    return active_parent && passive_parent;
  }

  // constructor for PREDICT
  Edge(WordID c, const EGrammarNode* d, const FSTNode* q_and_r) :
#ifdef DEBUG_CHART_PARSER
    id(++id_count),
#endif
    cat(c), dot(d), q(q_and_r), r(q_and_r), active_parent(NULL), passive_parent(NULL), tps(NULL) {}
  Edge(WordID c, const EGrammarNode* d, const FSTNode* q_and_r, const Edge* act_parent) :
#ifdef DEBUG_CHART_PARSER
    id(++id_count),
#endif
    cat(c), dot(d), q(q_and_r), r(q_and_r), active_parent(act_parent), passive_parent(NULL), tps(NULL) {}

  // constructors for SCAN
  Edge(WordID c, const EGrammarNode* d, const FSTNode* i, const FSTNode* j,
       const Edge* act_par, const TargetPhraseSet* translations) :
#ifdef DEBUG_CHART_PARSER
    id(++id_count),
#endif
    cat(c), dot(d), q(i), r(j), active_parent(act_par), passive_parent(NULL), tps(translations) {}

  Edge(WordID c, const EGrammarNode* d, const FSTNode* i, const FSTNode* j,
       const Edge* act_par, const TargetPhraseSet* translations,
       const SparseVector<double>& feats) :
#ifdef DEBUG_CHART_PARSER
    id(++id_count),
#endif
    cat(c), dot(d), q(i), r(j), active_parent(act_par), passive_parent(NULL), tps(translations),
    features(new SparseVector<double>(feats)) {}

  // constructors for COMPLETE
  Edge(WordID c, const EGrammarNode* d, const FSTNode* i, const FSTNode* j,
       const Edge* act_par, const Edge *pas_par) :
#ifdef DEBUG_CHART_PARSER
    id(++id_count),
#endif
    cat(c), dot(d), q(i), r(j), active_parent(act_par), passive_parent(pas_par), tps(NULL) {
      assert(pas_par->IsPassive());
      assert(act_par->IsActive());
    }

  Edge(WordID c, const EGrammarNode* d, const FSTNode* i, const FSTNode* j,
       const Edge* act_par, const Edge *pas_par, const SparseVector<double>& feats) :
#ifdef DEBUG_CHART_PARSER
    id(++id_count),
#endif
    cat(c), dot(d), q(i), r(j), active_parent(act_par), passive_parent(pas_par), tps(NULL),
    features(new SparseVector<double>(feats)) {
      assert(pas_par->IsPassive());
      assert(act_par->IsActive());
    }

  // constructor for COMPLETE query
  Edge(const FSTNode* _r) :
#ifdef DEBUG_CHART_PARSER
    id(0),
#endif
    cat(0), dot(NULL), q(NULL),
    r(_r), active_parent(NULL), passive_parent(NULL), tps(NULL) {}
  // constructor for MERGE quere
  Edge(const FSTNode* _q, int) :
#ifdef DEBUG_CHART_PARSER
    id(0),
#endif
    cat(0), dot(NULL), q(_q),
    r(NULL), active_parent(NULL), passive_parent(NULL), tps(NULL) {}
};
#ifdef DEBUG_CHART_PARSER
int Edge::id_count = 0;
#endif

ostream& operator<<(ostream& os, const Edge& e) {
  string type = "PREDICT";
  if (e.IsCreatedByScan())
    type = "SCAN";
  else if (e.IsCreatedByComplete())
    type = "COMPLETE";
  os << "["
#ifdef DEBUG_CHART_PARSER
     << '(' << e.id << ") "
#else
     << '(' << &e << ") "
#endif
     << "q=" << e.q << ", r=" << e.r
     << ", cat="<< TD::Convert(e.cat*-1) << ", dot="
     << e.dot
#ifdef DEBUG_CHART_PARSER
     << e.dot->hint
#endif
     << (e.IsActive() ? ", Active" : ", Passive")
     << ", " << type;
#ifdef DEBUG_CHART_PARSER
  if (e.active_parent) { os << ", act.parent=(" << e.active_parent->id << ')'; }
  if (e.passive_parent) { os << ", psv.parent=(" << e.passive_parent->id << ')'; }
#endif
  if (e.tps) { os << ", tps=" << e.tps; }
  return os << ']';
}

struct Traversal {
  const Edge* const edge;      // result from the active / passive combination
  const Edge* const active;
  const Edge* const passive;
  Traversal(const Edge* me, const Edge* a, const Edge* p) : edge(me), active(a), passive(p) {}
};

struct UniqueTraversalHash {
  size_t operator()(const Traversal* t) const {
    size_t x = 5381;
    x = ((x << 5) + x) ^ reinterpret_cast<size_t>(t->active);
    x = ((x << 5) + x) ^ reinterpret_cast<size_t>(t->passive);
    x = ((x << 5) + x) ^ t->edge->IsActive();
    return x;
  }
};

struct UniqueTraversalEquals {
  size_t operator()(const Traversal* a, const Traversal* b) const {
    return (a->passive == b->passive && a->active == b->active && a->edge->IsActive() == b->edge->IsActive());
  }
};

struct UniqueEdgeHash {
  size_t operator()(const Edge* e) const {
    size_t x = 5381;
    if (e->IsActive()) {
      x = ((x << 5) + x) ^ reinterpret_cast<size_t>(e->dot);
      x = ((x << 5) + x) ^ reinterpret_cast<size_t>(e->q);
      x = ((x << 5) + x) ^ reinterpret_cast<size_t>(e->r);
      x = ((x << 5) + x) ^ static_cast<size_t>(e->cat);
      x += 13;
    } else {  // with passive edges, we don't care about the dot
      x = ((x << 5) + x) ^ reinterpret_cast<size_t>(e->q);
      x = ((x << 5) + x) ^ reinterpret_cast<size_t>(e->r);
      x = ((x << 5) + x) ^ static_cast<size_t>(e->cat);
    }
    return x;
  }
};

struct UniqueEdgeEquals {
  bool operator()(const Edge* a, const Edge* b) const {
    if (a->IsActive() != b->IsActive()) return false;
    if (a->IsActive()) {
      return (a->cat == b->cat) && (a->dot == b->dot) && (a->q == b->q) && (a->r == b->r);
    } else {
      return (a->cat == b->cat) && (a->q == b->q) && (a->r == b->r);
    }
  }
};

struct REdgeHash {
  size_t operator()(const Edge* e) const {
    size_t x = 5381;
    x = ((x << 5) + x) ^ reinterpret_cast<size_t>(e->r);
    return x;
  }
};

struct REdgeEquals {
  bool operator()(const Edge* a, const Edge* b) const {
    return (a->r == b->r);
  }
};

struct QEdgeHash {
  size_t operator()(const Edge* e) const {
    size_t x = 5381;
    x = ((x << 5) + x) ^ reinterpret_cast<size_t>(e->q);
    return x;
  }
};

struct QEdgeEquals {
  bool operator()(const Edge* a, const Edge* b) const {
    return (a->q == b->q);
  }
};

struct EdgeQueue {
  queue<const Edge*> q;
  EdgeQueue() {}
  void clear() { while(!q.empty()) q.pop(); }
  bool HasWork() const { return !q.empty(); }
  const Edge* Next() { const Edge* res = q.front(); q.pop(); return res; }
  void AddEdge(const Edge* s) { q.push(s); }
};

class EarleyComposerImpl {
 public:
  EarleyComposerImpl(WordID start_cat, const FSTNode& q_0) : start_cat_(start_cat), q_0_(&q_0) {}

  // returns false if the intersection is empty
  bool Compose(const EGrammar& g, Hypergraph* forest) {
    goal_node = NULL;
    EGrammar::const_iterator sit = g.find(start_cat_);
    forest->ReserveNodes(kMAX_NODES);
    assert(sit != g.end());
    Edge* init = new Edge(start_cat_, &sit->second, q_0_);
    assert(IncorporateNewEdge(init));
    while (exp_agenda.HasWork() || agenda.HasWork()) {
      while(exp_agenda.HasWork()) {
        const Edge* edge = exp_agenda.Next();
        FinishEdge(edge, forest);
      }
      if (agenda.HasWork()) {
        const Edge* edge = agenda.Next();
#ifdef DEBUG_CHART_PARSER
        cerr << "processing (" << edge->id << ')' << endl;
#endif
        if (edge->IsActive()) {
          if (edge->dot->HasTerminals())
            DoScan(edge);
          if (edge->dot->HasNonTerminals()) {
            DoMergeWithPassives(edge);
            DoPredict(edge, g);
          }
        } else {
          DoComplete(edge);
        }
      }
    }
    if (goal_node) {
      forest->PruneUnreachable(goal_node->id_);
      forest->EpsilonRemove(kEPS);
    }
    FreeAll();
    return goal_node;
  }

  void FreeAll() {
    for (int i = 0; i < free_list_.size(); ++i)
      delete free_list_[i];
    free_list_.clear();
    for (int i = 0; i < traversal_free_list_.size(); ++i)
      delete traversal_free_list_[i];
    traversal_free_list_.clear();
    all_traversals.clear();
    exp_agenda.clear();
    agenda.clear();
    tps2node.clear();
    edge2node.clear();
    all_edges.clear();
    passive_edges.clear();
    active_edges.clear();
  }

  ~EarleyComposerImpl() {
    FreeAll();
  }

  // returns the total number of edges created during composition
  int EdgesCreated() const {
    return free_list_.size();
  }

 private:
  void DoScan(const Edge* edge) {
    // here, we assume that the FST will potentially have many more outgoing
    // edges than the grammar, which will be just a couple.  If you want to
    // efficiently handle the case where both are relatively large, this code
    // will need to change how the intersection is done.  The best general
    // solution would probably be the Baeza-Yates double binary search.

    const EGrammarNode* dot = edge->dot;
    const FSTNode* r = edge->r;
    const map<WordID, EGrammarNode>& terms = dot->GetTerminals();
    for (map<WordID, EGrammarNode>::const_iterator git = terms.begin();
         git != terms.end(); ++git) {
      const FSTNode* next_r = r->Extend(git->first);
      if (!next_r) continue;
      const EGrammarNode* next_dot = &git->second;
      const bool grammar_continues = next_dot->GrammarContinues();
      const bool rule_completes    = next_dot->RuleCompletes();
      assert(grammar_continues || rule_completes);
      const SparseVector<double>& input_features = next_dot->GetCFGProductionFeatures();
      // create up to 4 new edges!
      if (next_r->HasOutgoingNonEpsilonEdges()) {     // are there further symbols in the FST?
        const TargetPhraseSet* translations = NULL;
        if (rule_completes)
          IncorporateNewEdge(new Edge(edge->cat, next_dot, edge->q, next_r, edge, translations, input_features));
        if (grammar_continues)
          IncorporateNewEdge(new Edge(edge->cat, next_dot, edge->q, next_r, edge, translations));
      }
      if (next_r->HasData()) {   // indicates a loop back to q_0 in the FST
        const TargetPhraseSet* translations = next_r->GetTranslations();
        if (rule_completes)
          IncorporateNewEdge(new Edge(edge->cat, next_dot, edge->q, q_0_, edge, translations, input_features));
        if (grammar_continues)
          IncorporateNewEdge(new Edge(edge->cat, next_dot, edge->q, q_0_, edge, translations));
      }
    }
  }

  void DoPredict(const Edge* edge, const EGrammar& g) {
    const EGrammarNode* dot = edge->dot;
    const map<WordID, EGrammarNode>& non_terms = dot->GetNonTerminals();
    for (map<WordID, EGrammarNode>::const_iterator git = non_terms.begin();
         git != non_terms.end(); ++git) {
      const WordID nt_to_predict = git->first;
      //cerr << edge->id << " -- " << TD::Convert(nt_to_predict*-1) << endl;
      EGrammar::const_iterator egi = g.find(nt_to_predict);
      if (egi == g.end()) {
        cerr << "[ERROR] Can't find any grammar rules with a LHS of type "
             << TD::Convert(-1*nt_to_predict) << '!' << endl;
        continue;
      }
      assert(edge->IsActive());
      const EGrammarNode* new_dot = &egi->second;
      Edge* new_edge = new Edge(nt_to_predict, new_dot, edge->r, edge);
      IncorporateNewEdge(new_edge);
    }
  }

  void DoComplete(const Edge* passive) {
#ifdef DEBUG_CHART_PARSER
    cerr << "  complete: " << *passive << endl;
#endif
    const WordID completed_nt = passive->cat;
    const FSTNode* q = passive->q;
    const FSTNode* next_r = passive->r;
    const Edge query(q);
    const pair<unordered_multiset<const Edge*, REdgeHash, REdgeEquals>::iterator,
         unordered_multiset<const Edge*, REdgeHash, REdgeEquals>::iterator > p =
      active_edges.equal_range(&query);
    for (unordered_multiset<const Edge*, REdgeHash, REdgeEquals>::iterator it = p.first;
         it != p.second; ++it) {
      const Edge* active = *it;
#ifdef DEBUG_CHART_PARSER
      cerr << "    pos: " << *active << endl;
#endif
      const EGrammarNode* next_dot = active->dot->Extend(completed_nt);
      if (!next_dot) continue;
      const SparseVector<double>& input_features = next_dot->GetCFGProductionFeatures();
      // add up to 2 rules
      if (next_dot->RuleCompletes())
        IncorporateNewEdge(new Edge(active->cat, next_dot, active->q, next_r, active, passive, input_features));
      if (next_dot->GrammarContinues())
        IncorporateNewEdge(new Edge(active->cat, next_dot, active->q, next_r, active, passive));
    }
  }

  void DoMergeWithPassives(const Edge* active) {
    // edge is active, has non-terminals, we need to find the passives that can extend it
    assert(active->IsActive());
    assert(active->dot->HasNonTerminals());
#ifdef DEBUG_CHART_PARSER
    cerr << "  merge active with passives: ACT=" << *active << endl;
#endif
    const Edge query(active->r, 1);
    const pair<unordered_multiset<const Edge*, QEdgeHash, QEdgeEquals>::iterator,
         unordered_multiset<const Edge*, QEdgeHash, QEdgeEquals>::iterator > p =
      passive_edges.equal_range(&query);
    for (unordered_multiset<const Edge*, QEdgeHash, QEdgeEquals>::iterator it = p.first;
         it != p.second; ++it) {
      const Edge* passive = *it;
      const EGrammarNode* next_dot = active->dot->Extend(passive->cat);
      if (!next_dot) continue;
      const FSTNode* next_r = passive->r;
      const SparseVector<double>& input_features = next_dot->GetCFGProductionFeatures();
      if (next_dot->RuleCompletes())
        IncorporateNewEdge(new Edge(active->cat, next_dot, active->q, next_r, active, passive, input_features));
      if (next_dot->GrammarContinues())
        IncorporateNewEdge(new Edge(active->cat, next_dot, active->q, next_r, active, passive));
    }
  }

  // take ownership of edge memory, add to various indexes, etc
  // returns true if this edge is new
  bool IncorporateNewEdge(Edge* edge) {
    free_list_.push_back(edge);
    if (edge->passive_parent && edge->active_parent) {
      Traversal* t = new Traversal(edge, edge->active_parent, edge->passive_parent);
      traversal_free_list_.push_back(t);
      if (all_traversals.find(t) != all_traversals.end()) {
        return false;
      } else {
        all_traversals.insert(t);
      }
    }
    exp_agenda.AddEdge(edge);
    return true;
  }

  bool FinishEdge(const Edge* edge, Hypergraph* hg) {
    bool is_new = false;
    if (all_edges.find(edge) == all_edges.end()) {
#ifdef DEBUG_CHART_PARSER
      cerr << *edge << " is NEW\n";
#endif
      all_edges.insert(edge);
      is_new = true;
      if (edge->IsPassive()) passive_edges.insert(edge);
      if (edge->IsActive()) active_edges.insert(edge);
      agenda.AddEdge(edge);
    } else {
#ifdef DEBUG_CHART_PARSER
      cerr << *edge << " is NOT NEW.\n";
#endif
    }
    AddEdgeToTranslationForest(edge, hg);
    return is_new;
  }

  // build the translation forest
  void AddEdgeToTranslationForest(const Edge* edge, Hypergraph* hg) {
    assert(hg->nodes_.size() < kMAX_NODES);
    Hypergraph::Node* tps = NULL;
    // first add any target language rules
    if (edge->tps) {
      Hypergraph::Node*& node = tps2node[(size_t)edge->tps];
      if (!node) {
        // cerr << "Creating phrases for " << edge->tps << endl;
        const vector<TRulePtr>& rules = edge->tps->GetRules();
        node = hg->AddNode(kPHRASE);
        for (int i = 0; i < rules.size(); ++i) {
          Hypergraph::Edge* hg_edge = hg->AddEdge(rules[i], Hypergraph::TailNodeVector());
          hg_edge->feature_values_ += rules[i]->GetFeatureValues();
          hg->ConnectEdgeToHeadNode(hg_edge, node);
        }
      }
      tps = node;
    }
    Hypergraph::Node*& head_node = edge2node[edge];
    if (!head_node)
      head_node = hg->AddNode(kPHRASE);
    if (edge->cat == start_cat_ && edge->q == q_0_ && edge->r == q_0_ && edge->IsPassive()) {
      assert(goal_node == NULL || goal_node == head_node);
      goal_node = head_node;
    }
    Hypergraph::TailNodeVector tail;
    SparseVector<double> extra;
    if (edge->IsCreatedByPredict()) {
      // extra.set_value(FD::Convert("predict"), 1);
    } else if (edge->IsCreatedByScan()) {
      tail.push_back(edge2node[edge->active_parent]->id_);
      if (tps) {
        tail.push_back(tps->id_);
      }
      //extra.set_value(FD::Convert("scan"), 1);
    } else if (edge->IsCreatedByComplete()) {
      tail.push_back(edge2node[edge->active_parent]->id_);
      tail.push_back(edge2node[edge->passive_parent]->id_);
      //extra.set_value(FD::Convert("complete"), 1);
    } else {
      assert(!"unexpected edge type!");
    }
    //cerr << head_node->id_ << "<--" << *edge << endl;

#ifdef DEBUG_CHART_PARSER
      for (int i = 0; i < tail.size(); ++i)
        if (tail[i] == head_node->id_) {
          cerr << "ERROR: " << *edge << "\n   i=" << i << endl;
          if (i == 1) { cerr << "\tP: " << *edge->passive_parent << endl; }
          if (i == 0) { cerr << "\tA: " << *edge->active_parent << endl; }
          assert(!"self-loop found!");
        }
#endif
    Hypergraph::Edge* hg_edge = NULL;
    if (tail.size() == 0) {
      hg_edge = hg->AddEdge(kEPSRule, tail);
    } else if (tail.size() == 1) {
      hg_edge = hg->AddEdge(kX1, tail);
    } else if (tail.size() == 2) {
      hg_edge = hg->AddEdge(kX1X2, tail);
    }
    if (edge->features)
      hg_edge->feature_values_ += *edge->features;
    hg_edge->feature_values_ += extra;
    hg->ConnectEdgeToHeadNode(hg_edge, head_node);
  }

  Hypergraph::Node* goal_node;
  EdgeQueue exp_agenda;
  EdgeQueue agenda;
  unordered_map<size_t, Hypergraph::Node*> tps2node;
  unordered_map<const Edge*, Hypergraph::Node*, UniqueEdgeHash, UniqueEdgeEquals> edge2node;
  unordered_set<const Traversal*, UniqueTraversalHash, UniqueTraversalEquals> all_traversals;
  unordered_set<const Edge*, UniqueEdgeHash, UniqueEdgeEquals> all_edges;
  unordered_multiset<const Edge*, QEdgeHash, QEdgeEquals> passive_edges;
  unordered_multiset<const Edge*, REdgeHash, REdgeEquals> active_edges;
  vector<Edge*> free_list_;
  vector<Traversal*> traversal_free_list_;
  const WordID start_cat_;
  const FSTNode* const q_0_;
};

#ifdef DEBUG_CHART_PARSER
static string TrimRule(const string& r) {
  size_t start = r.find(" |||") + 5;
  size_t end = r.rfind(" |||");
  return r.substr(start, end - start);
}
#endif

void AddGrammarRule(const string& r, EGrammar* g) {
  const size_t pos = r.find(" ||| ");
  if (pos == string::npos || r[0] != '[') {
    cerr << "Bad rule: " << r << endl;
    return;
  }
  const size_t rpos = r.rfind(" ||| ");
  string feats;
  string rs = r;
  if (rpos != pos) {
    feats = r.substr(rpos + 5);
    rs = r.substr(0, rpos);
  }
  string rhs = rs.substr(pos + 5);
  string trule = rs + " ||| " + rhs + " ||| " + feats;
  TRule tr(trule);
#ifdef DEBUG_CHART_PARSER
  string hint_last_rule;
#endif
  EGrammarNode* cur = &(*g)[tr.GetLHS()];
  cur->is_root = true;
  for (int i = 0; i < tr.FLength(); ++i) {
    WordID sym = tr.f()[i];
#ifdef DEBUG_CHART_PARSER
    hint_last_rule = TD::Convert(sym < 0 ? -sym : sym);
    cur->hint += " <@@> (*" + hint_last_rule + ") " + TrimRule(tr.AsString());
#endif
    if (sym < 0)
      cur = &cur->ntptr[sym];
    else
      cur = &cur->tptr[sym];
  }
#ifdef DEBUG_CHART_PARSER
  cur->hint += " <@@> (" + hint_last_rule + "*) " + TrimRule(tr.AsString());
#endif
  cur->is_some_rule_complete = true;
  cur->input_features = tr.GetFeatureValues();
}

EarleyComposer::~EarleyComposer() {
  delete pimpl_;
}

EarleyComposer::EarleyComposer(const FSTNode* fst) {
  InitializeConstants();
  pimpl_ = new EarleyComposerImpl(kUNIQUE_START, *fst);
}

bool EarleyComposer::Compose(const Hypergraph& src_forest, Hypergraph* trg_forest) {
  // first, convert the src forest into an EGrammar
  EGrammar g;
  const int nedges = src_forest.edges_.size();
  const int nnodes = src_forest.nodes_.size();
  vector<int> cats(nnodes);
  bool assign_cats = false;
  for (int i = 0; i < nnodes; ++i)
    if (assign_cats) {
      cats[i] = TD::Convert("CAT_" + boost::lexical_cast<string>(i)) * -1;
    } else {
      cats[i] = src_forest.nodes_[i].cat_;
    }
  // construct the grammar
  for (int i = 0; i < nedges; ++i) {
    const Hypergraph::Edge& edge = src_forest.edges_[i];
    const vector<WordID>& src = edge.rule_->f();
    EGrammarNode* cur = &g[cats[edge.head_node_]];
    cur->is_root = true;
    int ntc = 0;
    for (int j = 0; j < src.size(); ++j) {
      WordID sym = src[j];
      if (sym <= 0) {
        sym = cats[edge.tail_nodes_[ntc]];
        ++ntc;
        cur = &cur->ntptr[sym];
      } else {
        cur = &cur->tptr[sym];
      }
    }
    cur->is_some_rule_complete = true;
    cur->input_features = edge.feature_values_;
  }
  EGrammarNode& goal_rule = g[kUNIQUE_START];
  assert((goal_rule.ntptr.size() == 1 && goal_rule.tptr.size() == 0) ||
         (goal_rule.ntptr.size() == 0 && goal_rule.tptr.size() == 1));

  return pimpl_->Compose(g, trg_forest);
}

bool EarleyComposer::Compose(istream* in, Hypergraph* trg_forest) {
  EGrammar g;
  while(*in) {
    string line;
    getline(*in, line);
    if (line.empty()) continue;
    AddGrammarRule(line, &g);
  }

  return pimpl_->Compose(g, trg_forest);
}


text:
import pytracer.color
import pytracer.materials
import pytracer.camera
import pytracer.scene
import pytracer.objects
import pytracer.vector
import pytracer.main
import pytracer.lights
import pytracer.ray


text:
<?xml version="1.0" encoding="UTF-8"?>
<ruleset xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         name="pmd-level-5"
         xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd">
   <description>Level 5 PMD Plugin preferences rule set</description>
   <exclude-pattern>org.gavaghan.geodesy/**</exclude-pattern>
   <rule ref="rulesets/java/logging-java.xml/InvalidSlf4jMessageFormat"/>
</ruleset>


text:
import java.util.function.Function;

public class UtilTest {
    public static void main(String[] args) {
        Function<String, String> fn = new Function<String, String>() {
            @Override
            public String apply(String s) {
                return "test";
            }
        };
        System.out.println(fn);
    }
}


text:
package org.scalacheck

import language.higherKinds
import scala.annotation.tailrec
import scala.collection.immutable.BitSet
import scala.util.{Failure, Success, Try}
import scala.concurrent.duration.{Duration, FiniteDuration}
import java.math.BigInteger
import rng.Seed

sealed trait Cogen[T] extends Serializable {

  def perturb(seed: Seed, t: T): Seed

  def cogen[A](t: T, g: Gen[A]): Gen[A] =
    Gen.gen((p, seed) => g.doApply(p, perturb(seed, t)))

  def contramap[S](f: S => T): Cogen[S] =
    Cogen((seed: Seed, s: S) => perturb(seed, f(s)))
}

object Cogen extends CogenArities with CogenLowPriority with CogenVersionSpecific {

  def apply[T](implicit ev: Cogen[T]): Cogen[T] = ev

  def apply[T](f: T => Long): Cogen[T] = new Cogen[T] {
    def perturb(seed: Seed, t: T): Seed = seed.reseed(f(t))
  }

  def apply[T](f: (Seed, T) => Seed): Cogen[T] =
    new Cogen[T] {
      def perturb(seed: Seed, t: T): Seed = f(seed, t)
    }

  def it[T, U](f: T => Iterator[U])(implicit U: Cogen[U]): Cogen[T] =
    new Cogen[T] {
      def perturb(seed: Seed, t: T): Seed =
        f(t).foldLeft(seed)(U.perturb).next
    }

  def perturb[T](seed: Seed, t: T)(implicit cg: Cogen[T]): Seed =
    cg.perturb(seed, t)

  implicit lazy val cogenUnit: Cogen[Unit] = Cogen(_ => 0L)

  implicit lazy val cogenBoolean: Cogen[Boolean] =
    Cogen(b => if (b) 1L else 0L)

  implicit lazy val cogenByte: Cogen[Byte] = Cogen(_.toLong)
  implicit lazy val cogenShort: Cogen[Short] = Cogen(_.toLong)
  implicit lazy val cogenChar: Cogen[Char] = Cogen(_.toLong)
  implicit lazy val cogenInt: Cogen[Int] = Cogen(_.toLong)
  implicit lazy val cogenLong: Cogen[Long] = Cogen(n => n)

  implicit lazy val cogenFloat: Cogen[Float] =
    Cogen(n => java.lang.Float.floatToIntBits(n).toLong)

  implicit lazy val cogenDouble: Cogen[Double] =
    Cogen(n => java.lang.Double.doubleToLongBits(n))

  implicit lazy val bigInt: Cogen[BigInt] =
    Cogen[Array[Byte]].contramap(_.toByteArray)

  implicit lazy val bigDecimal: Cogen[BigDecimal] = {

    // Normalize unscaled values and scaling factors by moving powers of ten from value to scaling factor.
    @tailrec
    def normalize(unscaled: BigInteger, scale: Int): (BigInteger, Int) = {
      val divideAndRemainder = unscaled.divideAndRemainder(BigInteger.TEN)
      val quotient = divideAndRemainder(0)
      val remainder = divideAndRemainder(1)
      val canNormalize = (unscaled.abs.compareTo(BigInteger.TEN) >= 0) &&
        (remainder == BigInteger.ZERO) &&
        (scale != Int.MaxValue) &&
        (scale != Int.MinValue)
      if (canNormalize) normalize(quotient, scale - 1) else (unscaled, scale)
    }

    // If the unscaled value is zero then the scaling factor doesn't matter. Otherwise perturb based on both.
    Cogen((seed: Seed, n: BigDecimal) =>
      if (n.bigDecimal.unscaledValue == BigInteger.ZERO)
        Cogen[Int].perturb(seed, 0)
      else {
        val (unscaled, scale) = normalize(n.bigDecimal.unscaledValue, n.scale)
        Cogen[(Int, Array[Byte])].perturb(seed, (scale, unscaled.toByteArray))
    })
  }

  implicit lazy val bitSet: Cogen[BitSet] =
    Cogen.it(_.iterator)

  implicit def cogenOption[A](implicit A: Cogen[A]): Cogen[Option[A]] =
    Cogen((seed, o) => o.fold(seed)(a => A.perturb(seed.next, a)))

  implicit def cogenEither[A, B](implicit A: Cogen[A], B: Cogen[B]): Cogen[Either[A, B]] =
    Cogen((seed, e) => e.fold(a => A.perturb(seed, a), b => B.perturb(seed.next, b)))

  implicit def cogenArray[A](implicit A: Cogen[A]): Cogen[Array[A]] =
    Cogen((seed, as) => perturbArray(seed, as))

  implicit def cogenString: Cogen[String] =
    Cogen.it(_.iterator)

  implicit def cogenSymbol: Cogen[Symbol] =
    Cogen[String].contramap(_.name)

  implicit def cogenList[A: Cogen]: Cogen[List[A]] =
    Cogen.it(_.iterator)

  implicit def cogenVector[A: Cogen]: Cogen[Vector[A]] =
    Cogen.it(_.iterator)

  implicit def cogenStream[A: Cogen]: Cogen[Stream[A]] =
    Cogen.it(_.iterator)

  implicit def cogenSet[A: Cogen: Ordering]: Cogen[Set[A]] =
    Cogen.it(_.toVector.sorted.iterator)

  implicit def cogenMap[K: Cogen: Ordering, V: Cogen]: Cogen[Map[K, V]] =
    Cogen.it(_.toVector.sortBy(_._1).iterator)

  implicit def cogenFunction0[Z: Cogen]: Cogen[() => Z] =
    Cogen[Z].contramap(f => f())

  implicit val cogenException: Cogen[Exception] =
    Cogen[String].contramap(_.toString)

  implicit val cogenThrowable: Cogen[Throwable] =
    Cogen[String].contramap(_.toString)

  implicit def cogenTry[A: Cogen]: Cogen[Try[A]] =
    Cogen((seed: Seed, x: Try[A]) => x match {
      case Success(a) => Cogen[A].perturb(seed.next, a)
      case Failure(e) => Cogen[Throwable].perturb(seed, e)
    })

  implicit val cogenFiniteDuration: Cogen[FiniteDuration] =
    Cogen[Long].contramap(_.toNanos)

  implicit val cogenDuration: Cogen[Duration] =
    Cogen((seed: Seed, x: Duration) => x match {
      case d: FiniteDuration => Cogen[FiniteDuration].perturb(seed, d)
      // Undefined -> NaN, Inf -> PositiveInfinity, MinusInf -> NegativeInf
      // We could just use `toUnit` for finite durations too, but the Long => Double
      // conversion is lossy, so this approach may be better.
      case d => Cogen[Double].perturb(seed, d.toUnit(java.util.concurrent.TimeUnit.NANOSECONDS))
    })

  implicit def cogenPartialFunction[A: Arbitrary, B: Cogen]: Cogen[PartialFunction[A, B]] =
    Cogen[A => Option[B]].contramap(_.lift)

  def perturbPair[A, B](seed: Seed, ab: (A, B))(implicit A: Cogen[A], B: Cogen[B]): Seed =
    B.perturb(A.perturb(seed, ab._1), ab._2)

  def perturbArray[A](seed: Seed, as: Array[A])(implicit A: Cogen[A]): Seed = {
    var s = seed
    var i = 0
    while (i < as.length) { s = A.perturb(s, as(i)); i += 1 }
    s.next
  }
}

trait CogenLowPriority {
  implicit def cogenSeq[CC[x] <: Seq[x], A: Cogen]: Cogen[CC[A]] =
    Cogen.it(_.iterator)
}


text:
using System.IO;
using Aspose.Imaging.Brushes;
using Aspose.Imaging.ImageOptions;
using Aspose.Imaging.Sources;
using System;

/*
This project uses Automatic Package Restore feature of NuGet to resolve Aspose.Imaging for .NET API reference
when the project is build. Please check https://Docs.nuget.org/consume/nuget-faq for more information.
If you do not wish to use NuGet, you can manually download Aspose.Imaging for .NET API from http://www.aspose.com/downloads,
install it and then add its reference to this project. For any issues, questions or suggestions
please feel free to contact us using http://www.aspose.com/community/forums/default.aspx
*/

namespace Aspose.Imaging.Examples.CSharp.DrawingAndFormattingImages
{
    class DrawingRectangle
    {
        public static void Run()
        {
            Console.WriteLine("Running example DrawingRectangle");
            // The path to the documents directory.
            string dataDir = RunExamples.GetDataDir_DrawingAndFormattingImages() + "SampleRectangle_out.bmp";

            // Creates an instance of FileStream
            using (FileStream stream = new FileStream(dataDir, FileMode.Create))
            {
                // Create an instance of BmpOptions and set its various properties
                BmpOptions saveOptions = new BmpOptions();
                saveOptions.BitsPerPixel = 32;

                // Set the Source for BmpOptions and Create an instance of Image
                saveOptions.Source = new StreamSource(stream);
                using (Image image = Image.Create(saveOptions, 100, 100))
                {
                    // Create and initialize an instance of Graphics class,  Clear Graphics surface, Draw a rectangle shapes and  save all changes.
                    Graphics graphic = new Graphics(image);
                    graphic.Clear(Color.Yellow);
                    graphic.DrawRectangle(new Pen(Color.Red), new Rectangle(30, 10, 40, 80));
                    graphic.DrawRectangle(new Pen(new SolidBrush(Color.Blue)), new Rectangle(10, 30, 80, 40));
                    image.Save();
                }
            }

            Console.WriteLine("Finished example DrawingRectangle");
        }
    }
}


text:
enum Foo {
    Hello,
    World
};

inline int bar( Foo foo ) {
  switch(foo) {
    case Hello: return 0;
    // Warn because of missung case for World
  }
  return 1;
}


text:
title: UNB Strike Analysis
subtitle: Performed a 3rd-party analysis of the UNB strike salary and budget figures.
layout: default
modal-id: 1
img: UNBStrike.jpg
thumbnail: UNBStrike-thumbnail.jpg
alt: Graph of the UNB strike salaries.
description: When the University of New Brunswick went on strike in 2014, a member of Citizens Code stepped forward to provide a 3rd-party perspective on the university expenditures on professors, instructors and president salaries as they compared to similar institutions nation-wide.
---


text:
using System.ComponentModel;
using System.Windows;

namespace VKExport
{
	public partial class MainWindow : Window
	{
		public MainWindow()
		{
			InitializeComponent();

			this.DataContext = App.Locator.MainWindow;

			OpenAuthorizationWindow();
		}

		private void OpenAuthorizationWindow()
		{
			var auth = new AuthWindow();
			auth.Authorized += App.Locator.MainWindow.OnAuthorized;
			auth.Closing += OnClosing;
			auth.ShowDialog();
		}

		private void OnClosing(object sender, CancelEventArgs e)
		{
			if (!App.Locator.MainWindow.IsAuth)
			{
				this.Close();
			}
		}

	}
}


text:
``KernelRidge``
============================================================================

.. autoclass:: ibex.sklearn.kernel_ridge.KernelRidge
    :members:
    :special-members:
    :show-inheritance:


text:
package com.simplaapliko.trips.presentation.model.mapper;

import com.simplaapliko.trips.domain.model.TripDate;
import com.simplaapliko.trips.presentation.model.TripDateModel;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Singleton;

@Singleton
public class TripDateModelMapper {

    @Inject public TripDateModelMapper() {}

    public TripDateModel transform(TripDate tripDate) {
        TripDateModel tripDateModel = null;
        if (tripDate != null) {
            tripDateModel = new TripDateModel.Builder()
                    .setId(tripDate.getId())
                    .setTripId(tripDate.getTripId())
                    .setDate(tripDate.getDate())
                    .setColorId(tripDate.getColorId())
                    .build();
        }
        return tripDateModel;
    }

    public TripDate transform(TripDateModel tripDateModel) {
        TripDate tripDate = null;
        if (tripDateModel != null) {
            tripDate = new TripDate.Builder()
                    .setId(tripDateModel.getId())
                    .setTripId(tripDateModel.getTripId())
                    .setDate(tripDateModel.getDate())
                    .setColorId(tripDateModel.getColorId())
                    .build();
        }
        return tripDate;
    }

    public List<TripDateModel> transform(List<TripDate> tripDates) {
        List<TripDateModel> tripDateModels = new ArrayList<>();
        if (tripDates != null) {
            for (TripDate tripDate : tripDates) {
                TripDateModel tripDateModel = transform(tripDate);
                tripDateModels.add(tripDateModel);
            }
        }
        return tripDateModels;
    }
}


text:
using FluentAssertions;
using System;
using Xunit;

namespace Mash.ArmTools.Tests
{
    public class ResourceTests
    {
        private static readonly string _subscriptionId = Guid.NewGuid().ToString();
        private const string _resourceGroup = "rgName";
        private const string _provider = "Microsoft.GenericProvider";
        private const string _resourceType = "customResourceType";
        private const string _resourceName = "customResourceName";
        private const string _ancestor1Type = "ancestor1Type";
        private const string _ancestor1Name = "ancestor1Name";
        private const string _ancestor2Type = "ancestor2Type";
        private const string _ancestor2Name = "ancestor2Name";

        [Theory]
        [InlineData(null)]
        [InlineData("")]
        [InlineData(" ")]
        [InlineData("/doesNotStartWithSubscriptions")]
        [InlineData("/subscriptions")]
        [InlineData("/subscriptions/subscriptionId/notResourceGroups")]
        [InlineData("/subscriptions/subscriptionId/resourceGroups")]
        [InlineData("/subscriptions/subscriptionId/resourceGroups/rgName")]
        [InlineData("/subscriptions/subscriptionId/resourceGroups/rgName/notProviders")]
        [InlineData("/subscriptions/subscriptionId/resourceGroups/rgName/providers")]
        [InlineData("/subscriptions/subscriptionId/resourceGroups/rgName/providers/providerNamespace")]
        [InlineData("/subscriptions/subscriptionId/resourceGroups/rgName/providers/providerNamespace/resourceType")]
        [InlineData("/subscriptions/subscriptionId/resourceGroups/rgName/providers/providerNamespace/ancestorType/ancestor/resourceType")]
        public void Resource_Parse_Failures(string resourceId)
        {
            // arrange and act
            Action parse = () => Resource.Parse(resourceId);

            // assert
            parse.Should().Throw<ArgumentException>($"[{resourceId}] should be invalid");
        }

        [Fact]
        public void Resource_Parse_TopLevelResource()
        {
            // arrange
            string[] parts = new[]
            {
                ResourceConstants.Subscriptions,
                _subscriptionId,
                ResourceConstants.ResourceGroups,
                _resourceGroup,
                ResourceConstants.Providers,
                _provider,
                _resourceType,
                _resourceName,
            };
            string input = String.Join(ResourceConstants.ResourceIdSplitter, parts);

            // act
            var resourceId = Resource.Parse(input);

            // assert
            resourceId.SubscriptionId.Should().Be(_subscriptionId);
            resourceId.ResourceGroupName.Should().Be(_resourceGroup);
            resourceId.ProviderNamespace.Should().Be(_provider);
            resourceId.ResourceType.Should().Be(_resourceType);
            resourceId.Name.Should().Be(_resourceName);
            resourceId.FullResourceType.Should().Be($"{_provider}/{_resourceType}");
            resourceId.Ancestors.Should().BeEmpty();
            resourceId.IsTopLevel.Should().BeTrue();
        }

        [Fact]
        public void Resource_Parse_NestedResource()
        {
            // arrange
            string[] parts = new[]
            {
                ResourceConstants.Subscriptions,
                _subscriptionId,
                ResourceConstants.ResourceGroups,
                _resourceGroup,
                ResourceConstants.Providers,
                _provider,
                _ancestor1Type,
                _ancestor1Name,
                _resourceType,
                _resourceName,
            };
            string input = String.Join(ResourceConstants.ResourceIdSplitter, parts);

            // act
            var resourceId = Resource.Parse(input);

            // assert
            resourceId.SubscriptionId.Should().Be(_subscriptionId);
            resourceId.ResourceGroupName.Should().Be(_resourceGroup);
            resourceId.ProviderNamespace.Should().Be(_provider);
            resourceId.ResourceType.Should().Be(_resourceType);
            resourceId.Name.Should().Be(_resourceName);
            resourceId.FullResourceType.Should().Be($"{_provider}/{_ancestor1Type}/{_resourceType}");
            resourceId.Ancestors.Should().BeEquivalentTo(new[] { _ancestor1Name });
            resourceId.IsTopLevel.Should().BeFalse();
        }

        [Fact]
        public void Resource_Parse_TopLevelParent()
        {
            // arrange
            var resourceId = new Resource(
                _subscriptionId,
                _resourceGroup,
                _provider,
                _resourceType,
                _resourceName,
                new[] { _ancestor1Type },
                new[] { _ancestor1Name });

            // act
            var parent = resourceId.Parent;

            // assert
            parent.SubscriptionId.Should().Be(_subscriptionId);
            parent.ResourceGroupName.Should().Be(_resourceGroup);
            parent.ProviderNamespace.Should().Be(_provider);
            parent.ResourceType.Should().Be(_ancestor1Type);
            parent.Name.Should().Be(_ancestor1Name);
            parent.FullResourceType.Should().Be($"{_provider}/{_ancestor1Type}");
            parent.Ancestors.Should().BeEquivalentTo(new string[0]);
            parent.IsTopLevel.Should().BeTrue();
        }

        [Fact]
        public void Resource_Parse_NestedParent()
        {
            // arrange
            var resourceId = new Resource(
                _subscriptionId,
                _resourceGroup,
                _provider,
                _resourceType,
                _resourceName,
                new[] { _ancestor1Type, _ancestor2Type },
                new[] { _ancestor1Name, _ancestor2Name });

            // act
            var parent = resourceId.Parent;

            // assert
            parent.SubscriptionId.Should().Be(_subscriptionId);
            parent.ResourceGroupName.Should().Be(_resourceGroup);
            parent.ProviderNamespace.Should().Be(_provider);
            parent.ResourceType.Should().Be(_ancestor2Type);
            parent.Name.Should().Be(_ancestor2Name);
            parent.FullResourceType.Should().Be($"{_provider}/{_ancestor1Type}/{_ancestor2Type}");
            parent.Ancestors.Should().BeEquivalentTo(new[] { _ancestor1Name });
            parent.IsTopLevel.Should().BeFalse();
        }

        [Fact]
        public void Resource_Parse_DeepResource()
        {
            // arrange
            string[] parts = new[]
            {
                ResourceConstants.Subscriptions,
                _subscriptionId,
                ResourceConstants.ResourceGroups,
                _resourceGroup,
                ResourceConstants.Providers,
                _provider,
                _ancestor1Type,
                _ancestor1Name,
                _ancestor2Type,
                _ancestor2Name,
                _resourceType,
                _resourceName,
            };
            string input = String.Join(ResourceConstants.ResourceIdSplitter, parts);

            // act
            var resourceId = Resource.Parse(input);

            // assert
            resourceId.SubscriptionId.Should().Be(_subscriptionId);
            resourceId.ResourceGroupName.Should().Be(_resourceGroup);
            resourceId.ProviderNamespace.Should().Be(_provider);
            resourceId.ResourceType.Should().Be(_resourceType);
            resourceId.Name.Should().Be(_resourceName);
            resourceId.FullResourceType.Should().Be($"{_provider}/{_ancestor1Type}/{_ancestor2Type}/{_resourceType}");
            resourceId.Ancestors.Should().BeEquivalentTo(new[] { _ancestor1Name, _ancestor2Name });
            resourceId.IsTopLevel.Should().BeFalse();
        }
    }
}


text:
package patterns.creational.builder.example;

class Product {

    private String name;
    private String color;

    Product() {
    }

    String getColor() {
        return color;
    }

    void setColor(final String color) {
        this.color = color;
    }

    String getName() {
        return name;
    }

    void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Product [name = " + name + ", color = " + color + "]";
    }

}


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

import cn.felord.wepay.ali.sdk.api.AlipayObject;
import cn.felord.wepay.ali.sdk.api.internal.mapping.ApiField;

/**
 * 口碑商户人群组列表查询接口
 *
 * @author auto create
 * @version $Id: $Id
 */
public class KoubeiMarketingCampaignCrowdBatchqueryModel extends AlipayObject {

	private static final long serialVersionUID = 3595621652859386265L;

	/**
	 * 人群名称
	 */
	@ApiField("name")
	private String name;

	/**
	 * 分页页码,从1开始计数,如果不填写默认为1
	 */
	@ApiField("page_number")
	private String pageNumber;

	/**
	 * 分页大小,每页显示的数目,如果不填写默认为20
	 */
	@ApiField("page_size")
	private String pageSize;

	/**
	 *
Getter for the field name.

	 *
	 * @return a {@link java.lang.String} object.
	 */
	public String getName() {
		return this.name;
	}
	/**
	 *
Setter for the field name.

	 *
	 * @param name a {@link java.lang.String} object.
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 *
Getter for the field pageNumber.

	 *
	 * @return a {@link java.lang.String} object.
	 */
	public String getPageNumber() {
		return this.pageNumber;
	}
	/**
	 *
Setter for the field pageNumber.

	 *
	 * @param pageNumber a {@link java.lang.String} object.
	 */
	public void setPageNumber(String pageNumber) {
		this.pageNumber = pageNumber;
	}

	/**
	 *
Getter for the field pageSize.

	 *
	 * @return a {@link java.lang.String} object.
	 */
	public String getPageSize() {
		return this.pageSize;
	}
	/**
	 *
Setter for the field pageSize.

	 *
	 * @param pageSize a {@link java.lang.String} object.
	 */
	public void setPageSize(String pageSize) {
		this.pageSize = pageSize;
	}

}