text:
namespace RBAC {

  type MBeans = { [name: string]: Jmx.Folder };
  type BulkRequest = { [name: string]: string[] };
  type BulkResponse = { [name: string]: Operations };
  type Operations = { [name: string]: Operation };
  type Operation = { ObjectName: string, Method: string, CanInvoke: boolean };

  export class JmxTreeProcessor {

    constructor(
      private jolokia: Jolokia.IJolokia,
      private jolokiaStatus: JVM.JolokiaStatus,
      private rbacTasks: RBACTasks) {
    }

    /**
     * Process JMX tree and attach RBAC info (canInvoke) to it in advance.
     *
     * Matrix of supported RBAC Mbeans per platform:
     * +-------------------------+-----------+--------------+---------------+-------------------+
     * |        Platform         | ACL MBean | RBACRegistry | RBACDecorator |  Process method   |
     * +-------------------------+-----------+--------------+---------------+-------------------+
     * | Karaf                   | o         | o            | o             | processWithRBAC() |
     * | WildFly                 | x (dummy) | o            | x             | processGeneral()  |
     * | Spring Boot             | x (dummy) | o            | x             | processGeneral()  |
     * | Artemis                 | o         | o            | x             | processGeneral()  |
     * | Jolokia (no hawtio.war) | x         | x            | x             | processGeneral()  |
     * +-------------------------+-----------+--------------+---------------+-------------------+
     *
     * Object names for the RBAC MBeans:
     * - ACL MBean:     "*:type=security,area=jmx,*"
     * - RBACRegistry:  "hawtio:type=security,name=RBACRegistry"
     * - RBACDecorator: "hawtio:type=security,area=jolokia,name=RBACDecorator"
     */
    process(tree: Jmx.Folder): void {
      log.debug("Processing tree", tree);
      this.rbacTasks.getACLMBean().then((aclMBean) => {
        const mbeans = this.flatten(tree);
        switch (this.jolokiaStatus.listMethod) {
          case JVM.JolokiaListMethod.LIST_OPTIMISED:
            log.debug("Process JMX tree: optimised list mode");
            if (this.hasDecoratedRBAC(mbeans)) {
              log.debug("JMX tree already decorated with RBAC");
              this.processWithRBAC(mbeans);
            } else {
              log.debug("JMX tree not decorated with RBAC, fetching RBAC info now");
              this.processGeneral(aclMBean, mbeans);
            }
            log.debug("Processed tree mbeans with RBAC", mbeans);
            break;
          case JVM.JolokiaListMethod.LIST_GENERAL:
          case JVM.JolokiaListMethod.LIST_CANT_DETERMINE:
          default:
            log.debug("Process JMX tree: general mode");
            this.processGeneral(aclMBean, mbeans);
            log.debug("Processed tree mbeans", mbeans);
            break;
        }
      });
    }

    private flatten(tree: Jmx.Folder): MBeans {
      const mbeans: MBeans = {};
      this.flattenFolder(mbeans, tree);
      return mbeans;
    }

    /**
     * Recursive method to flatten MBeans folder
     */
    private flattenFolder(mbeans: MBeans, folder: Jmx.Folder): void {
      if (!Core.isBlank(folder.objectName)) {
        mbeans[folder.objectName] = folder;
      }
      if (folder.isFolder()) {
        folder.children.forEach(child => this.flattenFolder(mbeans, child as Jmx.Folder));
      }
    }

    /**
     * Check if RBACDecorator has been applied to the MBean tree at server side.
     */
    private hasDecoratedRBAC(mbeans: MBeans): boolean {
      const node = _.find(mbeans,
        folder => !_.isEmpty(folder.mbean.op) && _.isEmpty(folder.mbean.opByString));
      return _.isNil(node);
    }

    private processWithRBAC(mbeans: MBeans): void {
      // we already have everything related to RBAC in place, except 'class' property
      _.forEach(mbeans, (node: Jmx.Folder, mbeanName: string) => {
        const mbean = node.mbean;
        const canInvoke = mbean && (_.isNil(mbean.canInvoke) || mbean.canInvoke);
        this.addCanInvokeToClass(node, canInvoke);
      });
    }

    private processGeneral(aclMBean: string, mbeans: MBeans): void {
      const requests: Jolokia.IRequest[] = [];
      const bulkRequest: BulkRequest = {};
      // register canInvoke requests for each MBean and accumulate bulkRequest for all ops
      _.forEach(mbeans, (folder, mbeanName) => {
        this.addCanInvokeRequests(aclMBean, mbeanName, folder, requests, bulkRequest);
      });
      // register the bulk request finally based on the accumulated bulkRequest
      requests.push({
        type: 'exec',
        mbean: aclMBean,
        operation: 'canInvoke(java.util.Map)',
        arguments: [bulkRequest]
      });
      // send batch request
      log.debug('Batch canInvoke request:', requests);
      this.jolokia.request(requests, Core.onSuccess(
        (response) => this.applyCanInvoke(mbeans, response),
        { error: (response) => { /* silently ignore */ } }));
    }

    private addCanInvokeRequests(aclMBean: string, mbeanName: string, folder: Jmx.Folder,
      requests: Jolokia.IRequest[], bulkRequest: BulkRequest): void {
      // request for MBean
      requests.push({
        type: 'exec',
        mbean: aclMBean,
        operation: 'canInvoke(java.lang.String)',
        arguments: [mbeanName]
      });
      // bulk request for MBean ops
      if (folder.mbean && folder.mbean.op) {
        // opByString is later filled in addOperation()
        folder.mbean.opByString = {};
        const opList: string[] = [];
        _.forEach(folder.mbean.op, (op: Core.JMXOperation, opName: string) => {
          if (_.isArray(op)) {
            // overloaded ops
            _.forEach(op, (op) => this.addOperation(folder, opList, opName, op));
          } else {
            // single op
            this.addOperation(folder, opList, opName, op);
          }
        });
        if (!_.isEmpty(opList)) {
          bulkRequest[mbeanName] = opList;
        }
      }
    }

    private addOperation(folder: Jmx.Folder, opList: string[], opName: string, op: Core.JMXOperation): void {
      const opString = Core.operationToString(opName, op.args);
      // enrich the mbean by indexing the full operation string so we can easily look it up later
      folder.mbean.opByString[opString] = op;
      opList.push(opString);
    }

    private applyCanInvoke(mbeans: MBeans, response: Jolokia.IResponse): void {
      const mbean = response.request.arguments[0];
      if (mbean && _.isString(mbean)) {
        // single request
        const canInvoke = response.value;
        mbeans[mbean].mbean.canInvoke = response.value;
        this.addCanInvokeToClass(mbeans[mbean], canInvoke);
      } else {
        // batch request
        const bulkResponse: BulkResponse = response.value;
        log.debug('Batch canInvoke response:', bulkResponse);
        _.forEach(bulkResponse, (ops: Operations, mbeanName: string) => {
          _.forEach(ops, (op: Operation, opName: string) => {
            mbeans[mbeanName].mbean.opByString[opName]['canInvoke'] = op.CanInvoke;
          });
        });
      }
    }

    private addCanInvokeToClass(mbean: Jmx.Folder, canInvoke: boolean): void {
      const toAdd = canInvoke ? "can-invoke" : "cant-invoke";
      mbean.class = this.addClass(this.stripClasses(mbean.class), toAdd);
      if (!canInvoke) {
        // change the tree node icon to lock here
        mbean.icon = 'fa fa-lock';
      }
    }

    private stripClasses(css: string): string {
      if (Core.isBlank(css)) {
        return css;
      }
      let parts = css.split(" ");
      let answer = [];
      parts.forEach((part) => {
        if (part !== "can-invoke" && part !== "cant-invoke") {
          answer.push(part);
        }
      });
      return answer.join(" ").trim();
    }

    private addClass(css: string, _class: string): string {
      if (Core.isBlank(css)) {
        return _class;
      }
      let parts = css.split(" ");
      parts.push(_class);
      return _.uniq(parts).join(" ").trim();
    }

  }

}


text:
package com.jota.patterns.builder.builder;

import com.jota.patterns.builder.model.Bodywork;
import com.jota.patterns.builder.model.Motor;
import com.jota.patterns.builder.model.MotorDiesel;
import com.jota.patterns.builder.model.Vehicle;
import com.jota.patterns.builder.model.Wheel;

public class SedanBuilder extends VehicleBuilder {

  @Override protected void defineVehicle() {
    vehicle = new Vehicle();
    vehicle.setBrand("Sedan");
    vehicle.setColor("Red");
  }

  @Override protected void buildMotor() {
    Motor motor = new MotorDiesel();
    vehicle.setMotor(motor);
  }

  @Override protected void buildWheel() {
    Wheel wheel = new Wheel();
    wheel.setDiameter(16);
    wheel.setTire("normal");
    vehicle.setWheel(wheel);
  }

  @Override protected void buildBodywork() {
    Bodywork bodywork = new Bodywork();
    bodywork.setMaterial("aluminum");
    bodywork.setType("sedan");
    vehicle.setBodywork(bodywork);
  }
}

redpajama_set_name: RedPajamaGithub


text:
package org.kuali.kra.lookup;

import org.apache.commons.lang.StringUtils;
import org.kuali.rice.kew.api.KewApiConstants;
import org.kuali.rice.kns.lookup.HtmlData;
import org.kuali.rice.kns.lookup.HtmlData.AnchorHtmlData;
import org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl;
import org.kuali.rice.kns.web.struts.form.LookupForm;
import org.kuali.rice.kns.web.ui.Field;
import org.kuali.rice.krad.bo.BusinessObject;
import org.kuali.rice.krad.document.Document;
import org.kuali.rice.krad.util.KRADConstants;
import org.kuali.rice.krad.util.ObjectUtils;
import org.kuali.rice.krad.util.UrlFactory;

import java.util.*;
import java.util.Map.Entry;

public abstract class KraLookupableHelperServiceImpl extends KualiLookupableHelperServiceImpl {

    private static final String COLUMN = ":";
    private static final String VIEW = "view";
    protected static final String MEDUSA = "medusa";
    /**
     * create 'edit' link
     * @see org.kuali.rice.kns.lookup.AbstractLookupableHelperServiceImpl#getCustomActionUrls(org.kuali.rice.krad.bo.BusinessObject, java.util.List)
     */
    @Override
    public List<HtmlData> getCustomActionUrls(BusinessObject businessObject, List pkNames) {
        List<HtmlData> htmlDataList = new ArrayList<HtmlData>();
        addEditHtmlData(htmlDataList, businessObject);
        return htmlDataList;

}

    /**
     * @param businessObject
     * @return
     */
    protected void addEditHtmlData(List<HtmlData> htmlDataList, BusinessObject businessObject) {
        Properties parameters = new Properties();
        parameters.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, KRADConstants.DOC_HANDLER_METHOD);
        parameters.put(KRADConstants.PARAMETER_COMMAND, KewApiConstants.INITIATE_COMMAND);
        parameters.put(KRADConstants.DOCUMENT_TYPE_NAME, getDocumentTypeName());
        parameters.put(getKeyFieldName(), ObjectUtils.getPropertyValue(businessObject, getKeyFieldName()).toString());
        String href = UrlFactory.parameterizeUrl("../"+getHtmlAction(), parameters);

        AnchorHtmlData anchorHtmlData = new AnchorHtmlData(href,
                KRADConstants.DOC_HANDLER_METHOD, KRADConstants.MAINTENANCE_EDIT_METHOD_TO_CALL);
        htmlDataList.add(anchorHtmlData);
    }

    protected AnchorHtmlData getViewLink(Document document) {
        AnchorHtmlData htmlData = new AnchorHtmlData();
        htmlData.setDisplayText(VIEW);
        Properties parameters = new Properties();
        parameters.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, KRADConstants.DOC_HANDLER_METHOD);
        parameters.put(KRADConstants.PARAMETER_COMMAND, KewApiConstants.DOCSEARCH_COMMAND);
        parameters.put(KRADConstants.DOCUMENT_TYPE_NAME, getDocumentTypeName());
        parameters.put("viewDocument", "true");
        parameters.put("docId", document.getDocumentNumber());
        String href  = UrlFactory.parameterizeUrl("../"+getHtmlAction(), parameters);

        htmlData.setHref(href);
        return htmlData;

    }

    protected AnchorHtmlData getMedusaLink(Document document, Boolean readOnly) {
        AnchorHtmlData htmlData = new AnchorHtmlData();
        htmlData.setDisplayText(MEDUSA);
        Properties parameters = new Properties();
        parameters.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, "medusa");
        parameters.put(KRADConstants.PARAMETER_COMMAND, KewApiConstants.DOCSEARCH_COMMAND);
        parameters.put(KRADConstants.DOCUMENT_TYPE_NAME, getDocumentTypeName());
        parameters.put("viewDocument", readOnly.toString());
        parameters.put("docId", document.getDocumentNumber());
        String href  = UrlFactory.parameterizeUrl("../"+getHtmlAction(), parameters);

        htmlData.setHref(href);
        return htmlData;
    }

    /**
     *
     * @param document
     * @param methodToCall method to call on action
     * @param linkName
     * @param readOnly whether the document should be readOnly or not
     * @return
     */
    protected AnchorHtmlData getCustomLink(Document document, String methodToCall, String linkName, Boolean readOnly) {
        AnchorHtmlData htmlData = new AnchorHtmlData();
        htmlData.setDisplayText(linkName);
        Properties parameters = new Properties();
        parameters.put(KRADConstants.DISPATCH_REQUEST_PARAMETER, methodToCall);
        parameters.put(KRADConstants.PARAMETER_COMMAND, KewApiConstants.DOCSEARCH_COMMAND);
        parameters.put(KRADConstants.DOCUMENT_TYPE_NAME, getDocumentTypeName());
        parameters.put("viewDocument", readOnly.toString());
        parameters.put("docId", document.getDocumentNumber());
        String href  = UrlFactory.parameterizeUrl("../"+getHtmlAction(), parameters);

        htmlData.setHref(href);
        return htmlData;
    }

    /**
     * To force to it to show action links, such as 'edit' if it is not 'lookup' to search of return value.
     * @see org.kuali.core.lookup.AbstractLookupableHelperServiceImpl#performLookup(org.kuali.core.web.struts.form.LookupForm, java.util.Collection, boolean)
     */
    @Override
    public Collection performLookup(LookupForm lookupForm, Collection resultTable, boolean bounded) {
        if (!lookupForm.isHideReturnLink()) {
            lookupForm.setSuppressActions(true);
        } else {
            lookupForm.setShowMaintenanceLinks(true);
        }
        return super.performLookup(lookupForm, resultTable, bounded);
    }

    /**
     *
     * This method to set both fields if child class override getSearchResults
     * @param fieldValues
     */
    protected void setBackLocationDocFormKey(Map<String, String> fieldValues) {

        //need to set backlocation & docformkey here.  Otherwise, they are empty
        for (Entry<String,String> entry : fieldValues.entrySet()) {
            if (entry.getKey().equals(KRADConstants.BACK_LOCATION)) {
                setBackLocation(entry.getValue());
            } else if (entry.getKey().equals(KRADConstants.DOC_FORM_KEY)) {
                setDocFormKey(entry.getValue());
            }
        }
    }

    /**
     *
     * This method is to set up field definitions for lookup fields.
     * These fields does not have a reference object defined for it in repository & bo.
     * @param field
     * @param keyName
     * @param className
     */
    protected void updateLookupField(Field field, String keyName, String className) {
        if (StringUtils.isNotBlank(keyName) && StringUtils.isNotBlank(className)) {
            field.setFieldConversions(keyName+COLUMN+field.getPropertyName());
            field.setLookupParameters(field.getPropertyName()+COLUMN+keyName);
            field.setInquiryParameters(field.getPropertyName()+COLUMN+keyName);
            field.setQuickFinderClassNameImpl(className);
        }

    }

    /**
     * htmlaction for 'edit' link
     */
    protected abstract String getHtmlAction();

    /**
     *
     * This method to set document type of the lookup bo's document
     * @return
     */
    protected abstract String getDocumentTypeName();

    /**
     *
     * This method is set the key field to retrieve bo for editing.  May change to  a map if there are multiple fields.
     * @return
     */
    protected abstract String getKeyFieldName();

}


text:
require 'cinch'
require 'cinch/tyrant/cmd'
require 'cinch/tyrant/simple-memory-cache'
require 'tyrant/factions'
require 'tyrant/levenshtein'
require 'tyrant/time'
require 'yaml'

module Cinch; module Plugins; class TyrantFaction
  include Cinch::Plugin

  def initialize(*args)
    super
    file = config[:yaml_file] || ''
    @factions = File.exist?(file) ? YAML.load_file(file) : {}
    @keys = @factions.keys
    @cache = Cinch::Tyrant::SimpleMemoryCache.new
    @failures = {}
  end

  match(/f(?:action)?(\s+-[a-z]+)?(?:\s+(.+))?$/i, method: :faction)
  match(/f(?:action)?id\s+(-[a-z]+\s+)?(\d+)/i, method: :faction_id)
  match(/link(?: (.*))?/i, method: :link)
  match(/update rankings/i, method: :update_rankings)
  match(/update conquest/i, method: :update_conquest)

  COMMANDS = [
    Cinch::Tyrant::Cmd.new('factions', 'faction', '[flags] [name]', true,
      'Looks up information of the named faction ' +
      '(default this channel\'s faction). ' +
      'Flags: -c: Conquest attacks, -f: Founder, -m: Message.'
    ),
    Cinch::Tyrant::Cmd.new('factions', 'link', '',
      lambda { |m| is_member?(m) },
      'Shows apply link of this channel\'s faction.'
    ),
    Cinch::Tyrant::Cmd.new('factions', 'link', '<faction>',
      lambda { |m| is_warmaster?(m) },
      'Shows apply link of the named faction.'
    ),
  ]

  def update_rankings(m)
    return if !m.user.master?

    tyrant = Tyrants.get(config[:checker])
    @factions = tyrant.update_rankings
    @keys = @factions.keys
  end

  def update_conquest(m)
    return if !m.user.master?

    tyrant = Tyrants.get(config[:checker])
    @factions = tyrant.update_conquest
    @keys = @factions.keys
  end

  def link(m, faction_name)
    # To trigger, must be me and PM, or in an allowed channel
    return if !m.channel && !m.user.master?
    return if m.channel && !is_member?(m)

    if faction_name
      if !m.channel || is_warmaster?(m)
        faction_id = @factions.get_first(faction_name)
        if !faction_id
          m.reply("#{faction_name} not found")
          return
        end
      else
        return
      end
    else
      faction_id = BOT_CHANNELS[m.channel.name].id
    end

    m.reply("http://www.kongregate.com/games/synapticon/tyrant?kv_apply=#{faction_id}")
  end

  def self.conquest_attacks(json)
    attacks = json['conquest_attacks'].to_i
    return '6/6 attacks' if attacks == 6
    # Far I can tell conquest_attack_recharge is actually time of last attack
    last_attack = json['conquest_attack_recharge'].to_i
    time_since_last = Time.now.to_i - last_attack
    next_charge = -time_since_last % (4 * ::Tyrant::Time::HOUR)
    "#{attacks}/6 attacks - next in #{::Tyrant::Time::format_time(next_charge)}"
  end

  def faction(m, verbose, faction_name)
    # To trigger, must be me and PM, or in an allowed channel
    return if !m.channel && !m.user.master?
    return if m.channel && !is_friend?(m)

    wait_times = config[:wait_time] || {}
    wait_time = wait_times[m.channel.name.downcase] || 900

    if m.user.signed_on_at.to_i + wait_time > Time.now.to_i
      m.reply('STAY A WHILE AND LISTEN! ' +
              'It is very rude to ask for info right after signing on.', true)
      return
    end

    last_fail = m.channel && @failures[m.channel.name]
    if faction_name && last_fail == faction_name && !m.user.master?
      m.reply('No, I really don\'t know about that faction, ' +
              'so please stop trying. It won\'t help.', true)
      return
    end

    # Remember, this quits your faction, so only run with dummy accounts
    tyrant = Tyrants.get(config[:checker])

    # If name provided, use it.
    # If not, use the faction associated with this channel, if any.
    if faction_name
      faction_id = @factions.get_first(faction_name)
    elsif m.channel && BOT_CHANNELS[m.channel.name]
      faction_id = BOT_CHANNELS[m.channel.name].id
    else
      return
    end

    if !faction_id
      @failures[m.channel.name] = faction_name if m.channel
      @keys.sort_by! { |x| Levenshtein.distance(x, faction_name.downcase) }
      suggestions = @keys.take(3).join(', ')
      best = @keys[0]
      m.reply("#{faction_name} not found! Did you mean: #{suggestions}?")
      faction_id = @factions.get_first(best)
      faction_name = best
    end

    json, _ = @cache.lookup(faction_id.to_i, 600, tolerate_exceptions: true) {
      tyrant.faction_data(faction_id)
    }
    if !json['result']
      m.reply('Failed to get info on "' + (faction_name || 'own faction') +
              '". This probably means they disbanded.')
    else
      send_faction_info(m, verbose, json)
    end
  end

  def faction_id(m, verbose, faction_id)
    # To trigger, must be me
    return if !m.user.master?

    # Remember, this quits your faction, so only run with dummy accounts
    tyrant = Tyrants.get(config[:checker])

    json, _ = @cache.lookup(faction_id.to_i, 600, tolerate_exceptions: true) {
      tyrant.faction_data(faction_id)
    }
    if !json['result']
      m.reply('Failed to look up "' + faction_id +
              '". This probably means they disbanded.')
    else
      send_faction_info(m, verbose, json)
    end
  end

  def send_faction_info(m, verbose, json)
    m.reply(::Tyrant.format_faction(json))
    expected_id = m.channel && BOT_CHANNELS[m.channel].id
    if verbose && (m.user.master? || json['faction_id'].to_i == expected_id)
      if verbose.include?('m')
        m.reply('Message: ' + (json['message'] || 'nil'))
      end
      if verbose.include?('f')
        id = json['creator_id']
        name = ::Tyrant::name_of_id(json['creator_id'])
        m.reply("Founder #{name}[#{id}]")
      end
      if verbose.include?('c')
        m.reply(self.class.conquest_attacks(json))
      end
    end
  end

end; end; end


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

    <bean id="propertiesConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"/>

    <bean id="space" class="org.openspaces.core.space.UrlSpaceFactoryBean">
        <property name="url" value="/./space"/>
        <property name="lookupGroups" value="${user.name}" />
    </bean>

    <bean id="space2" class="org.openspaces.core.space.UrlSpaceFactoryBean" depends-on="space">
        <property name="url" value="/./space2"/>
        <property name="lookupGroups" value="${user.name}" />
    </bean>

    <bean id="jbossTransactionManager"
          class="com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionManagerImple">
    </bean>

    <bean id="jbossUserTransaction"
          class="com.arjuna.ats.internal.jta.transaction.arjunacore.UserTransactionImple"/>

    <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
        <property name="transactionManager" ref="jbossTransactionManager"/>
        <property name="userTransaction" ref="jbossUserTransaction"/>
    </bean>

    <bean id="gigaSpace1" class="org.openspaces.core.GigaSpaceFactoryBean">
        <property name="space" ref="space"/>
        <property name="transactionManager" ref="transactionManager"/>
    </bean>

    <bean id="gigaSpace2" class="org.openspaces.core.GigaSpaceFactoryBean">
        <property name="space" ref="space2"/>
        <property name="transactionManager" ref="transactionManager"/>
    </bean>
</beans>


text:
#include <test/support/tdb_catch.h>
#include "../error_api_external.h"
#include "../error_api_internal.h"

TEST_CASE("C API: tiledb_error_message argument validation", "[capi][error]") {
  SECTION("null error") {
    const char* p;
    auto rc{tiledb_error_message(nullptr, &p)};
    CHECK(tiledb_status(rc) == TILEDB_ERR);
  }
  SECTION("null error message") {
    auto error{tiledb_error_handle_t::make_handle("foo")};
    REQUIRE(error != nullptr);
    auto rc{tiledb_error_message(error, nullptr)};
    CHECK(tiledb_status(rc) == TILEDB_ERR);
    tiledb_error_free(&error);
  }
}

TEST_CASE("C API: tiledb_error_free argument validation", "[capi][error]") {
  /*
   * `void` returns mean we have no return status to check
   */
  SECTION("null error") {
    CHECK_NOTHROW(tiledb_error_free(nullptr));
  }
  SECTION("non-null bad error") {
    tiledb_error_handle_t* error{nullptr};
    REQUIRE_NOTHROW(tiledb_error_free(&error));
  }
}


text:
@page guides/contributing/bug-report Reporting Bugs
@parent guides/contribute 1

@description Learn how to submit a bug report.

@body

CanJS uses [GitHub Issues](https://github.com/canjs/canjs/issues/new) to track bugs. However,
CanJS is made up of many individual GitHub repositories. Ideally, bugs are created within the
repository whose code is causing the issue.  For example, issues with
[can-define] can be created at [canjs/can-define/issues/new](https://github.com/canjs/can-define/issues/new).

If you do not know which repository your issue belongs to, that's totally ok!  Please
create your issue in the main
[canjs/canjs issues page](https://github.com/canjs/canjs/issues/new).  The core team will
move the issue to the correct repository if necessary.

When creating an issue, it's very helpful to include:

 - Small examples using tools like JS Bin. You can clone the following [CanJS bin](https://jsbin.com/losoceranu/1/edit?html,js,output) that includes everything in CanJS. Make
   sure it's pointing at the same version of CanJS you are using.
 - Breaking unit tests (optional). See [guides/contributing/code].
 - Proposed fix solutions (optional)

Also, please search for previous tickets.  If there's something similar, add to that, or
give it a 👍.

Finally, if there are any questions, reach out to
us on the [CanJS forums](http://forums.donejs.com/c/canjs) or talk to us on
the [Gitter canjs/canjs channel](https://gitter.im/canjs/canjs).


text:
// Copyright 2019 ThoughtWorks, Inc.

package util

import "errors"

func RLimit() (int, error) {
	// TODO: Run some system commands to figure out the max no. of open file descriptors.
	return -1, errors.New("Not implemented for windows")
}


text:
<?php

namespace App\Providers;

use Dotenv\Dotenv;
use Exception;
use Illuminate\Queue\Events\JobProcessing;
use Illuminate\Support\Env;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Queue;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        // Check for queue workers with outdated source code
        Queue::before(function (JobProcessing $event) {
            // Re/load .env file
            $app = app();
            Dotenv::create(
                Env::getRepository(),
                $app->environmentPath(),
                $app->environmentFile()
            )->safeLoad();

            if (Env::get('APP_HASH') !== config('projectconfig.hash')) {
                $errorMessage = 'Queue workers run with outdated source code!';
                Log::error($errorMessage);
                $event->job->fail(new Exception($errorMessage));
            }
        });
    }
}


text:
package com.code4a.jlibrarydemo.home.frag.home;

import android.os.Bundle;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.code4a.jlibrary.base.BaseFragment;
import com.code4a.jlibrarydemo.R;
import com.code4a.jlibrarydemo.home.frag.home.four.FourFragment;
import com.code4a.jlibrarydemo.home.frag.home.one.OneFragment;
import com.code4a.jlibrarydemo.home.frag.home.three.ThreeFragment;
import com.code4a.jlibrarydemo.home.frag.home.two.TwoFragment;
import com.code4a.jlibrarydemo.widget.ViewPagerIndicator;

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

import butterknife.BindView;

public class HomeFragment extends BaseFragment {

    @BindView(R.id.vp)
    ViewPager viewPager;
    @BindView(R.id.indicator)
    ViewPagerIndicator indicator;

    private FragmentPagerAdapter mAdapter;
    private List<BaseFragment> mList;
    private List<String> mDatas;
    private final int itemCount = 4;

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        mList = new ArrayList<>(itemCount);
        mList.add(OneFragment.getInstance());
        mList.add(TwoFragment.getInstance());
        mList.add(ThreeFragment.getInstance());
        mList.add(FourFragment.getInstance());
        mDatas = new ArrayList<>(itemCount);
        for (int i = 0; i < itemCount; i++) {
            mDatas.add("i=" + i);
        }

        mAdapter = new FragmentPagerAdapter(getFragmentManager()) {
            @Override
            public BaseFragment getItem(int position) {
                return mList.get(position);
            }

            @Override
            public int getCount() {
                return mList.size();
            }
        };

        viewPager.setAdapter(mAdapter);
        //将viewpager与indicator绑定
        indicator.setDatas(mDatas);
        indicator.setViewPager(viewPager);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home;
    }
}


text:
import feedparser
from pathlib import Path
import webbrowser
import gevent
import geventhttpclient

from geventhttpclient import HTTPClient
from geventhttpclient.url import URL

def request(method, url, level=0):
    url = URL(url)
    http = HTTPClient.from_url(url, concurrency=10)
    response = getattr(http, method)(url.request_uri)
    if response.status_code in (301, 302) and level < 3:
        [loc] = [v for n, v in response.headers if n.lower() == 'location']
        return request(method, loc, level+1)

    result = response.status_code, response.read()
    http.close()
    return result

def latest_comic(rss_feed):
    try:
        status, body = request('get', rss_feed)
    except Exception as e:
        print(e)
        print("BAD URL %s" % e)
        return

    if status != 200:
        print('BAD STATUS', status, rss_feed)
        return
    rss = feedparser.parse(body)
    entries = rss.get('entries')
    if entries:
        guid = entries[0].get('guid', "")
        if guid.startswith("http"):
            return guid

def latest_good_comic(rss_feed):
    latest = latest_comic(rss_feed)
    try:
        if request('head', latest)[0] < 400:
            return latest
    except Exception:
        pass

def open_list(rss_list):
    jobs = [gevent.spawn(latest_good_comic, rss_url) for rss_url in rss_list]
    gevent.joinall(jobs)
    print([job.value for job in jobs])

if __name__ == '__main__':
    from time import time
    t0 = time()
    rss_list = (Path.cwd() / "good.txt").read_text().splitlines()
    open_list(rss_list[:20])
    print('Elapsed time:', time() - t0)


text:
"""
The SFTP server that acts as a reverse proxy
"""
import base64, os, fcntl, tty, struct

from twisted.enterprise import adbapi

from twisted.cred import portal, checkers, credentials, error
from twisted.conch import error, avatar
from twisted.conch.unix import SSHSessionForUnixConchUser,UnixConchUser, SFTPServerForUnixConchUser, UnixSFTPDirectory, UnixSFTPFile
from twisted.conch.checkers import SSHPublicKeyDatabase
from twisted.conch.ssh import factory, userauth, connection, keys, session
from twisted.internet import reactor, protocol, defer, task
from twisted.internet.defer import inlineCallbacks
from twisted.python import log, logfile
import logging
from zope.interface import implements
from twisted.python import components, failure
from twisted.conch.ssh import session, forwarding, filetransfer
from pysftpproxy.client import SFTPServerProxyClient
from twisted.internet.protocol import Protocol, ReconnectingClientFactory
from twisted.conch.ls import lsLine
from pysftpproxy.levfilelogger import LevelFileLogObserver
from pysftpproxy.storageredis import StorageRedis

import sys

publicKeyPath  = os.environ.get("SFTPPROXY_PUBLICKEY_PATH",
    "/home/rauburtin/.ssh/id_rsa.pub")
publicKey = open(publicKeyPath,"r").read()
if publicKey.endswith("\n"):
    publicKey = publicKey[:-1]

privateKeyPath = os.environ.get("SFTPPROXY_PRIVATEKEY_PATH",
    "/home/rauburtin/.ssh/id_rsa")
privateKey = open(privateKeyPath,"r").read()
if privateKey.endswith("\n"):
    privateKey = privateKey[:-1]

class PublicKeyCredentialsChecker:
    implements(checkers.ICredentialsChecker)
    #Only chek publick keys used by client
    credentialInterfaces = (credentials.ISSHPrivateKey,)

    def requestAvatarId(self, credentials):
        # check http://wiki.velannes.com/doku.php?id=python:programmes:twisted_ssh_server
        publickey = base64.b64encode(credentials.blob)
        log.msg("Client publickey:%s" % (publickey), logLevel=logging.DEBUG)
        log.msg("Client username %s" % (credentials.username), logLevel=logging.DEBUG)

        sredis = StorageRedis()
        username = sredis.get_username(publickey)

        log.msg("username from redis",username, logLevel=logging.DEBUG)

        if username == credentials.username:
	    return defer.succeed(credentials.username)
        else:
            return defer.fail(error.UnauthorizedLogin(
                "invalid pubkey for username: %s" % (credentials.username)))

class ProxySSHUser(avatar.ConchUser):

    def __init__(self, username):
        avatar.ConchUser.__init__(self)
        self.username = username
        self.otherGroups = []

        self.channelLookup.update({'session':session.SSHSession})
        self.subsystemLookup['sftp'] = filetransfer.FileTransferServer
        #here we can create the client
        #need to pass the remote ssh server ip and port
        log.msg("Start SFTPServerProxyClient", logLevel=logging.DEBUG)
        sredis = StorageRedis()
        self.userinfo = sredis.get_userinfo(username)

        log.msg("userinfo from redis",self.userinfo, logLevel=logging.DEBUG)

        self.proxyclient = SFTPServerProxyClient(
                remote=self.userinfo['remote'],
                port=int(self.userinfo['port']),
                user=self.userinfo['user'])

    def getUserGroupId(self):
        userid = int(os.environ.get("SFTPPROXY_USERID",1000))
        groupid = int(os.environ.get("SFTPPROXY_GROUPID",1000))
        return userid,groupid

    def getHomeDir(self):
        return self.userinfo.get('home', os.environ.get("SFTPPROXY_HOME",'/home/rauburtin'))

    def getOtherGroups(self):
        return self.otherGroups

    def _runAsUser(self, f, *args, **kw):
        euid = os.geteuid()
        egid = os.getegid()
        groups = os.getgroups()
        uid, gid = self.getUserGroupId()
        os.setegid(0)
        os.seteuid(0)
        os.setgroups(self.getOtherGroups())
        os.setegid(gid)
        os.seteuid(uid)
        try:
            f = iter(f)
        except TypeError:
            f = [(f, args, kw)]
        try:
            for i in f:
                func = i[0]
                args = len(i) > 1 and i[1] or ()
                kw = len(i) > 2 and i[2] or {}
                r = func(*args, **kw)
        finally:
            os.setegid(0)
            os.seteuid(0)
            os.setgroups(groups)
            os.setegid(egid)
            os.seteuid(euid)
        return r

    def logout(self):
        # Remove all listeners.
        log.msg("logout", logLevel=logging.DEBUG )
        for listener in self.listeners.itervalues():
            self._runAsUser(listener.stopListening)
        log.msg(
            'avatar %s logging out (%i)'
            % (self.username, len(self.listeners)), logLevel=logging.DEBUG)

class ProxySSHRealm:
    implements(portal.IRealm)

    def requestAvatar(self, avatarId, mind, *interfaces):
        return interfaces[0], ProxySSHUser(avatarId), lambda: None

class ProxySFTPSession(SFTPServerForUnixConchUser):

    def gotVersion(self, otherVersion, extData):
        return {}

    def openFile(self, filename, flags, attrs):
        log.msg("openFile filename:%s flags:%s attrs:%s" % (filename, flags, attrs), logLevel=logging.DEBUG)
        return self.avatar.proxyclient.client.openFile(filename, flags, attrs)

    def removeFile(self, filename):
        log.msg("removeFile filename:%s" % (filename), logLevel=logging.DEBUG)
        return self.avatar.proxyclient.client.removeFile(filename)

    def renameFile(self, oldpath, newpath):
        log.msg("renameFile oldpath:%s newpath:%s" % (oldpath, newpath), logLevel=logging.DEBUG)
        return self.avatar.proxyclient.client.renameFile(oldpath, newpath)

    def makeDirectory(self, path, attrs):
        log.msg("makeDirectory path:%s attrs:%s" % (path, attrs), logLevel=logging.DEBUG)
        return self.avatar.proxyclient.client.makeDirectory(path, attrs)

    def removeDirectory(self, path):
        log.msg("removeDirectory path:%s" % (path), logLevel=logging.DEBUG)
        return self.avatar.proxyclient.client.removeDirectory(path)

    def openDirectory(self, path):
        log.msg("openDirectory path:%s" % (path), logLevel=logging.DEBUG)
        files = []
        def _getFiles(openDir):
            def append(f):
                files.append(f)
                return openDir
            d = defer.maybeDeferred(openDir.next)
            d.addCallback(append)
            d.addCallback(_getFiles)
            d.addErrback(_close, openDir)
            return d

        def _close(_, openDir):
            d = openDir.close()
            return d
        def _setFiles(d,proxy):
            proxy.set_files(files)
            return d
        def _openDirectory(_,path):
           d = self.avatar.proxyclient.client.openDirectory(path)
           return d

        #not so bad, with _openDirectory(path)
        #d =  task.deferLater(reactor,1,_openDirectory,path)
        d = self.avatar.proxyclient.dcli
        d.addCallback(_openDirectory,path)
        d.addCallback(_getFiles)
        d.addCallback(ProxySFTPDirectory,files)

        return d

    def getAttrs(self, path, followLinks):
        log.msg("getAttrs path:%s followLinks:%s" % (path, followLinks), logLevel=logging.DEBUG)
        return self.avatar.proxyclient.client.getAttrs(path, followLinks)

    def setAttrs(self, path, attrs):
        log.msg("setAttrs path:%s attrs:%s" % (path, attrs), logLevel=logging.DEBUG)
        return self.avatar.proxyclient.client.setAttrs(path, attrs)

    def readLink(self, path):
        log.msg("readLink path:%s" % (path), logLevel=logging.DEBUG)
        return self.avatar.proxyclient.client.readLink(path)

    def makeLink(self, linkPath, targetPath):
        log.msg("makeLink linkPath:%s targetPath:%s" % (linkPath, targetPath), logLevel=logging.DEBUG)
        return self.avatar.proxyclient.client.makeLink(linkPath, targetPath)

    def realPath(self, path):
        if not hasattr(self.avatar.proxyclient, "client"):
            return os.path.realpath(self._absPath(path))
        else:
            log.msg("realPath path:%s" % (path), logLevel=logging.DEBUG)
            return self.avatar.proxyclient.client.realPath(path)

    def extendedRequest(self, extName, extData):
        raise NotImplementedError

class ProxySFTPDirectory:
    def __init__(self,d,files):
        self.files=files
    def set_files(self,files):
        self.files=files
    def __iter__(self):
        return self
    def next(self):
        try:
            f = self.files.pop(0)
            return (f[0],f[1],f[2])
        except IndexError:
            raise StopIteration
    def close(self):
        pass

class ProxySSHFactory(factory.SSHFactory):

    publicKeys  = { 'ssh-rsa': keys.Key.fromString(data=publicKey)   }
    privateKeys = { 'ssh-rsa': keys.Key.fromString(data=privateKey) }
    services = {
        'ssh-userauth': userauth.SSHUserAuthServer,
        'ssh-connection': connection.SSHConnection
    }

class ProxySFTPServer(object):
    def __init__(self, filelog=None, dirlog="../log/", rotateLengthMB=1,
            maxRotatedFiles=100):
        f = sys.stdout
        if filelog:
            if not os.path.isdir(dirlog):
                os.makedirs(dirlog)
            f  = logfile.LogFile(filelog, dirlog,
                    rotateLength=rotateLengthMB*1000000,
                    maxRotatedFiles=maxRotatedFiles)

        self.logger = LevelFileLogObserver(f, logging.DEBUG)

    def run(self):

        global portal

        #Start Logging
        log.addObserver(self.logger.emit)
        log.msg("Logging started", logLevel=logging.DEBUG)

        portal = portal.Portal(ProxySSHRealm())

        components.registerAdapter(ProxySFTPSession, ProxySSHUser, filetransfer.ISFTPServer)
        portal.registerChecker(PublicKeyCredentialsChecker())
        ProxySSHFactory.portal = portal

        reactor.listenTCP(int(os.environ.get("SFTPPROXY_PORT",5022)), ProxySSHFactory())
        reactor.run()

if __name__ == '__main__':
    proxyserver = ProxySFTPServer()
    proxyserver.run()


text:
package dfstudio.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author msgile
 * @author $LastChangedBy$
 * @version $Revision$  $LastChangedDate$
 * @since 3/5/15
 */
public class FileInputStreamSource implements InputStreamSource {
  public final File file;

  public FileInputStreamSource(File file) {
    this.file = file;
  }

  @Override
  public InputStream open() throws IOException {
    return new FileInputStream(file);
  }
}


text:
require File.expand_path('../../test_helper', File.dirname(__FILE__))

class FieldIntegerTagTest < ActiveSupport::TestCase

  def test_initialize_tag
    assert tag = ComfortableMexicanSofa::Tag::FieldInteger.initialize_tag(
      cms_pages(:default), '{{ cms:field:content:integer }}'
    )
    assert_equal 'content', tag.identifier
    assert_nil tag.namespace
    assert tag = ComfortableMexicanSofa::Tag::FieldInteger.initialize_tag(
      cms_pages(:default), '{{cms:field:content:integer}}'
    )
    assert_equal 'content', tag.identifier
    assert tag = ComfortableMexicanSofa::Tag::FieldInteger.initialize_tag(
      cms_pages(:default), '{{cms:field:dash-content:integer}}'
    )
    assert_equal 'dash-content', tag.identifier
    assert tag = ComfortableMexicanSofa::Tag::FieldInteger.initialize_tag(
      cms_pages(:default), '{{cms:field:namespace.content:integer}}'
    )
    assert_equal 'namespace.content', tag.identifier
    assert_equal 'namespace', tag.namespace
  end

  def test_initialize_tag_failure
    [
      '{{cms:field:content:not_integer}}',
      '{{cms:field:content}}',
      '{{cms:not_field:content}}',
      '{not_a_tag}'
    ].each do |tag_signature|
      assert_nil ComfortableMexicanSofa::Tag::FieldInteger.initialize_tag(
        cms_pages(:default), tag_signature
      )
    end
  end

  def test_content_and_render
    tag = ComfortableMexicanSofa::Tag::FieldInteger.initialize_tag(
      cms_pages(:default), '{{cms:field:content:integer}}'
    )
    assert tag.block.content.blank?
    tag.block.content = '5'
    assert_equal '5', tag.content
    assert_equal '', tag.render
  end

end


text:
package options

import (
	"github.com/spf13/cobra"
)

// GenerateOptions is the top level wrapper for the generate command.
type GenerateOptions struct {
	AnnotationOptions
	Registry RegistryOptions
}

var _ Interface = (*GenerateOptions)(nil)

// AddFlags implements Interface
func (o *GenerateOptions) AddFlags(cmd *cobra.Command) {
	o.AnnotationOptions.AddFlags(cmd)
	o.Registry.AddFlags(cmd)
}


text:
import { css } from 'lit';

export default css`
  .form-control .form-control__label {
    display: none;
  }

  .form-control .form-control__help-text {
    display: none;
  }

  /* Label */
  .form-control--has-label .form-control__label {
    display: inline-block;
    color: var(--sl-input-label-color);
    margin-bottom: var(--sl-spacing-3x-small);
  }

  .form-control--has-label.form-control--small .form-control__label {
    font-size: var(--sl-input-label-font-size-small);
  }

  .form-control--has-label.form-control--medium .form-control__label {
    font-size: var(--sl-input-label-font-size-medium);
  }

  .form-control--has-label.form-control--large .form-control_label {
    font-size: var(--sl-input-label-font-size-large);
  }

  :host([required]) .form-control--has-label .form-control__label::after {
    content: var(--sl-input-required-content);
    margin-inline-start: var(--sl-input-required-content-offset);
    color: var(--sl-input-required-content-color);
  }

  /* Help text */
  .form-control--has-help-text .form-control__help-text {
    display: block;
    color: var(--sl-input-help-text-color);
  }

  .form-control--has-help-text .form-control__help-text ::slotted(*) {
    margin-top: var(--sl-spacing-3x-small);
  }

  .form-control--has-help-text.form-control--small .form-control__help-text {
    font-size: var(--sl-input-help-text-font-size-small);
  }

  .form-control--has-help-text.form-control--medium .form-control__help-text {
    font-size: var(--sl-input-help-text-font-size-medium);
  }

  .form-control--has-help-text.form-control--large .form-control__help-text {
    font-size: var(--sl-input-help-text-font-size-large);
  }

  .form-control--has-help-text.form-control--radio-group .form-control__help-text {
    margin-top: var(--sl-spacing-2x-small);
  }
`;


text:
module PulseMeter
  module Sensor
    module Timelined
      # Calculates n'th percentile in interval
      class Percentile < Timeline
        attr_reader :p_value

        def initialize(name, options)
          @p_value = assert_ranged_float!(options, :p, 0, 1)
          super(name, options)
        end

        def aggregate_event(key, value)
          redis.zadd(key, value, "#{value}::#{uniqid}")
        end

        def summarize(key)
          count = redis.zcard(key)
          if count > 0
            position = @p_value > 0 ? (@p_value * count).round - 1 : 0
            el = redis.zrange(key, position, position)[0]
            redis.zscore(key, el).to_f
          else
            nil
          end
        end

      end
    end
  end
end


text:
package commands.arithmeticCommands;

import java.util.ArrayList;

public class Greater extends Compare{

	public Greater(ArrayList<Object> p) {
		super.setParams(p);
		params = p;
	}

	public double execute() {
		double a = (Double) params.get(params.size() - 2);
		double b = (Double) params.get(params.size() - 1);
		if(a > b)
			return 1;
		return 0;
	}

}


text:
package is.hail.expr.ir.lowering

import is.hail.annotations.{Annotation, ExtendedOrdering, Region, SafeRow}
import is.hail.asm4s.{AsmFunction1RegionLong, LongInfo, classInfo}
import is.hail.backend.ExecuteContext
import is.hail.expr.ir._
import is.hail.expr.ir.functions.{ArrayFunctions, IRRandomness, UtilFunctions}
import is.hail.io.{BufferSpec, TypedCodecSpec}
import is.hail.rvd.RVDPartitioner
import is.hail.types.{RStruct, tcoerce}
import is.hail.types.physical.stypes.PTypeReferenceSingleCodeType
import is.hail.types.physical.{PArray, PStruct}
import is.hail.types.virtual._
import is.hail.utils._
import org.apache.spark.sql.Row

import scala.collection.mutable.ArrayBuffer

object LowerDistributedSort {
  def localSort(ctx: ExecuteContext, stage: TableStage, sortFields: IndexedSeq[SortField], relationalLetsAbove: Map[String, IR]): TableStage = {
    val numPartitions = stage.partitioner.numPartitions
    val collected = stage.collectWithGlobals(relationalLetsAbove, "shuffle_local_sort")

    val (Some(PTypeReferenceSingleCodeType(resultPType: PStruct)), f) = ctx.timer.time("LowerDistributedSort.localSort.compile")(Compile[AsmFunction1RegionLong](ctx,
      FastIndexedSeq(),
      FastIndexedSeq(classInfo[Region]), LongInfo,
      collected,
      print = None,
      optimize = true))

    val fRunnable = ctx.timer.time("LowerDistributedSort.localSort.initialize")(f(ctx.theHailClassLoader, ctx.fs, 0, ctx.r))
    val resultAddress = ctx.timer.time("LowerDistributedSort.localSort.run")(fRunnable(ctx.r))
    val rowsAndGlobal = ctx.timer.time("LowerDistributedSort.localSort.toJavaObject")(SafeRow.read(resultPType, resultAddress)).asInstanceOf[Row]

    val rowsType = resultPType.fieldType("rows").asInstanceOf[PArray]
    val rowType = rowsType.elementType.asInstanceOf[PStruct]
    val rows = rowsAndGlobal.getAs[IndexedSeq[Annotation]](0)
    val kType = TStruct(sortFields.map(f => (f.field, rowType.virtualType.fieldType(f.field))): _*)

    val sortedRows = localAnnotationSort(ctx, rows, sortFields, rowType.virtualType)

    val nPartitionsAdj = math.max(math.min(sortedRows.length, numPartitions), 1)
    val itemsPerPartition = (sortedRows.length.toDouble / nPartitionsAdj).ceil.toInt

    if (itemsPerPartition == 0)
      return TableStage(
        globals = Literal(resultPType.fieldType("global").virtualType, rowsAndGlobal.get(1)),
        partitioner = RVDPartitioner.empty(kType),
        TableStageDependency.none,
        MakeStream(FastSeq(), TStream(TStruct())),
        _ => MakeStream(FastSeq(), TStream(stage.rowType))
      )

    // partitioner needs keys to be ascending
    val partitionerKeyType = TStruct(sortFields.takeWhile(_.sortOrder == Ascending).map(f => (f.field, rowType.virtualType.fieldType(f.field))): _*)
    val partitionerKeyIndex = partitionerKeyType.fieldNames.map(f => rowType.fieldIdx(f))

    val partitioner = new RVDPartitioner(partitionerKeyType,
      sortedRows.grouped(itemsPerPartition).map { group =>
        val first = group.head.asInstanceOf[Row].select(partitionerKeyIndex)
        val last = group.last.asInstanceOf[Row].select(partitionerKeyIndex)
        Interval(first, last, includesStart = true, includesEnd = true)
      }.toIndexedSeq)

    TableStage(
      globals = Literal(resultPType.fieldType("global").virtualType, rowsAndGlobal.get(1)),
      partitioner = partitioner,
      TableStageDependency.none,
      contexts = mapIR(
        StreamGrouped(
          ToStream(Literal(rowsType.virtualType, sortedRows)),
          I32(itemsPerPartition))
        )(ToArray(_)),
      ctxRef => ToStream(ctxRef))
  }

  private def localAnnotationSort(
    ctx: ExecuteContext,
    annotations: IndexedSeq[Annotation],
    sortFields: IndexedSeq[SortField],
    rowType: TStruct
  ): IndexedSeq[Annotation] = {
    val sortColIndexOrd = sortFields.map { case SortField(n, so) =>
      val i = rowType.fieldIdx(n)
      val f = rowType.fields(i)
      val fo = f.typ.ordering
      if (so == Ascending) fo else fo.reverse
    }.toArray

    val ord: Ordering[Annotation] = ExtendedOrdering.rowOrdering(sortColIndexOrd).toOrdering

    val kType = TStruct(sortFields.map(f => (f.field, rowType.fieldType(f.field))): _*)
    val kIndex = kType.fieldNames.map(f => rowType.fieldIdx(f))
    ctx.timer.time("LowerDistributedSort.localSort.sort")(annotations.sortBy{ a: Annotation =>
      a.asInstanceOf[Row].select(kIndex).asInstanceOf[Annotation]
    }(ord))
  }

  def distributedSort(
    ctx: ExecuteContext,
    inputStage: TableStage,
    sortFields: IndexedSeq[SortField],
    relationalLetsAbove: Map[String, IR],
    rowTypeRequiredness: RStruct,
    optTargetNumPartitions: Option[Int] = None
  ): TableStage = {

    val oversamplingNum = 3
    val seed = 7L
    val maxBranchingFactor = ctx.getFlag("shuffle_max_branch_factor").toInt
    val defaultBranchingFactor = if (inputStage.numPartitions < maxBranchingFactor) {
      Math.max(2, inputStage.numPartitions)
    } else {
      maxBranchingFactor
    }

    val sizeCutoff = ctx.getFlag("shuffle_cutoff_to_local_sort").toInt

    val (keyToSortBy, _) = inputStage.rowType.select(sortFields.map(sf => sf.field))

    val spec = TypedCodecSpec(rowTypeRequiredness.canonicalPType(inputStage.rowType), BufferSpec.wireSpec)
    val reader = PartitionNativeReader(spec, "__dummy_uid")
    val initialTmpPath = ctx.createTmpPath("hail_shuffle_temp_initial")
    val writer = PartitionNativeWriter(spec, keyToSortBy.fieldNames, initialTmpPath, None, None, trackTotalBytes = true)

    log.info("DISTRIBUTED SORT: PHASE 1: WRITE DATA")
    val initialStageDataRow = CompileAndEvaluate[Annotation](ctx, inputStage.mapCollectWithGlobals(relationalLetsAbove, "shuffle_initial_write") { part =>
      WritePartition(part, UUID4(), writer)
    }{ case (part, globals) =>
      val streamElement = Ref(genUID(), part.typ.asInstanceOf[TArray].elementType)
      bindIR(StreamAgg(ToStream(part), streamElement.name,
        MakeStruct(FastSeq(
          "min" -> AggFold.min(GetField(streamElement, "firstKey"), sortFields),
          "max" -> AggFold.max(GetField(streamElement, "lastKey"), sortFields)
        ))
      )) { intervalRange => MakeTuple.ordered(Seq(part, globals, intervalRange)) }
    }).asInstanceOf[Row]
    val (initialPartInfo, initialGlobals, intervalRange) = (initialStageDataRow(0).asInstanceOf[IndexedSeq[Row]], initialStageDataRow(1).asInstanceOf[Row], initialStageDataRow(2).asInstanceOf[Row])
    val initialGlobalsLiteral = Literal(inputStage.globalType, initialGlobals)
    val initialChunks = initialPartInfo.map(row => Chunk(initialTmpPath + row(0).asInstanceOf[String], row(1).asInstanceOf[Long], row.getLong(5)))

    val initialInterval = Interval(intervalRange(0), intervalRange(1), true, true)
    val initialSegment = SegmentResult(IndexedSeq(0), initialInterval, initialChunks)

    val totalNumberOfRows = initialChunks.map(_.size).sum
    optTargetNumPartitions.foreach(i => assert(i >= 1, s"Must request positive number of partitions. Requested ${i}"))
    val targetNumPartitions = optTargetNumPartitions.getOrElse(inputStage.numPartitions)

    val idealNumberOfRowsPerPart: Long = if (targetNumPartitions == 0) 1 else {
      Math.max(1L, totalNumberOfRows / targetNumPartitions)
    }

    var loopState = LoopState(IndexedSeq(initialSegment), IndexedSeq.empty[SegmentResult], IndexedSeq.empty[OutputPartition])

    var i = 0
    val rand = new IRRandomness(seed)

    /*
    Loop state keeps track of three things. largeSegments are too big to sort locally so have to broken up.
    smallSegments are small enough to be sorted locally. readyOutputParts are any partitions that we noticed were
    sorted already during course of the recursion. Loop continues until there are no largeSegments left. Then we
    sort the small segments and combine them with readyOutputParts to get the final table.
     */

    while (!loopState.largeSegments.isEmpty) {
      val partitionDataPerSegment = segmentsToPartitionData(loopState.largeSegments, idealNumberOfRowsPerPart)
      assert(partitionDataPerSegment.size == loopState.largeSegments.size)

      val numSamplesPerPartitionPerSegment = partitionDataPerSegment.map { partData =>
        val partitionCountsForOneSegment = partData.map(_.currentPartSize)
        val recordsInSegment = partitionCountsForOneSegment.sum
        val branchingFactor = math.min(recordsInSegment, defaultBranchingFactor)
        howManySamplesPerPartition(rand, recordsInSegment, Math.min(recordsInSegment, (branchingFactor * oversamplingNum) - 1).toInt, partitionCountsForOneSegment)
      }

      val numSamplesPerPartition = numSamplesPerPartitionPerSegment.flatten

      val perPartStatsCDAContextData = partitionDataPerSegment.flatten.zip(numSamplesPerPartition).map { case (partData, numSamples) =>
        Row(partData.indices.last, partData.files, coerceToInt(partData.currentPartSize), numSamples, partData.currentPartByteSize)
      }
      val perPartStatsCDAContexts = ToStream(Literal(TArray(TStruct(
        "segmentIdx" -> TInt32,
        "files" -> TArray(TString),
        "sizeOfPartition" -> TInt32,
        "numSamples" -> TInt32,
        "byteSize" -> TInt64)), perPartStatsCDAContextData))
      val perPartStatsIR = cdaIR(perPartStatsCDAContexts, MakeStruct(Seq()), s"shuffle_part_stats_iteration_$i"){ (ctxRef, _) =>
        val filenames = GetField(ctxRef, "files")
        val samples = SeqSample(GetField(ctxRef, "sizeOfPartition"), GetField(ctxRef, "numSamples"), NA(TRNGState), false)
        val partitionStream = flatMapIR(ToStream(filenames)) { fileName =>
          mapIR(
            ReadPartition(
              MakeStruct(Array("partitionIndex" -> I64(0), "partitionPath" -> fileName)),
              tcoerce[TStruct](spec._vType), reader)
          ) { partitionElement =>
            SelectFields(partitionElement, keyToSortBy.fields.map(_.name))
          }
        }
        MakeStruct(IndexedSeq(
          "segmentIdx" -> GetField(ctxRef, "segmentIdx"),
          "byteSize" -> GetField(ctxRef, "byteSize"),
          "partData" -> samplePartition(partitionStream, samples, sortFields)))
      }

      /*
      Aggregate over the segments, to compute the pivots, whether it's already sorted, and what key interval is contained in that segment.
      Also get the min and max of each individual partition. That way if it's sorted already, we know the partitioning to use.
       */
      val pivotsPerSegmentAndSortedCheck = ToArray(bindIR(perPartStatsIR) { perPartStats =>
        mapIR(StreamGroupByKey(ToStream(perPartStats), IndexedSeq("segmentIdx"), missingEqual = true)) { oneGroup =>
          val streamElementRef = Ref(genUID(), oneGroup.typ.asInstanceOf[TIterable].elementType)
          val dataRef = Ref(genUID(), streamElementRef.typ.asInstanceOf[TStruct].fieldType("partData"))
          val sizeRef = Ref(genUID(), streamElementRef.typ.asInstanceOf[TStruct].fieldType("byteSize"))
          bindIR(StreamAgg(oneGroup, streamElementRef.name, {
            AggLet(dataRef.name, GetField(streamElementRef, "partData"), AggLet(sizeRef.name, GetField(streamElementRef, "byteSize"),
              MakeStruct(Seq(
                ("byteSize", ApplyAggOp(Sum())(sizeRef)),
                ("min", AggFold.min(GetField(dataRef, "min"), sortFields)), // Min of the mins
                ("max", AggFold.max(GetField(dataRef, "max"), sortFields)), // Max of the maxes
                ("perPartMins", ApplyAggOp(Collect())(GetField(dataRef, "min"))), // All the mins
                ("perPartMaxes", ApplyAggOp(Collect())(GetField(dataRef, "max"))), // All the maxes
                ("samples", ApplyAggOp(Collect())(GetField(dataRef, "samples"))),
                ("eachPartSorted", AggFold.all(GetField(dataRef, "isSorted"))),
                ("perPartIntervalTuples", ApplyAggOp(Collect())(MakeTuple.ordered(Seq(GetField(dataRef, "min"), GetField(dataRef, "max")))))
              )), false), false)
          })) { aggResults =>
            val sortedOversampling = sortIR(flatMapIR(ToStream(GetField(aggResults, "samples"))) { onePartCollectedArray => ToStream(onePartCollectedArray)}) { case (left, right) =>
              ApplyComparisonOp(StructLT(keyToSortBy, sortFields), left, right)
            }
            val minArray = MakeArray(GetField(aggResults, "min"))
            val maxArray = MakeArray(GetField(aggResults, "max"))
            val tuplesInSortedOrder = tuplesAreSorted(GetField(aggResults, "perPartIntervalTuples"), sortFields)
            bindIR(sortedOversampling) { sortedOversampling =>
              bindIR(ArrayLen(sortedOversampling)) { numSamples =>
                val sortedSampling = bindIR(
                  /* calculate a 'good' branch factor based on part sizes */
                  UtilFunctions.intMax(I32(2),
                    UtilFunctions.intMin(
                      UtilFunctions.intMin(numSamples, I32(defaultBranchingFactor)),
                      (I64(2L) * (GetField(aggResults, "byteSize").floorDiv(I64(sizeCutoff)))).toI))
                ) { branchingFactor =>
                  ToArray(mapIR(StreamRange(I32(1), branchingFactor, I32(1))) { idx =>
                    If(ArrayLen(sortedOversampling) ceq 0,
                      Die(strConcat("aggresults=", aggResults, ", idx=", idx, ", sortedOversampling=", sortedOversampling, ", numSamples=", numSamples, ", branchingFactor=", branchingFactor), sortedOversampling.typ.asInstanceOf[TArray].elementType, -1),
                      ArrayRef(sortedOversampling, Apply("floor", Seq(), IndexedSeq(idx.toD * ((numSamples + 1) / branchingFactor)), TFloat64, ErrorIDs.NO_ERROR).toI - 1))

                  })
                }
                MakeStruct(Seq(
                  "pivotsWithEndpoints" -> ArrayFunctions.extend(ArrayFunctions.extend(minArray, sortedSampling), maxArray),
                  "isSorted" -> ApplySpecial("land", Seq.empty[Type], Seq(GetField(aggResults, "eachPartSorted"), tuplesInSortedOrder), TBoolean, ErrorIDs.NO_ERROR),
                  "intervalTuple" -> MakeTuple.ordered(Seq(GetField(aggResults, "min"), GetField(aggResults, "max"))),
                  "perPartMins" -> GetField(aggResults, "perPartMins"),
                  "perPartMaxes" -> GetField(aggResults, "perPartMaxes")
                ))
              }
            }
          }
        }
      })

      log.info(s"DISTRIBUTED SORT: PHASE ${i+1}: STAGE 1: SAMPLE VALUES FROM PARTITIONS")
      // Going to check now if it's fully sorted, as well as collect and sort all the samples.
      val pivotsWithEndpointsAndInfoGroupedBySegmentNumber = CompileAndEvaluate[Annotation](ctx, pivotsPerSegmentAndSortedCheck)
        .asInstanceOf[IndexedSeq[Row]].map(x => (x(0).asInstanceOf[IndexedSeq[Row]], x(1).asInstanceOf[Boolean], x(2).asInstanceOf[Row], x(3).asInstanceOf[IndexedSeq[Row]], x(4).asInstanceOf[IndexedSeq[Row]]))

      val pivotCounts = pivotsWithEndpointsAndInfoGroupedBySegmentNumber
        .map(_._1.length)
        .groupBy(identity)
        .toArray
        .map { case (i, values) => (i, values.length) }
        .sortBy(_._1)
        .map { case (nPivots, nSegments) => s"$nPivots pivots: $nSegments" }

      log.info(s"DISTRIBUTED SORT: PHASE ${i + 1}: pivot counts:\n  ${pivotCounts.mkString("\n  ")}")

      val (sortedSegmentsTuples, unsortedPivotsWithEndpointsAndInfoGroupedBySegmentNumber) = pivotsWithEndpointsAndInfoGroupedBySegmentNumber.zipWithIndex.partition { case ((_, isSorted, _, _, _), _) => isSorted}

      val outputPartitions = sortedSegmentsTuples.flatMap { case ((_, _, _, partMins, partMaxes), originalSegmentIdx) =>
        val segmentToBreakUp = loopState.largeSegments(originalSegmentIdx)
        val currentSegmentPartitionData = partitionDataPerSegment(originalSegmentIdx)
        val partRanges = partMins.zip(partMaxes)
        assert(partRanges.size == currentSegmentPartitionData.size)

        currentSegmentPartitionData.zip(partRanges).zipWithIndex.map { case ((pi, (intervalStart, intervalEnd)), idx) =>
          OutputPartition(segmentToBreakUp.indices :+ idx, Interval(intervalStart, intervalEnd, true, true), pi.files)
        }
      }

      val remainingUnsortedSegments = unsortedPivotsWithEndpointsAndInfoGroupedBySegmentNumber.map {case (_, idx) => loopState.largeSegments(idx)}

      val (newBigUnsortedSegments, newSmallSegments) = if (unsortedPivotsWithEndpointsAndInfoGroupedBySegmentNumber.size > 0) {

        val pivotsWithEndpointsGroupedBySegmentNumber = unsortedPivotsWithEndpointsAndInfoGroupedBySegmentNumber.map{ case (r, _) => r._1 }

        val pivotsWithEndpointsGroupedBySegmentNumberLiteral = Literal(TArray(TArray(keyToSortBy)), pivotsWithEndpointsGroupedBySegmentNumber)

        val tmpPath = ctx.createTmpPath("hail_shuffle_temp")
        val unsortedPartitionDataPerSegment = unsortedPivotsWithEndpointsAndInfoGroupedBySegmentNumber.map { case (_, idx) => partitionDataPerSegment(idx)}

        val partitionDataPerSegmentWithPivotIndex = unsortedPartitionDataPerSegment.zipWithIndex.map { case (partitionDataForOneSegment, indexIntoPivotsArray) =>
          partitionDataForOneSegment.map(x => (x.indices, x.files, x.currentPartSize, indexIntoPivotsArray))
        }

        val distributeContextsData = partitionDataPerSegmentWithPivotIndex.flatten.zipWithIndex.map { case (part, partIdx) => Row(part._1.last, part._2, partIdx, part._4) }
        val distributeContexts = ToStream(Literal(TArray(TStruct("segmentIdx" -> TInt32, "files" -> TArray(TString), "partIdx" -> TInt32, "indexIntoPivotsArray" -> TInt32)), distributeContextsData))
        val distributeGlobals = MakeStruct(IndexedSeq("pivotsWithEndpointsGroupedBySegmentIdx" -> pivotsWithEndpointsGroupedBySegmentNumberLiteral))

        val distribute = cdaIR(distributeContexts, distributeGlobals, s"shuffle_distribute_iteration_$i") { (ctxRef, globalsRef) =>
          val segmentIdx = GetField(ctxRef, "segmentIdx")
          val indexIntoPivotsArray = GetField(ctxRef, "indexIntoPivotsArray")
          val pivotsWithEndpointsGroupedBySegmentIdx = GetField(globalsRef, "pivotsWithEndpointsGroupedBySegmentIdx")
          val path = invoke("concat", TString, Str(tmpPath + "_"), invoke("str", TString, GetField(ctxRef, "partIdx")))
          val filenames = GetField(ctxRef, "files")
          val partitionStream = flatMapIR(ToStream(filenames)) { fileName =>
            ReadPartition(MakeStruct(Array("partitionIndex" -> I64(0), "partitionPath" -> fileName)), tcoerce[TStruct](spec._vType), reader)
          }
          MakeTuple.ordered(IndexedSeq(segmentIdx, StreamDistribute(partitionStream, ArrayRef(pivotsWithEndpointsGroupedBySegmentIdx, indexIntoPivotsArray), path, StructCompare(keyToSortBy, keyToSortBy, sortFields.toArray), spec)))
        }

        log.info(s"DISTRIBUTED SORT: PHASE ${i+1}: STAGE 2: DISTRIBUTE")
        val distributeResult = CompileAndEvaluate[Annotation](ctx, distribute)
          .asInstanceOf[IndexedSeq[Row]].map(row => (
          row(0).asInstanceOf[Int],
          row(1).asInstanceOf[IndexedSeq[Row]].map(innerRow => (
            innerRow(0).asInstanceOf[Interval],
            innerRow(1).asInstanceOf[String],
            innerRow(2).asInstanceOf[Int],
            innerRow(3).asInstanceOf[Long])
          )))

        // distributeResult is a numPartitions length array of arrays, where each inner array tells me what
        // files were written to for each partition, as well as the number of entries in that file.
        val protoDataPerSegment = orderedGroupBy[(Int, IndexedSeq[(Interval, String, Int, Long)]), Int](distributeResult, x => x._1).map { case (_, seqOfChunkData) => seqOfChunkData.map(_._2) }

        val transposedIntoNewSegments = protoDataPerSegment.zip(remainingUnsortedSegments.map(_.indices)).flatMap { case (oneOldSegment, priorIndices) =>
          val headLen = oneOldSegment.head.length
          assert(oneOldSegment.forall(x => x.length == headLen))
          (0 until headLen).map(colIdx => (oneOldSegment.map(row => row(colIdx)), priorIndices))
        }

        val dataPerSegment = transposedIntoNewSegments.zipWithIndex.map { case ((chunksWithSameInterval, priorIndices), newIndex) =>
          val interval = chunksWithSameInterval.head._1
          val chunks = chunksWithSameInterval.map{ case (_, filename, numRows, numBytes) => Chunk(filename, numRows, numBytes)}
          val newSegmentIndices = priorIndices :+ newIndex
          SegmentResult(newSegmentIndices, interval, chunks)
        }

        // Decide whether a segment is small enough to be removed from consideration.
        dataPerSegment.partition { sr =>
          val isBig = sr.chunks.map(_.byteSize).sum > sizeCutoff
          // Need to call it "small" if it can't be further subdivided.
          isBig && (sr.interval.left.point != sr.interval.right.point) && (sr.chunks.map(_.size).sum > 1)
        }
      } else { (IndexedSeq.empty[SegmentResult], IndexedSeq.empty[SegmentResult]) }

      log.info(s"DISTRIBUTED SORT: PHASE ${i + 1}: ${newSmallSegments.length}/${newSmallSegments.length + newBigUnsortedSegments.length} segments can be locally sorted")

      loopState = LoopState(newBigUnsortedSegments, loopState.smallSegments ++ newSmallSegments, loopState.readyOutputParts ++ outputPartitions)
      i = i + 1
    }

    val needSortingFilenames = loopState.smallSegments.map(_.chunks.map(_.filename))
    val needSortingFilenamesContext = Literal(TArray(TArray(TString)), needSortingFilenames)

    val sortedFilenamesIR = cdaIR(ToStream(needSortingFilenamesContext), MakeStruct(Seq()), "shuffle_local_sort") { case (ctxRef, _) =>
      val filenames = ctxRef
      val partitionInputStream = flatMapIR(ToStream(filenames)) { fileName =>
        ReadPartition(MakeStruct(Array("partitionIndex" -> I64(0), "partitionPath" -> fileName)), tcoerce[TStruct](spec._vType), reader)
      }
      val newKeyFieldNames = keyToSortBy.fields.map(_.name)
      val sortedStream = ToStream(sortIR(partitionInputStream) { (refLeft, refRight) =>
        ApplyComparisonOp(StructLT(keyToSortBy, sortFields), SelectFields(refLeft, newKeyFieldNames), SelectFields(refRight, newKeyFieldNames))
      })
      WritePartition(sortedStream, UUID4(), writer)
    }

    log.info(s"DISTRIBUTED SORT: PHASE ${i+1}: LOCALLY SORT FILES")
    log.info(s"DISTRIBUTED_SORT: ${needSortingFilenames.length} segments to sort")
    val sortedFilenames = CompileAndEvaluate[Annotation](ctx, sortedFilenamesIR).asInstanceOf[IndexedSeq[Row]].map(_(0).asInstanceOf[String])
    val newlySortedSegments = loopState.smallSegments.zip(sortedFilenames).map { case (sr, newFilename) =>
      OutputPartition(sr.indices, sr.interval, IndexedSeq(initialTmpPath + newFilename))
    }

    val unorderedOutputPartitions = newlySortedSegments ++ loopState.readyOutputParts
    val orderedOutputPartitions = unorderedOutputPartitions.sortWith{ (srt1, srt2) => lessThanForSegmentIndices(srt1.indices, srt2.indices)}

    val contextData = {
      var filesCount: Long = 0
      for (segment <- orderedOutputPartitions) yield {
        val filesWithNums = segment.files.zipWithIndex.map { case (file, i) =>
          Row(i + filesCount, file)
        }
        filesCount += segment.files.length
        Row(filesWithNums)
      }
    }
    val contexts = ToStream(Literal(TArray(TStruct("files" -> TArray(TStruct("partitionIndex" -> TInt64, "partitionPath" -> TString)))), contextData))

    // Note: If all of the sort fields are not ascending, the the resulting table is sorted, but not keyed.
    val keyed = sortFields.forall(sf => sf.sortOrder == Ascending)
    val (partitionerKey, intervals) = if (keyed) {
      (keyToSortBy, orderedOutputPartitions.map{ segment => segment.interval})
    } else {
      (TStruct(), orderedOutputPartitions.map{ _ => Interval(Row(), Row(), true, false)})
    }

    val partitioner = new RVDPartitioner(partitionerKey, intervals)
    val finalTs = TableStage(initialGlobalsLiteral, partitioner, TableStageDependency.none, contexts, { ctxRef =>
      val files = GetField(ctxRef, "files")
      val partitionInputStream = flatMapIR(ToStream(files)) { fileInfo =>
        ReadPartition(fileInfo, tcoerce[TStruct](spec._vType), reader)
      }
      partitionInputStream
    })

    finalTs
  }

  def orderedGroupBy[T, U](is: IndexedSeq[T], func: T => U): IndexedSeq[(U, IndexedSeq[T])] = {
    val result = new ArrayBuffer[(U, IndexedSeq[T])](is.size)
    val currentGroup = new ArrayBuffer[T]()
    var lastKeySeen: Option[U] = None
    is.foreach { element =>
      val key = func(element)
      if (currentGroup.isEmpty) {
        currentGroup.append(element)
        lastKeySeen = Some(key)
      } else if (lastKeySeen.map(lastKey => lastKey == key).getOrElse(false)) {
        currentGroup.append(element)
      } else {
        result.append((lastKeySeen.get, currentGroup.result().toIndexedSeq))
        currentGroup.clear()
        currentGroup.append(element)
        lastKeySeen = Some(key)
      }
    }
    if (!currentGroup.isEmpty) {
      result.append((lastKeySeen.get, currentGroup))
    }
    result.result().toIndexedSeq
  }

  def lessThanForSegmentIndices(i1: IndexedSeq[Int], i2: IndexedSeq[Int]): Boolean = {
    var idx = 0
    val minLength = math.min(i1.length, i2.length)
    while (idx < minLength) {
      if (i1(idx) != i2(idx)) {
        return i1(idx) < i2(idx)
      }
      idx += 1
    }
    // For there to be no difference at this point, they had to be equal whole way. Assert that they're same length.
    assert(i1.length == i2.length)
    false
  }

  case class PartitionInfo(indices: IndexedSeq[Int], files: IndexedSeq[String], currentPartSize: Long, currentPartByteSize: Long)

  def segmentsToPartitionData(segments: IndexedSeq[SegmentResult], idealNumberOfRowsPerPart: Long): IndexedSeq[IndexedSeq[PartitionInfo]] = {
    segments.map { sr =>
      val chunkDataSizes = sr.chunks.map(_.size)
      val segmentSize = chunkDataSizes.sum
      val numParts = coerceToInt((segmentSize + idealNumberOfRowsPerPart - 1) / idealNumberOfRowsPerPart)
      var currentPartSize = 0L
      var currentPartByteSize = 0L
      val groupedIntoParts = new ArrayBuffer[PartitionInfo](numParts)
      val currentFiles = new ArrayBuffer[String]()
      sr.chunks.foreach { chunk =>
        if (chunk.size > 0) {
          currentFiles.append(chunk.filename)
          currentPartSize += chunk.size
          currentPartByteSize += chunk.byteSize
          if (currentPartSize >= idealNumberOfRowsPerPart) {
            groupedIntoParts.append(PartitionInfo(sr.indices, currentFiles.result().toIndexedSeq, currentPartSize, currentPartByteSize))
            currentFiles.clear()
            currentPartSize = 0
            currentPartByteSize = 0L
          }
        }
      }
      if (!currentFiles.isEmpty) {
        groupedIntoParts.append(PartitionInfo(sr.indices, currentFiles.result().toIndexedSeq, currentPartSize, currentPartByteSize))
      }
      groupedIntoParts.result()
    }
  }

  def howManySamplesPerPartition(rand: IRRandomness, totalNumberOfRecords: Long, initialNumSamplesToSelect: Int, partitionCounts: IndexedSeq[Long]): IndexedSeq[Int] = {
    var successStatesRemaining = initialNumSamplesToSelect
    var failureStatesRemaining = totalNumberOfRecords - successStatesRemaining

    val ans = new Array[Int](partitionCounts.size)

    var i = 0
    while (i < partitionCounts.size) {
      val numSuccesses = rand.rhyper(successStatesRemaining, failureStatesRemaining, partitionCounts(i)).toInt
      successStatesRemaining -= numSuccesses
      failureStatesRemaining -= (partitionCounts(i) - numSuccesses)
      ans(i) = numSuccesses
      i += 1
    }

    ans
  }

  def samplePartition(dataStream: IR, sampleIndices: IR, sortFields: IndexedSeq[SortField]): IR = {
    // Step 1: Join the dataStream zippedWithIdx on sampleIndices?
    // That requires sampleIndices to be a stream of structs
    val samplingIndexName = "samplingPartitionIndex"
    val structSampleIndices = mapIR(sampleIndices)(sampleIndex => MakeStruct(Seq((samplingIndexName, sampleIndex))))
    val dataWithIdx = zipWithIndex(dataStream)

    val leftName = genUID()
    val rightName = genUID()
    val leftRef = Ref(leftName, dataWithIdx.typ.asInstanceOf[TStream].elementType)
    val rightRef = Ref(rightName, structSampleIndices.typ.asInstanceOf[TStream].elementType)

    val joined = StreamJoin(dataWithIdx, structSampleIndices, IndexedSeq("idx"), IndexedSeq(samplingIndexName), leftName, rightName,
      MakeStruct(Seq(("elt", GetField(leftRef, "elt")), ("shouldKeep", ApplyUnaryPrimOp(Bang(), IsNA(rightRef))))),
      "left", requiresMemoryManagement = true)

    // Step 2: Aggregate over joined, figure out how to collect only the rows that are marked "shouldKeep"
    val streamElementType = joined.typ.asInstanceOf[TStream].elementType.asInstanceOf[TStruct]
    val streamElementName = genUID()
    val streamElementRef = Ref(streamElementName, streamElementType)
    val eltName = genUID()
    val eltType = dataStream.typ.asInstanceOf[TStream].elementType.asInstanceOf[TStruct]
    val eltRef = Ref(eltName, eltType)

    // Folding for isInternallySorted
    val aggFoldSortedZero = MakeStruct(Seq("lastKeySeen" -> NA(eltType), "sortedSoFar" -> true, "haveSeenAny" -> false))
    val aggFoldSortedAccumName1 = genUID()
    val aggFoldSortedAccumName2 = genUID()
    val isSortedStateType = TStruct("lastKeySeen" -> eltType, "sortedSoFar" -> TBoolean, "haveSeenAny" -> TBoolean)
    val aggFoldSortedAccumRef1 = Ref(aggFoldSortedAccumName1, isSortedStateType)
    val isSortedSeq =
      bindIR(GetField(aggFoldSortedAccumRef1, "lastKeySeen")) { lastKeySeenRef =>
        If(!GetField(aggFoldSortedAccumRef1, "haveSeenAny"),
          MakeStruct(Seq("lastKeySeen" -> eltRef, "sortedSoFar" -> true, "haveSeenAny" -> true)),
          If (ApplyComparisonOp(StructLTEQ(eltType, sortFields), lastKeySeenRef, eltRef),
            MakeStruct(Seq("lastKeySeen" -> eltRef, "sortedSoFar" -> GetField(aggFoldSortedAccumRef1, "sortedSoFar"), "haveSeenAny" -> true)),
            MakeStruct(Seq("lastKeySeen" -> eltRef, "sortedSoFar" -> false, "haveSeenAny" -> true))
          )
        )
      }
    val isSortedComb = aggFoldSortedAccumRef1 // Do nothing, as this will never be called in a StreamAgg

    StreamAgg(joined, streamElementName, {
      AggLet(eltName, GetField(streamElementRef, "elt"),
        MakeStruct(Seq(
          ("min", AggFold.min(eltRef, sortFields)),
          ("max", AggFold.max(eltRef, sortFields)),
          ("samples", AggFilter(GetField(streamElementRef, "shouldKeep"), ApplyAggOp(Collect())(eltRef), false)),
          ("isSorted", GetField(AggFold(aggFoldSortedZero, isSortedSeq, isSortedComb, aggFoldSortedAccumName1, aggFoldSortedAccumName2, false), "sortedSoFar"))
        )), false)
    })
  }

  // Given an IR of type TArray(TTuple(minKey, maxKey)), determine if there's any overlap between these closed intervals.
  def tuplesAreSorted(arrayOfTuples: IR, sortFields: IndexedSeq[SortField]): IR = {
    val intervalElementType = arrayOfTuples.typ.asInstanceOf[TArray].elementType.asInstanceOf[TTuple].types(0)

    foldIR(mapIR(rangeIR(1, ArrayLen(arrayOfTuples))) { idxOfTuple =>
      ApplyComparisonOp(StructLTEQ(intervalElementType, sortFields), GetTupleElement(ArrayRef(arrayOfTuples, idxOfTuple - 1), 1), GetTupleElement(ArrayRef(arrayOfTuples, idxOfTuple), 0))
    }, True()) { case (accum, elt) =>
      ApplySpecial("land", Seq.empty[Type], Seq(accum, elt), TBoolean, ErrorIDs.NO_ERROR)
    }
  }
}

/**
  * a "Chunk" is a file resulting from any StreamDistribute. Chunks are internally unsorted but contain
  * data between two pivots.
  */
case class Chunk(filename: String, size: Long, byteSize: Long)
/**
  * A SegmentResult is the set of chunks from various StreamDistribute tasks working on the same segment
  * of a previous iteration.
  */
case class SegmentResult(indices: IndexedSeq[Int], interval: Interval, chunks: IndexedSeq[Chunk])
case class OutputPartition(indices: IndexedSeq[Int], interval: Interval, files: IndexedSeq[String])
case class LoopState(largeSegments: IndexedSeq[SegmentResult], smallSegments: IndexedSeq[SegmentResult], readyOutputParts: IndexedSeq[OutputPartition])


text:
package com.opengamma.engine.fudgemsg;

import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;

import org.fudgemsg.FudgeField;
import org.fudgemsg.FudgeMsg;
import org.fudgemsg.MutableFudgeMsg;
import org.fudgemsg.mapping.FudgeDeserializer;
import org.fudgemsg.mapping.FudgeSerializer;
import org.threeten.bp.Duration;
import org.threeten.bp.Instant;

import com.opengamma.engine.ComputationTargetSpecification;
import com.opengamma.engine.value.ComputedValueResult;
import com.opengamma.engine.view.ViewCalculationResultModel;
import com.opengamma.engine.view.ViewResultModel;
import com.opengamma.engine.view.execution.ViewCycleExecutionOptions;
import com.opengamma.engine.view.impl.InMemoryViewResultModel;
import com.opengamma.id.UniqueId;
import com.opengamma.id.VersionCorrection;

/**
 * Base operation for {@link ViewDeltaResultModelFudgeBuilder} and {@link ViewComputationResultModelFudgeBuilder}.
 */
public abstract class ViewResultModelFudgeBuilder {
  private static final String FIELD_VIEWPROCESSID = "viewProcessId";
  private static final String FIELD_VIEWCYCLEID = "viewCycleId";
  private static final String FIELD_VIEW_CYCLE_EXECUTION_OPTIONS = "viewCycleExecutionOptions";
  private static final String FIELD_CALCULATION_TIME = "calculationTime";
  private static final String FIELD_CALCULATION_DURATION = "calculationDuration";
  private static final String FIELD_VERSION_CORRECTION = "versionCorrection";
  private static final String FIELD_RESULTS = "results";

  protected static MutableFudgeMsg createResultModelMessage(final FudgeSerializer serializer, final ViewResultModel resultModel) {
    final MutableFudgeMsg message = serializer.newMessage();
    message.add(FIELD_VIEWPROCESSID, resultModel.getViewProcessId());
    message.add(FIELD_VIEWCYCLEID, resultModel.getViewCycleId());
    message.add(FIELD_CALCULATION_TIME, resultModel.getCalculationTime());
    serializer.addToMessage(message, FIELD_VIEW_CYCLE_EXECUTION_OPTIONS, null, resultModel.getViewCycleExecutionOptions());
    serializer.addToMessage(message, FIELD_CALCULATION_DURATION, null, resultModel.getCalculationDuration());
    serializer.addToMessage(message, FIELD_VERSION_CORRECTION, null, resultModel.getVersionCorrection());
    final Collection<String> calculationConfigurations = resultModel.getCalculationConfigurationNames();
    final MutableFudgeMsg resultMsg = serializer.newMessage();
    for (final String calculationConfiguration : calculationConfigurations) {
      resultMsg.add(null, 1, calculationConfiguration);
      serializer.addToMessage(resultMsg, null, 2, resultModel.getCalculationResult(calculationConfiguration));
    }
    message.add(FIELD_RESULTS, resultMsg);
    return message;
  }

  protected InMemoryViewResultModel bootstrapCommonDataFromMessage(final FudgeDeserializer deserializer, final FudgeMsg message) {
    final UniqueId viewProcessId = message.getValue(UniqueId.class, FIELD_VIEWPROCESSID);
    final UniqueId viewCycleId = message.getValue(UniqueId.class, FIELD_VIEWCYCLEID);
    final ViewCycleExecutionOptions viewCycleExecutionOptions =
        deserializer.fieldValueToObject(ViewCycleExecutionOptions.class, message.getByName(FIELD_VIEW_CYCLE_EXECUTION_OPTIONS));
    final Instant calculationTime = message.getFieldValue(Instant.class, message.getByName(FIELD_CALCULATION_TIME));
    final FudgeField durationField = message.getByName(FIELD_CALCULATION_DURATION);
    final Duration calculationDuration = durationField != null ? deserializer.fieldValueToObject(Duration.class, durationField) : null;
    final VersionCorrection versionCorrection = deserializer.fieldValueToObject(VersionCorrection.class, message.getByName(FIELD_VERSION_CORRECTION));
    final Map<String, ViewCalculationResultModel> configurationMap = new HashMap<>();
    final Queue<String> keys = new LinkedList<>();
    final Queue<ViewCalculationResultModel> values = new LinkedList<>();
    for (final FudgeField field : message.getFieldValue(FudgeMsg.class, message.getByName(FIELD_RESULTS))) {
      if (field.getOrdinal() == 1) {
        final String key = deserializer.fieldValueToObject(String.class, field);
        if (values.isEmpty()) {
          keys.add(key);
        } else {
          configurationMap.put(key, values.remove());
        }
      } else if (field.getOrdinal() == 2) {
        final ViewCalculationResultModel value = deserializer.fieldValueToObject(ViewCalculationResultModel.class, field);
        if (keys.isEmpty()) {
          values.add(value);
        } else {
          configurationMap.put(keys.remove(), value);
        }
      }
    }

    final InMemoryViewResultModel resultModel = constructImpl();
    for (final Map.Entry<String, ViewCalculationResultModel> configurationEntry : configurationMap.entrySet()) {
      for (final ComputationTargetSpecification targetSpec : configurationEntry.getValue().getAllTargets()) {
        for (final ComputedValueResult value : configurationEntry.getValue().getAllValues(targetSpec)) {
          resultModel.addValue(configurationEntry.getKey(), value);
        }
      }
    }

    resultModel.setViewProcessId(viewProcessId);
    resultModel.setViewCycleId(viewCycleId);
    resultModel.setViewCycleExecutionOptions(viewCycleExecutionOptions);
    resultModel.setCalculationTime(calculationTime);
    resultModel.setCalculationDuration(calculationDuration);
    resultModel.setVersionCorrection(versionCorrection);

    return resultModel;
  }

  protected abstract InMemoryViewResultModel constructImpl();

}


text:
package php.runtime.env;

import java.io.File;

public class SourceMappedTraceInfo extends TraceInfo {
    public SourceMappedTraceInfo(StackTraceElement element) {
        super(element);
    }

    public SourceMappedTraceInfo(String fileName, int startLine, int startPosition) {
        super(fileName, startLine, startPosition);
    }

    public SourceMappedTraceInfo(Context context, int startLine, int endLine, int startPosition, int endPosition) {
        super(context, startLine, endLine, startPosition, endPosition);
    }

    public SourceMappedTraceInfo(Context context) {
        super(context);
    }
}


text:
<Record>
<Term>Antihypertensive Agents</Term>
<SemanticType>Pharmacologic Substance</SemanticType>
<SemanticType>Chemical Viewed Functionally</SemanticType>
<ParentTerm>Cardiovascular Agent</ParentTerm>
<ParentTerm>Cardiovascular Agents</ParentTerm>
<ClassificationPath>Chemical Actions and Uses/Pharmacologic Actions/Therapeutic Uses/Cardiovascular Agents/Antihypertensive Agents</ClassificationPath>
<ClassificationPath>Chemicals_and_Drugs_Kind/Drug, Food, Chemical or Biomedical Material/Pharmacologic Substance/Adjuvant/Cardiovascular Agent/Antihypertensive Agent</ClassificationPath>
<BroaderTerm>Therapeutic Uses</BroaderTerm>
<BroaderTerm>Pharmacologic Actions</BroaderTerm>
<BroaderTerm>Chemicals_and_Drugs_Kind</BroaderTerm>
<BroaderTerm>Cardiovascular Agent</BroaderTerm>
<BroaderTerm>Pharmacologic Substance</BroaderTerm>
<BroaderTerm>Adjuvant</BroaderTerm>
<BroaderTerm>Antihypertensive Agent</BroaderTerm>
<BroaderTerm>Chemical Actions and Uses</BroaderTerm>
<BroaderTerm>Antihypertensive Agents</BroaderTerm>
<BroaderTerm>Drug, Food, Chemical or Biomedical Material</BroaderTerm>
<BroaderTerm>Cardiovascular Agents</BroaderTerm>
<ChildTerm>Mecarbinate</ChildTerm>
<ChildTerm>Guanethidine</ChildTerm>
<ChildTerm>Pargyline</ChildTerm>
<ChildTerm>Eplerenone</ChildTerm>
<ChildTerm>Guanoxyfen Sulfate</ChildTerm>
<ChildTerm>Oxprenoate Potassium</ChildTerm>
<ChildTerm>Sinitrodil</ChildTerm>
<ChildTerm>Remikiren</ChildTerm>
<ChildTerm>Epoprostenol</ChildTerm>
<ChildTerm>Pempidine</ChildTerm>
<ChildTerm>Prizidilol Hydrochloride</ChildTerm>
<ChildTerm>Hydralazine</ChildTerm>
<ChildTerm>Hydralazine Hydrochloride</ChildTerm>
<ChildTerm>Mopidralazine</ChildTerm>
<ChildTerm>Fenperate</ChildTerm>
<ChildTerm>Methyldopa</ChildTerm>
<ChildTerm>Prazosin</ChildTerm>
<ChildTerm>Afurolol</ChildTerm>
<ChildTerm>Rescimetol</ChildTerm>
<ChildTerm>Saralasin Acetate</ChildTerm>
<ChildTerm>Deserpidine</ChildTerm>
<ChildTerm>Methyldopate Hydrochloride</ChildTerm>
<ChildTerm>Guanethidine Monosulfate</ChildTerm>
<ChildTerm>Pargyline Hydrochloride</ChildTerm>
<ChildTerm>Guanisoquine Sulfate</ChildTerm>
<ChildTerm>Bethanidine Sulfate</ChildTerm>
<ChildTerm>Lisinopril</ChildTerm>
<ChildTerm>Trethinium Tosilate</ChildTerm>
<ChildTerm>Acetryptine</ChildTerm>
<ChildTerm>Guanadrel Sulfate</ChildTerm>
<ChildTerm>Bietaserpine</ChildTerm>
<ChildTerm>Ambrisentan</ChildTerm>
<ChildTerm>Guanfacine Hydrochloride</ChildTerm>
<ChildTerm>Guanclofine</ChildTerm>
<ChildTerm>Mindodilol</ChildTerm>
<ChildTerm>Peratizole</ChildTerm>
<ChildTerm>Aliskiren</ChildTerm>
<ChildTerm>Sarakalim</ChildTerm>
<ChildTerm>Emakalim</ChildTerm>
<ChildTerm>Pinacidil</ChildTerm>
<ChildTerm>Minoxidil</ChildTerm>
<ChildTerm>Guanoxyfen</ChildTerm>
<ChildTerm>Guabenxan</ChildTerm>
<ChildTerm>Calcium Channel Blocker</ChildTerm>
<ChildTerm>Aliskiren Hemifumarate</ChildTerm>
<ChildTerm>Furnidipine</ChildTerm>
<ChildTerm>Azamethonium Bromide</ChildTerm>
<ChildTerm>Peraquinsin</ChildTerm>
<ChildTerm>Pentolinium Tartrate</ChildTerm>
<ChildTerm>Guanisoquin</ChildTerm>
<ChildTerm>Quinazosin Hydrochloride</ChildTerm>
<ChildTerm>Etozolin</ChildTerm>
<ChildTerm>Bosentan</ChildTerm>
<ChildTerm>Guanfacine</ChildTerm>
<ChildTerm>Enalaprilat</ChildTerm>
<ChildTerm>Prazosin Hydrochloride</ChildTerm>
<ChildTerm>Guanabenz Acetate</ChildTerm>
<ChildTerm>Racemetirosine</ChildTerm>
<ChildTerm>Trimethaphan Camsylate</ChildTerm>
<ChildTerm>Fosinoprilat</ChildTerm>
<ChildTerm>Benclonidine</ChildTerm>
<ChildTerm>Epoprostenol Sodium</ChildTerm>
<ChildTerm>Rescinnamine</ChildTerm>
<ChildTerm>Fandosentan</ChildTerm>
<ChildTerm>Flupranone</ChildTerm>
<ChildTerm>Piclonidine</ChildTerm>
<ChildTerm>Guanoclor</ChildTerm>
<ChildTerm>Treprostinil Sodium</ChildTerm>
<ChildTerm>Angiotensin II Receptor Antagonist</ChildTerm>
<ChildTerm>Neldazosin</ChildTerm>
<ChildTerm>Saralasin</ChildTerm>
<ChildTerm>Clonidine</ChildTerm>
<ChildTerm>Guanoclor Sulfate</ChildTerm>
<ChildTerm>Flavodilol</ChildTerm>
<ChildTerm>Guanadrel</ChildTerm>
<Synonym>Anti-Hypertensive Agents</Synonym>
<Synonym>Hypotensive Drugs</Synonym>
<Synonym>Anti Hypertensive Agents</Synonym>
<Synonym>Anti-Hypertensive Drugs</Synonym>
<Synonym>Antihypertensives</Synonym>
<Synonym>Antihypertensive Agents</Synonym>
<Synonym>Anti-Hypertensives</Synonym>
<Synonym>Hypotensives</Synonym>
<Synonym>Anti Hypertensives</Synonym>
<Synonym>Hypotensive Agents</Synonym>
<Synonym>Antihypertensive Medications</Synonym>
<Synonym>Antihypertensive Medication</Synonym>
<Synonym>Antihypertensive Agent</Synonym>
<Synonym>Anti Hypertensive Drugs</Synonym>
<Synonym>Antihypertensive Drugs</Synonym>
<Description>Any substance used in the treatment of acute or chronic hypertension regardless of pharmacological mechanism. Antihypertensive agents include diuretics, alpha-adrenergic and beta-adrenergic antagonists, angiotensin-converting enzyme inhibitors, calcium channel blockers, ganglionic blockers, and vasodilator agents.</Description>
<Description>Drugs used in the treatment of acute or chronic vascular HYPERTENSION regardless of pharmacological mechanism. Among the antihypertensive agents are DIURETICS; (especially DIURETICS, THIAZIDE); ADRENERGIC BETA-ANTAGONISTS; ADRENERGIC ALPHA-ANTAGONISTS; ANGIOTENSIN-CONVERTING ENZYME INHIBITORS; CALCIUM CHANNEL BLOCKERS; GANGLIONIC BLOCKERS; and VASODILATOR AGENTS.</Description>
<Source>RTI</Source>
<Source>MeSH</Source>
<Source>NCI Thesaurus</Source>
</Record>


text:
package org.nd4j.linalg.api.ops.impl.transforms;

import org.apache.commons.math3.util.FastMath;
import org.nd4j.linalg.api.complex.IComplexNumber;
import org.nd4j.linalg.api.ndarray.INDArray;
import org.nd4j.linalg.api.ops.BaseTransformOp;
import org.nd4j.linalg.api.ops.Op;
import org.nd4j.linalg.api.ops.TransformOp;
import org.nd4j.linalg.util.ComplexUtil;

/**
 * Tanh elementwise function
 *
 * @author Adam Gibson
 */
public class Tanh extends BaseTransformOp {

    public Tanh() {
    }

    public Tanh(INDArray x, INDArray z) {
        super(x, z);
    }

    public Tanh(INDArray x, INDArray z, long n) {
        super(x, z, n);
    }

    public Tanh(INDArray x, INDArray y, INDArray z, long n) {
        super(x, y, z, n);
    }

    public Tanh(INDArray x, INDArray y, INDArray z) {
        super(x, y, z, x.lengthLong());
    }

    public Tanh(INDArray x) {
        super(x);
    }

    @Override
    public int opNum() {
        return 15;
    }

    @Override
    public String name() {
        return "tanh";
    }

    @Override
    public IComplexNumber op(IComplexNumber origin, double other) {
        return ComplexUtil.tanh(origin);
    }

    @Override
    public IComplexNumber op(IComplexNumber origin, float other) {
        return ComplexUtil.tanh(origin);
    }

    @Override
    public IComplexNumber op(IComplexNumber origin, IComplexNumber other) {
        return ComplexUtil.tanh(origin);
    }

    @Override
    public float op(float origin, float other) {
        return (float) FastMath.tanh(origin);
    }

    @Override
    public double op(double origin, double other) {
        return FastMath.tanh(origin);
    }

    @Override
    public double op(double origin) {
        return FastMath.tanh(origin);
    }

    @Override
    public float op(float origin) {
        return (float) FastMath.tanh(origin);
    }

    @Override
    public IComplexNumber op(IComplexNumber origin) {
        return ComplexUtil.tanh(origin);
    }

    @Override
    public TransformOp derivative() {
        return new TanhDerivative(x, y, z, n);
    }

    @Override
    public Op opForDimension(int index, int dimension) {
        INDArray xAlongDimension = x.vectorAlongDimension(index, dimension);
        if (y() != null)
            return new Tanh(xAlongDimension, y.vectorAlongDimension(index, dimension), z.vectorAlongDimension(index, dimension), xAlongDimension.length());
        else
            return new Tanh(xAlongDimension, z.vectorAlongDimension(index, dimension), xAlongDimension.length());

    }

    @Override
    public Op opForDimension(int index, int... dimension) {
        INDArray xAlongDimension = x.tensorAlongDimension(index, dimension);
        if (y() != null)
            return new Tanh(xAlongDimension, y.tensorAlongDimension(index, dimension), z.tensorAlongDimension(index, dimension), xAlongDimension.length());
        else
            return new Tanh(xAlongDimension, z.tensorAlongDimension(index, dimension), xAlongDimension.length());

    }
}


text:
                New Game

                Load Game

                Exit


text:
import React from 'react';
import PropTypes from 'prop-types';
// import { findDOMNode } from 'react-dom';
import {graphql, compose} from 'react-apollo';
// import classNames from 'classnames';
// import { Link } from 'react-router-dom';
// import Button from '../../../components/Button';
// import TextField from '../../../components/textField';
// import Overlay from '../../../components/Overlay';
import Buttress from '../../../components/Buttress';
// import Card from '../../../components/Card';

// import ClickAwayListener from '../../../components/utils/ClickAwayListener';
// import renderHTML from 'react-render-html';
import HeadTag from 'react-head';
// import Image from '../../../components/Image';
import Autosuggest from '../../../components/Autosuggest';
import SUGGEST from '../../../mutations/Suggest';
import EntitysFetchData from '../../../hoc/EntitysFetchData';
import INDEX_SEARCH from '../../../mutations/IndexSearch';
// if(process.env.WEBPACK) require('./index.css');

class Home extends React.Component {
  state = {
    suggestions: [],
  };

  // componentWillMount(){
  //   const url = this.context.history.location.pathname;
  //   if(url.split('/')[1] === 'private'){
  //     this.setState({
  //       editText: true,
  //     });
  //   }
  // }
  componentDidMount() {
    document.body.style = 'background-image: url(https://pbs.twimg.com/media/B-zyjyFUYAAsuoj.jpg); background-size: cover';
  }
  suggestions(token){
    this.props.getSuggestionsMutation({
      variables: {token}
    })
    .then(res =>{
      this.setState({
        suggestions: res.data.suggest
      });
    });
  }

  render(){
    const {
      focusText,
      editText
    } = this.state;

    const {
      latest,
      latest:{hierarchy},
      waypoint,
      hierarchyQuery,
      setHierarchyQuery,
      entitiesLayoutQuery
    } = this.props;

    return(

        <Buttress
          size={40}
          className="text-center"
          >
          <HeadTag tag="title">Главная страница</HeadTag>

            Главная страница
            <Autosuggest
              suggestions={this.state.suggestions}
              getToken={token=> this.suggestions(token)}
              getSuggestion={suggestion=> this.search(suggestion)}
            />

        </Buttress>

    );
  }

};

Home.propTypes = {

};
Home.defaultProps = {

};
// Home.contextTypes = {
//   history: PropTypes.object
// };
export default compose (
  EntitysFetchData,
  graphql (SUGGEST, {name: 'getSuggestionsMutation'}),
  graphql (INDEX_SEARCH, {name: 'searchMutation'})
)(Home);