text:
import  numpy as np
import  mxnet as mx
from mxnet.gluon.data.vision import transforms
import os
import sys
import re
import habana as hb
from onnx import  helper
import onnx
from util import *
from onnx import AttributeProto, TensorProto, GraphProto
from  dataset import  pre_process_vgg,pre_process_coco_resnet34,pre_process_coco_resnet34_tf
import cv2
import json
from PIL import Image
from torchvision import transforms
from pycocotools.coco import COCO

def resnet50_prepare_calib_dataset(img_list_file_name,img_path=''):
    if(img_path==''):
        print('path parameters is empty')
        return
    if(img_list_file_name == ''):
        print('nned image list file name')
        return
    first = True
    with open(img_list_file_name, 'r') as f:
        for image_name in f:
            input_img_full_name = os.path.join(img_path, image_name.strip())
            img = cv2.imread(input_img_full_name)
            #img = mx.image.imread(input_img_full_name)
            img = pre_process_vgg(img,[224,224,3])
            img = img.transpose((2, 0, 1))  # transpose input
            img = np.expand_dims(img, axis=0)  # batchify
            if(first):
                dataset = img.copy()
                first = False
            else:
                dataset=np.ma.concatenate([dataset,img.copy()],axis=0)
    return dataset

def mlperf_resnet50_compile_recipe(model_dir, onnx_model_name, calib_image_list_file, image_dir, BATCH_SIZE):
    print('loading onnx file: ', onnx_model_name)
    print('onnx model directory path: ', model_dir)
    base_name = os.path.splitext(os.path.basename(onnx_model_name))[0]
    RECIPE_NAME = base_name + '_batch' + str(BATCH_SIZE) +'.recipe'
    NEW_ONNX   = base_name + '_batch' + str(BATCH_SIZE) +'.onnx'
    RECIPE_NAME = os.path.join(model_dir,RECIPE_NAME)
    NEW_ONNX = os.path.join(model_dir,NEW_ONNX)
    onnx_model_name = os.path.join(model_dir,onnx_model_name)
    input_shape = [BATCH_SIZE, 3, 224, 224]
    print('input_shape: ',input_shape)
    onnx_model = onnx.load(onnx_model_name)
    print('Removing softmax from onnx and adding globalAveragePool')
    for i,node in zip(range(len(onnx_model.graph.node)),onnx_model.graph.node):
        if (node.name == 'graph_outputs_Identity__6'):
            softmax_identity = node
        if (node.op_type == 'ReduceMean'):
            ri = i
        if (node.name == 'graph_outputs_Identity__4'):
            argmax_identity = node
        # bypass for identity issue
        if (node.op_type == 'ArgMax'):
            node.output[0] = 'ArgMax:0'
    onnx_model.graph.node.remove(softmax_identity)
    #bypass for identity issue
    onnx_model.graph.node.remove(argmax_identity)
    for node in onnx_model.graph.node:
        if (node.op_type == 'Softmax'):
            onnx_model.graph.node.remove(node)
    for output in onnx_model.graph.output:
        if (output.name == 'softmax_tensor:0'):
            onnx_model.graph.output.remove(output)
    onnx_model.graph.node.insert(ri+1, helper.make_node('GlobalAveragePool', [onnx_model.graph.node[ri].input[0]],
                                                     [onnx_model.graph.node[ri].output[0]],
                                                     'GlobalAveragePool'))

    onnx_model.graph.node.remove(onnx_model.graph.node[ri])
    input_name = ['input_tensor:0']
    output_name = ['ArgMax:0']
    profile = hb.profile.Performance()
    profile.num_low_precision_layers_to_raise = 0
    profile.inputs_as_nhwc = True
    hb_model = hb.model.create(profile)
    print('saving new onnx model file: ', NEW_ONNX)
    onnx.save(onnx_model, NEW_ONNX)
    print('Genarating Habana model from onnx model: ',NEW_ONNX)
    hb.model.from_onnx(hb_model, NEW_ONNX)
    print('Preparing calibration dataset')
    data_load = resnet50_prepare_calib_dataset(calib_image_list_file,image_dir)
    mxnet_iter = mx.io.NDArrayIter(data=data_load, label=None, batch_size=32,data_name=input_name[0])
    print('Running calibration')
    dynamic_range = hb.model.calibrate.measure_mxnet(hb_model, mx.cpu(), mxnet_iter, 16)
    print('Running Habana model quantization')
    hb.model.calibrate.quantize(hb_model, dynamic_range)
    hb.model.set_inputs_info(hb_model, {input_name[0]: {"shape": input_shape, "batch_pos": 0}})
    hb.model.set_outputs_info(hb_model, {output_name[0]: {"batch_pos": 0}})
    print('compiling recipe file: ',RECIPE_NAME)
    hb.model.compile_recipe(hb_model, RECIPE_NAME, compile_params={"visualization": 1})
    print('DONE')

def preprocess_imagenet_images(image_count,image_dir,model_dir,habana_recipe=None,BATCH_SIZE=10):
    ocnt = image_count;
    base,last_dir = os.path.split(image_dir)
    habana_recipe = os.path.join(model_dir,habana_recipe)
    output_file_dir = os.path.join(base, "imagenet_habana")
    os.makedirs(output_file_dir, exist_ok=True)
    full_input_list_name = os.path.join(image_dir, "val_map.txt")
    full_output_list_name = os.path.join(output_file_dir, "val_map.txt")
    output_list_pid = open(full_output_list_name,'w')
    input_name = 'input_tensor:0'
    input_shape = [BATCH_SIZE, 3, 224, 224]
    profile = hb.profile.Performance()
    profile.num_low_precision_layers_to_raise = 0
    profile.inputs_as_nhwc = True
    hb_model = hb.model.create(profile)
    device_id = hb.device.acquire()
    if(os.path.exists(habana_recipe) == False):
        print("didn't find the recipe file: ",habana_recipe)
        return
    topology_id = hb.device.load(device_id, habana_recipe)
    hb.device.activate(device_id, topology_id)
    tensors_info = hb.device.get_inputs_outputs_info(device_id, topology_id)
    input_info = tensors_info['inputs'][input_name]
    if(image_count <= 0):
        return
    with open(full_input_list_name, 'r') as f:
        for s in f:
            image_name, label = re.split(r"\s+", s.strip())
            output_file_name = os.path.splitext(os.path.basename(image_name))[0];
            output_file_name = output_file_name + ".bin"
            input_img_full_name = os.path.join(image_dir, image_name)
            output_img_full_name = os.path.join(output_file_dir, output_file_name)
            img = cv2.imread(input_img_full_name)
            #img = mx.image.imread(input_img_full_name)
            img = pre_process_vgg(img,[224,224,3])
            input_data =  img #img.transpose((1, 2, 0)).copy()  # transpose input
            input_data = quantize(input_data, zp=input_info['zp'], scale=input_info['scale'], dtype=input_info['data_type'])
            output_fid = open(output_img_full_name,'w')
            input_data.tofile(output_fid)
            output_fid.close()
            line = output_file_name + " " + str(label) + "\n"
            output_list_pid.write(line)
            image_count=image_count-1
            if (image_count == 0):
                break
            if(((ocnt-image_count) % 500) == 0):
                print('processed:',ocnt-image_count,' images')
    output_list_pid.close()

def print_option1_command_line():
    print("options[1] - compiling Habana recipe file")
    print('param[1] - True')
    print('param[2] - Model directory full path')
    print('param[3] - Onnx model file name')
    print('param[4] - Calib image list file name')
    print('param[5] - Source image directory')
    print('param[6] - Batch size')

def print_option2_command_line():
    print("option[2]- pre process the imagenet database")
    print('param[1] - False')
    print('param[2] - Number of images to be preprocessed')
    print('param[3] - Source image directory name')
    print('param[4] - Model directory full path')
    print('param[5] - Habana recipe file name')
    print('param[6] - Batch size')

def main():
    isPrepareRecipe = False
    print(len(sys.argv))
    if (len(sys.argv) < 7):
        print_option1_command_line()
        print_option2_command_line()
    else:
        if(sys.argv[1] == 'True'):
            isPrepareRecipe = True
        elif(sys.argv[1] == 'False'):
            isPrepareRecipe = False
        else:
            print('Need to choose between two options:')
            print('options 1 - Habana recipe compilation')
            print_option1_command_line()
            print('options 2 - image dataset preprocess')
            print_option2_command_line()
            return
        if(isPrepareRecipe):
            run_func = mlperf_resnet50_compile_recipe
            if(len(sys.argv) == 7):
                batch_size = int(sys.argv[6])
                run_func(sys.argv[2],sys.argv[3],sys.argv[4],sys.argv[5], batch_size)
            else:
                print_option1_command_line()
        else:
            run_func = preprocess_imagenet_images
            if(len(sys.argv) == 7):
                batch_size = int(sys.argv[6])
                image_count = int(sys.argv[2])
                run_func(image_count,sys.argv[3], sys.argv[4],sys.argv[5], batch_size)
            else:
               print_option2_command_line()

if __name__ == "__main__":
    main()


text:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.codemilltech.vmfirstnav">
	<uses-sdk android:minSdkVersion="15" />
	<application android:label="VMFirstNav">
	</application>
</manifest>

redpajama_set_name: RedPajamaGithub


text:
namespace remoting {

std::unique_ptr<AudioDecoder> AudioDecoder::CreateAudioDecoder(
    const protocol::SessionConfig& config) {
  if (config.audio_config().codec == protocol::ChannelConfig::CODEC_OPUS) {
    return base::WrapUnique(new AudioDecoderOpus());
  }

  NOTIMPLEMENTED();
  return nullptr;
}

}  // namespace remoting


text:
<!DOCTYPE html>

    <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
    <link type="text/css" rel="stylesheet" href="/css/demo.css">
    <link type="text/css" rel="stylesheet" href="/css/scatter-matrix.css">

      Scatter-Matrix

      Explore matrix data (samples as rows, observations as columns) using
      scatterplot matrix. Simply supply a CSV with a header. For example:


text:
from datetime import datetime

def parseTimestamp(timeData):
    # EX:
    # YY   MM DD hh mm
    # 2014 02 13 02 00
    timeStr = "%s/%s/%s %s:%s" % (timeData[2], timeData[1], timeData[0], timeData[3], timeData[4])
    return datetime.strptime(timeStr, "%d/%m/%Y %H:%M")

# based on code written by frostymoose
def parseSpectralWave(waveFile):
    waveFile.readline() # ignore first line header
    data = []

    for line in waveFile:
        line = line.replace('(', '').replace(')', '')
        line = line.split()
        timestamp = line[:5]
        sepFreq = float(line[5])
        rawFreqs = line[6:]

        timestamp = parseTimestamp(timestamp)

        freqs = []
        i = 0

        # Format: (amplitude, frequency)
        while i < len(rawFreqs):
            freq = float(rawFreqs[i+1]), float(rawFreqs[i])
            freqs.append(freq)
            i += 2

        data.append({
            "freqs": freqs,
            "sepFreq": sepFreq,
            "timestamp": timestamp.isoformat()
        })

    return data


text:
<?php

	//members表导入 有用信息到 users表

	//1.连接数据库
	// $conn = mysqli_connect('localhost','root','root');
	$conn = mysqli_connect('localhost','fly123','123456');

	if (!$conn) {
		die("connect error!!!
".mysqli_connect_error());
	}
	mysqli_select_db($conn,"mytestBase");
	//查询 members表
	// $sql = "select * from members;";
	//测试
	// $sql = "insert into users(fullname,weixin,login) values('fly123','123','123455');";

	//插入所有行 并拷贝指定列
	// $sql = 'insert into users(fullname,weixin,login) select name,weixin,id from members;';
	//拷贝avatar到 originPhotoUrl
	// $sql = 'update users,members set users.originPhotoUrl= members.avatar where  members.id = users.login;';
	//替换 /small/ 到 /big/
	// $sql = "update users set lowPhotoUrl = replace(lowPhotoUrl,'/big/','/small/')";
	//查看指定行的结果
	// $sql = 'select users.originPhotoUrl,users.normalPhotoUrl,users.originCoverUrl,users.normalCoverUrl,users.lowPhotoUrl from mytestBase.users;';
	//清空表中数据
	$sql = 'delete from users;';

	$result = mysqli_query($conn,$sql);
	echo '
'. var_dump($result).'
';
	//查看指定行的结果
	$sql1 = 'select users.fullname,users.weixin,users.login from users;';
	$result = mysqli_query($conn,$sql1);

	if ($result->num_rows >0) {
		 // 输出数据
    	while($row = $result->fetch_assoc()) {
        	echo "id: " . $row["id"]. " - fullname: " . $row["fullname"]. " weixin" . $row["weixin"]. "
";
    	}
	}else
		echo "无结果";

	echo "
连接成功
";
	mysqli_close();

?>


text:
layout: organization
category: local

title: St. Vincents Services
impact_area: Human Rights
keywords:
location_services:
location_offices:
website:

description:
mission: |
  To provide services to needy families in the community.

cash_grants:
grants:
service_opp:
services:

learn:
cont_relationship:

salutation:
first_name:
last_name:
title_contact_person:

city: Brooklyn
state: NY
address: |
  66 Boerum Place
  Brooklyn NY 11201
lat: 40.68964
lng: -73.990898
phone: 718-522-3700
ext:
fax:
email:
preferred_contact:
contact_person_intro:
---


text:
package pl.setblack.pongi.games.api;

import org.junit.jupiter.api.Test;

class GameStateTest {
    private static final float MINIMAL_DIST = 0.001f;

    @Test
    public void shouldMoveBallOnPush() {

    }

    private GameState createGameState(
            final Player player1, final Player player2, final Ball ball ) {
        final GameState state = new GameState(ball, Players.of(player1, player2), 0);
        return state;
    }

    private GameState createGameState( final float player1pos, final float player2pos,
                                       Ball ball) {
        return  createGameState(
                new Player( 0, "ply1",
                        movePaddleAllTheWay(Paddle.createPaddleForPlayer(1), player1pos)),
                new Player( 0, "ply2",
                        movePaddleAllTheWay(Paddle.createPaddleForPlayer(2), player2pos)),
                ball);

    }

    private Paddle movePaddleAllTheWay( Paddle paddle, float pos) {
        final Paddle p = paddle.movingTo(pos);
        if ( Math.abs(p.y - pos) < MINIMAL_DIST) {
               return p;
        } else {
            return movePaddleAllTheWay(p.paddleMove(1000), pos);
        }
    }
}


text:
package org.Webgatherer.ExperimentalLabs.EmailExtraction;

import com.google.inject.Guice;
import com.google.inject.Injector;
import org.Webgatherer.CoreEngine.Core.ThreadCommunication.ThreadCommunication;
import org.Webgatherer.CoreEngine.Core.ThreadCommunication.ThreadCommunicationBase;
import org.Webgatherer.CoreEngine.lib.WebDriverFactory;
import org.Webgatherer.ExperimentalLabs.DependencyInjection.DependencyBindingModule;
import org.Webgatherer.Persistence.InputOutput.PersistenceImpl_WriteToFile;
import org.Webgatherer.Persistence.InputOutput.WriterOutputQueueToFile;
import org.Webgatherer.Utility.ReadFiles;
import org.Webgatherer.WorkflowExample.Workflows.Implementations.WebGatherer.EnumUrlRetrieveOptions;

import java.util.List;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

/**
 * @author Rick Dane
 */
public class ExtractEmailsFromFileMain {

    private static final String inputFilePath = "/home/user/Dropbox/Rick/WebGatherer/Output/google/searchResultLinks.txt";
    private static final String outputFilePath = "/home/user/Dropbox/Rick/WebGatherer/Output/extractedEmails.txt";
    private static ReadFiles readFiles;
    private static int sizeOfStringArrayEnum = 9;

    private static WriterOutputQueueToFile writerOutputQueueToFile;

    public static void main(String[] args) {

        Injector injector = Guice.createInjector(new DependencyBindingModule());

        PageRetrieverThreadManagerEmailExtraction pageRetrieverThreadManager = injector.getInstance(PageRetrieverThreadManagerEmailExtraction.class);

        ThreadCommunication threadCommunication = injector.getInstance(ThreadCommunication.class);
        pageRetrieverThreadManager.configure(threadCommunication);

        readFiles = injector.getInstance(ReadFiles.class);

        writerOutputQueueToFile = injector.getInstance(WriterOutputQueueToFile.class);

        threadCommunication.setPageQueue(prepareQueue());

        while (!threadCommunication.isPageQueueEmpty()) {
            try {
                Thread.sleep(25);
            } catch (InterruptedException e) {
            }
            pageRetrieverThreadManager.run(EnumUrlRetrieveOptions.HTMLPAGE.ordinal());
        }

        try {
            Thread.sleep(20000);
        } catch (InterruptedException e) {
        }

        writerOutputQueueToFile.writeQueueToFile(outputFilePath, threadCommunication, ThreadCommunicationBase.PageQueueEntries.EMAIL_ADDRESSES.ordinal(), true, ",");

    }

    private static Queue<String> prepareQueue() {
        List<String> urls = readFiles.readLinesToList(inputFilePath);

        Queue queue = new ConcurrentLinkedQueue<String>();

        for (String curUrl : urls) {
            String[] testEntry = new String[sizeOfStringArrayEnum];
            testEntry[ThreadCommunicationBase.PageQueueEntries.BASE_URL.ordinal()] = curUrl;
            queue.add(testEntry);
        }
        return queue;
    }
}


text:
using System;
using System.Threading;
using NLog;

namespace Asterix.Framework.Common
{
    public static class Wait
    {
        private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
        public static void Until(Func<bool> condition, int timeOutInSecond = 60, int intervalInSecond = 1)
        {
            Logger.Info("Until was called");

            if (WaitHelper.WaitForCoditionComesTrueOrTimeOutIsReached(condition, timeOutInSecond, intervalInSecond))
            {
                return;
            }

            Logger.Error("Timeout reached without the condition had come true");
            throw new TimeoutException("Timeout reached without the condition had come true");
        }
    }
}


text:
/* vim: set ts=2 et sw=2 sts=2 fileencoding=utf-8: */
import scala.util.control.Exception._
import scala.util.{Try, Success, Failure}
import scala.collection.JavaConverters._
import scala.xml.Elem

import org.apache.poi.ss.usermodel._
import org.apache.poi.xssf.usermodel.XSSFWorkbook
import org.scalatra._

import java.io._
import java.net.URI
import java.nio.file._
import java.util.Properties

import javax.servlet.http.HttpServlet

import exceler.common._
import exceler.excel._
import excellib.ImplicitConversions._

import CommonLib._

object ExcelerConfig {

  val CONF_KEY = "exceler.conf"
  val DIR_KEY = "exceler.dir"
  val PORT_KEY = "exceler.port"

  val DEFAULT_CONF = "file:./exceler.conf"
  val DEFAULT_DIR ="."
  val DEFAULT_PORT ="9000"

  lazy val properties = MyProperties(CONF_KEY, new URI(DEFAULT_CONF))

  lazy val dir:String =
    properties.get(DIR_KEY).getOrElse(DEFAULT_DIR)

  lazy val port:Int =
    properties.get(PORT_KEY).getOrElse(DEFAULT_PORT).toInt
}


text:
namespace Castle.DynamicProxy.Test
{
	using System;
	using System.Collections;
	using System.Collections.Specialized;
	using System.Reflection;
	using System.Diagnostics;
	using System.Reflection.Emit;

	using Castle.DynamicProxy.Builder.CodeBuilder;
	using Castle.DynamicProxy.Builder.CodeGenerators;
	using Castle.DynamicProxy.Builder.CodeBuilder.SimpleAST;

	using NUnit.Framework;
	using Castle.DynamicProxy.Test.Classes;
	using System.Threading;

	[TestFixture]
	public class EasyTypeBuilderTestCase
	{
		ModuleScope module;

		[SetUp]
		public void Init()
		{
			module = new ModuleScope();
		}

		public void RunPEVerify()
		{
#if false
			if (module.SaveAssembly())
			{
				Process process = new Process();
				process.StartInfo.UseShellExecute = false;
				process.StartInfo.RedirectStandardOutput = true;
				process.StartInfo.Arguments = ModuleScope.FILE_NAME;
				// Hack. Should it find in the path?
				// I thought it would.
				process.StartInfo.FileName = @"C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\PEVerify.exe";
				process.Start();
				process.WaitForExit( Int32.MaxValue );
				if (process.ExitCode != 0)
				{
					Assert.Fail( process.StandardOutput.ReadToEnd() );
				}
			}
#endif
		}

		[Test]
		public void CreateSimpleType()
		{
			EasyType typebuilder = new EasyType( module, "mytype" );

			Type newType = typebuilder.BuildType();
			Assert.IsNotNull( newType );
			object instance = Activator.CreateInstance( newType );
			Assert.IsNotNull( instance );

			RunPEVerify();
		}

		[Test]
		public void CreateSimpleTypeWithExplicitDefaultConstructor()
		{
			EasyType typebuilder = new EasyType( module, "mytype" );
			typebuilder.CreateDefaultConstructor();

			Type newType = typebuilder.BuildType();
			Assert.IsNotNull( newType );
			object instance = Activator.CreateInstance( newType );
			Assert.IsNotNull( instance );

			RunPEVerify();
		}

		[Test]
		public void CreateSimpleTypeWithConstructor()
		{
			EasyType typebuilder = new EasyType( module, "mytype" );

			ArgumentReference arg1 = new ArgumentReference( typeof(String) );
			ArgumentReference arg2 = new ArgumentReference( typeof(int) );

			typebuilder.CreateConstructor( arg1, arg2 );

			Type newType = typebuilder.BuildType();
			Assert.IsNotNull( newType );
			object instance = Activator.CreateInstance( newType, new object[] { "message", 10 } );
			Assert.IsNotNull( instance );

			RunPEVerify();
		}

		[Test]
		public void EmptyMethodReturningVoid()
		{
			EasyType typebuilder = new EasyType( module, "mytype" );

			EasyMethod emptyMethod = typebuilder.CreateMethod( "DoSomething",
				new ReturnReferenceExpression( typeof(void) ) );

			Type newType = typebuilder.BuildType();
			Assert.IsNotNull( newType );
			object instance = Activator.CreateInstance( newType );
			Assert.IsNotNull( instance );

			MethodInfo method = instance.GetType().GetMethod("DoSomething");
			method.Invoke( instance, new object[0] );

			RunPEVerify();
		}

		[Test]
		public void EmptyMethodReturningInt()
		{
			EasyType typebuilder = new EasyType( module, "mytype" );

			EasyMethod emptyMethod = typebuilder.CreateMethod( "DoSomething",
				new ReturnReferenceExpression(typeof(int)) );

			Type newType = typebuilder.BuildType();
			Assert.IsNotNull( newType );
			object instance = Activator.CreateInstance( newType );
			Assert.IsNotNull( instance );

			MethodInfo method = instance.GetType().GetMethod("DoSomething");
			Assert.AreEqual( 0, method.Invoke( instance, new object[0] ) );

			RunPEVerify();
		}

		[Test]
		public void MethodCalc()
		{
			EasyType typebuilder = new EasyType( module, "mytype" );

			ArgumentReference arg1 = new ArgumentReference(typeof(int));
			ArgumentReference arg2 = new ArgumentReference(typeof(int));
			ReturnReferenceExpression ret = new ReturnReferenceExpression(typeof(int));

			EasyMethod calcMethod = typebuilder.CreateMethod( "Calc", ret, arg1, arg2 );

			calcMethod.CodeBuilder.AddStatement(
				new ReturnStatement(
					new BinaryExpression( BinaryExpression.Add, arg1.ToExpression(), arg2.ToExpression() ) ) );

			Type newType = typebuilder.BuildType();
			Assert.IsNotNull( newType );
			object instance = Activator.CreateInstance( newType );
			Assert.IsNotNull( instance );

			MethodInfo method = instance.GetType().GetMethod("Calc");
			Assert.AreEqual( 2, method.Invoke( instance, new object[] { 1,1 } ) );
			Assert.AreEqual( 5, method.Invoke( instance, new object[] { 3,2 } ) );

			RunPEVerify();
		}

		[Test]
		public void FieldsStoreAndLoad()
		{
			EasyType typebuilder = new EasyType( module, "mytype" );

			FieldReference field1 = typebuilder.CreateField( "field1", typeof(int) );
			FieldReference field2 = typebuilder.CreateField( "field2", typeof(string) );

			{
				ArgumentReference arg1 = new ArgumentReference(typeof(int));
				ArgumentReference arg2 = new ArgumentReference(typeof(string));

				EasyConstructor constr = typebuilder.CreateConstructor( arg1, arg2 );
				constr.CodeBuilder.InvokeBaseConstructor();
				constr.CodeBuilder.AddStatement( new AssignStatement( field1, arg1.ToExpression() ) );
				constr.CodeBuilder.AddStatement( new AssignStatement( field2, arg2.ToExpression() ) );
				constr.CodeBuilder.AddStatement( new ReturnStatement() );
			}

			{
				ReturnReferenceExpression ret1 = new ReturnReferenceExpression( typeof(int) );
				EasyMethod m1 = typebuilder.CreateMethod( "GetField1", ret1 );
				m1.CodeBuilder.AddStatement( new ReturnStatement( field1 ) );

				ReturnReferenceExpression ret2 = new ReturnReferenceExpression( typeof(string) );
				EasyMethod m2 = typebuilder.CreateMethod( "GetField2", ret2 );
				m2.CodeBuilder.AddStatement( new ReturnStatement( field2 ) );
			}

			Type newType = typebuilder.BuildType();
			Assert.IsNotNull( newType );
			object instance = Activator.CreateInstance( newType, new object[] { 10, "hello" } );
			Assert.IsNotNull( instance );

			MethodInfo method1 = instance.GetType().GetMethod("GetField1");
			MethodInfo method2 = instance.GetType().GetMethod("GetField2");
			Assert.AreEqual( 10, method1.Invoke( instance, new object[0] ));
			Assert.AreEqual( "hello", method2.Invoke( instance, new object[0] ));

			RunPEVerify();
		}

		[Test]
		public void MethodInvokingMethod()
		{
			EasyType typebuilder = new EasyType( module, "mytype" );

			ArgumentReference rarg1 = new ArgumentReference(typeof(int));
			ArgumentReference rarg2 = new ArgumentReference(typeof(int));
			ReturnReferenceExpression rret = new ReturnReferenceExpression(typeof(int));
			EasyMethod realCalcMethod = typebuilder.CreateMethod( "RealCalc", rret, rarg1, rarg2 );
			realCalcMethod.CodeBuilder.AddStatement(
				new ReturnStatement(
					new BinaryExpression( BinaryExpression.Add, rarg1.ToExpression(), rarg2.ToExpression() ) ) );

			ArgumentReference arg1 = new ArgumentReference(typeof(int));
			ArgumentReference arg2 = new ArgumentReference(typeof(int));
			ReturnReferenceExpression ret = new ReturnReferenceExpression(typeof(int));
			EasyMethod calcMethod = typebuilder.CreateMethod( "Calc", ret, arg1, arg2 );
			calcMethod.CodeBuilder.AddStatement(
				new ReturnStatement(
					new MethodInvocationExpression( realCalcMethod, arg1.ToExpression(), arg2.ToExpression() ) ) );

			Type newType = typebuilder.BuildType();
			Assert.IsNotNull( newType );
			object instance = Activator.CreateInstance( newType, new object[0] );
			Assert.IsNotNull( instance );

			MethodInfo method = instance.GetType().GetMethod("Calc");
			Assert.AreEqual( 2, method.Invoke( instance, new object[] { 1,1 } ) );
			Assert.AreEqual( 5, method.Invoke( instance, new object[] { 3,2 } ) );
			method = instance.GetType().GetMethod("RealCalc");
			Assert.AreEqual( 2, method.Invoke( instance, new object[] { 1,1 } ) );
			Assert.AreEqual( 5, method.Invoke( instance, new object[] { 3,2 } ) );

			RunPEVerify();
		}

		[Test]
		public void NewInstanceExpression()
		{
			EasyType typebuilder = new EasyType( module, "mytype" );

			FieldReference cachefield = typebuilder.CreateField( "cache", typeof(ArrayList) );

			EasyConstructor constructor = typebuilder.CreateConstructor( );
			constructor.CodeBuilder.InvokeBaseConstructor();
			constructor.CodeBuilder.AddStatement( new AssignStatement(cachefield,
				new NewInstanceExpression( typeof(ArrayList), new Type[0] ) ) );
			constructor.CodeBuilder.AddStatement( new ReturnStatement() );

			ReturnReferenceExpression ret = new ReturnReferenceExpression(typeof(ArrayList));
			EasyMethod getCache = typebuilder.CreateMethod( "GetCache", ret );
			getCache.CodeBuilder.AddStatement( new ReturnStatement( cachefield ) );

			Type newType = typebuilder.BuildType();
			Assert.IsNotNull( newType );
			object instance = Activator.CreateInstance( newType, new object[0] );
			Assert.IsNotNull( instance );

			MethodInfo method = instance.GetType().GetMethod("GetCache");
			Assert.IsNotNull( method.Invoke( instance, new object[0] ) );

			RunPEVerify();
		}

		[Test]
		public void BlockWithLock()
		{
			EasyType typebuilder = new EasyType( module, "mytype" );

			FieldReference cachefield = typebuilder.CreateField( "cache", typeof(ArrayList) );

			EasyConstructor constructor = typebuilder.CreateConstructor( );
			constructor.CodeBuilder.InvokeBaseConstructor();

			LockBlockExpression block = new LockBlockExpression( SelfReference.Self );

			block.AddStatement( new AssignStatement(cachefield,
				new NewInstanceExpression( typeof(ArrayList), new Type[0] ) ) );

			constructor.CodeBuilder.AddStatement( new ExpressionStatement(block) );
			constructor.CodeBuilder.AddStatement( new ReturnStatement() );

			ReturnReferenceExpression ret = new ReturnReferenceExpression(typeof(ArrayList));
			EasyMethod getCache = typebuilder.CreateMethod( "GetCache", ret );
			getCache.CodeBuilder.AddStatement( new ReturnStatement( cachefield ) );

			Type newType = typebuilder.BuildType();
			Assert.IsNotNull( newType );
			object instance = Activator.CreateInstance( newType, new object[0] );
			Assert.IsNotNull( instance );

			MethodInfo method = instance.GetType().GetMethod("GetCache");
			Assert.IsNotNull( method.Invoke( instance, new object[0] ) );

			RunPEVerify();
		}

		[Test]
		public void Conditionals()
		{
			EasyType typebuilder = new EasyType( module, "mytype" );

			FieldReference cachefield = typebuilder.CreateField( "cache", typeof(IDictionary) );

			ArgumentReference arg = new ArgumentReference( typeof(bool) );

			EasyConstructor constructor = typebuilder.CreateConstructor( arg );
			constructor.CodeBuilder.InvokeBaseConstructor();

			ConditionExpression exp = new ConditionExpression(OpCodes.Brtrue_S, arg.ToExpression());
			exp.AddTrueStatement( new AssignStatement(cachefield,
				new NewInstanceExpression( typeof(HybridDictionary), new Type[0] ) ) );
			exp.AddFalseStatement( new AssignStatement(cachefield,
				new NewInstanceExpression( typeof(Hashtable), new Type[0] ) ) );

			constructor.CodeBuilder.AddStatement( new ExpressionStatement(exp) );
			constructor.CodeBuilder.AddStatement( new ReturnStatement() );

			ReturnReferenceExpression ret = new ReturnReferenceExpression(typeof(IDictionary));
			EasyMethod getCache = typebuilder.CreateMethod( "GetCache", ret );
			getCache.CodeBuilder.AddStatement( new ReturnStatement( cachefield ) );

			Type newType = typebuilder.BuildType();
			object instance = Activator.CreateInstance( newType, new object[] { true } );
			MethodInfo method = instance.GetType().GetMethod("GetCache");
			object dic = method.Invoke( instance, new object[0] );
			Assert.IsTrue( dic is HybridDictionary );

			instance = Activator.CreateInstance( newType, new object[] { false } );
			dic = method.Invoke( instance, new object[0] );
			Assert.IsTrue( dic is Hashtable );

			RunPEVerify();
		}

		[Test]
		public void ArrayRefs()
		{
			EasyType typebuilder = new EasyType( module, "mytype" );

			FieldReference field1 = typebuilder.CreateField( "field1", typeof(object) );
			FieldReference field2 = typebuilder.CreateField( "field2", typeof(object) );

			ArgumentReference arg = new ArgumentReference( typeof(object[]) );

			EasyConstructor constructor = typebuilder.CreateConstructor( arg );
			constructor.CodeBuilder.InvokeBaseConstructor();

			constructor.CodeBuilder.AddStatement( new AssignStatement(field1,
				new LoadRefArrayElementExpression( 0, arg ) ) );
			constructor.CodeBuilder.AddStatement( new AssignStatement(field2,
				new LoadRefArrayElementExpression( 1, arg ) ) );

			constructor.CodeBuilder.AddStatement( new ReturnStatement() );

			ReturnReferenceExpression ret1 = new ReturnReferenceExpression(typeof(object));
			EasyMethod getField1 = typebuilder.CreateMethod( "GetField1", ret1 );
			getField1.CodeBuilder.AddStatement( new ReturnStatement( field1 ) );

			ReturnReferenceExpression ret2 = new ReturnReferenceExpression(typeof(object));
			EasyMethod getField2 = typebuilder.CreateMethod( "GetField2", ret2 );
			getField2.CodeBuilder.AddStatement( new ReturnStatement( field2 ) );

			Type newType = typebuilder.BuildType();
			object[] innerArgs = new object[] { "hammett", "verissimo" };
			object instance = Activator.CreateInstance( newType, new object[] { innerArgs } );

			MethodInfo method = instance.GetType().GetMethod("GetField1");
			object result = method.Invoke( instance, new object[0] );
			Assert.AreEqual( "hammett", result );

			method = instance.GetType().GetMethod("GetField2");
			result = method.Invoke( instance, new object[0] );
			Assert.AreEqual( "verissimo", result );

			RunPEVerify();
		}

		[Test]
		public void CreateCallable()
		{
			EasyType typebuilder = new EasyType( module, "mytype" );
			EasyCallable callable = typebuilder.CreateCallable( new ReturnReferenceExpression(typeof(string)) );

			FieldReference field1 = typebuilder.CreateField( "field1", callable.TypeBuilder );

			SimpleCallback sc = new SimpleCallback();

			ArgumentReference arg = new ArgumentReference( typeof(SimpleCallback) );
			EasyConstructor constructor = typebuilder.CreateConstructor( arg );
			constructor.CodeBuilder.InvokeBaseConstructor();

			constructor.CodeBuilder.AddStatement( new AssignStatement(field1,
				new NewInstanceExpression( callable,
					arg.ToExpression(),
					new MethodPointerExpression( arg, typeof(SimpleCallback).GetMethod("Run") ) ) ) );

			constructor.CodeBuilder.AddStatement( new ReturnStatement() );

			ReturnReferenceExpression ret1 = new ReturnReferenceExpression(typeof(string));
			EasyMethod getField1 = typebuilder.CreateMethod( "Exec", ret1 );
			getField1.CodeBuilder.AddStatement(
				new ReturnStatement(
					new ConvertExpression( typeof(String),
					new MethodInvocationExpression( field1,
						callable.Callmethod,
							new ReferencesToObjectArrayExpression() ) ) ) );

			Type newType = typebuilder.BuildType();

			RunPEVerify();

			object instance = Activator.CreateInstance( newType, new object[] { sc } );

			MethodInfo method = instance.GetType().GetMethod("Exec");
			object result = method.Invoke( instance, new object[0] );
			Assert.AreEqual( "hello", result );
		}

		[Test]
		public void CreateMoreComplexCallable()
		{
			EasyType typebuilder = new EasyType( module, "mytype" );

			ArgumentReference arg1 = new ArgumentReference( typeof(int) );
			ArgumentReference arg2 = new ArgumentReference( typeof(DateTime) );
			ArgumentReference arg3 = new ArgumentReference( typeof(object) );
			EasyCallable callable = typebuilder.CreateCallable(
				new ReturnReferenceExpression(typeof(string)),
				arg1, arg2, arg3 );

			FieldReference field1 = typebuilder.CreateField( "field1", callable.TypeBuilder );

			SimpleCallback sc = new SimpleCallback();

			ArgumentReference arg = new ArgumentReference( typeof(SimpleCallback) );
			EasyConstructor constructor = typebuilder.CreateConstructor( arg );
			constructor.CodeBuilder.InvokeBaseConstructor();

			constructor.CodeBuilder.AddStatement( new AssignStatement(field1,
				new NewInstanceExpression( callable,
				arg.ToExpression(),
				new MethodPointerExpression( arg, typeof(SimpleCallback).GetMethod("RunAs") ) ) ) );

			constructor.CodeBuilder.AddStatement( new ReturnStatement() );

			arg1 = new ArgumentReference( typeof(int) );
			arg2 = new ArgumentReference( typeof(DateTime) );
			arg3 = new ArgumentReference( typeof(object) );

			ReturnReferenceExpression ret1 = new ReturnReferenceExpression(typeof(string));

			EasyMethod getField1 = typebuilder.CreateMethod( "Exec", ret1, arg1, arg2, arg3 );
			getField1.CodeBuilder.AddStatement(
				new ReturnStatement(
					new ConvertExpression( typeof(String),
						new MethodInvocationExpression( field1,
							callable.Callmethod,
							new ReferencesToObjectArrayExpression(arg1, arg2, arg3) ) ) ) );

			Type newType = typebuilder.BuildType();

			RunPEVerify();

			object instance = Activator.CreateInstance( newType, new object[] { sc } );

			MethodInfo method = instance.GetType().GetMethod("Exec");
			object result = method.Invoke( instance, new object[] { 1, DateTime.Now, "" } );
			Assert.AreEqual( "hello2", result );
		}

		public class SimpleCallback
		{
			public String Run()
			{
				return "hello";
			}

			public String RunAs(int value, DateTime dt, object placeholder)
			{
				return "hello2";
			}
		}

		[Test]
		public void EmptyMethodWithPrimitiveTypeRefArg()
		{
			EasyType typebuilder = new EasyType( module, "mytype" );

			int refArgInst = 42;
			Type refType = GetPrimitiveRefType(ref refArgInst);

			ArgumentReference refArg = new ArgumentReference( refType );
			ReturnReferenceExpression ret = new ReturnReferenceExpression(typeof(int));

			EasyMethod emptyMethod = typebuilder.CreateMethod( "DoSomething", ret, refArg );

			Type newType = typebuilder.BuildType();
			Assert.IsNotNull( newType );
			object instance = Activator.CreateInstance( newType );
			Assert.IsNotNull( instance );

			MethodInfo method = instance.GetType().GetMethod("DoSomething");
			method.Invoke( instance, new object[]{refArgInst} );

			Assert.AreEqual(42, refArgInst, "Argument made round-trip successfully");

			RunPEVerify();
		}

		[Test]
		public void EmptyMethodWithReferenceTypeRefArg()
		{
			EasyType typebuilder = new EasyType(module, "mytype");

			string refArgInst = "foobar";
			Type refType = GetReferenceRefType(ref refArgInst);

			ArgumentReference refArg = new ArgumentReference(refType);
			ReturnReferenceExpression ret = new ReturnReferenceExpression(typeof(int));

			EasyMethod emptyMethod = typebuilder.CreateMethod("DoSomething", ret, refArg);

			Type newType = typebuilder.BuildType();
			Assert.IsNotNull(newType);
			object instance = Activator.CreateInstance(newType);
			Assert.IsNotNull(instance);

			MethodInfo method = instance.GetType().GetMethod("DoSomething");
			method.Invoke(instance, new object[] { refArgInst });

			Assert.AreEqual("foobar", refArgInst, "Argument made round-trip successfully");

			RunPEVerify();
		}

		[Test]
		public void EmptyMethodWithStructTypeRefArg()
		{
			EasyType typebuilder = new EasyType(module, "mytype");

			DateTime refArgInst = new DateTime(2005, 1, 1);
			Type refType = GetStructRefType(ref refArgInst);

			Assert.IsTrue(refType.IsByRef);

			ArgumentReference refArg = new ArgumentReference(refType);
			ReturnReferenceExpression ret = new ReturnReferenceExpression(typeof(int));

			EasyMethod emptyMethod = typebuilder.CreateMethod("DoSomething", ret, refArg);

			Type newType = typebuilder.BuildType();
			Assert.IsNotNull(newType);
			object instance = Activator.CreateInstance(newType);
			Assert.IsNotNull(instance);

			MethodInfo method = instance.GetType().GetMethod("DoSomething");
			method.Invoke(instance, new object[] { refArgInst });

			Assert.AreEqual(new DateTime(2005, 1, 1), refArgInst, "Argument made round-trip successfully");

			RunPEVerify();
		}

		[Test]
		public void EmptyMethodWithEnumTypeRefArg()
		{
			EasyType typebuilder = new EasyType(module, "mytype");

			SByteEnum refArgInst = SByteEnum.Two;
			Type refType = GetEnumRefType(ref refArgInst);

			Assert.IsTrue(refType.IsByRef);

			ArgumentReference refArg = new ArgumentReference(refType);
			ReturnReferenceExpression ret = new ReturnReferenceExpression(typeof(int));

			EasyMethod emptyMethod = typebuilder.CreateMethod("DoSomething", ret, refArg);

			Type newType = typebuilder.BuildType();
			Assert.IsNotNull(newType);
			object instance = Activator.CreateInstance(newType);
			Assert.IsNotNull(instance);

			MethodInfo method = instance.GetType().GetMethod("DoSomething");
			method.Invoke(instance, new object[] { refArgInst });

			Assert.AreEqual(SByteEnum.Two, refArgInst, "Argument made round-trip successfully");

			RunPEVerify();
		}

		public Type GetPrimitiveRefType(ref int refArg)
		{
			// Need this because .Net 1.1 does not have the Type.MakeByRefType method.
			ParameterInfo[] parameters = this.GetType().GetMethod("GetPrimitiveRefType").GetParameters();
			Assert.AreEqual(1, parameters.Length);
			Type refType = parameters[0].ParameterType;
			Assert.IsTrue(refType.IsByRef);
			return refType;
		}

		public Type GetReferenceRefType(ref string refArg)
		{
			// Need this because .Net 1.1 does not have the Type.MakeByRefType method.
			ParameterInfo[] parameters = this.GetType().GetMethod("GetReferenceRefType").GetParameters();
			Assert.AreEqual(1, parameters.Length);
			Type refType = parameters[0].ParameterType;
			Assert.IsTrue(refType.IsByRef);
			return refType;
		}

		public Type GetStructRefType(ref DateTime refArg)
		{
			// Need this because .Net 1.1 does not have the Type.MakeByRefType method.
			ParameterInfo[] parameters = this.GetType().GetMethod("GetStructRefType").GetParameters();
			Assert.AreEqual(1, parameters.Length);
			Type refType = parameters[0].ParameterType;
			Assert.IsTrue(refType.IsByRef);
			return refType;
		}

		public Type GetEnumRefType(ref SByteEnum refArg)
		{
			// Need this because .Net 1.1 does not have the Type.MakeByRefType method.
			ParameterInfo[] parameters = this.GetType().GetMethod("GetEnumRefType").GetParameters();
			Assert.AreEqual(1, parameters.Length);
			Type refType = parameters[0].ParameterType;
			Assert.IsTrue(refType.IsByRef);
			return refType;
		}
	}
}


text:
// Generated by the protocol buffer compiler.  DO NOT EDIT!
// source: tensorflow/core/protobuf/worker.proto

package org.tensorflow.distruntime;

/**
 * Protobuf type {@code tensorflow.TraceOpts}
 */
public  final class TraceOpts extends
    com.google.protobuf.GeneratedMessageV3 implements
    // @@protoc_insertion_point(message_implements:tensorflow.TraceOpts)
    TraceOptsOrBuilder {
  // Use TraceOpts.newBuilder() to construct.
  private TraceOpts(com.google.protobuf.GeneratedMessageV3.Builder<?> builder) {
    super(builder);
  }
  private TraceOpts() {
    duration_ = 0D;
    useStepProfiler_ = false;
    useKernelProfiler_ = false;
    useExtendedProfiler_ = false;
    useGpuProfiler_ = false;
    useSampleProfiler_ = false;
  }

  @java.lang.Override
  public final com.google.protobuf.UnknownFieldSet
  getUnknownFields() {
    return com.google.protobuf.UnknownFieldSet.getDefaultInstance();
  }
  private TraceOpts(
      com.google.protobuf.CodedInputStream input,
      com.google.protobuf.ExtensionRegistryLite extensionRegistry)
      throws com.google.protobuf.InvalidProtocolBufferException {
    this();
    int mutable_bitField0_ = 0;
    try {
      boolean done = false;
      while (!done) {
        int tag = input.readTag();
        switch (tag) {
          case 0:
            done = true;
            break;
          default: {
            if (!input.skipField(tag)) {
              done = true;
            }
            break;
          }
          case 9: {

            duration_ = input.readDouble();
            break;
          }
          case 16: {

            useStepProfiler_ = input.readBool();
            break;
          }
          case 24: {

            useKernelProfiler_ = input.readBool();
            break;
          }
          case 32: {

            useExtendedProfiler_ = input.readBool();
            break;
          }
          case 40: {

            useGpuProfiler_ = input.readBool();
            break;
          }
          case 48: {

            useSampleProfiler_ = input.readBool();
            break;
          }
        }
      }
    } catch (com.google.protobuf.InvalidProtocolBufferException e) {
      throw e.setUnfinishedMessage(this);
    } catch (java.io.IOException e) {
      throw new com.google.protobuf.InvalidProtocolBufferException(
          e).setUnfinishedMessage(this);
    } finally {
      makeExtensionsImmutable();
    }
  }
  public static final com.google.protobuf.Descriptors.Descriptor
      getDescriptor() {
    return org.tensorflow.distruntime.WorkerProtos.internal_static_tensorflow_TraceOpts_descriptor;
  }

  protected com.google.protobuf.GeneratedMessageV3.FieldAccessorTable
      internalGetFieldAccessorTable() {
    return org.tensorflow.distruntime.WorkerProtos.internal_static_tensorflow_TraceOpts_fieldAccessorTable
        .ensureFieldAccessorsInitialized(
            org.tensorflow.distruntime.TraceOpts.class, org.tensorflow.distruntime.TraceOpts.Builder.class);
  }

  public static final int DURATION_FIELD_NUMBER = 1;
  private double duration_;
  /**
   *

   * Length of the trace to be taken, in seconds.
   *

   *
   * double duration = 1;
   */
  public double getDuration() {
    return duration_;
  }

  public static final int USE_STEP_PROFILER_FIELD_NUMBER = 2;
  private boolean useStepProfiler_;
  /**
   *

   * If true, capture step profile locally in each worker. Currently
   * unimplemented.
   *

   *
   * bool use_step_profiler = 2;
   */
  public boolean getUseStepProfiler() {
    return useStepProfiler_;
  }

  public static final int USE_KERNEL_PROFILER_FIELD_NUMBER = 3;
  private boolean useKernelProfiler_;
  /**
   *

   * If true, capture kernel events from each worker.
   *

   *
   * bool use_kernel_profiler = 3;
   */
  public boolean getUseKernelProfiler() {
    return useKernelProfiler_;
  }

  public static final int USE_EXTENDED_PROFILER_FIELD_NUMBER = 4;
  private boolean useExtendedProfiler_;
  /**
   *

   * If true, capture extended profiling events from TensorFlow process.
   *

   *
   * bool use_extended_profiler = 4;
   */
  public boolean getUseExtendedProfiler() {
    return useExtendedProfiler_;
  }

  public static final int USE_GPU_PROFILER_FIELD_NUMBER = 5;
  private boolean useGpuProfiler_;
  /**
   *

   * If true, capture GPU profiling events locally on each
   * machine. Currently unimplemented.
   *

   *
   * bool use_gpu_profiler = 5;
   */
  public boolean getUseGpuProfiler() {
    return useGpuProfiler_;
  }

  public static final int USE_SAMPLE_PROFILER_FIELD_NUMBER = 6;
  private boolean useSampleProfiler_;
  /**
   *

   * If true, collect sampled profile events. Currently unimplemented.
   *

   *
   * bool use_sample_profiler = 6;
   */
  public boolean getUseSampleProfiler() {
    return useSampleProfiler_;
  }

  private byte memoizedIsInitialized = -1;
  public final boolean isInitialized() {
    byte isInitialized = memoizedIsInitialized;
    if (isInitialized == 1) return true;
    if (isInitialized == 0) return false;

    memoizedIsInitialized = 1;
    return true;
  }

  public void writeTo(com.google.protobuf.CodedOutputStream output)
                      throws java.io.IOException {
    if (duration_ != 0D) {
      output.writeDouble(1, duration_);
    }
    if (useStepProfiler_ != false) {
      output.writeBool(2, useStepProfiler_);
    }
    if (useKernelProfiler_ != false) {
      output.writeBool(3, useKernelProfiler_);
    }
    if (useExtendedProfiler_ != false) {
      output.writeBool(4, useExtendedProfiler_);
    }
    if (useGpuProfiler_ != false) {
      output.writeBool(5, useGpuProfiler_);
    }
    if (useSampleProfiler_ != false) {
      output.writeBool(6, useSampleProfiler_);
    }
  }

  public int getSerializedSize() {
    int size = memoizedSize;
    if (size != -1) return size;

    size = 0;
    if (duration_ != 0D) {
      size += com.google.protobuf.CodedOutputStream
        .computeDoubleSize(1, duration_);
    }
    if (useStepProfiler_ != false) {
      size += com.google.protobuf.CodedOutputStream
        .computeBoolSize(2, useStepProfiler_);
    }
    if (useKernelProfiler_ != false) {
      size += com.google.protobuf.CodedOutputStream
        .computeBoolSize(3, useKernelProfiler_);
    }
    if (useExtendedProfiler_ != false) {
      size += com.google.protobuf.CodedOutputStream
        .computeBoolSize(4, useExtendedProfiler_);
    }
    if (useGpuProfiler_ != false) {
      size += com.google.protobuf.CodedOutputStream
        .computeBoolSize(5, useGpuProfiler_);
    }
    if (useSampleProfiler_ != false) {
      size += com.google.protobuf.CodedOutputStream
        .computeBoolSize(6, useSampleProfiler_);
    }
    memoizedSize = size;
    return size;
  }

  private static final long serialVersionUID = 0L;
  @java.lang.Override
  public boolean equals(final java.lang.Object obj) {
    if (obj == this) {
     return true;
    }
    if (!(obj instanceof org.tensorflow.distruntime.TraceOpts)) {
      return super.equals(obj);
    }
    org.tensorflow.distruntime.TraceOpts other = (org.tensorflow.distruntime.TraceOpts) obj;

    boolean result = true;
    result = result && (
        java.lang.Double.doubleToLongBits(getDuration())
        == java.lang.Double.doubleToLongBits(
            other.getDuration()));
    result = result && (getUseStepProfiler()
        == other.getUseStepProfiler());
    result = result && (getUseKernelProfiler()
        == other.getUseKernelProfiler());
    result = result && (getUseExtendedProfiler()
        == other.getUseExtendedProfiler());
    result = result && (getUseGpuProfiler()
        == other.getUseGpuProfiler());
    result = result && (getUseSampleProfiler()
        == other.getUseSampleProfiler());
    return result;
  }

  @java.lang.Override
  public int hashCode() {
    if (memoizedHashCode != 0) {
      return memoizedHashCode;
    }
    int hash = 41;
    hash = (19 * hash) + getDescriptor().hashCode();
    hash = (37 * hash) + DURATION_FIELD_NUMBER;
    hash = (53 * hash) + com.google.protobuf.Internal.hashLong(
        java.lang.Double.doubleToLongBits(getDuration()));
    hash = (37 * hash) + USE_STEP_PROFILER_FIELD_NUMBER;
    hash = (53 * hash) + com.google.protobuf.Internal.hashBoolean(
        getUseStepProfiler());
    hash = (37 * hash) + USE_KERNEL_PROFILER_FIELD_NUMBER;
    hash = (53 * hash) + com.google.protobuf.Internal.hashBoolean(
        getUseKernelProfiler());
    hash = (37 * hash) + USE_EXTENDED_PROFILER_FIELD_NUMBER;
    hash = (53 * hash) + com.google.protobuf.Internal.hashBoolean(
        getUseExtendedProfiler());
    hash = (37 * hash) + USE_GPU_PROFILER_FIELD_NUMBER;
    hash = (53 * hash) + com.google.protobuf.Internal.hashBoolean(
        getUseGpuProfiler());
    hash = (37 * hash) + USE_SAMPLE_PROFILER_FIELD_NUMBER;
    hash = (53 * hash) + com.google.protobuf.Internal.hashBoolean(
        getUseSampleProfiler());
    hash = (29 * hash) + unknownFields.hashCode();
    memoizedHashCode = hash;
    return hash;
  }

  public static org.tensorflow.distruntime.TraceOpts parseFrom(
      com.google.protobuf.ByteString data)
      throws com.google.protobuf.InvalidProtocolBufferException {
    return PARSER.parseFrom(data);
  }
  public static org.tensorflow.distruntime.TraceOpts parseFrom(
      com.google.protobuf.ByteString data,
      com.google.protobuf.ExtensionRegistryLite extensionRegistry)
      throws com.google.protobuf.InvalidProtocolBufferException {
    return PARSER.parseFrom(data, extensionRegistry);
  }
  public static org.tensorflow.distruntime.TraceOpts parseFrom(byte[] data)
      throws com.google.protobuf.InvalidProtocolBufferException {
    return PARSER.parseFrom(data);
  }
  public static org.tensorflow.distruntime.TraceOpts parseFrom(
      byte[] data,
      com.google.protobuf.ExtensionRegistryLite extensionRegistry)
      throws com.google.protobuf.InvalidProtocolBufferException {
    return PARSER.parseFrom(data, extensionRegistry);
  }
  public static org.tensorflow.distruntime.TraceOpts parseFrom(java.io.InputStream input)
      throws java.io.IOException {
    return com.google.protobuf.GeneratedMessageV3
        .parseWithIOException(PARSER, input);
  }
  public static org.tensorflow.distruntime.TraceOpts parseFrom(
      java.io.InputStream input,
      com.google.protobuf.ExtensionRegistryLite extensionRegistry)
      throws java.io.IOException {
    return com.google.protobuf.GeneratedMessageV3
        .parseWithIOException(PARSER, input, extensionRegistry);
  }
  public static org.tensorflow.distruntime.TraceOpts parseDelimitedFrom(java.io.InputStream input)
      throws java.io.IOException {
    return com.google.protobuf.GeneratedMessageV3
        .parseDelimitedWithIOException(PARSER, input);
  }
  public static org.tensorflow.distruntime.TraceOpts parseDelimitedFrom(
      java.io.InputStream input,
      com.google.protobuf.ExtensionRegistryLite extensionRegistry)
      throws java.io.IOException {
    return com.google.protobuf.GeneratedMessageV3
        .parseDelimitedWithIOException(PARSER, input, extensionRegistry);
  }
  public static org.tensorflow.distruntime.TraceOpts parseFrom(
      com.google.protobuf.CodedInputStream input)
      throws java.io.IOException {
    return com.google.protobuf.GeneratedMessageV3
        .parseWithIOException(PARSER, input);
  }
  public static org.tensorflow.distruntime.TraceOpts parseFrom(
      com.google.protobuf.CodedInputStream input,
      com.google.protobuf.ExtensionRegistryLite extensionRegistry)
      throws java.io.IOException {
    return com.google.protobuf.GeneratedMessageV3
        .parseWithIOException(PARSER, input, extensionRegistry);
  }

  public Builder newBuilderForType() { return newBuilder(); }
  public static Builder newBuilder() {
    return DEFAULT_INSTANCE.toBuilder();
  }
  public static Builder newBuilder(org.tensorflow.distruntime.TraceOpts prototype) {
    return DEFAULT_INSTANCE.toBuilder().mergeFrom(prototype);
  }
  public Builder toBuilder() {
    return this == DEFAULT_INSTANCE
        ? new Builder() : new Builder().mergeFrom(this);
  }

  @java.lang.Override
  protected Builder newBuilderForType(
      com.google.protobuf.GeneratedMessageV3.BuilderParent parent) {
    Builder builder = new Builder(parent);
    return builder;
  }
  /**
   * Protobuf type {@code tensorflow.TraceOpts}
   */
  public static final class Builder extends
      com.google.protobuf.GeneratedMessageV3.Builder<Builder> implements
      // @@protoc_insertion_point(builder_implements:tensorflow.TraceOpts)
      org.tensorflow.distruntime.TraceOptsOrBuilder {
    public static final com.google.protobuf.Descriptors.Descriptor
        getDescriptor() {
      return org.tensorflow.distruntime.WorkerProtos.internal_static_tensorflow_TraceOpts_descriptor;
    }

    protected com.google.protobuf.GeneratedMessageV3.FieldAccessorTable
        internalGetFieldAccessorTable() {
      return org.tensorflow.distruntime.WorkerProtos.internal_static_tensorflow_TraceOpts_fieldAccessorTable
          .ensureFieldAccessorsInitialized(
              org.tensorflow.distruntime.TraceOpts.class, org.tensorflow.distruntime.TraceOpts.Builder.class);
    }

    // Construct using org.tensorflow.distruntime.TraceOpts.newBuilder()
    private Builder() {
      maybeForceBuilderInitialization();
    }

    private Builder(
        com.google.protobuf.GeneratedMessageV3.BuilderParent parent) {
      super(parent);
      maybeForceBuilderInitialization();
    }
    private void maybeForceBuilderInitialization() {
      if (com.google.protobuf.GeneratedMessageV3
              .alwaysUseFieldBuilders) {
      }
    }
    public Builder clear() {
      super.clear();
      duration_ = 0D;

      useStepProfiler_ = false;

      useKernelProfiler_ = false;

      useExtendedProfiler_ = false;

      useGpuProfiler_ = false;

      useSampleProfiler_ = false;

      return this;
    }

    public com.google.protobuf.Descriptors.Descriptor
        getDescriptorForType() {
      return org.tensorflow.distruntime.WorkerProtos.internal_static_tensorflow_TraceOpts_descriptor;
    }

    public org.tensorflow.distruntime.TraceOpts getDefaultInstanceForType() {
      return org.tensorflow.distruntime.TraceOpts.getDefaultInstance();
    }

    public org.tensorflow.distruntime.TraceOpts build() {
      org.tensorflow.distruntime.TraceOpts result = buildPartial();
      if (!result.isInitialized()) {
        throw newUninitializedMessageException(result);
      }
      return result;
    }

    public org.tensorflow.distruntime.TraceOpts buildPartial() {
      org.tensorflow.distruntime.TraceOpts result = new org.tensorflow.distruntime.TraceOpts(this);
      result.duration_ = duration_;
      result.useStepProfiler_ = useStepProfiler_;
      result.useKernelProfiler_ = useKernelProfiler_;
      result.useExtendedProfiler_ = useExtendedProfiler_;
      result.useGpuProfiler_ = useGpuProfiler_;
      result.useSampleProfiler_ = useSampleProfiler_;
      onBuilt();
      return result;
    }

    public Builder clone() {
      return (Builder) super.clone();
    }
    public Builder setField(
        com.google.protobuf.Descriptors.FieldDescriptor field,
        Object value) {
      return (Builder) super.setField(field, value);
    }
    public Builder clearField(
        com.google.protobuf.Descriptors.FieldDescriptor field) {
      return (Builder) super.clearField(field);
    }
    public Builder clearOneof(
        com.google.protobuf.Descriptors.OneofDescriptor oneof) {
      return (Builder) super.clearOneof(oneof);
    }
    public Builder setRepeatedField(
        com.google.protobuf.Descriptors.FieldDescriptor field,
        int index, Object value) {
      return (Builder) super.setRepeatedField(field, index, value);
    }
    public Builder addRepeatedField(
        com.google.protobuf.Descriptors.FieldDescriptor field,
        Object value) {
      return (Builder) super.addRepeatedField(field, value);
    }
    public Builder mergeFrom(com.google.protobuf.Message other) {
      if (other instanceof org.tensorflow.distruntime.TraceOpts) {
        return mergeFrom((org.tensorflow.distruntime.TraceOpts)other);
      } else {
        super.mergeFrom(other);
        return this;
      }
    }

    public Builder mergeFrom(org.tensorflow.distruntime.TraceOpts other) {
      if (other == org.tensorflow.distruntime.TraceOpts.getDefaultInstance()) return this;
      if (other.getDuration() != 0D) {
        setDuration(other.getDuration());
      }
      if (other.getUseStepProfiler() != false) {
        setUseStepProfiler(other.getUseStepProfiler());
      }
      if (other.getUseKernelProfiler() != false) {
        setUseKernelProfiler(other.getUseKernelProfiler());
      }
      if (other.getUseExtendedProfiler() != false) {
        setUseExtendedProfiler(other.getUseExtendedProfiler());
      }
      if (other.getUseGpuProfiler() != false) {
        setUseGpuProfiler(other.getUseGpuProfiler());
      }
      if (other.getUseSampleProfiler() != false) {
        setUseSampleProfiler(other.getUseSampleProfiler());
      }
      onChanged();
      return this;
    }

    public final boolean isInitialized() {
      return true;
    }

    public Builder mergeFrom(
        com.google.protobuf.CodedInputStream input,
        com.google.protobuf.ExtensionRegistryLite extensionRegistry)
        throws java.io.IOException {
      org.tensorflow.distruntime.TraceOpts parsedMessage = null;
      try {
        parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry);
      } catch (com.google.protobuf.InvalidProtocolBufferException e) {
        parsedMessage = (org.tensorflow.distruntime.TraceOpts) e.getUnfinishedMessage();
        throw e.unwrapIOException();
      } finally {
        if (parsedMessage != null) {
          mergeFrom(parsedMessage);
        }
      }
      return this;
    }

    private double duration_ ;
    /**
     *

     * Length of the trace to be taken, in seconds.
     *

     *
     * double duration = 1;
     */
    public double getDuration() {
      return duration_;
    }
    /**
     *

     * Length of the trace to be taken, in seconds.
     *

     *
     * double duration = 1;
     */
    public Builder setDuration(double value) {

      duration_ = value;
      onChanged();
      return this;
    }
    /**
     *

     * Length of the trace to be taken, in seconds.
     *

     *
     * double duration = 1;
     */
    public Builder clearDuration() {

      duration_ = 0D;
      onChanged();
      return this;
    }

    private boolean useStepProfiler_ ;
    /**
     *

     * If true, capture step profile locally in each worker. Currently
     * unimplemented.
     *

     *
     * bool use_step_profiler = 2;
     */
    public boolean getUseStepProfiler() {
      return useStepProfiler_;
    }
    /**
     *

     * If true, capture step profile locally in each worker. Currently
     * unimplemented.
     *

     *
     * bool use_step_profiler = 2;
     */
    public Builder setUseStepProfiler(boolean value) {

      useStepProfiler_ = value;
      onChanged();
      return this;
    }
    /**
     *

     * If true, capture step profile locally in each worker. Currently
     * unimplemented.
     *

     *
     * bool use_step_profiler = 2;
     */
    public Builder clearUseStepProfiler() {

      useStepProfiler_ = false;
      onChanged();
      return this;
    }

    private boolean useKernelProfiler_ ;
    /**
     *

     * If true, capture kernel events from each worker.
     *

     *
     * bool use_kernel_profiler = 3;
     */
    public boolean getUseKernelProfiler() {
      return useKernelProfiler_;
    }
    /**
     *

     * If true, capture kernel events from each worker.
     *

     *
     * bool use_kernel_profiler = 3;
     */
    public Builder setUseKernelProfiler(boolean value) {

      useKernelProfiler_ = value;
      onChanged();
      return this;
    }
    /**
     *

     * If true, capture kernel events from each worker.
     *

     *
     * bool use_kernel_profiler = 3;
     */
    public Builder clearUseKernelProfiler() {

      useKernelProfiler_ = false;
      onChanged();
      return this;
    }

    private boolean useExtendedProfiler_ ;
    /**
     *

     * If true, capture extended profiling events from TensorFlow process.
     *

     *
     * bool use_extended_profiler = 4;
     */
    public boolean getUseExtendedProfiler() {
      return useExtendedProfiler_;
    }
    /**
     *

     * If true, capture extended profiling events from TensorFlow process.
     *

     *
     * bool use_extended_profiler = 4;
     */
    public Builder setUseExtendedProfiler(boolean value) {

      useExtendedProfiler_ = value;
      onChanged();
      return this;
    }
    /**
     *

     * If true, capture extended profiling events from TensorFlow process.
     *

     *
     * bool use_extended_profiler = 4;
     */
    public Builder clearUseExtendedProfiler() {

      useExtendedProfiler_ = false;
      onChanged();
      return this;
    }

    private boolean useGpuProfiler_ ;
    /**
     *

     * If true, capture GPU profiling events locally on each
     * machine. Currently unimplemented.
     *

     *
     * bool use_gpu_profiler = 5;
     */
    public boolean getUseGpuProfiler() {
      return useGpuProfiler_;
    }
    /**
     *

     * If true, capture GPU profiling events locally on each
     * machine. Currently unimplemented.
     *

     *
     * bool use_gpu_profiler = 5;
     */
    public Builder setUseGpuProfiler(boolean value) {

      useGpuProfiler_ = value;
      onChanged();
      return this;
    }
    /**
     *

     * If true, capture GPU profiling events locally on each
     * machine. Currently unimplemented.
     *

     *
     * bool use_gpu_profiler = 5;
     */
    public Builder clearUseGpuProfiler() {

      useGpuProfiler_ = false;
      onChanged();
      return this;
    }

    private boolean useSampleProfiler_ ;
    /**
     *

     * If true, collect sampled profile events. Currently unimplemented.
     *

     *
     * bool use_sample_profiler = 6;
     */
    public boolean getUseSampleProfiler() {
      return useSampleProfiler_;
    }
    /**
     *

     * If true, collect sampled profile events. Currently unimplemented.
     *

     *
     * bool use_sample_profiler = 6;
     */
    public Builder setUseSampleProfiler(boolean value) {

      useSampleProfiler_ = value;
      onChanged();
      return this;
    }
    /**
     *

     * If true, collect sampled profile events. Currently unimplemented.
     *

     *
     * bool use_sample_profiler = 6;
     */
    public Builder clearUseSampleProfiler() {

      useSampleProfiler_ = false;
      onChanged();
      return this;
    }
    public final Builder setUnknownFields(
        final com.google.protobuf.UnknownFieldSet unknownFields) {
      return this;
    }

    public final Builder mergeUnknownFields(
        final com.google.protobuf.UnknownFieldSet unknownFields) {
      return this;
    }

    // @@protoc_insertion_point(builder_scope:tensorflow.TraceOpts)
  }

  // @@protoc_insertion_point(class_scope:tensorflow.TraceOpts)
  private static final org.tensorflow.distruntime.TraceOpts DEFAULT_INSTANCE;
  static {
    DEFAULT_INSTANCE = new org.tensorflow.distruntime.TraceOpts();
  }

  public static org.tensorflow.distruntime.TraceOpts getDefaultInstance() {
    return DEFAULT_INSTANCE;
  }

  private static final com.google.protobuf.Parser<TraceOpts>
      PARSER = new com.google.protobuf.AbstractParser<TraceOpts>() {
    public TraceOpts parsePartialFrom(
        com.google.protobuf.CodedInputStream input,
        com.google.protobuf.ExtensionRegistryLite extensionRegistry)
        throws com.google.protobuf.InvalidProtocolBufferException {
        return new TraceOpts(input, extensionRegistry);
    }
  };

  public static com.google.protobuf.Parser<TraceOpts> parser() {
    return PARSER;
  }

  @java.lang.Override
  public com.google.protobuf.Parser<TraceOpts> getParserForType() {
    return PARSER;
  }

  public org.tensorflow.distruntime.TraceOpts getDefaultInstanceForType() {
    return DEFAULT_INSTANCE;
  }

}


text:
<?php

namespace IdeaSeven\Core\Facades;
use Illuminate\Support\Facades\Facade;

class MenuConnectorFacade extends Facade
{
    /**
     * Get the binding in the IoC container
     *
     * @return string
     */
    protected static function getFacadeAccessor()
    {
        return 'MenuConnector'; // the IoC binding.
    }
}


text:
* [Release] Legacy PATRIC Release
* [Package] Add forever to package.json
* [Feature] Banner

## 3.6.12 - Sep 17, 2021
* [Bug Fix] Authentication fix

## 3.6.11 - Sep 1, 2021
* [Feature] Authentication update
* [Bug Fix] MSA fix

## 3.6.10 - Apr 26, 2021
* [Minor] [#1015](https://github.com/PATRIC3/p3_web/pull/1015)
* [Bug Fix] [#1014](https://github.com/PATRIC3/p3_web/pull/1014)

## 3.6.9 - Jan 19, 2021

* [Minor] Disable Reconstruct Metabolic Model service UI. [#1008](https://github.com/PATRIC3/p3_web/pull/1008)
* [Minor] Code refactoring. [#1007](https://github.com/PATRIC3/p3_web/pull/1007)

## 3.6.8 - Dec 8, 2020

* [Feature] Table viewer for CSV/TSV files in workspaces.  Allows filtering, sorting, etc.  Detects genome ids, feature ids, and more. [#1004](https://github.com/PATRIC3/p3_web/pull/1004)

* [Feature] New shortcut links on Taxon > Features view [#1004](https://github.com/PATRIC3/p3_web/pull/1004)

* [Minor] Update query for Data API upadate. [#fe383d2](https://github.com/PATRIC3/p3_web/commit/fe383d257024734d15c6e60c916eb78edbe7e838)

* [Bug Fixes] [#1005](https://github.com/PATRIC3/p3_web/pull/1005)

## 3.6.7 - Sept 22, 2020

* [Bug Fix] ID Mapping: fix handling of uniprotkb_accession. [#2370](https://github.com/PATRIC3/patric3_website/issues/2370)

* [Enhancement] TnSeq: add protocol and primer support for transit3 upgrade. [#1002](https://github.com/PATRIC3/p3_web/pull/1002)

* [Enhancement] Improve and enable all SRA validation; add SRA to Variation Service. [#1000](https://github.com/PATRIC3/p3_web/pull/1000), [#1001](https://github.com/PATRIC3/p3_web/pull/1001)

## 3.6.6 - August 14, 2020

* [Bug Fix] Allow SRA on most apps (pending validation fixes). [56cb481](https://github.com/PATRIC3/p3_web/commit/56cb481914410172c6a34b566541241f1231150d)

* [Bug Fix] Support newick files with no genome ids. [#985](https://github.com/PATRIC3/p3_web/pull/985)

* [Minor] Add BEI links; add dates to feature overview. [#992](https://github.com/PATRIC3/p3_web/pull/992)

* [Minor] Improve Pathway Summary error handling. [#977](https://github.com/PATRIC3/p3_web/pull/977)

* [Minor] Fix link (and auth) to ModelSEED.

* [Patch] Handle auth issue preventing re-login.

* [Dev Enhancement] Allow devs to specify build container IDs in app submissions; related improvements. [7a4ff1f](https://github.com/PATRIC3/p3_web/commit/7a4ff1fd47f7da545322d5a1240756fcabae1f2f)

* [Dev Enhancement] Navigate to default job result viewer if not implemented. [#994](https://github.com/PATRIC3/p3_web/pull/994)

## 3.6.5 - June 5, 2020

* [Bug Fix] Fix assembly job result viewer within Comprehensive Genome Analysis job results. [#970](https://github.com/PATRIC3/p3_web/pull/970)

* [Bug Fix] Fix tree button in Phylogenetic Tree job result viewer.  [#2361](https://github.com/PATRIC3/patric3_website/issues/2361)

* [Bug Fix] Prevent sending wrong query from initiation in transcriptomics tab; handle interaction tab correctly in genome_list viewer. [edc59e2](https://github.com/PATRIC3/p3_web/commit/edc59e287659e2c5b15e14f1addee872f7c9ee78), [7f6eb2f](https://github.com/PATRIC3/p3_web/commit/7f6eb2fe0f62424d486a3be85e9abab134efde3f)

* [Enhancement] Support gzip files in Similar Genome Finder service. [#972](https://github.com/PATRIC3/p3_web/pull/972)

* [Enhancement] Add video tutorial links to assembly, annotation, and family sorter. [#2360](https://github.com/PATRIC3/patric3_website/issues/2360)

## 3.6.4 - May 17, 2020

* [Feature] Save heatmap view as TSV or JSON.  [#2356](https://github.com/PATRIC3/patric3_website/issues/2356)
* [Enhancement] Set default distance to 1 for Similar Genome Finder. [#965](https://github.com/PATRIC3/p3_web/pull/965)

* [Bug Fix] Support for using latest host genomes in RNA-seq > HISAT2.
[#2357](https://github.com/PATRIC3/patric3_website/issues/2357)

* [Bug Fix] Fix numbers on circular viewer for private genomes.  [#2355](https://github.com/PATRIC3/patric3_website/issues/2355)

* [Bug Fix] Fix navigation in reloading circular viewer.  [#967](https://github.com/PATRIC3/p3_web/pull/967)

* [Minor]  [#2296](https://github.com/PATRIC3/patric3_website/issues/2296), [#2350](https://github.com/PATRIC3/patric3_website/issues/2350)


text:
/* ========================================================================== */
/* === Source/Mongoose_BoundaryHeap.cpp ===================================== */
/* ========================================================================== */

/* -----------------------------------------------------------------------------
 * Mongoose Graph Partitioning Library  Copyright (C) 2017-2018,
 * Scott P. Kolodziej, Nuri S. Yeralan, Timothy A. Davis, William W. Hager
 * Mongoose is licensed under Version 3 of the GNU General Public License.
 * Mongoose is also available under other licenses; contact authors for details.
 * -------------------------------------------------------------------------- */

#include "Mongoose_BoundaryHeap.hpp"
#include "Mongoose_CutCost.hpp"
#include "Mongoose_Debug.hpp"
#include "Mongoose_Internal.hpp"
#include "Mongoose_Logger.hpp"

namespace Mongoose
{

//-----------------------------------------------------------------------------
// This function inserts the specified vertex into the graph
//-----------------------------------------------------------------------------
void bhLoad(EdgeCutProblem *graph, const EdgeCut_Options *options)
{
    /* Load the boundary heaps. */
    Int n               = graph->n;
    Int *Gp             = graph->p;
    Int *Gi             = graph->i;
    double *Gx          = graph->x;
    double *Gw          = graph->w;
    bool *partition     = graph->partition;
    double *gains       = graph->vertexGains;
    Int *externalDegree = graph->externalDegree;

    /* Keep track of the cut cost. */
    CutCost cost;
    cost.heuCost   = 0.0;
    cost.cutCost   = 0.0;
    cost.W[0]      = 0.0;
    cost.W[1]      = 0.0;
    cost.imbalance = 0.0;

    /* Compute the gains & discover if the vertex is on the boundary. */
    for (Int k = 0; k < n; k++)
    {
        bool kPartition = partition[k];
        cost.W[kPartition] += (Gw) ? Gw[k] : 1;

        double gain = 0.0;
        Int exD     = 0;
        for (Int p = Gp[k]; p < Gp[k + 1]; p++)
        {
            double edgeWeight = (Gx) ? Gx[p] : 1;
            bool onSameSide   = (kPartition == partition[Gi[p]]);
            gain += (onSameSide ? -edgeWeight : edgeWeight);
            if (!onSameSide)
            {
                exD++;
                cost.cutCost += edgeWeight;
            }
        }
        gains[k]          = gain;
        externalDegree[k] = exD;
        if (exD > 0)
            bhInsert(graph, k);
    }

    /* Save the cut cost to the graph. */
    graph->cutCost = cost.cutCost;
    graph->W0      = cost.W[0];
    graph->W1      = cost.W[1];

    double targetSplit = options->target_split;
    ASSERT(targetSplit > 0);
    ASSERT(targetSplit <= 0.5);

    graph->imbalance = targetSplit - std::min(graph->W0, graph->W1) / graph->W;
    graph->heuCost   = (graph->cutCost
                      + (fabs(graph->imbalance) > options->soft_split_tolerance
                             ? fabs(graph->imbalance) * graph->H
                             : 0.0));
}

//-----------------------------------------------------------------------------
// This function inserts the specified vertex into the graph's boundary heap
//-----------------------------------------------------------------------------
void bhInsert(EdgeCutProblem *graph, Int vertex)
{
    /* Unpack structures */
    Int vp        = graph->partition[vertex];
    Int *bhHeap   = graph->bhHeap[vp];
    Int size      = graph->bhSize[vp];
    double *gains = graph->vertexGains;

    bhHeap[size] = vertex;
    graph->BH_putIndex(vertex, size);

    heapifyUp(graph, bhHeap, gains, vertex, size, gains[vertex]);

    /* Save the size. */
    graph->bhSize[vp] = size + 1;
}

//-----------------------------------------------------------------------------
// Removes the specified vertex from its heap.
// To do this, we swap the last element in the heap with the element we
// want to remove. Then we heapify up and heapify down.
//-----------------------------------------------------------------------------
void bhRemove(EdgeCutProblem *graph, const EdgeCut_Options *options, Int vertex, double gain,
              bool partition, Int bhPosition)
{
    (void)options; // Unused variable
    (void)gain;    // Unused variable

    double *gains = graph->vertexGains;
    Int *bhIndex  = graph->bhIndex;
    Int *bhHeap   = graph->bhHeap[partition];
    Int size      = (--graph->bhSize[partition]);

    /* If we removed the last position in the heap, there's nothing to do. */
    if (bhPosition == size)
    {
        bhIndex[vertex] = 0;
        return;
    }

    /* Replace the vertex with the last element in the heap. */
    Int v = bhHeap[bhPosition] = bhHeap[size];
    graph->BH_putIndex(v, bhPosition);

    /* Finish the delete of "vertex" from the heap. */
    bhIndex[vertex] = 0;

    /* Bubble up then bubble down with a reread of v because it may have
     * changed during heapifyUp. */
    heapifyUp(graph, bhHeap, gains, v, bhPosition, gains[v]);
    v = bhHeap[bhPosition];
    heapifyDown(graph, bhHeap, size, gains, v, bhPosition, gains[v]);
}

//-----------------------------------------------------------------------------
// Starting at a position, this function will heapify from a vertex upwards
//-----------------------------------------------------------------------------
void heapifyUp(EdgeCutProblem *graph, Int *bhHeap, double *gains, Int vertex,
               Int position, double gain)
{
    if (position == 0)
        return;

    Int posParent = graph->BH_getParent(position);
    Int pVertex   = bhHeap[posParent];
    double pGain  = gains[pVertex];

    /* If we need to swap this vertex with the parent then: */
    if (pGain < gain)
    {
        bhHeap[posParent] = vertex;
        bhHeap[position]  = pVertex;
        graph->BH_putIndex(vertex, posParent);
        graph->BH_putIndex(pVertex, position);
        heapifyUp(graph, bhHeap, gains, vertex, posParent, gain);
    }
}

//-----------------------------------------------------------------------------
// Starting at a position, this function will heapify from a vertex downwards
//-----------------------------------------------------------------------------
void heapifyDown(EdgeCutProblem *graph, Int *bhHeap, Int size, double *gains, Int vertex,
                 Int position, double gain)
{
    if (position >= size)
        return;

    Int lp = graph->BH_getLeftChild(position);
    Int rp = graph->BH_getRightChild(position);

    Int lv = (lp < size ? bhHeap[lp] : -1);
    Int rv = (rp < size ? bhHeap[rp] : -1);

    double lg = (lv >= 0 ? gains[lv] : -INFINITY);
    double rg = (rv >= 0 ? gains[rv] : -INFINITY);

    if (gain < lg || gain < rg)
    {
        if (lg > rg)
        {
            bhHeap[position] = lv;
            graph->BH_putIndex(lv, position);
            bhHeap[lp] = vertex;
            graph->BH_putIndex(vertex, lp);
            heapifyDown(graph, bhHeap, size, gains, vertex, lp, gain);
        }
        else
        {
            bhHeap[position] = rv;
            graph->BH_putIndex(rv, position);
            bhHeap[rp] = vertex;
            graph->BH_putIndex(vertex, rp);
            heapifyDown(graph, bhHeap, size, gains, vertex, rp, gain);
        }
    }
}

} // end namespace Mongoose


text:
.. highlight:: psql

.. _window-functions:

================
Window functions
================

Window functions are :ref:`functions <gloss-function>` which perform a
computation across a set of rows which are related to the current row. This is
comparable to :ref:`aggregation functions <aggregation-functions>`, but window
functions do not cause multiple rows to be grouped into a single row.

.. rubric:: Table of contents

.. contents::
   :local:

.. _window-function-call:

Window function call
====================

.. _window-call-synopsis:

Synopsis
--------

The synopsis of a window function call is one of the following

::

   function_name ( { * | [ expression [, expression ... ] ] } )
                 [ FILTER ( WHERE condition ) ]
                 [ { RESPECT | IGNORE } NULLS ]
                 over_clause

where ``function_name`` is a name of a :ref:`general-purpose window
<window-functions-general-purpose>` or :ref:`aggregate function
<aggregation-functions>` and ``expression`` is a column reference, :ref:`scalar
function <scalar-functions>` or literal.

If ``FILTER`` is specified, then only the rows that met the :ref:`WHERE
<sql-select-where>` condition are supplied to the window function. Only window
functions that are :ref:`aggregates <aggregation>` accept the ``FILTER``
clause.

If ``IGNORE NULLS`` option is specified, then the null values are excluded from
the window function executions. The window functions that support this option
are: :ref:`window-functions-lead`, :ref:`window-functions-lag`,
:ref:`window-functions-first-value`, :ref:`window-functions-last-value`,
and :ref:`window-functions-nth-value`. If a function supports this option and
it is not specified, then ``RESPECT NULLS`` is set by default.

The :ref:`window-definition-over` clause is what declares a function to be a
window function.

The window function call that uses a ``wildcard`` instead of an ``expression``
as a function argument is supported only by the ``count(*)`` aggregate
function.

.. _window-definition:

Window definition
=================

.. _window-definition-over:

OVER
----

.. _window-definition-over-synopsis:

Synopsis
........

::

   OVER { window_name | ( [ window_definition ] ) }

where ``window_definition`` has the syntax

::

   window_definition:
      [ window_name ]
      [ PARTITION BY expression [, ...] ]
      [ ORDER BY expression [ ASC | DESC ] [ NULLS { FIRST | LAST } ] [, ...] ]
      [ { RANGE | ROWS } BETWEEN frame_start AND frame_end ]

The ``window_name`` refers to ``window_definition`` defined in the
:ref:`WINDOW <sql-select-window>` clause.

The ``frame_start`` and ``frame_end`` can be one of

::

   UNBOUNDED PRECEDING
   offset PRECEDING
   CURRENT ROW
   offset FOLLOWING
   UNBOUNDED FOLLOWING

The default frame definition is ``RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT
ROW``. If ``frame_end`` is omitted it defaults to ``CURRENT ROW``.

``frame_start`` cannot be ``FOLLOWING`` or ``UNBOUNDED FOLLOWING`` and
``frame_end`` cannot be ``PRECEDING`` or ``UNBOUNDED PRECEDING``.

In ``RANGE`` mode if the ``frame_start`` is ``CURRENT ROW`` the frame starts
with the current row's first peer (a row that the window's ``ORDER BY``
:ref:`expression <gloss-expression>` sorts as equal to the current row), while
a ``frame_end`` of ``CURRENT ROW`` means the frame will end with the current's
row last peer row.

In ``ROWS`` mode ``CURRENT_ROW`` means the current row.

The ``offset PRECEDING`` and ``offset FOLLOWING`` options vary in meaning
depending on the frame mode. In ``ROWS`` mode, the ``offset`` is an integer
indicating that the frame start or end is offsetted by that many rows before or
after the current row. In ``RANGE`` mode, the use of a custom ``offset`` option
requires that there is exactly one ``ORDER BY`` column in the window
definition. The frame contains those rows whose ordering column value is no
more than ``offset`` minus (for ``PRECEDING``) or plus (for ``FOLLOWING``) the
current row's ordering column value. Because the value of ``offset`` is
subtracted/added to the values of the ordering column, only type combinations
that support addition/subtraction operations are allowed. For instance, when
the ordering column is of type :ref:`timestamp <type-timestamp>`, the
``offset`` expression can be an :ref:`interval <type-interval>`.

The :ref:`window-definition-over` clause defines the ``window`` containing the
appropriate rows which will take part in the ``window function`` computation.

An empty :ref:`window-definition-over` clause defines a ``window`` containing
all the rows in the result set.

Example::

   cr> SELECT dept_id, COUNT(*) OVER() AS cnt FROM employees ORDER BY 1, 2;
   +---------+-----+
   | dept_id | cnt |
   +---------+-----+
   |    4001 |  18 |
   |    4001 |  18 |
   |    4001 |  18 |
   |    4002 |  18 |
   |    4002 |  18 |
   |    4002 |  18 |
   |    4002 |  18 |
   |    4003 |  18 |
   |    4003 |  18 |
   |    4003 |  18 |
   |    4003 |  18 |
   |    4003 |  18 |
   |    4004 |  18 |
   |    4004 |  18 |
   |    4004 |  18 |
   |    4006 |  18 |
   |    4006 |  18 |
   |    4006 |  18 |
   +---------+-----+
   SELECT 18 rows in set (... sec)

The ``PARTITION BY`` clause groups the rows within a window into
partitions which are processed separately by the window function, each
partition in turn becoming a window. If ``PARTITION BY`` is not specified, all
the rows are considered a single partition.

Example::

   cr> SELECT dept_id, ROW_NUMBER() OVER(PARTITION BY dept_id) AS row_num
   ... FROM employees ORDER BY 1, 2;
   +---------+---------+
   | dept_id | row_num |
   +---------+---------+
   |    4001 |       1 |
   |    4001 |       2 |
   |    4001 |       3 |
   |    4002 |       1 |
   |    4002 |       2 |
   |    4002 |       3 |
   |    4002 |       4 |
   |    4003 |       1 |
   |    4003 |       2 |
   |    4003 |       3 |
   |    4003 |       4 |
   |    4003 |       5 |
   |    4004 |       1 |
   |    4004 |       2 |
   |    4004 |       3 |
   |    4006 |       1 |
   |    4006 |       2 |
   |    4006 |       3 |
   +---------+---------+
   SELECT 18 rows in set (... sec)

If ``ORDER BY`` is supplied the ``window`` definition consists of a range of
rows starting with the first row in the ``partition`` and ending with the
current row, plus any subsequent rows that are equal to the current row, which
are the current row's ``peers``.

Example::

   cr> SELECT
   ...   dept_id,
   ...   sex,
   ...   COUNT(*) OVER(PARTITION BY dept_id ORDER BY sex) AS cnt
   ... FROM employees
   ... ORDER BY 1, 2, 3
   +---------+-----+-----+
   | dept_id | sex | cnt |
   +---------+-----+-----+
   |    4001 | M   |   3 |
   |    4001 | M   |   3 |
   |    4001 | M   |   3 |
   |    4002 | F   |   1 |
   |    4002 | M   |   4 |
   |    4002 | M   |   4 |
   |    4002 | M   |   4 |
   |    4003 | M   |   5 |
   |    4003 | M   |   5 |
   |    4003 | M   |   5 |
   |    4003 | M   |   5 |
   |    4003 | M   |   5 |
   |    4004 | F   |   1 |
   |    4004 | M   |   3 |
   |    4004 | M   |   3 |
   |    4006 | F   |   1 |
   |    4006 | M   |   3 |
   |    4006 | M   |   3 |
   +---------+-----+-----+
   SELECT 18 rows in set (... sec)

.. NOTE::

   Taking into account the ``peers`` concept mentioned above, for an empty
   :ref:`window-definition-over` clause all the rows in the result set are
   ``peers``.

.. NOTE::

   :ref:`Aggregation functions <aggregation>` will be treated as ``window
   functions`` when used in conjunction with the :ref:`window-definition-over`
   clause.

.. NOTE::

   Window definitions order or partitioned by an array column type are
   currently not supported.

In the ``UNBOUNDED FOLLOWING`` case the ``window`` for each row starts with
each row and ends with the last row in the current ``partition``. If the
``current row`` has ``peers`` the ``window`` will include (or start with) all
the ``current row`` peers and end at the upper bound of the ``partition``.

Example::

   cr> SELECT
   ...   dept_id,
   ...   sex,
   ...   COUNT(*) OVER(
   ...     PARTITION BY dept_id
   ...     ORDER BY
   ...       sex RANGE BETWEEN CURRENT ROW
   ...       AND UNBOUNDED FOLLOWING
   ...   ) partitionByDeptOrderBySex
   ... FROM employees
   ... ORDER BY 1, 2, 3
   +---------+-----+---------------------------+
   | dept_id | sex | partitionbydeptorderbysex |
   +---------+-----+---------------------------+
   |    4001 | M   |                         3 |
   |    4001 | M   |                         3 |
   |    4001 | M   |                         3 |
   |    4002 | F   |                         4 |
   |    4002 | M   |                         3 |
   |    4002 | M   |                         3 |
   |    4002 | M   |                         3 |
   |    4003 | M   |                         5 |
   |    4003 | M   |                         5 |
   |    4003 | M   |                         5 |
   |    4003 | M   |                         5 |
   |    4003 | M   |                         5 |
   |    4004 | F   |                         3 |
   |    4004 | M   |                         2 |
   |    4004 | M   |                         2 |
   |    4006 | F   |                         3 |
   |    4006 | M   |                         2 |
   |    4006 | M   |                         2 |
   +---------+-----+---------------------------+
   SELECT 18 rows in set (... sec)

.. _window-definition-named-windows:

Named windows
-------------

It is possible to define a list of named window definitions that can be
referenced in :ref:`window-definition-over` clauses. To do this, use the
:ref:`sql-select-window` clause in the :ref:`sql-select` clause.

Named windows are particularly useful when the same window definition
could be used in multiple :ref:`window-definition-over` clauses. For instance

::

   cr> SELECT
   ...   x,
   ...   FIRST_VALUE(x) OVER (w) AS "first",
   ...   LAST_VALUE(x) OVER (w) AS "last"
   ... FROM (VALUES (1), (2), (3), (4)) AS t(x)
   ... WINDOW w AS (ORDER BY x)
   +---+-------+------+
   | x | first | last |
   +---+-------+------+
   | 1 |     1 |    1 |
   | 2 |     1 |    2 |
   | 3 |     1 |    3 |
   | 4 |     1 |    4 |
   +---+-------+------+
   SELECT 4 rows in set (... sec)

If a ``window_name`` is specified in the window definition of the
:ref:`window-definition-over` clause, then there must be a named window entry
that matches the ``window_name`` in the window definition list of the
:ref:`sql-select-window` clause.

If the :ref:`window-definition-over` clause has its own non-empty window
definition and references a window definition from the :ref:`sql-select-window`
clause, then it can only add clauses from the referenced window, but not
overwrite them.

::

   cr> SELECT
   ...   x,
   ...   LAST_VALUE(x) OVER (w ORDER BY x) AS y
   ... FROM (VALUES
   ...      (1, 1),
   ...      (2, 1),
   ...      (3, 2),
   ...      (4, 2) ) AS t(x, y)
   ... WINDOW w AS (PARTITION BY y)
   +---+---+
   | x | y |
   +---+---+
   | 1 | 1 |
   | 2 | 2 |
   | 3 | 3 |
   | 4 | 4 |
   +---+---+
   SELECT 4 rows in set (... sec)

Otherwise, an attempt to override the clauses of the referenced window by the
window definition of the :ref:`window-definition-over` clause will result in
failure.

::

   cr> SELECT
   ...   FIRST_VALUE(x) OVER (w ORDER BY x)
   ... FROM (VALUES(1), (2), (3), (4)) as t(x)
   ... WINDOW w AS (ORDER BY x)
   SQLParseException[Cannot override ORDER BY clause of window w]

It is not possible to define the ``PARTITION BY`` clause in the window
definition of the :ref:`window-definition-over` clause if it references a
window definition from the :ref:`sql-select-window` clause.

The window definitions in the :ref:`sql-select-window` clause cannot define
its own window frames, if they are referenced by non-empty window definitions
of the :ref:`window-definition-over` clauses.

The definition of the named window can itself begin with a ``window_name``.  In
this case all the elements of interconnected named windows will be copied to
the window definition of the :ref:`window-definition-over` clause if it
references the named window definition that has subsequent window
references. The window definitions in the ``WINDOW`` clause permits only
backward references.

::

   cr> SELECT
   ...   x,
   ...   ROW_NUMBER() OVER (w) AS y
   ... FROM (VALUES
   ...      (1, 1),
   ...      (3, 2),
   ...      (2, 1)) AS t (x, y)
   ... WINDOW p AS (PARTITION BY y),
   ...        w AS (p ORDER BY x)
   +---+---+
   | x | y |
   +---+---+
   | 1 | 1 |
   | 2 | 2 |
   | 3 | 1 |
   +---+---+
   SELECT 3 rows in set (... sec)

.. _window-functions-general-purpose:

General-purpose window functions
================================

``row_number()``
----------------

Returns the number of the current row within its window.

Example::

   cr> SELECT
   ...  col1,
   ...  ROW_NUMBER() OVER(ORDER BY col1) as row_num
   ... FROM (VALUES('x'), ('y'), ('z')) AS t;
   +------+---------+
   | col1 | row_num |
   +------+---------+
   | x    |       1 |
   | y    |       2 |
   | z    |       3 |
   +------+---------+
   SELECT 3 rows in set (... sec)

.. _window-functions-first-value:

``first_value(arg)``
--------------------

Returns the argument value :ref:`evaluated <gloss-evaluation>` at the first row
within the window.

Its return type is the type of its argument.

Example::

   cr> SELECT
   ...  col1,
   ...  FIRST_VALUE(col1) OVER (ORDER BY col1) AS value
   ... FROM (VALUES('x'), ('y'), ('y'), ('z')) AS t;
   +------+-------+
   | col1 | value |
   +------+-------+
   | x    | x     |
   | y    | x     |
   | y    | x     |
   | z    | x     |
   +------+-------+
   SELECT 4 rows in set (... sec)

.. _window-functions-last-value:

``last_value(arg)``
-------------------

Returns the argument value :ref:`evaluated <gloss-evaluation>` at the last row
within the window.

Its return type is the type of its argument.

Example::

   cr> SELECT
   ...  col1,
   ...  LAST_VALUE(col1) OVER(ORDER BY col1) AS value
   ... FROM (VALUES('x'), ('y'), ('y'), ('z')) AS t;
   +------+-------+
   | col1 | value |
   +------+-------+
   | x    | x     |
   | y    | y     |
   | y    | y     |
   | z    | z     |
   +------+-------+
   SELECT 4 rows in set (... sec)

.. _window-functions-nth-value:

``nth_value(arg, number)``
--------------------------

Returns the argument value :ref:`evaluated <gloss-evaluation>` at row that is
the nth row within the window. ``NULL`` is returned if the nth row doesn't
exist in the window.

Its return type is the type of its first argument.

Example::

   cr> SELECT
   ...  col1,
   ...  NTH_VALUE(col1, 3) OVER(ORDER BY col1) AS val
   ... FROM (VALUES ('x'), ('y'), ('y'), ('z')) AS t;
   +------+------+
   | col1 | val  |
   +------+------+
   | x    | NULL |
   | y    | y    |
   | y    | y    |
   | z    | y    |
   +------+------+
   SELECT 4 rows in set (... sec)

.. _window-functions-lag:

``lag(arg [, offset [, default] ])``
------------------------------------

.. _window-functions-lag-synopsis:

Synopsis
........

::

   lag(argument any [, offset integer [, default any]])

Returns the argument value :ref:`evaluated <gloss-evaluation>` at the row that
precedes the current row by the offset within the partition. If there is no
such row, the return value is ``default``. If ``offset`` or ``default``
arguments are missing, they default to ``1`` and ``null``, respectively.

Both ``offset`` and ``default`` are evaluated with respect to the current row.

If ``offset`` is ``0``, then argument value is evaluated for the current row.

The ``default`` and ``argument`` data types must match.

Example::

   cr> SELECT
   ...   dept_id,
   ...   year,
   ...   budget,
   ...   LAG(budget) OVER(
   ...      PARTITION BY dept_id) prev_budget
   ... FROM (VALUES
   ...      (1, 2017, 45000),
   ...      (1, 2018, 35000),
   ...      (2, 2017, 15000),
   ...      (2, 2018, 65000),
   ...      (2, 2019, 12000))
   ... as t (dept_id, year, budget);
   +---------+------+--------+-------------+
   | dept_id | year | budget | prev_budget |
   +---------+------+--------+-------------+
   |       1 | 2017 |  45000 |        NULL |
   |       1 | 2018 |  35000 |       45000 |
   |       2 | 2017 |  15000 |        NULL |
   |       2 | 2018 |  65000 |       15000 |
   |       2 | 2019 |  12000 |       65000 |
   +---------+------+--------+-------------+
   SELECT 5 rows in set (... sec)

.. _window-functions-lead:

``lead(arg [, offset [, default] ])``
-------------------------------------

.. _window-functions-lead-synopsis:

Synopsis
........

::

   lead(argument any [, offset integer [, default any]])

The ``lead`` function is the counterpart of the :ref:`lag window function
<window-functions-lag>` as it allows the :ref:`evaluation <gloss-evaluation>`
of the argument at rows that follow the current row. ``lead`` returns the
argument value evaluated at the row that follows the current row by the offset
within the partition. If there is no such row, the return value is ``default``.
If ``offset`` or ``default`` arguments are missing, they default to ``1`` or
``null``, respectively.

Both ``offset`` and ``default`` are evaluated with respect to the current row.

If ``offset`` is ``0``, then argument value is evaluated for the current row.

The ``default`` and ``argument`` data types must match.

Example::

   cr> SELECT
   ...   dept_id,
   ...   year,
   ...   budget,
   ...   LEAD(budget) OVER(
   ...      PARTITION BY dept_id) next_budget
   ... FROM (VALUES
   ...      (1, 2017, 45000),
   ...      (1, 2018, 35000),
   ...      (2, 2017, 15000),
   ...      (2, 2018, 65000),
   ...      (2, 2019, 12000))
   ... as t (dept_id, year, budget);
   +---------+------+--------+-------------+
   | dept_id | year | budget | next_budget |
   +---------+------+--------+-------------+
   |       1 | 2017 |  45000 |       35000 |
   |       1 | 2018 |  35000 |        NULL |
   |       2 | 2017 |  15000 |       65000 |
   |       2 | 2018 |  65000 |       12000 |
   |       2 | 2019 |  12000 |        NULL |
   +---------+------+--------+-------------+
   SELECT 5 rows in set (... sec)

.. _window-functions-rank:

``rank()``
----------

.. _window-functions-rank-synopsis:

Synopsis
........

::

    rank()

Returns the rank of every row within a partition of a result set.

Within each partition, the rank of the first row is ``1``. Subsequent tied
rows are given the same rank, and the potential rank of the next row
is incremented. Because of this, ranks may not be sequential.

Example::

    cr> SELECT
    ...   name,
    ...   department_id,
    ...   salary,
    ...   RANK() OVER (ORDER BY salary desc) as salary_rank
    ... FROM (VALUES
    ...      ('Bobson Dugnutt', 1, 2000),
    ...      ('Todd Bonzalez', 2, 2500),
    ...      ('Jess Brewer', 1, 2500),
    ...      ('Safwan Buchanan', 1, 1900),
    ...      ('Hal Dodd', 1, 2500),
    ...      ('Gillian Hawes', 2, 2000))
    ... as t (name, department_id, salary);
    +-----------------+---------------+--------+-------------+
    | name            | department_id | salary | salary_rank |
    +-----------------+---------------+--------+-------------+
    | Todd Bonzalez   |             2 |   2500 |           1 |
    | Jess Brewer     |             1 |   2500 |           1 |
    | Hal Dodd        |             1 |   2500 |           1 |
    | Bobson Dugnutt  |             1 |   2000 |           4 |
    | Gillian Hawes   |             2 |   2000 |           4 |
    | Safwan Buchanan |             1 |   1900 |           6 |
    +-----------------+---------------+--------+-------------+
    SELECT 6 rows in set (... sec)

.. _window-functions-dense-rank:

``dense_rank()``
----------------

.. _window-functions-dense-rank-synopsis:

Synopsis
........

::

    dense_rank()

Returns the rank of every row within a partition of a result set, similar to
``rank``. However, unlike ``rank``, ``dense_rank`` always returns sequential
rank values.

Within each partition, the rank of the first row is ``1``. Subsequent tied
rows are given the same rank.

Example::

    cr> SELECT
    ...   name,
    ...   department_id,
    ...   salary,
    ...   DENSE_RANK() OVER (ORDER BY salary desc) as salary_rank
    ... FROM (VALUES
    ...      ('Bobson Dugnutt', 1, 2000),
    ...      ('Todd Bonzalez', 2, 2500),
    ...      ('Jess Brewer', 1, 2500),
    ...      ('Safwan Buchanan', 1, 1900),
    ...      ('Hal Dodd', 1, 2500),
    ...      ('Gillian Hawes', 2, 2000))
    ... as t (name, department_id, salary);
    +-----------------+---------------+--------+-------------+
    | name            | department_id | salary | salary_rank |
    +-----------------+---------------+--------+-------------+
    | Todd Bonzalez   |             2 |   2500 |           1 |
    | Jess Brewer     |             1 |   2500 |           1 |
    | Hal Dodd        |             1 |   2500 |           1 |
    | Bobson Dugnutt  |             1 |   2000 |           2 |
    | Gillian Hawes   |             2 |   2000 |           2 |
    | Safwan Buchanan |             1 |   1900 |           3 |
    +-----------------+---------------+--------+-------------+
    SELECT 6 rows in set (... sec)

.. _window-aggregate-functions:

Aggregate window functions
==========================

See :ref:`aggregation`.