text:
/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.facebook.presto.hive.metastore;

import com.facebook.presto.hive.PartitionOfflineException;
import com.facebook.presto.hive.TableOfflineException;
import com.facebook.presto.spi.PrestoException;
import com.facebook.presto.spi.SchemaTableName;
import com.facebook.presto.spi.TableNotFoundException;
import org.apache.hadoop.hive.common.FileUtils;
import org.apache.hadoop.hive.metastore.ProtectMode;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Properties;

import static com.facebook.presto.hive.HiveSplitManager.PRESTO_OFFLINE;
import static com.facebook.presto.spi.StandardErrorCode.NOT_SUPPORTED;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Strings.isNullOrEmpty;
import static java.util.stream.Collectors.toList;
import static org.apache.hadoop.hive.metastore.MetaStoreUtils.typeToThriftType;
import static org.apache.hadoop.hive.metastore.ProtectMode.getProtectModeFromString;
import static org.apache.hadoop.hive.metastore.api.hive_metastoreConstants.BUCKET_COUNT;
import static org.apache.hadoop.hive.metastore.api.hive_metastoreConstants.BUCKET_FIELD_NAME;
import static org.apache.hadoop.hive.metastore.api.hive_metastoreConstants.FILE_INPUT_FORMAT;
import static org.apache.hadoop.hive.metastore.api.hive_metastoreConstants.FILE_OUTPUT_FORMAT;
import static org.apache.hadoop.hive.metastore.api.hive_metastoreConstants.META_TABLE_COLUMNS;
import static org.apache.hadoop.hive.metastore.api.hive_metastoreConstants.META_TABLE_COLUMN_TYPES;
import static org.apache.hadoop.hive.metastore.api.hive_metastoreConstants.META_TABLE_LOCATION;
import static org.apache.hadoop.hive.metastore.api.hive_metastoreConstants.META_TABLE_NAME;
import static org.apache.hadoop.hive.metastore.api.hive_metastoreConstants.META_TABLE_PARTITION_COLUMNS;
import static org.apache.hadoop.hive.metastore.api.hive_metastoreConstants.META_TABLE_PARTITION_COLUMN_TYPES;
import static org.apache.hadoop.hive.serde.serdeConstants.SERIALIZATION_DDL;
import static org.apache.hadoop.hive.serde.serdeConstants.SERIALIZATION_LIB;

public class MetastoreUtil
{
    private MetastoreUtil()
    {
    }

    public static Properties getHiveSchema(Table table)
    {
        // Mimics function in Hive: MetaStoreUtils.getTableMetadata(Table)
        return getHiveSchema(
                table.getStorage(),
                table.getDataColumns(),
                table.getDataColumns(),
                table.getParameters(),
                table.getDatabaseName(),
                table.getTableName(),
                table.getPartitionColumns());
    }

    public static Properties getHiveSchema(Partition partition, Table table)
    {
        // Mimics function in Hive: MetaStoreUtils.getSchema(Partition, Table)
        return getHiveSchema(
                partition.getStorage(),
                partition.getColumns(),
                table.getDataColumns(),
                table.getParameters(),
                table.getDatabaseName(),
                table.getTableName(),
                table.getPartitionColumns());
    }

    private static Properties getHiveSchema(
            Storage sd,
            List<Column> dataColumns,
            List<Column> tableDataColumns,
            Map<String, String> parameters,
            String databaseName,
            String tableName,
            List<Column> partitionKeys)
    {
        // Mimics function in Hive:
        // MetaStoreUtils.getSchema(StorageDescriptor, StorageDescriptor, Map<String, String>, String, String, List<FieldSchema>)

        Properties schema = new Properties();

        schema.setProperty(FILE_INPUT_FORMAT, sd.getStorageFormat().getInputFormat());
        schema.setProperty(FILE_OUTPUT_FORMAT, sd.getStorageFormat().getOutputFormat());

        schema.setProperty(META_TABLE_NAME, databaseName + "." + tableName);
        schema.setProperty(META_TABLE_LOCATION, sd.getLocation());

        if (sd.getBucketProperty().isPresent()) {
            schema.setProperty(BUCKET_FIELD_NAME, sd.getBucketProperty().get().getBucketedBy().get(0));
            schema.setProperty(BUCKET_COUNT, Integer.toString(sd.getBucketProperty().get().getBucketCount()));
        }
        else {
            schema.setProperty(BUCKET_COUNT, "0");
        }

        for (Map.Entry<String, String> param : sd.getSerdeParameters().entrySet()) {
            schema.setProperty(param.getKey(), (param.getValue() != null) ? param.getValue() : "");
        }
        schema.setProperty(SERIALIZATION_LIB, sd.getStorageFormat().getSerDe());

        StringBuilder columnNameBuilder = new StringBuilder();
        StringBuilder columnTypeBuilder = new StringBuilder();
        StringBuilder columnCommentBuilder = new StringBuilder();
        boolean first = true;
        for (Column column : tableDataColumns) {
            if (!first) {
                columnNameBuilder.append(",");
                columnTypeBuilder.append(":");
                columnCommentBuilder.append('\0');
            }
            columnNameBuilder.append(column.getName());
            columnTypeBuilder.append(column.getType());
            columnCommentBuilder.append(column.getComment().orElse(""));
            first = false;
        }
        String columnNames = columnNameBuilder.toString();
        String columnTypes = columnTypeBuilder.toString();
        schema.setProperty(META_TABLE_COLUMNS, columnNames);
        schema.setProperty(META_TABLE_COLUMN_TYPES, columnTypes);
        schema.setProperty("columns.comments", columnCommentBuilder.toString());

        schema.setProperty(SERIALIZATION_DDL, toThriftDdl(tableName, dataColumns));

        String partString = "";
        String partStringSep = "";
        String partTypesString = "";
        String partTypesStringSep = "";
        for (Column partKey : partitionKeys) {
            partString += partStringSep;
            partString += partKey.getName();
            partTypesString += partTypesStringSep;
            partTypesString += partKey.getType().getHiveTypeName().toString();
            if (partStringSep.length() == 0) {
                partStringSep = "/";
                partTypesStringSep = ":";
            }
        }
        if (partString.length() > 0) {
            schema.setProperty(META_TABLE_PARTITION_COLUMNS, partString);
            schema.setProperty(META_TABLE_PARTITION_COLUMN_TYPES, partTypesString);
        }

        if (parameters != null) {
            for (Map.Entry<String, String> entry : parameters.entrySet()) {
                // add non-null parameters to the schema
                if (entry.getValue() != null) {
                    schema.setProperty(entry.getKey(), entry.getValue());
                }
            }
        }

        return schema;
    }

    public static ProtectMode getProtectMode(Partition partition)
    {
        return getProtectMode(partition.getParameters());
    }

    public static ProtectMode getProtectMode(Table table)
    {
        return getProtectMode(table.getParameters());
    }

    public static String makePartName(List<Column> partitionColumns, List<String> values)
    {
        checkArgument(partitionColumns.size() == values.size(), "Partition value count does not match the partition column count");
        checkArgument(values.stream().allMatch(Objects::nonNull), "partitionValue must not have null elements");

        List<String> partitionColumnNames = partitionColumns.stream().map(Column::getName).collect(toList());
        return FileUtils.makePartName(partitionColumnNames, values);
    }

    public static String getPartitionLocation(Table table, Optional<Partition> partition)
    {
        if (!partition.isPresent()) {
            return table.getStorage().getLocation();
        }
        return partition.get().getStorage().getLocation();
    }

    private static String toThriftDdl(String structName, List<Column> columns)
    {
        // Mimics function in Hive:
        // MetaStoreUtils.getDDLFromFieldSchema(String, List<FieldSchema>)
        StringBuilder ddl = new StringBuilder();
        ddl.append("struct ");
        ddl.append(structName);
        ddl.append(" { ");
        boolean first = true;
        for (Column column : columns) {
            if (first) {
                first = false;
            }
            else {
                ddl.append(", ");
            }
            ddl.append(typeToThriftType(column.getType().getHiveTypeName().toString()));
            ddl.append(' ');
            ddl.append(column.getName());
        }
        ddl.append("}");
        return ddl.toString();
    }

    private static ProtectMode getProtectMode(Map<String, String> parameters)
    {
        if (!parameters.containsKey(ProtectMode.PARAMETER_NAME)) {
            return new ProtectMode();
        }
        else {
            return getProtectModeFromString(parameters.get(ProtectMode.PARAMETER_NAME));
        }
    }

    public static void verifyOnline(SchemaTableName tableName, Optional<String> partitionName, ProtectMode protectMode, Map<String, String> parameters)
    {
        if (protectMode.offline) {
            if (partitionName.isPresent()) {
                throw new PartitionOfflineException(tableName, partitionName.get(), false, null);
            }
            throw new TableOfflineException(tableName, false, null);
        }

        String prestoOffline = parameters.get(PRESTO_OFFLINE);
        if (!isNullOrEmpty(prestoOffline)) {
            if (partitionName.isPresent()) {
                throw new PartitionOfflineException(tableName, partitionName.get(), true, prestoOffline);
            }
            throw new TableOfflineException(tableName, true, prestoOffline);
        }
    }

    public static void verifyCanDropColumn(ExtendedHiveMetastore metastore, String databaseName, String tableName, String columnName)
    {
        Table table = metastore.getTable(databaseName, tableName)
                .orElseThrow(() -> new TableNotFoundException(new SchemaTableName(databaseName, tableName)));

        if (table.getPartitionColumns().stream().anyMatch(column -> column.getName().equals(columnName))) {
            throw new PrestoException(NOT_SUPPORTED, "Cannot drop partition columns");
        }
        if (table.getDataColumns().size() <= 1) {
            throw new PrestoException(NOT_SUPPORTED, "Cannot drop the only non-partition column in a table");
        }
    }
}


text:
import sys

extensions = []

templates_path = ['_templates']

source_suffix = '.rst'

master_doc = 'index'

project = 'django-rest-swagger'
copyright = '2014, Marc Gibbons'

sys.path.append("../../")
from rest_framework_swagger import VERSION
version = VERSION
release = VERSION

exclude_patterns = []

pygments_style = 'sphinx'
html_theme = 'default'

html_static_path = ['_static']

htmlhelp_basename = 'django-rest-swaggerdoc'

latex_elements = {
}

latex_documents = [
    ('index', 'django-rest-swagger.tex', 'django-rest-swagger Documentation',
     'Marc Gibbons', 'manual'),
]

man_pages = [
    ('index', 'django-rest-swagger', 'django-rest-swagger Documentation',
     ['Marc Gibbons'], 1)
]

texinfo_documents = [
    ('index', 'django-rest-swagger', 'django-rest-swagger Documentation',
     'Marc Gibbons', 'django-rest-swagger', 'One line description of project.',
     'Miscellaneous'),
]

redpajama_set_name: RedPajamaGithub


text:
import {expect} from 'chai';
import * as esprima from 'esprima';

import {discoverDependencies, executeExpression, contextify} from '../src/utils';

describe('utils', () => {

  describe('discoverDependencies', () => {

    it('should discover simple dependencies', () => {
      const validIds = ['GROSS', 'NET', 'STEP_1', 'RESULT'];
      const expression = `RESULT = (STEP_1 + GROSS - NET) / NET * STEP_1`;

      const deps = discoverDependencies(expression, validIds);

      expect(deps.sort()).to.be.deep.equal(['RESULT', 'STEP_1', 'GROSS', 'NET'].sort());
    });

    it('should discover dependencies in code block', () => {
      const validIds = ['RESULT', 'GROSS', 'NET', 'CREDIT', 'STEP_1'];
      const expression = `
        var grossAndNet = GROSS + pow(NET - GROSS, STEP_1);
        var minusCredit = grossAndNet - CREDIT;
        RESULT = minusCredit - STEP_1 * NET; `;

      const deps = discoverDependencies(expression, validIds);

      expect(deps.sort()).to.be.deep.equal(['RESULT', 'GROSS', 'NET', 'STEP_1', 'CREDIT'].sort());
    });

    it('should discover dependencies from a syntax tree', () => {
      const validIds = ['RESULT', 'GROSS', 'NET', 'STEP_1'];
      const expression = `RESULT = (STEP_1 + GROSS - NET) / NET * STEP_1`;

      const deps = discoverDependencies(esprima.parse(expression), validIds);

      expect(deps.sort()).to.be.deep.equal(['RESULT', 'STEP_1', 'GROSS', 'NET'].sort());
    });

  });

  describe('executeExpression', () => {

    it('should scope execution', () => {
      const context = { a: false };
      executeExpression(`a = Array.isArray([])`, context);
      expect(context.a).to.be.true;
      expect((global as any).a).to.be.undefined;
    });

    it('should successfully modify context', () => {
      const context: any = { a: 1, b: 2 };
      executeExpression(`c = a + b`, context);
      expect(context.c).to.be.equals(3);
    });

    it('should fail to access dangerous globals', () => {
      const context: any = {};
      expect(() => executeExpression(`setTimeout(null);`, context)).throw();
      expect(() => executeExpression(`require('fs');`, context)).throw();
      expect(() => executeExpression(`alert('♥');`, context)).throw();
      expect(() => executeExpression(`new Buffer();`, context)).throw();
    });

  });

  describe('contextify', () => {

    it('should return the same object', () => {
      const sandbox = { a: false };
      const context = contextify(sandbox);
      expect(sandbox).to.be.equals(context);
    });

    it('should contain math methods and properties', () => {
      const context = contextify({});
      expect(context.pow).to.be.equals(Math.pow);
      expect(context.log).to.be.equals(Math.log);
      expect(context.max).to.be.equals(Math.max);
      expect(context.min).to.be.equals(Math.min);
      expect(context.E).to.be.equals(Math.E);
      expect(context.PI).to.be.equals(Math.PI);
    });

    it('should work within vm', () => {
      const context = contextify({ a: 0 });
      executeExpression(`a = round(floor(min(pow(3, 2), 2.5))* 3.6)`, context);
      expect(context.a).to.be.equals(7);
    });

  });

});


text:
package nl.rostykerei.cci.ch01.q07;

/**
 * Transpose and reverse implementation of {@link RotateMatrix}.
 *
 * @author Rosty Kerei
 */
public final class RotateMatrixTranspose implements RotateMatrix {

    @Override
    public int[][] rotate(final int[][] image) {
        final int n = image.length;

        // Matrix transpose
        for (int row = 0; row < n; row++) {
            for (int col = row; col < n; col++) {
                int temp = image[row][col];
                image[row][col] = image[col][row];
                image[col][row] = temp;
            }
        }

        // Reverse
        for (int row = 0; row < n; row++) {
            for (int col = 0; col < n / 2; col++) {
                int temp = image[row][col];
                image[row][col] = image[row][n - col - 1];
                image[row][n - col - 1] = temp;
            }
        }

        return image;
    }
}


text:
package micc.theguardiansapp.beaconHelper;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.RemoteException;
import android.util.Log;

import com.estimote.sdk.Beacon;
import com.estimote.sdk.BeaconManager;
import com.estimote.sdk.Region;
import com.estimote.sdk.Utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by nagash on 01/05/15.
 */
public class ForegroundBeaconManager
{

    private static final String TAG = "SIMPLE_BEACON_MANAGER";

    private static final String ESTIMOTE_PROXIMITY_UUID = "B9407F30-F5F8-466E-AFF9-25556B57FE6D";
    private static final int MAJOR = 27910;
    private static final Region MY_BEACONS = new Region("regionId", ESTIMOTE_PROXIMITY_UUID, MAJOR, null);

    private boolean started = false;

    // non usiamo questo valore direttamente nel codice, è il valore che i beacons dovrebbero avere
    // nella loro memoria, usando questo valore possiamo ottimizzare i valori del tempo di scan nei
    // telefoni.
    private int BEACON_PULSE_INTERVAL = 1000;

    // dopo BEACON_MAX_TIMEOUT volte che un beacon non è
    // reperibile si scarta
    // --> si scarta dopo (millisecondi):
    // CASO PESSIMO:  (SCANNING_PERIOD + SCANNING_DELAY) * BEACON_MAX_TIMEOUT + SCANNING_PERIOD
    // CASO OTTIMO:   (SCANNING_PERIOD + SCANNING_DELAY) * BEACON_MAX_TIMEOUT
    private int BEACON_MAX_TIMEOUT = 3; //
    private int SCANNING_PERIOD = BEACON_PULSE_INTERVAL + BEACON_PULSE_INTERVAL/2;
    private int SCANNING_DELAY = BEACON_PULSE_INTERVAL/4;

    private int REFRESH_BEACON_SLEEP_TIME = 3000;

    // CON UN VALORE MINORE DI 0 SI ACCETTANO TUTTI !!!!
    private static final int PROXIMITY_DISTANCE = -1;

    Thread refresherThread;
    private MyBeaconListener listener;

    private volatile Map<Integer, Beacon> proximityBeacons;
    private Map<Integer, Integer> beacon_retry_map = new HashMap<>(10);

    private BeaconManager beaconManager;
    Context context;

    private volatile boolean runRefreshTask = false;

    public void clear()
    {
        this.beacon_retry_map.clear();
        this.proximityBeacons.clear();
    }

    public ForegroundBeaconManager(Context context, MyBeaconListener listener) {
        beaconManager = new BeaconManager(context);
        this.context = context;
        this.listener = listener;
        proximityBeacons = new HashMap<>(10);

        init();
    }

    private void init() {
        //beaconManager.setBackgroundScanPeriod(1000, 3000 );

        beaconManager.setForegroundScanPeriod(SCANNING_PERIOD, SCANNING_DELAY);

        // Should be invoked in #onCreate.
        beaconManager.setRangingListener(new BeaconManager.RangingListener() {

            @Override
            public void onBeaconsDiscovered(Region region, List<Beacon> beacons) {
                Log.d(TAG, "Ranged beacons: " + beacons);
                onBeaconProximity(beacons);
            }

        });

    }

    public boolean start()  {
         // Should be invoked in #onStart.
        beaconManager.connect(new BeaconManager.ServiceReadyCallback() {
            @Override
            public void onServiceReady() {
                try {
                    beaconManager.startRanging(MY_BEACONS);
                    started = true;
                } catch (RemoteException e) {
                    Log.e(TAG, "Cannot start ranging", e);
                }
            }
        });
        //startRefreshTask();
        return true;
    }
    public boolean startAndCheck(Activity activity)  {
        // Should be invoked in #onStart.
        if(!beaconManager.hasBluetooth())
        {
            return false;
        }
        else if(!beaconManager.isBluetoothEnabled())
        {
            // enable bluetooth with activity
        }
        start();
        //startRefreshTask();
        return true;
    }

    public void stop() {

        // Should be invoked in #onStop.
        try {
            beaconManager.stopRanging(MY_BEACONS);
            started = false;
        } catch (RemoteException e) {
            Log.e(TAG, "Cannot stop but it does not matter now", e);
        }

        // When no longer needed. Should be invoked in #onDestroy.
        beaconManager.disconnect();
    }

    public final boolean hasBluetooth() {
        return beaconManager.hasBluetooth();
    }
    public final boolean isBluetoothEnabled(){
        return beaconManager.isBluetoothEnabled();
    }

    public final int getBeaconSize(){
        return proximityBeacons.size();
    }

    public final Map<Integer, Beacon> getProximityBeacons(){
        return new HashMap<>(proximityBeacons);
    }

    protected final double getDistance(Beacon beacon) {
        return Utils.computeAccuracy(beacon);
    }

    protected final boolean isInProximity(Beacon beacon)
    {
        if(PROXIMITY_DISTANCE >= 0) {
            double distance = getDistance(beacon);
            if (distance <= PROXIMITY_DISTANCE)
                return true;
            else return false;
        }
        else return true;
    }

    private void onBeaconProximity(List<Beacon> detectedBeacons)
    {

        // proximityBeaconsNewList: COSTRUISCO LA LISTA DI TUTTI I BEACONS ENTRO LA PROSSIMITÁ SCELTA
        List<Beacon> proximityBeaconsNewList =  new ArrayList<>(detectedBeacons.size());

        for (Beacon beacon : detectedBeacons)
        {
            if (isInProximity(beacon))
            {
                proximityBeaconsNewList.add(beacon);
            }
        }

        List<Beacon> newInOld = new ArrayList<>(proximityBeaconsNewList.size());
        List<Beacon> newNotInOld = new ArrayList<>(proximityBeaconsNewList.size());
        HashMap<Integer, Beacon> oldNotNew = new HashMap<>(proximityBeacons);

        for(Beacon b2 : proximityBeaconsNewList)
        {
            oldNotNew.remove(b2.getMinor());

            if ( proximityBeacons.containsKey(b2.getMinor()) )
            {
                newInOld.add(b2);
            }
            else
            {
                newNotInOld.add(b2);
            }

        }
        // newInOld contiene tutti gli elementi della nuova lista che sono anche nella vecchia lista.
        // newNotInOld contiene tutti gli elementi della nuova lista che non sono nella vecchia lista.
        // oldNotInNew contiene tutti gli elementi della vecchia lista che non sono nella nuova.

        List<Beacon> removedBeacons = new ArrayList<>(this.proximityBeacons.size());

        for( Beacon b : newInOld)
        {
            // voglio refreshare tutti gli istanti di last refresh di ogni beacon considerato vicino
            // se necessario eliminarlo e poi re inserirlo, altrimenti re inserirlo e basta (se fa..)
            beacon_retry_map.remove(b.getMinor());
            beacon_retry_map.put(b.getMinor(), 0);
        }
        for( Beacon b : newNotInOld)
        {
            // Inserisco tutti i nuovi beacon rilevati che non erano giá in lista
            beacon_retry_map.put(b.getMinor(), 0);
            proximityBeacons.put(b.getMinor(), b);
        }

        for( Beacon b : oldNotNew.values() )
        {
            // per ogni beacon vecchio che a questo refresh non è presente,
            // se è passato troppo tempo dall'ultimo refresh verrá eliminato.

            int nRetry =  beacon_retry_map.get(b.getMinor());
            nRetry++;
            if(nRetry > BEACON_MAX_TIMEOUT)
            {
                proximityBeacons.remove(b.getMinor());
                beacon_retry_map.remove(b.getMinor());
                removedBeacons.add(b);
            }
            else beacon_retry_map.put(b.getMinor(), nRetry);
        }

        if(newNotInOld.size() > 0)
            listener.onNewBeacons(newNotInOld);
        if(removedBeacons.size() > 0)
        listener.onRemovedBeacons(removedBeacons);
    }

//* * * * * * * * * * * *  REFRESH TASK * * * * * * * * * * * * * * * * * * * * * * * * * * * *

    private class RefreshTask extends AsyncTask<Void, Void, Boolean>
    {

        private volatile List<Beacon> removedBeacons;

        public boolean  refreshBeacons()
        {
            long istant = System.currentTimeMillis();
            removedBeacons = new ArrayList<>(proximityBeacons.size());

            for( Beacon b : proximityBeacons.values() )
            {
                // per ogni beacon, se è passato troppo tempo dall'ultima volta che è stato trovato
                // col bluetooth, verrá eliminato.

                int nRetry =  beacon_retry_map.get(b.getMinor());
                nRetry++;
                if(nRetry > BEACON_MAX_TIMEOUT)
                {
                    proximityBeacons.remove(b);
                    removedBeacons.add(b);
                }
                else beacon_retry_map.put(b.getMinor(), nRetry);
            }
            return( removedBeacons.size() > 0);
        }

        @Override
        protected Boolean doInBackground(Void... params) {

            try {
                Thread.currentThread().sleep(REFRESH_BEACON_SLEEP_TIME);
            }
            catch (InterruptedException e) {
            }
            return refreshBeacons();
        }

        @Override
        protected void onPostExecute(Boolean removedSomething) {
            super.onPostExecute(removedSomething);
            if(removedSomething) listener.onRemovedBeacons(removedBeacons);
            if(runRefreshTask)
                new RefreshTask().execute();

        }

    }

    private void startRefreshTask(){
        if(this.runRefreshTask == false )
        {
            this.runRefreshTask = true;
            new RefreshTask().execute();
        }
    }

    private void stopRefreshTask() {
        this.runRefreshTask = false;
    }

//    private void startThread()
//    {
//
//        AsyncTask t = new
//
//        refresherThread = new Thread(new Runnable() {
//
//            public void run()
//            {
//
//                while(!Thread.currentThread().isInterrupted())
//                {
//
//
//
//
//                }
//
//            }
//
//
//
//        });
//    }
//    private void stopThread()
//    {
//        refresherThread.interrupt();
//    }

    //
//    private class BeaconProximityTask extends AsyncTask<List<Beacon>, Void, Boolean>
//    {
//
//        @Override
//        protected Boolean doInBackground(List<Beacon> ... detectedBeacons) {
//            onBeaconProximity(detectedBeacons[0]);
//            return true;
//        }
//
//        @Override
//        protected void onPostExecute(Boolean aBoolean) {
//            super.onPostExecute(aBoolean);
//        }
//    }

}


text:
FROM php:7.2.8-alpine

LABEL maintainer="hyancat.cn@gmail.com"

# Prepare Env

ADD repositories /etc/apk/repositories

RUN set -xe; \
        \
        apk add --no-cache \
        git \
        memcached-dev \
        freetype-dev \
        libjpeg-turbo-dev \
        libpng-dev \
        jpeg-dev \
        zlib-dev \
        libxml2 \
        imagemagick imagemagick-dev \
        curl \
        openssl openssl-dev

# PHP config

ADD php.ini /usr/local/etc/php/php.ini

# Install PHP extensions

RUN docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ --with-png-dir=/usr/include/ \
        && docker-php-ext-install zip \
        && docker-php-ext-install gd \
        && docker-php-ext-install mbstring \
        && docker-php-ext-install pdo_mysql \
        && docker-php-ext-install opcache

RUN apk add --no-cache --virtual .phpize-deps $PHPIZE_DEPS linux-headers

RUN pecl install imagick-3.4.3 && docker-php-ext-enable imagick
RUN pecl install redis-3.1.6 && docker-php-ext-enable redis
RUN pecl install swoole-2.0.10 && docker-php-ext-enable swoole
RUN pecl install inotify-2.0.0 && docker-php-ext-enable inotify

RUN apk del .phpize-deps

# User Permission

RUN adduser -D -h /docker docker

WORKDIR /docker

COPY helloworld.php /docker/

ENV SWOOLE_PORT 1215
EXPOSE $SWOOLE_PORT

EXPOSE 9501
VOLUME ["/docker"]

CMD ["php", "/docker/helloworld.php"]


text:
namespace safe_browsing {

SafeBrowsingProtocolConfig::SafeBrowsingProtocolConfig()
    : disable_auto_update(false) {}

SafeBrowsingProtocolConfig::SafeBrowsingProtocolConfig(
    const SafeBrowsingProtocolConfig& other) = default;

SafeBrowsingProtocolConfig::~SafeBrowsingProtocolConfig() {}

// static
std::string SafeBrowsingProtocolManagerHelper::Version() {
  if (version_info::GetVersionNumber().empty())
    return "0.1";
  else
    return version_info::GetVersionNumber();
}

// static
std::string SafeBrowsingProtocolManagerHelper::ComposeUrl(
    const std::string& prefix,
    const std::string& method,
    const std::string& client_name,
    const std::string& version,
    const std::string& additional_query) {
  DCHECK(!prefix.empty() && !method.empty() &&
         !client_name.empty() && !version.empty());
  std::string url = base::StringPrintf("%s/%s?client=%s&appver=%s&pver=3.0",
                                       prefix.c_str(), method.c_str(),
                                       client_name.c_str(), version.c_str());
  std::string api_key = google_apis::GetAPIKey();
  if (!api_key.empty()) {
    base::StringAppendF(&url, "&key=%s",
                        net::EscapeQueryParamValue(api_key, true).c_str());
  }
  if (!additional_query.empty()) {
    DCHECK(url.find("?") != std::string::npos);
    url.append("&");
    url.append(additional_query);
  }
  return url;
}

// static
std::string SafeBrowsingProtocolManagerHelper::ComposeUrl(
    const std::string& prefix,
    const std::string& method,
    const std::string& client_name,
    const std::string& version,
    const std::string& additional_query,
    ExtendedReportingLevel reporting_level) {
  std::string url =
      ComposeUrl(prefix, method, client_name, version, additional_query);
  url.append(base::StringPrintf("&ext=%d", reporting_level));
  return url;
}

}  // namespace safe_browsing


text:
package org.leguan.credentials;

import org.leguan.AbstractFactory;

public class CredentialsStoreFactory extends AbstractFactory<CredentialsStore> {

}


text:
"use strict";

// eslint-disable-next-line no-var, no-undef -- This is the reason
hello = true;


text:
import { Connection } from '../database';
import {Message, SUCCESS_OK} from "../messages";
import * as statusTypes from '../config/statusTypes';

export class ConfigController
{
    private database: Connection;

    constructor()
    {
        this.database = Connection.getInstance();
    }

    public isWifiSSIDMatching(ssid: string): any
    {
        const correctSSID = this.database.currentSettings.wifiSSID;
        return { data: { check: correctSSID.localeCompare(ssid) === 0 }, message: new Message(SUCCESS_OK, 'SSID was checked!') };
    }

}


text:
namespace Apache.Ignite.Core.Compute
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics.CodeAnalysis;
    using Apache.Ignite.Core.Cluster;
    using Apache.Ignite.Core.Common;
    using Apache.Ignite.Core.Impl.Compute;

    /// <summary>
    /// Convenience adapter for <see cref="IComputeTask{TArg,TJobRes,TTaskRes}"/> interface
    /// </summary>
    public abstract class ComputeTaskAdapter<TArg, TJobRes, TTaskRes> : IComputeTask<TArg, TJobRes, TTaskRes>
    {
        /// <summary>
        /// Default implementation which will wait for all jobs to complete before
        /// calling <see cref="IComputeTask{A,T,R}.Reduce"/> method.
        /// <p/>
        /// If remote job resulted in exception <see cref="IComputeJobResult{T}.Exception()"/>
        /// is not <c>null</c>),
        /// then <see cref="ComputeJobResultPolicy.Failover"/>  policy will be returned if
        /// the exception is instance of <see cref="ClusterTopologyException"/>
        /// or <see cref="ComputeExecutionRejectedException"/>, which means that
        /// remote node either failed or job execution was rejected before it got a chance to start. In all
        /// other cases the exception will be rethrown which will ultimately cause task to fail.
        /// </summary>
        /// <param name="res">Received remote Ignite executable result.</param>
        /// <param name="rcvd">All previously received results.</param>
        /// <returns>Result policy that dictates how to process further upcoming job results.</returns>
        [SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods")]
        public virtual ComputeJobResultPolicy OnResult(IComputeJobResult<TJobRes> res,
            IList<IComputeJobResult<TJobRes>> rcvd)
        {
            Exception err = res.Exception;

            if (err != null)
            {
                if (Compute.IsFailoverException(err))
                    return ComputeJobResultPolicy.Failover;

                throw new IgniteException("Remote job threw user exception (override or implement " +
                    "IComputeTask.result(..) method if you would like to have automatic failover for this exception).",
                    err);
            }

            return ComputeJobResultPolicy.Wait;
        }

        /// <summary>
        /// This method is called to map or split Ignite task into multiple Ignite jobs. This is the
        /// first method that gets called when task execution starts.
        /// </summary>
        /// <param name="subgrid">Nodes available for this task execution. Note that order of nodes is
        /// guaranteed to be randomized by container. This ensures that every time you simply iterate
        /// through Ignite nodes, the order of nodes will be random which over time should result into
        /// all nodes being used equally.</param>
        /// <param name="arg">Task execution argument. Can be <c>null</c>. This is the same argument
        /// as the one passed into <c>ICompute.Execute()</c> methods.</param>
        /// <returns>
        /// Map of Ignite jobs assigned to subgrid node. If <c>null</c> or empty map is returned,
        /// exception will be thrown.
        /// </returns>
        public abstract IDictionary<IComputeJob<TJobRes>, IClusterNode> Map(IList<IClusterNode> subgrid, TArg arg);

        /// <summary>
        /// Reduces (or aggregates) results received so far into one compound result to be returned to
        /// caller via task.
        /// <para />
        /// Note, that if some jobs did not succeed and could not be failed over then the list of
        /// results passed into this method will include the failed results. Otherwise, failed
        /// results will not be in the list.
        /// </summary>
        /// <param name="results">Received job results. Note that if task class has
        /// <see cref="ComputeTaskNoResultCacheAttribute" /> attribute, then this list will be empty.</param>
        /// <returns>
        /// Task result constructed from results of remote executions.
        /// </returns>
        public abstract TTaskRes Reduce(IList<IComputeJobResult<TJobRes>> results);
    }
}


text:
<?php

namespace Soda\Cms\Foundation\Providers\Traits;

use Illuminate\Foundation\AliasLoader;

trait RegistersBindingsAndDependencies
{
    /**
     * Register bindings.
     *
     * @param array $bindings
     */
    public function registerBindings(array $bindings)
    {
        foreach ($bindings as $abstract => $concrete) {
            $this->app->bind($abstract, $concrete);
        }
    }

    /**
     * Register instances.
     *
     * @param array $instances
     */
    public function registerInstances(array $instances)
    {
        foreach ($instances as $abstract => $concrete) {
            $this->app->instance($abstract, $concrete);
        }
    }

    /**
     * Register singletons.
     *
     * @param array $singletons
     */
    public function registerSingletons(array $singletons)
    {
        foreach ($singletons as $abstract => $concrete) {
            $this->app->instance($abstract, $concrete);
        }
    }

    /**
     * Register aliases.
     *
     * @param array $aliasGroup
     */
    public function registerAliases(array $aliasGroup)
    {
        foreach ($aliasGroup as $key => $aliases) {
            foreach ($aliases as $alias) {
                $this->app->alias($key, $alias);
            }
        }
    }

    /**
     * Register facades.
     *
     * @param array $facades
     */
    protected function registerFacades(array $facades)
    {
        AliasLoader::getInstance($facades);
    }

    /**
     * Register dependencies.
     *
     * @param array $services
     */
    protected function registerDependencies(array $services)
    {
        foreach ($services as $service) {
            $this->app->register($service);
        }
    }
}


text:
package com.borsaistanbul.services;

import java.util.List;

import com.borsaistanbul.entities.User;

public interface UserService {

	public void addUser(User user);
	public List<User> getUserList();
	public void updateUser(User user);
	public void deleteUser(String id);
	public User getUser(String id);

}


text:
'use strict';

// Project authorization helpers
var hasAuthorization = function (req, res, next) {
    if (!req.user.isAdmin) {
        return res.status(401).send('User is not authorized');
    }
    next();
};

var hasPermissions = function (req, res, next) {

    req.body.permissions = req.body.permissions || ['authenticated'];

    for (var i = 0; i < req.body.permissions.length; i++) {
        var permission = req.body.permissions[i];
        if (req.acl.user.allowed.indexOf(permission) === -1) {
            return res.status(401).send('User not allowed to assign ' + permission + ' permission.');
        }
    }
    next();
};

//var isLoggedIn = function (req, res) {
//
//}

module.exports = function (Projects, app, auth) {

    var projects = require('../controllers/projects')(Projects);

    app.route('/api/projects')
            .get(auth.requiresLogin, projects.getProjects)
            .put(auth.requiresLogin, projects.countProjects)
            .post(auth.requiresLogin, hasPermissions, projects.create);
    app.route('/api/states')
            .get(projects.allStates);
    app.route('/api/projects/:projectID')
            .get(auth.isMongoId, auth.requiresLogin, projects.show)
            .put(auth.isMongoId, auth.requiresLogin, projects.update)
            .delete(auth.isMongoId, auth.requiresLogin, hasAuthorization, projects.destroy);
    app.route('/api/projects/rev/:projectID')
            .put(auth.isMongoId, auth.requiresLogin, projects.addReview);
    app.route('/api/projects/rej/:projectID')
            .put(auth.isMongoId, auth.requiresLogin, projects.addRejected);
    app.route('/api/projects/sign/:projectID')
            .put(auth.isMongoId, auth.requiresLogin, projects.addSigned);
    app.route('/api/projects/payment/:projectID')
            .put(auth.isMongoId, auth.requiresLogin, projects.addPayment);
    // TODO: Figure out some way to pass user credentials along with these two
    // requests in order to enable proper access control:
//    app.route('/api/projects/upload')
//            .post(auth.isMongoId, auth.requiresLogin, projects.addAppendix);
//    app.route('/api/projects/data/:projectID')
//            .get(auth.isMongoId, auth.requiresLogin, projects.accessAppendix);
    app.route('/api/projects/upload')
            .post(projects.addAppendix); //     <- Fix this.
    app.route('/api/projects/data/:projectID')
            .get(projects.accessAppendix); //   <- ...and this.
    app.route('/api/projects/end/:projectID')
            .put(auth.isMongoId, auth.requiresLogin, projects.addEnded);
    app.route('/api/projects/appr/:projectID')
            .put(auth.isMongoId, auth.requiresLogin, projects.addApproved);
    app.route('/api/projects/endReport/:projectID')
            .put(auth.isMongoId, auth.requiresLogin, projects.addEndReport);
    app.route('/api/projects/intReport/:projectID')
            .put(auth.isMongoId, auth.requiresLogin, projects.addIntReport);
    app.route('/api/projects/byOrg/:organisationId')
            .get(auth.requiresLogin, projects.byOrg);
    app.route('/api/projects/regRepPDF/:projectID')
            .put(auth.requiresLogin, projects.createRegRep);
    app.route('/api/projects/endRepPDF/:projectID')
            .put(auth.requiresLogin, projects.createEndRep);

    app.param('params', projects.getProjects);
    app.param('projectID', projects.project);
};


text:
function percentile(values, position) {
    if (values.length === 0) {
        return 0;
    }

    values = Array.from(values).sort();
    const idx = Math.floor(values.length * position);
    if (values.length % 2 == 1) {
        return values[idx];
    } else {
        return (values[idx] + values[idx - 1]) / 2;
    }
}

function mean(values) {
    if (values.length === 0) {
        return 0;
    }

    return values.reduce((prev, curr) => prev + curr, 0) / values.length;
}

module.exports = {
    mean,
    percentile
};


text:
<?php defined('COREPATH') or exit('No direct script access allowed'); ?>

ERROR - 2014-10-03 16:50:46 --> Fatal Error - Class 'Authorization' not found in /home/hecncit/fuel/app/views/sign_in.php on line 28
ERROR - 2014-10-03 16:51:32 --> Warning - header() expects at most 3 parameters, 6 given in /home/hecncit/fuel/app/views/sign_in.php on line 34
ERROR - 2014-10-03 17:21:30 --> Parsing Error - syntax error, unexpected ')' in /home/hecncit/fuel/app/views/sign_in.php on line 51


text:
<?xml version="1.0" encoding="utf-8"?>
<!--

    The MIT License (MIT)

    Copyright (c) 2016 Johannes Schnatterer

    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in all
    copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    SOFTWARE.

-->
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="info.schnatterer.logbackandroiddemo.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_main" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>


text:
/* **************************************************************
*  Includes
****************************************************************/
#include <stdlib.h>     /* malloc, free, qsort */
#include <string.h>     /* memcpy, memset */
#include <stdio.h>      /* printf (debug) */
#include "bitstream.h"
#include "compiler.h"
#define FSE_STATIC_LINKING_ONLY
#include "fse.h"
#include "error_private.h"

/* **************************************************************
*  Error Management
****************************************************************/
#define FSE_isError ERR_isError
#define FSE_STATIC_ASSERT(c) { enum { FSE_static_assert = 1/(int)(!!(c)) }; }   /* use only *after* variable declarations */

/* **************************************************************
*  Templates
****************************************************************/
/*
  designed to be included
  for type-specific functions (template emulation in C)
  Objective is to write these functions only once, for improved maintenance
*/

/* safety checks */
#ifndef FSE_FUNCTION_EXTENSION
#  error "FSE_FUNCTION_EXTENSION must be defined"
#endif
#ifndef FSE_FUNCTION_TYPE
#  error "FSE_FUNCTION_TYPE must be defined"
#endif

/* Function names */
#define FSE_CAT(X,Y) X##Y
#define FSE_FUNCTION_NAME(X,Y) FSE_CAT(X,Y)
#define FSE_TYPE_NAME(X,Y) FSE_CAT(X,Y)

/* Function templates */

/* FSE_buildCTable_wksp() :
 * Same as FSE_buildCTable(), but using an externally allocated scratch buffer (`workSpace`).
 * wkspSize should be sized to handle worst case situation, which is `1<<max_tableLog * sizeof(FSE_FUNCTION_TYPE)`
 * workSpace must also be properly aligned with FSE_FUNCTION_TYPE requirements
 */
size_t FSE_buildCTable_wksp(FSE_CTable* ct, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog, void* workSpace, size_t wkspSize)
{
    U32 const tableSize = 1 << tableLog;
    U32 const tableMask = tableSize - 1;
    void* const ptr = ct;
    U16* const tableU16 = ( (U16*) ptr) + 2;
    void* const FSCT = ((U32*)ptr) + 1 /* header */ + (tableLog ? tableSize>>1 : 1) ;
    FSE_symbolCompressionTransform* const symbolTT = (FSE_symbolCompressionTransform*) (FSCT);
    U32 const step = FSE_TABLESTEP(tableSize);
    U32 cumul[FSE_MAX_SYMBOL_VALUE+2];

    FSE_FUNCTION_TYPE* const tableSymbol = (FSE_FUNCTION_TYPE*)workSpace;
    U32 highThreshold = tableSize-1;

    /* CTable header */
    if (((size_t)1 << tableLog) * sizeof(FSE_FUNCTION_TYPE) > wkspSize) return ERROR(tableLog_tooLarge);
    tableU16[-2] = (U16) tableLog;
    tableU16[-1] = (U16) maxSymbolValue;

    /* For explanations on how to distribute symbol values over the table :
    *  http://fastcompression.blogspot.fr/2014/02/fse-distributing-symbol-values.html */

    /* symbol start positions */
    {   U32 u;
        cumul[0] = 0;
        for (u=1; u<=maxSymbolValue+1; u++) {
            if (normalizedCounter[u-1]==-1) {  /* Low proba symbol */
                cumul[u] = cumul[u-1] + 1;
                tableSymbol[highThreshold--] = (FSE_FUNCTION_TYPE)(u-1);
            } else {
                cumul[u] = cumul[u-1] + normalizedCounter[u-1];
        }   }
        cumul[maxSymbolValue+1] = tableSize+1;
    }

    /* Spread symbols */
    {   U32 position = 0;
        U32 symbol;
        for (symbol=0; symbol<=maxSymbolValue; symbol++) {
            int nbOccurences;
            for (nbOccurences=0; nbOccurences<normalizedCounter[symbol]; nbOccurences++) {
                tableSymbol[position] = (FSE_FUNCTION_TYPE)symbol;
                position = (position + step) & tableMask;
                while (position > highThreshold) position = (position + step) & tableMask;   /* Low proba area */
        }   }

        if (position!=0) return ERROR(GENERIC);   /* Must have gone through all positions */
    }

    /* Build table */
    {   U32 u; for (u=0; u<tableSize; u++) {
        FSE_FUNCTION_TYPE s = tableSymbol[u];   /* note : static analyzer may not understand tableSymbol is properly initialized */
        tableU16[cumul[s]++] = (U16) (tableSize+u);   /* TableU16 : sorted by symbol order; gives next state value */
    }   }

    /* Build Symbol Transformation Table */
    {   unsigned total = 0;
        unsigned s;
        for (s=0; s<=maxSymbolValue; s++) {
            switch (normalizedCounter[s])
            {
            case  0: break;

            case -1:
            case  1:
                symbolTT[s].deltaNbBits = (tableLog << 16) - (1<<tableLog);
                symbolTT[s].deltaFindState = total - 1;
                total ++;
                break;
            default :
                {
                    U32 const maxBitsOut = tableLog - BIT_highbit32 (normalizedCounter[s]-1);
                    U32 const minStatePlus = normalizedCounter[s] << maxBitsOut;
                    symbolTT[s].deltaNbBits = (maxBitsOut << 16) - minStatePlus;
                    symbolTT[s].deltaFindState = total - normalizedCounter[s];
                    total +=  normalizedCounter[s];
    }   }   }   }

    return 0;
}

size_t FSE_buildCTable(FSE_CTable* ct, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog)
{
    FSE_FUNCTION_TYPE tableSymbol[FSE_MAX_TABLESIZE];   /* memset() is not necessary, even if static analyzer complain about it */
    return FSE_buildCTable_wksp(ct, normalizedCounter, maxSymbolValue, tableLog, tableSymbol, sizeof(tableSymbol));
}

#ifndef FSE_COMMONDEFS_ONLY

/*-**************************************************************
*  FSE NCount encoding-decoding
****************************************************************/
size_t FSE_NCountWriteBound(unsigned maxSymbolValue, unsigned tableLog)
{
    size_t const maxHeaderSize = (((maxSymbolValue+1) * tableLog) >> 3) + 3;
    return maxSymbolValue ? maxHeaderSize : FSE_NCOUNTBOUND;  /* maxSymbolValue==0 ? use default */
}

static size_t FSE_writeNCount_generic (void* header, size_t headerBufferSize,
                                       const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog,
                                       unsigned writeIsSafe)
{
    BYTE* const ostart = (BYTE*) header;
    BYTE* out = ostart;
    BYTE* const oend = ostart + headerBufferSize;
    int nbBits;
    const int tableSize = 1 << tableLog;
    int remaining;
    int threshold;
    U32 bitStream;
    int bitCount;
    unsigned charnum = 0;
    int previous0 = 0;

    bitStream = 0;
    bitCount  = 0;
    /* Table Size */
    bitStream += (tableLog-FSE_MIN_TABLELOG) << bitCount;
    bitCount  += 4;

    /* Init */
    remaining = tableSize+1;   /* +1 for extra accuracy */
    threshold = tableSize;
    nbBits = tableLog+1;

    while (remaining>1) {  /* stops at 1 */
        if (previous0) {
            unsigned start = charnum;
            while (!normalizedCounter[charnum]) charnum++;
            while (charnum >= start+24) {
                start+=24;
                bitStream += 0xFFFFU << bitCount;
                if ((!writeIsSafe) && (out > oend-2)) return ERROR(dstSize_tooSmall);   /* Buffer overflow */
                out[0] = (BYTE) bitStream;
                out[1] = (BYTE)(bitStream>>8);
                out+=2;
                bitStream>>=16;
            }
            while (charnum >= start+3) {
                start+=3;
                bitStream += 3 << bitCount;
                bitCount += 2;
            }
            bitStream += (charnum-start) << bitCount;
            bitCount += 2;
            if (bitCount>16) {
                if ((!writeIsSafe) && (out > oend - 2)) return ERROR(dstSize_tooSmall);   /* Buffer overflow */
                out[0] = (BYTE)bitStream;
                out[1] = (BYTE)(bitStream>>8);
                out += 2;
                bitStream >>= 16;
                bitCount -= 16;
        }   }
        {   int count = normalizedCounter[charnum++];
            int const max = (2*threshold-1)-remaining;
            remaining -= count < 0 ? -count : count;
            count++;   /* +1 for extra accuracy */
            if (count>=threshold) count += max;   /* [0..max[ [max..threshold[ (...) [threshold+max 2*threshold[ */
            bitStream += count << bitCount;
            bitCount  += nbBits;
            bitCount  -= (count<max);
            previous0  = (count==1);
            if (remaining<1) return ERROR(GENERIC);
            while (remaining<threshold) nbBits--, threshold>>=1;
        }
        if (bitCount>16) {
            if ((!writeIsSafe) && (out > oend - 2)) return ERROR(dstSize_tooSmall);   /* Buffer overflow */
            out[0] = (BYTE)bitStream;
            out[1] = (BYTE)(bitStream>>8);
            out += 2;
            bitStream >>= 16;
            bitCount -= 16;
    }   }

    /* flush remaining bitStream */
    if ((!writeIsSafe) && (out > oend - 2)) return ERROR(dstSize_tooSmall);   /* Buffer overflow */
    out[0] = (BYTE)bitStream;
    out[1] = (BYTE)(bitStream>>8);
    out+= (bitCount+7) /8;

    if (charnum > maxSymbolValue + 1) return ERROR(GENERIC);

    return (out-ostart);
}

size_t FSE_writeNCount (void* buffer, size_t bufferSize, const short* normalizedCounter, unsigned maxSymbolValue, unsigned tableLog)
{
    if (tableLog > FSE_MAX_TABLELOG) return ERROR(tableLog_tooLarge);   /* Unsupported */
    if (tableLog < FSE_MIN_TABLELOG) return ERROR(GENERIC);   /* Unsupported */

    if (bufferSize < FSE_NCountWriteBound(maxSymbolValue, tableLog))
        return FSE_writeNCount_generic(buffer, bufferSize, normalizedCounter, maxSymbolValue, tableLog, 0);

    return FSE_writeNCount_generic(buffer, bufferSize, normalizedCounter, maxSymbolValue, tableLog, 1);
}

/*-**************************************************************
*  Counting histogram
****************************************************************/
/*! FSE_count_simple
    This function counts byte values within `src`, and store the histogram into table `count`.
    It doesn't use any additional memory.
    But this function is unsafe : it doesn't check that all values within `src` can fit into `count`.
    For this reason, prefer using a table `count` with 256 elements.
    @return : count of most numerous element
*/
size_t FSE_count_simple(unsigned* count, unsigned* maxSymbolValuePtr,
                        const void* src, size_t srcSize)
{
    const BYTE* ip = (const BYTE*)src;
    const BYTE* const end = ip + srcSize;
    unsigned maxSymbolValue = *maxSymbolValuePtr;
    unsigned max=0;

    memset(count, 0, (maxSymbolValue+1)*sizeof(*count));
    if (srcSize==0) { *maxSymbolValuePtr = 0; return 0; }

    while (ip<end) count[*ip++]++;

    while (!count[maxSymbolValue]) maxSymbolValue--;
    *maxSymbolValuePtr = maxSymbolValue;

    { U32 s; for (s=0; s<=maxSymbolValue; s++) if (count[s] > max) max = count[s]; }

    return (size_t)max;
}

/* FSE_count_parallel_wksp() :
 * Same as FSE_count_parallel(), but using an externally provided scratch buffer.
 * `workSpace` size must be a minimum of `1024 * sizeof(unsigned)`` */
static size_t FSE_count_parallel_wksp(
                                unsigned* count, unsigned* maxSymbolValuePtr,
                                const void* source, size_t sourceSize,
                                unsigned checkMax, unsigned* const workSpace)
{
    const BYTE* ip = (const BYTE*)source;
    const BYTE* const iend = ip+sourceSize;
    unsigned maxSymbolValue = *maxSymbolValuePtr;
    unsigned max=0;
    U32* const Counting1 = workSpace;
    U32* const Counting2 = Counting1 + 256;
    U32* const Counting3 = Counting2 + 256;
    U32* const Counting4 = Counting3 + 256;

    memset(Counting1, 0, 4*256*sizeof(unsigned));

    /* safety checks */
    if (!sourceSize) {
        memset(count, 0, maxSymbolValue + 1);
        *maxSymbolValuePtr = 0;
        return 0;
    }
    if (!maxSymbolValue) maxSymbolValue = 255;            /* 0 == default */

    /* by stripes of 16 bytes */
    {   U32 cached = MEM_read32(ip); ip += 4;
        while (ip < iend-15) {
            U32 c = cached; cached = MEM_read32(ip); ip += 4;
            Counting1[(BYTE) c     ]++;
            Counting2[(BYTE)(c>>8) ]++;
            Counting3[(BYTE)(c>>16)]++;
            Counting4[       c>>24 ]++;
            c = cached; cached = MEM_read32(ip); ip += 4;
            Counting1[(BYTE) c     ]++;
            Counting2[(BYTE)(c>>8) ]++;
            Counting3[(BYTE)(c>>16)]++;
            Counting4[       c>>24 ]++;
            c = cached; cached = MEM_read32(ip); ip += 4;
            Counting1[(BYTE) c     ]++;
            Counting2[(BYTE)(c>>8) ]++;
            Counting3[(BYTE)(c>>16)]++;
            Counting4[       c>>24 ]++;
            c = cached; cached = MEM_read32(ip); ip += 4;
            Counting1[(BYTE) c     ]++;
            Counting2[(BYTE)(c>>8) ]++;
            Counting3[(BYTE)(c>>16)]++;
            Counting4[       c>>24 ]++;
        }
        ip-=4;
    }

    /* finish last symbols */
    while (ip<iend) Counting1[*ip++]++;

    if (checkMax) {   /* verify stats will fit into destination table */
        U32 s; for (s=255; s>maxSymbolValue; s--) {
            Counting1[s] += Counting2[s] + Counting3[s] + Counting4[s];
            if (Counting1[s]) return ERROR(maxSymbolValue_tooSmall);
    }   }

    {   U32 s; for (s=0; s<=maxSymbolValue; s++) {
            count[s] = Counting1[s] + Counting2[s] + Counting3[s] + Counting4[s];
            if (count[s] > max) max = count[s];
    }   }

    while (!count[maxSymbolValue]) maxSymbolValue--;
    *maxSymbolValuePtr = maxSymbolValue;
    return (size_t)max;
}

/* FSE_countFast_wksp() :
 * Same as FSE_countFast(), but using an externally provided scratch buffer.
 * `workSpace` size must be table of >= `1024` unsigned */
size_t FSE_countFast_wksp(unsigned* count, unsigned* maxSymbolValuePtr,
                     const void* source, size_t sourceSize, unsigned* workSpace)
{
    if (sourceSize < 1500) return FSE_count_simple(count, maxSymbolValuePtr, source, sourceSize);
    return FSE_count_parallel_wksp(count, maxSymbolValuePtr, source, sourceSize, 0, workSpace);
}

/* fast variant (unsafe : won't check if src contains values beyond count[] limit) */
size_t FSE_countFast(unsigned* count, unsigned* maxSymbolValuePtr,
                     const void* source, size_t sourceSize)
{
    unsigned tmpCounters[1024];
    return FSE_countFast_wksp(count, maxSymbolValuePtr, source, sourceSize, tmpCounters);
}

/* FSE_count_wksp() :
 * Same as FSE_count(), but using an externally provided scratch buffer.
 * `workSpace` size must be table of >= `1024` unsigned */
size_t FSE_count_wksp(unsigned* count, unsigned* maxSymbolValuePtr,
                 const void* source, size_t sourceSize, unsigned* workSpace)
{
    if (*maxSymbolValuePtr < 255)
        return FSE_count_parallel_wksp(count, maxSymbolValuePtr, source, sourceSize, 1, workSpace);
    *maxSymbolValuePtr = 255;
    return FSE_countFast_wksp(count, maxSymbolValuePtr, source, sourceSize, workSpace);
}

size_t FSE_count(unsigned* count, unsigned* maxSymbolValuePtr,
                 const void* src, size_t srcSize)
{
    unsigned tmpCounters[1024];
    return FSE_count_wksp(count, maxSymbolValuePtr, src, srcSize, tmpCounters);
}

/*-**************************************************************
*  FSE Compression Code
****************************************************************/
/*! FSE_sizeof_CTable() :
    FSE_CTable is a variable size structure which contains :
    `U16 tableLog;`
    `U16 maxSymbolValue;`
    `U16 nextStateNumber[1 << tableLog];`                         // This size is variable
    `FSE_symbolCompressionTransform symbolTT[maxSymbolValue+1];`  // This size is variable
Allocation is manual (C standard does not support variable-size structures).
*/
size_t FSE_sizeof_CTable (unsigned maxSymbolValue, unsigned tableLog)
{
    if (tableLog > FSE_MAX_TABLELOG) return ERROR(tableLog_tooLarge);
    return FSE_CTABLE_SIZE_U32 (tableLog, maxSymbolValue) * sizeof(U32);
}

FSE_CTable* FSE_createCTable (unsigned maxSymbolValue, unsigned tableLog)
{
    size_t size;
    if (tableLog > FSE_TABLELOG_ABSOLUTE_MAX) tableLog = FSE_TABLELOG_ABSOLUTE_MAX;
    size = FSE_CTABLE_SIZE_U32 (tableLog, maxSymbolValue) * sizeof(U32);
    return (FSE_CTable*)malloc(size);
}

void FSE_freeCTable (FSE_CTable* ct) { free(ct); }

/* provides the minimum logSize to safely represent a distribution */
static unsigned FSE_minTableLog(size_t srcSize, unsigned maxSymbolValue)
{
    U32 minBitsSrc = BIT_highbit32((U32)(srcSize - 1)) + 1;
    U32 minBitsSymbols = BIT_highbit32(maxSymbolValue) + 2;
    U32 minBits = minBitsSrc < minBitsSymbols ? minBitsSrc : minBitsSymbols;
    assert(srcSize > 1); /* Not supported, RLE should be used instead */
    return minBits;
}

unsigned FSE_optimalTableLog_internal(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue, unsigned minus)
{
    U32 maxBitsSrc = BIT_highbit32((U32)(srcSize - 1)) - minus;
    U32 tableLog = maxTableLog;
    U32 minBits = FSE_minTableLog(srcSize, maxSymbolValue);
    assert(srcSize > 1); /* Not supported, RLE should be used instead */
    if (tableLog==0) tableLog = FSE_DEFAULT_TABLELOG;
    if (maxBitsSrc < tableLog) tableLog = maxBitsSrc;   /* Accuracy can be reduced */
    if (minBits > tableLog) tableLog = minBits;   /* Need a minimum to safely represent all symbol values */
    if (tableLog < FSE_MIN_TABLELOG) tableLog = FSE_MIN_TABLELOG;
    if (tableLog > FSE_MAX_TABLELOG) tableLog = FSE_MAX_TABLELOG;
    return tableLog;
}

unsigned FSE_optimalTableLog(unsigned maxTableLog, size_t srcSize, unsigned maxSymbolValue)
{
    return FSE_optimalTableLog_internal(maxTableLog, srcSize, maxSymbolValue, 2);
}

/* Secondary normalization method.
   To be used when primary method fails. */

static size_t FSE_normalizeM2(short* norm, U32 tableLog, const unsigned* count, size_t total, U32 maxSymbolValue)
{
    short const NOT_YET_ASSIGNED = -2;
    U32 s;
    U32 distributed = 0;
    U32 ToDistribute;

    /* Init */
    U32 const lowThreshold = (U32)(total >> tableLog);
    U32 lowOne = (U32)((total * 3) >> (tableLog + 1));

    for (s=0; s<=maxSymbolValue; s++) {
        if (count[s] == 0) {
            norm[s]=0;
            continue;
        }
        if (count[s] <= lowThreshold) {
            norm[s] = -1;
            distributed++;
            total -= count[s];
            continue;
        }
        if (count[s] <= lowOne) {
            norm[s] = 1;
            distributed++;
            total -= count[s];
            continue;
        }

        norm[s]=NOT_YET_ASSIGNED;
    }
    ToDistribute = (1 << tableLog) - distributed;

    if ((total / ToDistribute) > lowOne) {
        /* risk of rounding to zero */
        lowOne = (U32)((total * 3) / (ToDistribute * 2));
        for (s=0; s<=maxSymbolValue; s++) {
            if ((norm[s] == NOT_YET_ASSIGNED) && (count[s] <= lowOne)) {
                norm[s] = 1;
                distributed++;
                total -= count[s];
                continue;
        }   }
        ToDistribute = (1 << tableLog) - distributed;
    }

    if (distributed == maxSymbolValue+1) {
        /* all values are pretty poor;
           probably incompressible data (should have already been detected);
           find max, then give all remaining points to max */
        U32 maxV = 0, maxC = 0;
        for (s=0; s<=maxSymbolValue; s++)
            if (count[s] > maxC) maxV=s, maxC=count[s];
        norm[maxV] += (short)ToDistribute;
        return 0;
    }

    if (total == 0) {
        /* all of the symbols were low enough for the lowOne or lowThreshold */
        for (s=0; ToDistribute > 0; s = (s+1)%(maxSymbolValue+1))
            if (norm[s] > 0) ToDistribute--, norm[s]++;
        return 0;
    }

    {   U64 const vStepLog = 62 - tableLog;
        U64 const mid = (1ULL << (vStepLog-1)) - 1;
        U64 const rStep = ((((U64)1<<vStepLog) * ToDistribute) + mid) / total;   /* scale on remaining */
        U64 tmpTotal = mid;
        for (s=0; s<=maxSymbolValue; s++) {
            if (norm[s]==NOT_YET_ASSIGNED) {
                U64 const end = tmpTotal + (count[s] * rStep);
                U32 const sStart = (U32)(tmpTotal >> vStepLog);
                U32 const sEnd = (U32)(end >> vStepLog);
                U32 const weight = sEnd - sStart;
                if (weight < 1)
                    return ERROR(GENERIC);
                norm[s] = (short)weight;
                tmpTotal = end;
    }   }   }

    return 0;
}

size_t FSE_normalizeCount (short* normalizedCounter, unsigned tableLog,
                           const unsigned* count, size_t total,
                           unsigned maxSymbolValue)
{
    /* Sanity checks */
    if (tableLog==0) tableLog = FSE_DEFAULT_TABLELOG;
    if (tableLog < FSE_MIN_TABLELOG) return ERROR(GENERIC);   /* Unsupported size */
    if (tableLog > FSE_MAX_TABLELOG) return ERROR(tableLog_tooLarge);   /* Unsupported size */
    if (tableLog < FSE_minTableLog(total, maxSymbolValue)) return ERROR(GENERIC);   /* Too small tableLog, compression potentially impossible */

    {   static U32 const rtbTable[] = {     0, 473195, 504333, 520860, 550000, 700000, 750000, 830000 };
        U64 const scale = 62 - tableLog;
        U64 const step = ((U64)1<<62) / total;   /* <== here, one division ! */
        U64 const vStep = 1ULL<<(scale-20);
        int stillToDistribute = 1<<tableLog;
        unsigned s;
        unsigned largest=0;
        short largestP=0;
        U32 lowThreshold = (U32)(total >> tableLog);

        for (s=0; s<=maxSymbolValue; s++) {
            if (count[s] == total) return 0;   /* rle special case */
            if (count[s] == 0) { normalizedCounter[s]=0; continue; }
            if (count[s] <= lowThreshold) {
                normalizedCounter[s] = -1;
                stillToDistribute--;
            } else {
                short proba = (short)((count[s]*step) >> scale);
                if (proba<8) {
                    U64 restToBeat = vStep * rtbTable[proba];
                    proba += (count[s]*step) - ((U64)proba<<scale) > restToBeat;
                }
                if (proba > largestP) largestP=proba, largest=s;
                normalizedCounter[s] = proba;
                stillToDistribute -= proba;
        }   }
        if (-stillToDistribute >= (normalizedCounter[largest] >> 1)) {
            /* corner case, need another normalization method */
            size_t const errorCode = FSE_normalizeM2(normalizedCounter, tableLog, count, total, maxSymbolValue);
            if (FSE_isError(errorCode)) return errorCode;
        }
        else normalizedCounter[largest] += (short)stillToDistribute;
    }

#if 0
    {   /* Print Table (debug) */
        U32 s;
        U32 nTotal = 0;
        for (s=0; s<=maxSymbolValue; s++)
            printf("%3i: %4i \n", s, normalizedCounter[s]);
        for (s=0; s<=maxSymbolValue; s++)
            nTotal += abs(normalizedCounter[s]);
        if (nTotal != (1U<<tableLog))
            printf("Warning !!! Total == %u != %u !!!", nTotal, 1U<<tableLog);
        getchar();
    }
#endif

    return tableLog;
}

/* fake FSE_CTable, for raw (uncompressed) input */
size_t FSE_buildCTable_raw (FSE_CTable* ct, unsigned nbBits)
{
    const unsigned tableSize = 1 << nbBits;
    const unsigned tableMask = tableSize - 1;
    const unsigned maxSymbolValue = tableMask;
    void* const ptr = ct;
    U16* const tableU16 = ( (U16*) ptr) + 2;
    void* const FSCT = ((U32*)ptr) + 1 /* header */ + (tableSize>>1);   /* assumption : tableLog >= 1 */
    FSE_symbolCompressionTransform* const symbolTT = (FSE_symbolCompressionTransform*) (FSCT);
    unsigned s;

    /* Sanity checks */
    if (nbBits < 1) return ERROR(GENERIC);             /* min size */

    /* header */
    tableU16[-2] = (U16) nbBits;
    tableU16[-1] = (U16) maxSymbolValue;

    /* Build table */
    for (s=0; s<tableSize; s++)
        tableU16[s] = (U16)(tableSize + s);

    /* Build Symbol Transformation Table */
    {   const U32 deltaNbBits = (nbBits << 16) - (1 << nbBits);
        for (s=0; s<=maxSymbolValue; s++) {
            symbolTT[s].deltaNbBits = deltaNbBits;
            symbolTT[s].deltaFindState = s-1;
    }   }

    return 0;
}

/* fake FSE_CTable, for rle input (always same symbol) */
size_t FSE_buildCTable_rle (FSE_CTable* ct, BYTE symbolValue)
{
    void* ptr = ct;
    U16* tableU16 = ( (U16*) ptr) + 2;
    void* FSCTptr = (U32*)ptr + 2;
    FSE_symbolCompressionTransform* symbolTT = (FSE_symbolCompressionTransform*) FSCTptr;

    /* header */
    tableU16[-2] = (U16) 0;
    tableU16[-1] = (U16) symbolValue;

    /* Build table */
    tableU16[0] = 0;
    tableU16[1] = 0;   /* just in case */

    /* Build Symbol Transformation Table */
    symbolTT[symbolValue].deltaNbBits = 0;
    symbolTT[symbolValue].deltaFindState = 0;

    return 0;
}

static size_t FSE_compress_usingCTable_generic (void* dst, size_t dstSize,
                           const void* src, size_t srcSize,
                           const FSE_CTable* ct, const unsigned fast)
{
    const BYTE* const istart = (const BYTE*) src;
    const BYTE* const iend = istart + srcSize;
    const BYTE* ip=iend;

    BIT_CStream_t bitC;
    FSE_CState_t CState1, CState2;

    /* init */
    if (srcSize <= 2) return 0;
    { size_t const initError = BIT_initCStream(&bitC, dst, dstSize);
      if (FSE_isError(initError)) return 0; /* not enough space available to write a bitstream */ }

#define FSE_FLUSHBITS(s)  (fast ? BIT_flushBitsFast(s) : BIT_flushBits(s))

    if (srcSize & 1) {
        FSE_initCState2(&CState1, ct, *--ip);
        FSE_initCState2(&CState2, ct, *--ip);
        FSE_encodeSymbol(&bitC, &CState1, *--ip);
        FSE_FLUSHBITS(&bitC);
    } else {
        FSE_initCState2(&CState2, ct, *--ip);
        FSE_initCState2(&CState1, ct, *--ip);
    }

    /* join to mod 4 */
    srcSize -= 2;
    if ((sizeof(bitC.bitContainer)*8 > FSE_MAX_TABLELOG*4+7 ) && (srcSize & 2)) {  /* test bit 2 */
        FSE_encodeSymbol(&bitC, &CState2, *--ip);
        FSE_encodeSymbol(&bitC, &CState1, *--ip);
        FSE_FLUSHBITS(&bitC);
    }

    /* 2 or 4 encoding per loop */
    while ( ip>istart ) {

        FSE_encodeSymbol(&bitC, &CState2, *--ip);

        if (sizeof(bitC.bitContainer)*8 < FSE_MAX_TABLELOG*2+7 )   /* this test must be static */
            FSE_FLUSHBITS(&bitC);

        FSE_encodeSymbol(&bitC, &CState1, *--ip);

        if (sizeof(bitC.bitContainer)*8 > FSE_MAX_TABLELOG*4+7 ) {  /* this test must be static */
            FSE_encodeSymbol(&bitC, &CState2, *--ip);
            FSE_encodeSymbol(&bitC, &CState1, *--ip);
        }

        FSE_FLUSHBITS(&bitC);
    }

    FSE_flushCState(&bitC, &CState2);
    FSE_flushCState(&bitC, &CState1);
    return BIT_closeCStream(&bitC);
}

size_t FSE_compress_usingCTable (void* dst, size_t dstSize,
                           const void* src, size_t srcSize,
                           const FSE_CTable* ct)
{
    unsigned const fast = (dstSize >= FSE_BLOCKBOUND(srcSize));

    if (fast)
        return FSE_compress_usingCTable_generic(dst, dstSize, src, srcSize, ct, 1);
    else
        return FSE_compress_usingCTable_generic(dst, dstSize, src, srcSize, ct, 0);
}

size_t FSE_compressBound(size_t size) { return FSE_COMPRESSBOUND(size); }

#define CHECK_V_F(e, f) size_t const e = f; if (ERR_isError(e)) return e
#define CHECK_F(f)   { CHECK_V_F(_var_err__, f); }

/* FSE_compress_wksp() :
 * Same as FSE_compress2(), but using an externally allocated scratch buffer (`workSpace`).
 * `wkspSize` size must be `(1<<tableLog)`.
 */
size_t FSE_compress_wksp (void* dst, size_t dstSize, const void* src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog, void* workSpace, size_t wkspSize)
{
    BYTE* const ostart = (BYTE*) dst;
    BYTE* op = ostart;
    BYTE* const oend = ostart + dstSize;

    U32   count[FSE_MAX_SYMBOL_VALUE+1];
    S16   norm[FSE_MAX_SYMBOL_VALUE+1];
    FSE_CTable* CTable = (FSE_CTable*)workSpace;
    size_t const CTableSize = FSE_CTABLE_SIZE_U32(tableLog, maxSymbolValue);
    void* scratchBuffer = (void*)(CTable + CTableSize);
    size_t const scratchBufferSize = wkspSize - (CTableSize * sizeof(FSE_CTable));

    /* init conditions */
    if (wkspSize < FSE_WKSP_SIZE_U32(tableLog, maxSymbolValue)) return ERROR(tableLog_tooLarge);
    if (srcSize <= 1) return 0;  /* Not compressible */
    if (!maxSymbolValue) maxSymbolValue = FSE_MAX_SYMBOL_VALUE;
    if (!tableLog) tableLog = FSE_DEFAULT_TABLELOG;

    /* Scan input and build symbol stats */
    {   CHECK_V_F(maxCount, FSE_count_wksp(count, &maxSymbolValue, src, srcSize, (unsigned*)scratchBuffer) );
        if (maxCount == srcSize) return 1;   /* only a single symbol in src : rle */
        if (maxCount == 1) return 0;         /* each symbol present maximum once => not compressible */
        if (maxCount < (srcSize >> 7)) return 0;   /* Heuristic : not compressible enough */
    }

    tableLog = FSE_optimalTableLog(tableLog, srcSize, maxSymbolValue);
    CHECK_F( FSE_normalizeCount(norm, tableLog, count, srcSize, maxSymbolValue) );

    /* Write table description header */
    {   CHECK_V_F(nc_err, FSE_writeNCount(op, oend-op, norm, maxSymbolValue, tableLog) );
        op += nc_err;
    }

    /* Compress */
    CHECK_F( FSE_buildCTable_wksp(CTable, norm, maxSymbolValue, tableLog, scratchBuffer, scratchBufferSize) );
    {   CHECK_V_F(cSize, FSE_compress_usingCTable(op, oend - op, src, srcSize, CTable) );
        if (cSize == 0) return 0;   /* not enough space for compressed data */
        op += cSize;
    }

    /* check compressibility */
    if ( (size_t)(op-ostart) >= srcSize-1 ) return 0;

    return op-ostart;
}

typedef struct {
    FSE_CTable CTable_max[FSE_CTABLE_SIZE_U32(FSE_MAX_TABLELOG, FSE_MAX_SYMBOL_VALUE)];
    BYTE scratchBuffer[1 << FSE_MAX_TABLELOG];
} fseWkspMax_t;

size_t FSE_compress2 (void* dst, size_t dstCapacity, const void* src, size_t srcSize, unsigned maxSymbolValue, unsigned tableLog)
{
    fseWkspMax_t scratchBuffer;
    FSE_STATIC_ASSERT(sizeof(scratchBuffer) >= FSE_WKSP_SIZE_U32(FSE_MAX_TABLELOG, FSE_MAX_SYMBOL_VALUE));   /* compilation failures here means scratchBuffer is not large enough */
    if (tableLog > FSE_MAX_TABLELOG) return ERROR(tableLog_tooLarge);
    return FSE_compress_wksp(dst, dstCapacity, src, srcSize, maxSymbolValue, tableLog, &scratchBuffer, sizeof(scratchBuffer));
}

size_t FSE_compress (void* dst, size_t dstCapacity, const void* src, size_t srcSize)
{
    return FSE_compress2(dst, dstCapacity, src, srcSize, FSE_MAX_SYMBOL_VALUE, FSE_DEFAULT_TABLELOG);
}

#endif   /* FSE_COMMONDEFS_ONLY */


text:
if [ -z $1 ]
then
	# Master branch
	BRANCH=""
else
	BRANCH="--branch "$1
fi
BUILD_DIR="cruise-control"
## Exit if git operation failed
git clone $BRANCH  https://github.com/linkedin/cruise-control $BUILD_DIR || exit "$?"

cd $BUILD_DIR
# Exit if build failed
./gradlew jar || exit "$?"
cp ./cruise-control-metrics-reporter/build/libs/cruise-control-metrics-reporter*.jar /ws/
cd ..
rm -rf $BUILD_DIR


text:
package hu.frankdavid.diss.expression

case class Cell(row: Int, col: Int) extends HasValue {
  def this() = this(0, 0)
}


text:
package provider

import (
	"github.com/hashicorp/terraform/helper/schema"
	"github.com/hashicorp/terraform/terraform"

	"os"

	sumo "github.com/erikvanbrakel/terraform-provider-sumologic/go-sumologic"
)

func Provider() terraform.ResourceProvider {
	return &schema.Provider{
		Schema: map[string]*schema.Schema{
			"access_id": {
				Type:     schema.TypeString,
				Optional: true,
				Default:  os.Getenv("SL_ACCESSID"),
			},
			"access_key": {
				Type:     schema.TypeString,
				Optional: true,
				Default:  os.Getenv("SL_ACCESSKEY"),
			},
			"environment": {
				Type:     schema.TypeString,
				Optional: true,
				Default:  "eu",
			},
		},
		ResourcesMap: map[string]*schema.Resource{
			"sumologic_collector":          resourceSumologicCollector(),
			"sumologic_http_source":        resourceSumologicHttpSource(),
			"sumologic_polling_source":     resourceSumologicPollingSource(),
			"sumologic_cloudsyslog_source": resourceSumologicCloudsyslogSource(),
		},
		ConfigureFunc: providerConfigure,
	}
}

func providerConfigure(d *schema.ResourceData) (interface{}, error) {
	return sumo.NewClient(
		d.Get("access_id").(string),
		d.Get("access_key").(string),
		d.Get("environment").(string),
	)
}


text:
/*
* ProximitySensor.cpp
*
* Created: 11/10/2014 8:47:35 PM
* Author: N
*/

#include "ProximitySensor.h"
#include "Pinouts.h"
#include <Arduino.h>
// default constructor
ProximitySensor::ProximitySensor()
{
//	pinMode(PROX_OUT, INPUT);
	int a = PROX_OUT;
	int b = 25;
	int c = 93;
	int d = 20150;
	_irPin=a;
	_avg=b;
	_tol=c/100;
	_model=d;

	analogReference(DEFAULT);
//	SharpIR sharp(a, b, c, d);
} //ProximitySensor
float ProximitySensor::distanceToObject() {
	//Detects closest object, returns distance in centimeters
	return dist;
}
bool ProximitySensor::detectObject() {
	return this->distanceToObject() > 700; //Detected an object less than n centimeters away
}
void ProximitySensor::exec() {
  Serial.print("Detect voltage at ");
  Serial.print(cm());
  Serial.print(" or at distance ");
  Serial.println(distance());
	dist=cm();
}
float ProximitySensor::cm() {
	int raw=analogRead(_irPin);
	float voltFromRaw=map(raw, 0, 1023, 0, 5000);
return voltFromRaw;
	int puntualDistance;
	if (_model==1080) {
		puntualDistance=27.728*pow(voltFromRaw/1000, -1.2045);
	} else if (_model==20150){
		puntualDistance=61.573*pow(voltFromRaw/1000, -1.1068);
	}
	return puntualDistance;
}

int ProximitySensor::distance() {
	_p=0;
	_sum=0;
	for (int i=0; i<_avg; i++){
		int foo=cm();
		if (foo>=(_tol*_previousDistance)){
			_previousDistance=foo;
			_sum=_sum+foo;
			_p++;
		}
	}
	int accurateDistance=_sum/_p;
	return accurateDistance;
}


text:
const configuration = require('../lib/configuration')
const noop = require('./track/noop')
const newrelic = require('./track/newrelic')
const env = require('./env.js')

configuration.load()

const disabledAnalytics = configuration.disableAnalytics
const isTesting = env.name === 'test'

module.exports = (isTesting || disabledAnalytics) ? noop : newrelic


text:
using System;

namespace KataNetHack.Console.Input
{
    public class Input : IInput, IInputPoller
    {
        public Func<ConsoleKeyInfo> ReadKey = () => System.Console.ReadKey(intercept:true);

        public event Action<InputResult> InputReceived;

        public void PollForInput()
        {
            InputResult inputReceived;
            var key = ReadKey();

            switch(key.Key)
            {
                case ConsoleKey.W:
                    inputReceived = InputResult.North;
                    break;
                case ConsoleKey.A:
                    inputReceived = InputResult.West;
                    break;
                case ConsoleKey.S:
                    inputReceived = InputResult.South;
                    break;
                case ConsoleKey.D:
                    inputReceived = InputResult.East;
                    break;
                default:
                    inputReceived = InputResult.Invalid;
                    break;
            }

            if(inputReceived != InputResult.Invalid)
                InputReceived?.Invoke(inputReceived);
        }
    }
}


text:
"""
PLC 3
"""

from minicps.devices import PLC
from threading import Thread
from utils import *

import time
import sys
import socket
import json
import select

P301 = ('P301', 3)
LIT301 = ('LIT301', 3)

LIT301_ADDR = IP['lit301']
P301_ADDR = IP['p301']
PLC101_ADDR = IP['plc101-HMI']

class HMISocket(Thread):
    """ Class that responds to the POLL command from HMI """
    def __init__(self, plc_object):
        Thread.__init__(self)
        self.plc = plc_object
        self.lit301 = 0.0

    def run(self):
        print "DEBUG entering socket thread run"
        self.sock = socket.socket()
        self.sock.connect((IP['HMI'], int(PORTS['hmi_poll_port'])))
        data = ''
        while True:
            while data=='':
                data = self.sock.recv(100)
                time.sleep(1)
                if data == "POLL":
                    self.sock.send("LIT301: "+str(self.lit101))
                data = ''
    def setLit301(self, val):
        self.lit301 = val

# TODO: real value tag where to read/write flow sensor
class PLC301(PLC):

    def pre_loop(self, sleep=0.1):
        print 'DEBUG: swat-s1 plc1 enters pre_loop'

    def main_loop(self):
        """plc1 main loop.

            - reads sensors value
            - drives actuators according to the control strategy
            - updates its enip server
        """

        print 'DEBUG: swat-s1 plc1 enters main_loop.'
        print

        count = 0
        while(count <= PLC_SAMPLES):

            lit301 = float(self.receive(LIT301, LIT301_ADDR))
            self.send_message(PLC101_ADDR, 8754, lit301)

            if lit301 >= LIT_301_M['HH'] :
                self.send(P301, 1, IP['plc301'])

            elif lit301 >= LIT_301_M['H']:
                self.send(P301, 1, IP['plc301'])

            elif lit301 <= LIT_301_M['LL']:
                self.send(P301, 0, IP['plc301'])

            elif lit301 <= LIT_301_M['L']:
                self.send(P301, 0, IP['plc301'])

    def send_message(self, ipaddr, port, message):
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.connect((ipaddr, port))

        msg_dict = dict.fromkeys(['Type', 'Variable'])
        msg_dict['Type'] = "Report"
        msg_dict['Variable'] = message
        message = json.dumps(str(msg_dict))

        try:
            ready_to_read, ready_to_write, in_error = select.select([sock, ], [sock, ], [], 5)
        except:
            print "Socket error"
            return
        if(ready_to_write > 0):
            sock.send(message)
        sock.close()

if __name__ == "__main__":

    plc301 = PLC301(name='plc301',state=STATE,protocol=PLC301_PROTOCOL,memory=GENERIC_DATA,disk=GENERIC_DATA)


text:
package com.github.kmkt.util.concurrent;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TaskWorkerRunner<T, R> {
    private static final Logger logger = LoggerFactory.getLogger(TaskWorkerRunner.class);

    /** タスク処理用 ThreadPool */
    protected final ExecutorService pool;
    /** 処理スレッド数の最大 */
    protected int maximimParallel;
    /** タスク処理の実体 TaskWorker のサプライヤ・ファクトリ */
    protected final TaskWorkerSupplier<T, R> workerSupplier;
    /** タスク処理が終了した TaskWorker の通知インタフェース */
    protected final TaskWorkerCollector workerCollector;
    /** CallbackCaller callback しない場合は null */
    protected final CallbackCaller<T, R> callbackCaller;

    /** 総タスク数カウンタ */
    private AtomicInteger tasks = new AtomicInteger(0);
    /** 処理中タスク数カウンタ */
    private AtomicInteger runningTasks = new AtomicInteger(0);

    /**
     * 処理終了時に CallbackCaller に終了タスクを引き渡す FutureTask
     * TaskRunner 専用の内部クラス
     *
     * @see java.util.concurrent.FutureTask
     */
    protected class CallbackFutureTask extends FutureTask<R> {
        private T req;  // TaskRunner が持つ TaskWorker への引数
        public CallbackFutureTask(TaskRunner runner) {
            super(runner);
            if (runner == null)
                throw new IllegalArgumentException("runner should not be null");

            req = runner.getTaskReq();
        }

        public T getTaskReq() {
            return req;
        }

        @Override
        protected void done() {
            // callback 有効時は CallbackCaller にリクエストと自身を登録する
            if (callbackCaller != null)
                callbackCaller.registerFinishedTask(req, this);
        }
    }

    /**
     * TaskWorker を呼び出す Callable wrapper
     */
    protected class TaskRunner implements Callable<R> {
        private T req;  // TaskWorker への引数

        /**
         *
         * @param taskreq TaskWorker への引数
         */
        public TaskRunner(T taskreq) {
            this.req = taskreq;
        }

        /**
         * TaskWorker への引数を取得する
         * @return
         */
        public T getTaskReq() {
            return req;
        }

        @Override
        public R call() throws Exception {
            TaskWorker<T, R> worker = null;
            try {
                runningTasks.incrementAndGet();
                worker = workerSupplier.get();    // TaskWorker 取得

                if (worker == null)
                    throw new TaskWorkerStartException("TaskWorkerSupplier returns null", req);

                R result = worker.doTask(req);
                return result;
            } finally {
                if (workerCollector != null && worker != null)
                    workerCollector.collect(worker);
                runningTasks.decrementAndGet();
                tasks.decrementAndGet();
            }
        }
    }

    /**
     * コールバック処理スレッド
     * @param <T> TaskWorker への引数の型
     * @param <R> TaskWorker からの返り値の型
     */
    private static class CallbackCaller<T, R> implements Runnable {
        /** 処理終了時の callback */
        private TaskCompleteListener<T, R> listener;

        private BlockingQueue<Pair> queue = new LinkedBlockingQueue<>();

        /**
         * タスクとその処理結果を含む Future のペア
         */
        private class Pair {
            T t;
            Future<R> r;
        }

        /**
         *
         * @param listener 処理終了時の callback
         */
        public CallbackCaller(TaskCompleteListener<T, R> listener) {
            if (listener == null)
                throw new IllegalArgumentException("listener should not be null");

            this.listener = listener;
        }

        @Override
        public void run() {
            logger.debug("Start task complete callback thread");
            try {
                while (true) {
                    Pair ele = queue.take();    // block
                    try {
                        listener.onComplete(ele.t, ele.r);  // callback
                    } catch (Exception e) {
                        logger.error("Unexpedted exception in TaskCompleteListener callback", e);
                    }
                    if (Thread.interrupted())
                        break;  // exit loop by interruption
                }
            } catch (InterruptedException e) {
                // exit loop by interruption
            } finally {
                if (Thread.interrupted()) {
                    logger.debug("Exit task complete callback thread by thread interruption");
                } else {
                    logger.debug("Exit task complete callback thread");
                }
            }
        }

        /**
         * 処理終了したタスクの登録
         * @param taskreq 処理終了したタスク
         * @param future taskreq の処理結果を含む Future
         */
        public void registerFinishedTask(T taskreq, Future<R> future) {
            Pair ele = new Pair();
            ele.t = taskreq;
            ele.r = future;
            try {
                queue.put(ele);
            } catch (InterruptedException e) {
                logger.error(null, e);      // not occure
            }
        }
    }

    /**
     * Future のみで処理完了待ちをする TaskWorkerRunner
     *
     * @param maxparallels 処理スレッド数の最大
     * @param supplier タスク処理の実体 TaskWorker のサプライヤorファクトリ
     */
    public TaskWorkerRunner(int maxparallels, TaskWorkerSupplier<T, R> supplier) {
        this(maxparallels, supplier, null, null);
    }

    /**
     * Future のみで処理完了待ちをする TaskWorkerRunner
     *
     * @param maxparallels 処理スレッド数の最大
     * @param supplier タスク処理の実体 TaskWorker のサプライヤorファクトリ
     * @param collector タスク処理が終了した TaskWorker の通知インタフェース
     */
    public TaskWorkerRunner(int maxparallels, TaskWorkerSupplier<T, R> supplier, TaskWorkerCollector collector) {
        this(maxparallels, supplier, collector, null);
    }

    /**
     * Future での処理完了待機と callback での処理完了通知をする TaskWorkerRunner
     *
     * @param maxparallels 処理スレッド数の最大
     * @param supplier タスク処理の実体 TaskWorker のサプライヤorファクトリ
     * @param listener 処理終了時の callback null時は callback しない
     */
    public TaskWorkerRunner(int maxparallels, TaskWorkerSupplier<T, R> supplier, TaskCompleteListener<T, R> listener) {
        this(maxparallels, supplier, null, listener);
    }

    /**
     * Future での処理完了待機と callback での処理完了通知をする TaskWorkerRunner
     *
     * @param maxparallels 処理スレッド数の最大
     * @param supplier タスク処理の実体 TaskWorker のサプライヤorファクトリ
     * @param collector タスク処理が終了した TaskWorker の通知インタフェース
     * @param listener 処理終了時の callback null時は callback しない
     */
    public TaskWorkerRunner(int maxparallels, TaskWorkerSupplier<T, R> supplier, TaskWorkerCollector collector, TaskCompleteListener<T, R> listener) {
        if (maxparallels <= 0)
            throw new IllegalArgumentException("maxparallels should be a positive integer");
        if (supplier == null)
            throw new IllegalArgumentException("supplier should not be null");

        maximimParallel = maxparallels;
        workerSupplier = supplier;
        workerCollector = collector;
        if (listener != null) {
            callbackCaller = new CallbackCaller<T, R>(listener);
        } else {
            callbackCaller = null;
        }

        int threads = (callbackCaller == null) ? maxparallels : maxparallels + 1;   // callback 有効時は callback 用スレッドを確保する
        pool = Executors.newFixedThreadPool(threads, new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
                Thread t = new Thread(r);
                t.setDaemon(true);
                return t;
            }
        });

        // callback 有効時は callback 用スレッドを確保する
        if (callbackCaller != null) {
            pool.execute(callbackCaller);
        }
    }

    /**
     * TaskWorkerRunner に処理対象の taskreq を送信する
     * @param taskreq TaskWorker に処理させる引数 nullable
     * @return タスクの保留完了を表すFuture
     */
    public Future<R> submit(T taskreq) {
        FutureTask<R> taskhost = new CallbackFutureTask(new TaskRunner(taskreq));
        tasks.incrementAndGet();
        pool.execute(taskhost);
        return taskhost;
    }

    /**
     * 未実行のタスク数を取得する
     * @return
     */
    public int getNumOfWaitingTask() {
        return tasks.get() - runningTasks.get();
    }

    /**
     * 処理中のタスク数を取得する
     * @return
     */
    public int getNumOfRunningTask() {
        return runningTasks.get();
    }

    /**
     * 処理中・未実行タスクを合わせた総タスク数を取得する
     * @return
     */
    public int getNumOfTask() {
        return tasks.get();
    }

    /**
     * 内部の ThreadPool をシャットダウンする
     * @see java.util.concurrent.ExecutorService.shutdown()
     */
    public void shutdown() {
        pool.shutdown();
    }

    /**
     * 内部の ThreadPool をシャットダウンする
     * @see java.util.concurrent.ExecutorService.shutdownNow()
     * @return 未処理のタスクのリスト
     */
    @SuppressWarnings("unchecked")
    public List<T> shutdownNow() {
        List<Runnable> remain = pool.shutdownNow();
        List<T> result = new ArrayList<T>(remain.size());
        for (Runnable r : remain) {
            if (!(r instanceof CallbackCaller)) {
                result.add(((CallbackFutureTask) r).getTaskReq());
            }
        }
        return result;
    }

    /**
     * 内部の ThreadPool のシャットダウン完了を待つ
     * @see java.util.concurrent.ExecutorService.awaitTermination(int, TimeUnit)
     *
     * @param timeout 待機する最長時間
     * @param unit timeout引数の時間単位
     * @return シャットダウンした場合は true それ以外は false
     * @throws InterruptedException
     */
    public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
        return pool.awaitTermination(timeout, unit);
    }
}