text:
package org.apache.flink.runtime.state.heap;

import org.apache.flink.runtime.state.InternalPriorityQueue;
import org.apache.flink.util.CloseableIterator;
import org.apache.flink.util.FlinkRuntimeException;

import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import java.util.Collection;
import java.util.function.Consumer;
import java.util.function.Predicate;

import static org.apache.flink.runtime.state.heap.KeyGroupPartitionedPriorityQueue.ENABLE_RELAXED_FIRING_ORDER_OPTIMIZATION;

/**
 * This class is an implementation of a {@link InternalPriorityQueue} with set semantics that internally consists of
 * two different storage types. The first storage is a (potentially slow) ordered set store manages the ground truth
 * about the elements in this queue. The second storage is a (fast) ordered set cache, typically with some limited
 * capacity. The cache is used to improve performance of accesses to the underlying store and contains an ordered
 * (partial) view on the top elements in the ordered store. We are currently applying simple write-through to keep cache
 * and store in sync on updates and refill the cache from the store when it is empty and we expect more elements
 * contained in the store.
 *
 * @param <E> the type if the managed elements.
 */
public class CachingInternalPriorityQueueSet<E> implements InternalPriorityQueue<E>, HeapPriorityQueueElement {

	/** A ordered set cache that contains a (partial) view on the top elements in the ordered store. */
	@Nonnull
	private final OrderedSetCache<E> orderedCache;

	/** A store with ordered set semantics that contains the ground truth of all inserted elements. */
	@Nonnull
	private final OrderedSetStore<E> orderedStore;

	/** This flag is true if there could be elements in the backend that are not in the cache (false positives ok). */
	private boolean storeOnlyElements;

	/** Management data for the {@link HeapPriorityQueueElement} trait. */
	private int pqManagedIndex;

	@SuppressWarnings("unchecked")
	public CachingInternalPriorityQueueSet(
		@Nonnull OrderedSetCache<E> orderedCache,
		@Nonnull OrderedSetStore<E> orderedStore) {
		this.orderedCache = orderedCache;
		this.orderedStore = orderedStore;
		// We are careful and set this to true. Could be set to false if we know for sure that the store is empty, but
		// checking this could be an expensive operation.
		this.storeOnlyElements = true;
		this.pqManagedIndex = HeapPriorityQueueElement.NOT_CONTAINED;
	}

	@Nullable
	@Override
	public E peek() {

		checkRefillCacheFromStore();

		return orderedCache.peekFirst();
	}

	@Override
	public void bulkPoll(@Nonnull Predicate<E> canConsume, @Nonnull Consumer<E> consumer) {
		if (ENABLE_RELAXED_FIRING_ORDER_OPTIMIZATION) {
			bulkPollRelaxedOrder(canConsume, consumer);
		} else {
			bulkPollStrictOrder(canConsume, consumer);
		}
	}

	private void bulkPollRelaxedOrder(@Nonnull Predicate<E> canConsume, @Nonnull Consumer<E> consumer) {
		if (orderedCache.isEmpty()) {
			bulkPollStore(canConsume, consumer);
		} else {
			while (!orderedCache.isEmpty() && canConsume.test(orderedCache.peekFirst())) {
				final E next = orderedCache.removeFirst();
				orderedStore.remove(next);
				consumer.accept(next);
			}

			if (orderedCache.isEmpty()) {
				bulkPollStore(canConsume, consumer);
			}
		}
	}

	private void bulkPollStrictOrder(@Nonnull Predicate<E> canConsume, @Nonnull Consumer<E> consumer) {
		E element;
		while ((element = peek()) != null && canConsume.test(element)) {
			poll();
			consumer.accept(element);
		}
	}

	private void bulkPollStore(@Nonnull Predicate<E> canConsume, @Nonnull Consumer<E> consumer) {
		try (CloseableIterator<E> iterator = orderedStore.orderedIterator()) {
			while (iterator.hasNext()) {
				final E next = iterator.next();
				if (canConsume.test(next)) {
					orderedStore.remove(next);
					consumer.accept(next);
				} else {
					orderedCache.add(next);
					while (iterator.hasNext() && !orderedCache.isFull()) {
						orderedCache.add(iterator.next());
					}
					break;
				}
			}
		} catch (Exception e) {
			throw new FlinkRuntimeException("Exception while bulk polling store.", e);
		}
	}

	@Nullable
	@Override
	public E poll() {

		checkRefillCacheFromStore();

		final E first = orderedCache.removeFirst();

		if (first != null) {
			// write-through sync
			orderedStore.remove(first);
		}

		return first;
	}

	@Override
	public boolean add(@Nonnull E toAdd) {

		checkRefillCacheFromStore();

		// write-through sync
		orderedStore.add(toAdd);

		final boolean cacheFull = orderedCache.isFull();

		if ((!cacheFull && !storeOnlyElements) || orderedCache.isInLowerBound(toAdd)) {

			if (cacheFull) {
				// we drop the element with lowest priority from the cache
				orderedCache.removeLast();
				// the dropped element is now only in the store
				storeOnlyElements = true;
			}

			orderedCache.add(toAdd);
			return toAdd.equals(orderedCache.peekFirst());
		} else {
			// we only added to the store
			storeOnlyElements = true;
			return false;
		}
	}

	@Override
	public boolean remove(@Nonnull E toRemove) {

		checkRefillCacheFromStore();

		boolean newHead = toRemove.equals(orderedCache.peekFirst());
		// write-through sync
		orderedStore.remove(toRemove);
		orderedCache.remove(toRemove);
		return newHead;
	}

	@Override
	public void addAll(@Nullable Collection<? extends E> toAdd) {

		if (toAdd == null) {
			return;
		}

		for (E element : toAdd) {
			add(element);
		}
	}

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

	@Override
	public boolean isEmpty() {
		checkRefillCacheFromStore();
		return orderedCache.isEmpty();
	}

	@Nonnull
	@Override
	public CloseableIterator<E> iterator() {
		if (storeOnlyElements) {
			return orderedStore.orderedIterator();
		} else {
			return orderedCache.orderedIterator();
		}
	}

	@Override
	public int getInternalIndex() {
		return pqManagedIndex;
	}

	@Override
	public void setInternalIndex(int updateIndex) {
		this.pqManagedIndex = updateIndex;
	}

	/**
	 * Refills the cache from the store when the cache is empty and we expect more elements in the store.
	 *
	 * TODO: We can think about exploiting the property that the store is already sorted when bulk-filling the cache.
	 */
	private void checkRefillCacheFromStore() {
		if (storeOnlyElements && orderedCache.isEmpty()) {
			try (final CloseableIterator<E> iterator = orderedStore.orderedIterator()) {
				while (iterator.hasNext() && !orderedCache.isFull()) {
					orderedCache.add(iterator.next());
				}
				storeOnlyElements = iterator.hasNext();
			} catch (Exception e) {
				throw new FlinkRuntimeException("Exception while refilling store from iterator.", e);
			}
		}
	}

	/**
	 * Interface for an ordered cache with set semantics of elements to be used in
	 * {@link CachingInternalPriorityQueueSet}. Cache implementations typically have a limited capacity as indicated
	 * via the {@link #isFull()} method.
	 *
	 * @param <E> the type of contained elements.
	 */
	public interface OrderedSetCache<E> {

		/**
		 * Adds the given element to the cache (if not yet contained). This method should only be called if the cache
		 * is not full.
		 * @param element element to add to the cache.
		 */
		void add(@Nonnull E element);

		/**
		 * Removes the given element from the cache (if contained).
		 * @param element element to remove from the cache.
		 */
		void remove(@Nonnull E element);

		/**
		 * Returns true if the cache is full and no more elements can be added.
		 */
		boolean isFull();

		/**
		 * Returns true if the cache is empty, i.e. contains ne elements.
		 */
		boolean isEmpty();

		/**
		 * Returns true, if the element is compares smaller than the currently largest element in the cache.
		 */
		boolean isInLowerBound(@Nonnull E toCheck);

		/**
		 * Removes and returns the first (smallest) element from the cache.
		 */
		@Nullable
		E removeFirst();

		/**
		 * Removes and returns the last (larges) element from the cache.
		 */
		@Nullable
		E removeLast();

		/**
		 * Returns the first (smallest) element from the cache (without removing it).
		 */
		@Nullable
		E peekFirst();

		/**
		 * Returns the last (larges) element from the cache (without removing it).
		 */
		@Nullable
		E peekLast();

		/**
		 * Returns an iterator over the store that returns element in order. The iterator must be closed by the client
		 * after usage.
		 */
		@Nonnull
		CloseableIterator<E> orderedIterator();
	}

	/**
	 * Interface for an ordered store with set semantics of elements to be used in
	 * {@link CachingInternalPriorityQueueSet}. Stores are assumed to have (practically) unlimited capacity, but their
	 * operations could all be expensive.
	 *
	 * @param <E> the type of contained elements.
	 */
	public interface OrderedSetStore<E> {

		/**
		 * Adds the given element to the store (if not yet contained).
		 * @param element element to add to the store.
		 */
		void add(@Nonnull E element);

		/**
		 * Removed the given element from the cache (if contained).
		 * @param element element to remove from the cache.
		 */
		void remove(@Nonnull E element);

		/**
		 * Returns the number of elements in the store.
		 */
		@Nonnegative
		int size();

		/**
		 * Returns an iterator over the store that returns element in order. The iterator must be closed by the client
		 * after usage.
		 */
		@Nonnull
		CloseableIterator<E> orderedIterator();
	}
}


text: <?php $system_configuration_links_faq_embed_enabled = true;?>

redpajama_set_name: RedPajamaGithub


text:
{{ company.name }}

 0">Owns: {{ owned }}

Owned By: {{ company.owned_by }}

            Login to see your own personalized score

Ethical Scores

          <horizontal-bars data="scores"></horizontal-bars>

Data on {{ company.name }}

{{ category.category.toUpperCase() }}

No Data in this category.  You can help us research!

{{ tag.tag_type }}: {{tag.value}}

                    From: {{ tag.reference.title }}
                    (External Link)
                    (See More)

EXCERPT

"{{ tag.excerpt }}"

ADDED BY

{{ tag.added_by }} at {{ tag.submitted_at }}


text:
<!DOCTYPE html>
<!--
Copyright (c) 2015 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->

<link rel="import" href="/tracing/base/scalar.html">
<link rel="import" href="/tracing/base/unit.html">
<link rel="import" href="/tracing/model/container_memory_dump.html">
<link rel="import" href="/tracing/model/memory_allocator_dump.html">
<link rel="import" href="/tracing/model/memory_dump_test_utils.html">

<script>
'use strict';

tr.b.unittest.testSuite(function() {
  var ContainerMemoryDump = tr.model.ContainerMemoryDump;
  var MemoryAllocatorDump = tr.model.MemoryAllocatorDump;
  var Scalar = tr.b.Scalar;
  var unitlessNumber_smallerIsBetter =
      tr.b.Unit.byName.unitlessNumber_smallerIsBetter;
  var newAllocatorDump = tr.model.MemoryDumpTestUtils.newAllocatorDump;
  var addChildDump = tr.model.MemoryDumpTestUtils.addChildDump;

  test('memoryAllocatorDumps_undefined', function() {
    var md = new ContainerMemoryDump(42);

    assert.isUndefined(md.memoryAllocatorDumps);
    assert.isUndefined(md.getMemoryAllocatorDumpByFullName('malloc'));
  });

  test('memoryAllocatorDumps_zero', function() {
    var md = new ContainerMemoryDump(42);
    md.memoryAllocatorDumps = [];

    assert.lengthOf(md.memoryAllocatorDumps, 0);
    assert.isUndefined(md.getMemoryAllocatorDumpByFullName('malloc'));
  });

  test('memoryAllocatorDumps_flat', function() {
    var md = new ContainerMemoryDump(42);

    var oilpanDump = newAllocatorDump(md, 'oilpan', {
      size: 1024,
      objects_count: new Scalar(unitlessNumber_smallerIsBetter, 7),
      inner_size: 768
    });
    var v8Dump = newAllocatorDump(md, 'v8', {
      size: 2048,
      objects_count: new Scalar(unitlessNumber_smallerIsBetter, 15),
      inner_size: 1999
    });
    md.memoryAllocatorDumps = [oilpanDump, v8Dump];

    assert.lengthOf(md.memoryAllocatorDumps, 2);
    assert.strictEqual(md.memoryAllocatorDumps[0], oilpanDump);
    assert.strictEqual(md.memoryAllocatorDumps[1], v8Dump);

    assert.strictEqual(
        md.getMemoryAllocatorDumpByFullName('oilpan'), oilpanDump);
    assert.strictEqual(md.getMemoryAllocatorDumpByFullName('v8'), v8Dump);
    assert.isUndefined(md.getMemoryAllocatorDumpByFullName('malloc'));
  });

  test('memoryAllocatorDumps_nested', function() {
    var md = new ContainerMemoryDump(42);

    var oilpanDump = newAllocatorDump(md, 'oilpan', {
      size: 1024,
      objects_count: new Scalar(unitlessNumber_smallerIsBetter, 7),
      inner_size: 768
    });

    var oilpanBucket1Dump = addChildDump(oilpanDump, 'bucket1', {
      size: 512,
      objects_count: new Scalar(unitlessNumber_smallerIsBetter, 3),
      inner_size: 256
    });

    var oilpanBucket2Dump = addChildDump(oilpanDump, 'bucket2', {
      size: 512,
      objects_count: new Scalar(unitlessNumber_smallerIsBetter, 4),
      inner_size: 512
    });

    var oilpanBucket2StringsDump = addChildDump(oilpanBucket2Dump, 'strings', {
      size: 512,
      objects_count: new Scalar(unitlessNumber_smallerIsBetter, 4),
      inner_size: 512
    });

    var v8Dump = newAllocatorDump(md, 'v8', {
      size: 2048,
      objects_count: new Scalar(unitlessNumber_smallerIsBetter, 15),
      inner_size: 1999
    });

    md.memoryAllocatorDumps = [oilpanDump, v8Dump];

    assert.lengthOf(md.memoryAllocatorDumps, 2);
    assert.strictEqual(md.memoryAllocatorDumps[0], oilpanDump);
    assert.strictEqual(md.memoryAllocatorDumps[1], v8Dump);

    assert.strictEqual(
        md.getMemoryAllocatorDumpByFullName('oilpan'), oilpanDump);
    assert.strictEqual(md.getMemoryAllocatorDumpByFullName('oilpan/bucket1'),
        oilpanBucket1Dump);
    assert.strictEqual(md.getMemoryAllocatorDumpByFullName('oilpan/bucket2'),
        oilpanBucket2Dump);
    assert.strictEqual(
        md.getMemoryAllocatorDumpByFullName('oilpan/bucket2/strings'),
        oilpanBucket2StringsDump);
    assert.strictEqual(md.getMemoryAllocatorDumpByFullName('v8'), v8Dump);
    assert.isUndefined(md.getMemoryAllocatorDumpByFullName('malloc'));
  });

  test('iterateRootAllocatorDumps', function() {
    var containerDump = new ContainerMemoryDump(42);

    var oilpanDump = new MemoryAllocatorDump(containerDump, 'oilpan');
    var v8Dump = new MemoryAllocatorDump(containerDump, 'v8');
    addChildDump(v8Dump, 'heaps');

    containerDump.memoryAllocatorDumps = [oilpanDump, v8Dump];

    var visitedAllocatorDumps = [];
    containerDump.iterateRootAllocatorDumps(
        function(dump) { this.visitedAllocatorDumps.push(dump); },
        { visitedAllocatorDumps: visitedAllocatorDumps });
    assert.sameMembers(visitedAllocatorDumps, [oilpanDump, v8Dump]);
  });

  test('forceRebuildingMemoryAllocatorDumpByFullNameIndex', function() {
    var containerDump = new ContainerMemoryDump(42);

    var v8Dump = new MemoryAllocatorDump(containerDump, 'v8');
    var v8HeapsDump = addChildDump(v8Dump, 'heaps');
    var v8HeapSmallDump = addChildDump(v8HeapsDump, 'S');

    // Setting the memory allocator dumps should update the index properly.
    containerDump.memoryAllocatorDumps = [v8Dump];
    assert.strictEqual(
        containerDump.getMemoryAllocatorDumpByFullName('v8'), v8Dump);
    assert.strictEqual(containerDump.getMemoryAllocatorDumpByFullName(
        'v8/heaps'), v8HeapsDump);
    assert.strictEqual(containerDump.getMemoryAllocatorDumpByFullName(
        'v8/heaps/S'), v8HeapSmallDump);

    // Add a second grandchild (v8/heaps/L).
    var v8HeapLargeDump = addChildDump(v8HeapsDump, 'L');

    // Setting the memory allocator dumps again should update the index
    // properly again.
    containerDump.memoryAllocatorDumps = [v8Dump];
    assert.strictEqual(
        containerDump.getMemoryAllocatorDumpByFullName('v8'), v8Dump);
    assert.strictEqual(containerDump.getMemoryAllocatorDumpByFullName(
        'v8/heaps'), v8HeapsDump);
    assert.strictEqual(containerDump.getMemoryAllocatorDumpByFullName(
        'v8/heaps/S'), v8HeapSmallDump);
    assert.strictEqual(containerDump.getMemoryAllocatorDumpByFullName(
        'v8/heaps/L'), v8HeapLargeDump);

    // Remove the first grandchild (v8/heaps/S).
    v8HeapsDump.children.splice(0, 1);

    // Force rebuilding the index and check that it was updated properly.
    containerDump.forceRebuildingMemoryAllocatorDumpByFullNameIndex();
    assert.strictEqual(
        containerDump.getMemoryAllocatorDumpByFullName('v8'), v8Dump);
    assert.strictEqual(containerDump.getMemoryAllocatorDumpByFullName(
        'v8/heaps'), v8HeapsDump);
    assert.isUndefined(containerDump.getMemoryAllocatorDumpByFullName(
        'v8/heaps/S'));
    assert.strictEqual(containerDump.getMemoryAllocatorDumpByFullName(
        'v8/heaps/L'), v8HeapLargeDump);
  });
});
</script>


text:
#include "Common.hpp"
#include "Projectile.hpp"
#include "AudioSource.hpp"
#include "Timer.hpp"

namespace HIC
{
	class Weapon
	{
	public:
		//-----------------------------
		// Constructors/Destructors
		//-----------------------------

		// Constructor for Weapon
		Weapon(float fireRate = 0.5f, uint32_t damage = 50U, uint32_t noBarrels = 1U);

		// Destructor for Weapon
		~Weapon();

	public:
		//--------------
		// Setters
		//--------------

		// Sets the fire rate to X bullets per second (1/fireRate)
		void SetFireRate(float bulletsPerSecond);

		// Sets the damage of the weapon to the given amount
		void SetDamage(uint32_t damage);

		// Gives the weapon another barrel
		void AddBarrel();

		//Sets whether or not the weapon should try to fire this frame (will decide to fire based on fire rate)
		void SetFire();

		//---------------
		// Getters
		//---------------

		//Returns the number of bullets fired per second
		float GetFireRate();

		//Returns the damage that a new projectile will deliver
		uint32_t GetDamage();

		//Returns the projectiles for collision
		deque<Projectile*>& GetProjectiles();

		//--------------
		// Other
		//--------------

		// Called to update the weapon
		void Update(float frameTime, const D3DXVECTOR3 playerPosition, const D3DXVECTOR3 playerFacingVector);

		// Removes a projectile from the arena
		void RemoveProjectile(std::deque<Projectile*>::iterator& iter);

		// Called to clear all projectiles currently alive
		void Clear();

		// Operator overload of the output stream operator
		friend std::ostream& operator<<(std::ostream& stream, const Weapon& weapon)
		{
			stream << weapon.mFireRate << std::endl;
			stream << weapon.mDamage << std::endl;
			stream << weapon.mNoBarrels << std::endl;
			return stream;
		}

		// Operator overload of the input stream operator
		friend std::istream& operator>>(std::istream& stream, Weapon& weapon)
		{
			stream >> weapon.mFireRate;
			stream >> weapon.mDamage;
			stream >> weapon.mNoBarrels;

			// Reset the timer
			weapon.mFireTimer.Reset(weapon.mFireRate);

			return stream;
		}
	public:
		static AudioSource* SHOOT_SOUND;
	private:
		// Shoots a projectile in a direction
		void Shoot(const D3DXVECTOR3& direction, const D3DXVECTOR3 playerFacingVector);
	private:
		float mFireRate;						// Length of time between bullet fires
		float mMaxFireRate;						// The maximum fire rate with the current number of barrels
		uint32_t mDamage;						// Amount of damage a bullet deals upon contact
		uint32_t mNoBarrels;					// Number of barrels the ship fires at once
		deque <Projectile*> mProjectiles;		// Vector of projectiles that this weapon has fired
		vector <Projectile*> mProjectilePool;	// Vector of projectiles that the weapon can fire
		Timer mFireTimer;						// The timer used to determine when the next projectile can be shot
		bool mTryFire;							//Whether or not the weapon should try to fire a round this frame
	};
}


text:
int start_master(void)
{
	int iterator;
	worker_list *new;

	// Initialize 0mq and workers list
	zeromq_context = zmq_ctx_new();
	workers = NULL;

	for(iterator = 0; iterator < global_config.worker_threads; iterator++)
	{
		new = calloc(1, sizeof(*new));
		snprintf(new->address, 19, "inproc://worker%d", iterator);
		new->socket = zmq_socket(zeromq_context, ZMQ_PAIR);
		zmq_bind(new->socket, new->address);
		// Add to circular list
		if(!workers)
		{
			new->next = new;
			workers = new;
		}
		else
		{
			new->next = workers->next;
			workers->next = new;
			workers = new;
		}
	}

	return 1;
}

void wait_for_workers(void)
{
	master_event online_notice;
	worker_list *first, *i;

	first = workers;
	i = first;
	do
	{
		zmq_recv(i->socket, &online_notice, sizeof(online_notice), 0);
		i = i->next;
	}
	while(i != first);
}

void signal_worker_shutdown(void)
{
	worker_event shutdown;
	worker_list *first, *i, *temp;

	shutdown.type = WORKER_SHUTDOWN;

	first = workers;
	i = first;
	do
	{
		zmq_send(i->socket, &shutdown, sizeof(shutdown), 0);
		uv_thread_join(&(i->thread));
		temp = i->next;
		free(i);
		i = temp;
	}
	while(i != first);
}

void shutdown_master(void)
{
	zmq_ctx_destroy(zeromq_context);
}


text:
<html>
<body>
This inspection reports properties in .fxml files (both attributes and tags) having default values and therefore redundant.
<!-- tooltip end -->
</body>
</html>


text:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">

<beans default-autowire="no">
    <bean id="validatorFactory" class="org.springmodules.commons.validator.DefaultValidatorFactory">
        <property name="validationConfigLocations">
            <list>
                <value>/WEB-INF/validation.xml</value>
                <value>/WEB-INF/validator-rules.xml</value>
            </list>
        </property>
    </bean>

    <bean id="beanValidator" class="org.springmodules.commons.validator.DefaultBeanValidator">
        <property name="validatorFactory" ref="validatorFactory"/>
    </bean>

</beans>


text:
import {MatCardModule} from '@angular/material/card';
import {runHarnessTests} from '@angular/material/card/testing/shared.spec';
import {MatCardHarness, MatCardSection} from './card-harness';

describe('Non-MDC-based MatCardHarness', () => {
  runHarnessTests(MatCardModule, MatCardHarness, {
    header: MatCardSection.HEADER,
    content: MatCardSection.CONTENT,
    actions: MatCardSection.ACTIONS,
    footer: MatCardSection.FOOTER
  });
});


text:
package com.intellij.testFramework.codeInsight.hierarchy;

import com.intellij.codeInsight.CodeInsightTestCase;
import com.intellij.ide.hierarchy.HierarchyNodeDescriptor;
import com.intellij.ide.hierarchy.HierarchyTreeStructure;
import com.intellij.openapi.util.Computable;
import com.intellij.openapi.util.JDOMUtil;
import org.jdom.Document;
import org.jdom.Element;

import java.io.File;
import java.util.*;

/**
 * Checks tree structure for Type Hierarchy (Ctrl+H), Call Hierarchy (Ctrl+Alt+H), Method Hierarchy (Ctrl+Shift+H).
 */
public abstract class HierarchyViewTestBase extends CodeInsightTestCase {

  private static final String NODE_ELEMENT_NAME = "node";
  private static final String ANY_NODES_ELEMENT_NAME = "any";
  private static final String TEXT_ATTR_NAME = "text";
  private static final String BASE_ATTR_NAME = "base";

  protected abstract String getBasePath();

  protected void doHierarchyTest(final Computable<HierarchyTreeStructure> treeStructureComputable, final String... fileNames)
    throws Exception {
    final String[] relFilePaths = new String[fileNames.length];
    for (int i = 0; i < fileNames.length; i++) {
      relFilePaths[i] = "/" + getBasePath() + "/" + fileNames[i];
    }
    configureByFiles(null, relFilePaths);

    final String verificationFilePath = getTestDataPath() + "/" + getBasePath() + "/" + getTestName(false) + "_verification.xml";
    checkHierarchyTreeStructure(treeStructureComputable.compute(), JDOMUtil.loadDocument(new File(verificationFilePath)));
  }

  private static void checkHierarchyTreeStructure(final HierarchyTreeStructure treeStructure, final Document document) {
    final HierarchyNodeDescriptor rootNodeDescriptor = (HierarchyNodeDescriptor)treeStructure.getRootElement();
    rootNodeDescriptor.update();
    final Element rootElement = document.getRootElement();
    if (rootElement == null || !NODE_ELEMENT_NAME.equals(rootElement.getName())) {
      throw new IllegalArgumentException("Incorrect root element in verification resource");
    }
    checkNodeDescriptorRecursively(treeStructure, rootNodeDescriptor, rootElement);
  }

  private static void checkNodeDescriptorRecursively(final HierarchyTreeStructure treeStructure,
                                                     final HierarchyNodeDescriptor descriptor,
                                                     final Element expectedElement) {
    checkBaseNode(treeStructure, descriptor, expectedElement);
    checkContent(descriptor, expectedElement);
    checkChildren(treeStructure, descriptor, expectedElement);
  }

  private static void checkBaseNode(final HierarchyTreeStructure treeStructure,
                                    final HierarchyNodeDescriptor descriptor,
                                    final Element expectedElement) {
    final String baseAttrValue = expectedElement.getAttributeValue(BASE_ATTR_NAME);
    final HierarchyNodeDescriptor baseDescriptor = treeStructure.getBaseDescriptor();
    final boolean mustBeBase = "true".equalsIgnoreCase(baseAttrValue);
    assertTrue("Incorrect base node", mustBeBase ? baseDescriptor == descriptor : baseDescriptor != descriptor);
  }

  private static void checkContent(final HierarchyNodeDescriptor descriptor, final Element expectedElement) {
    assertEquals(expectedElement.getAttributeValue(TEXT_ATTR_NAME), descriptor.getHighlightedText().getText());
  }

  private static void checkChildren(final HierarchyTreeStructure treeStructure,
                                    final HierarchyNodeDescriptor descriptor,
                                    final Element element) {
    if (element.getChild(ANY_NODES_ELEMENT_NAME) != null) {
      return;
    }

    final Object[] children = treeStructure.getChildElements(descriptor);
    //noinspection unchecked
    final List<Element> expectedChildren = new ArrayList<Element>(element.getChildren(NODE_ELEMENT_NAME));

    final StringBuilder messageBuilder = new StringBuilder("Actual children of [" + descriptor.getHighlightedText().getText() + "]:\n");
    for (Object child : children) {
      final HierarchyNodeDescriptor nodeDescriptor = (HierarchyNodeDescriptor)child;
      nodeDescriptor.update();
      messageBuilder.append("    [").append(nodeDescriptor.getHighlightedText().getText()).append("]\n");
    }
    assertEquals(messageBuilder.toString(), expectedChildren.size(), children.length);

    Arrays.sort(children, new Comparator<Object>() {
      @Override
      public int compare(final Object first, final Object second) {
        return ((HierarchyNodeDescriptor)first).getHighlightedText().getText()
          .compareTo(((HierarchyNodeDescriptor)second).getHighlightedText().getText());
      }
    });

    Collections.sort(expectedChildren, new Comparator<Element>() {
      @Override
      public int compare(final Element first, final Element second) {
        return first.getAttributeValue(TEXT_ATTR_NAME).compareTo(second.getAttributeValue(TEXT_ATTR_NAME));
      }
    });

    //noinspection unchecked
    final Iterator<Element> iterator = expectedChildren.iterator();
    for (Object child : children) {
      checkNodeDescriptorRecursively(treeStructure, ((HierarchyNodeDescriptor)child), iterator.next());
    }
  }

}


text:
/**
 *  * Craigslist example
 *
 * It predicts job category based on job description (called "job title").
 *
 * Launch following commands:
 *    export MASTER="local-cluster[3,2,4096]"
 *   bin/sparkling-shell -i examples/scripts/craigslistJobTitles.script.scala
 *
 * When running using spark shell or using scala rest API:
 *    SQLContext is available as sqlContext
 *    SparkContext is available as sc
 */
import org.apache.spark.mllib.feature.Word2Vec
import org.apache.spark.mllib.feature.Word2VecModel
import org.apache.spark.mllib.linalg._
import org.apache.spark.sql.DataFrame

def isHeader(line: String) = line.contains("category")
// Load and split data based on ","
val data = sc.textFile("examples/smalldata/craigslistJobTitles.csv").filter(x => !isHeader(x)).map(d => d.split(','))

// Extract job category from job description
val jobCategories = data.map(l => l(0))
val jobTitles = data.map(l => l(1))
// Count of different job categories
val labelCounts = jobCategories.map(n => (n, 1)).reduceByKey(_+_).collect.mkString("\n")

/* Should return:
(education,2438)
(administrative,2500)
(labor,2500)
(accounting,1593)
(customerservice,2319)
(foodbeverage,2495)
*/

// All strings which are not useful for text-mining
val stopwords = Set("ax","i","you","edu","s","t","m","subject","can","lines","re","what"
    ,"there","all","we","one","the","a","an","of","or","in","for","by","on"
    ,"but", "is", "in","a","not","with", "as", "was", "if","they", "are", "this", "and", "it", "have"
    , "from", "at", "my","be","by","not", "that", "to","from","com","org","like","likes","so")

// Compute rare words
val rareWords = jobTitles.flatMap(t => t.split("""\W+""").map(_.toLowerCase)).filter(word => """[^0-9]*""".r.pattern.matcher(word).matches).
  map(w => (w, 1)).reduceByKey(_+_).
  filter { case (k, v) => v < 2 }.map { case (k, v) => k }.
  collect.
  toSet

// Define tokenizer function
def token(line: String): Seq[String] = {
    //get rid of nonWords such as puncutation as opposed to splitting by just " "
    line.split("""\W+""")
    .map(_.toLowerCase)

    //remove mix of words+numbers
    .filter(word => """[^0-9]*""".r.pattern.matcher(word).matches)

    //remove stopwords defined above (you can add to this list if you want)
    .filterNot(word => stopwords.contains(word))

    //leave only words greater than 1 characters.
    //this deletes A LOT of words but useful to reduce our feature-set
    .filter(word => word.size >= 2)

    //remove rare occurences of words
    .filterNot(word => rareWords.contains(word))
}

val XXXwords = data.map(d => (d(0), token(d(1)).toSeq)).filter(s => s._2.length > 0)
val words = XXXwords.map(v => v._2)
val XXXlabels = XXXwords.map(v => v._1)

// Sanity Check
println(jobTitles.flatMap(lines => token(lines)).distinct.count)

// Make some helper functions
def sumArray (m: Array[Double], n: Array[Double]): Array[Double] = {
  for (i <- 0 until m.length) {m(i) += n(i)}
  return m
}

def divArray (m: Array[Double], divisor: Double) : Array[Double] = {
  for (i <- 0 until m.length) {m(i) /= divisor}
  return m
}

def wordToVector (w:String, m: Word2VecModel): Vector = {
  try {
    return m.transform(w)
  } catch {
    case e: Exception => return Vectors.zeros(100)
  }
}

//
// Word2Vec Model
//

val word2vec = new Word2Vec()
val model = word2vec.fit(words)

// Sanity Check
model.findSynonyms("teacher", 5).foreach(println)

val title_vectors = words.map(x => new DenseVector(
    divArray(x.map(m => wordToVector(m, model).toArray).
            reduceLeft(sumArray),x.length)).asInstanceOf[Vector])

//val title_pairs = words.map(x => (x,new DenseVector(
//    divArray(x.map(m => wordToVector(m, model).toArray).
//            reduceLeft(sumArray),x.length)).asInstanceOf[Vector]))

// Create H2OFrame
import org.apache.spark.mllib
case class CRAIGSLIST(target: String, a: mllib.linalg.Vector)

import org.apache.spark.h2o._
val h2oContext = new H2OContext(sc).start()
import h2oContext._

val resultRDD: DataFrame = XXXlabels.zip(title_vectors).map(v => CRAIGSLIST(v._1, v._2)).toDF

val table:H2OFrame = resultRDD

// OPEN FLOW UI
openFlow


text:
package com.google.common.collect.testing.google;

import com.google.common.annotations.GwtCompatible;
import com.google.common.collect.SetMultimap;

/**
 * A generator for {@code SetMultimap} implementations based on test data.
 *
 * @author Louis Wasserman
 */
@GwtCompatible
public interface TestSetMultimapGenerator<K, V>
    extends TestMultimapGenerator<K, V, SetMultimap<K, V>> {

}


text:
module SessionMethods

  protected

    # Member sessions

    def logged_in?
      unless current_user
        return false
      else
        return true
      end
    end

    def current_user
      unless @current_user == false
        @current_user ||= (login_from_session || login_from_cookie)
        return @current_user
      end
    end

    def current_user=(new_user)
      session[:id] = new_user ? new_user.id : nil
      @current_user = new_user || false
    end

    def login_from_session
      user = User.find_by_id(session[:id]) if session[:id]
      if user
        if user.enabled?
          self.current_user = user
          return current_user
        else
          # they may have been disabled mid-session
          logout_keeping_session!
        end
      end
    end

    def login_from_cookie
      user = User.find_by_remember_token(cookies[:auth_token]) if cookies[:auth_token]
      if user && user.remember_token?
        if user.enabled?
          self.current_user = user
          handle_remember_cookie!(user, false) # freshen cookie token (keeping date)
          return current_user
        else
          # they may have been disabled mid-session
          logout_keeping_session!
        end
      end
    rescue
      logout_killing_session!
    end

    # The session should only be reset at the tail end of a form POST --
    # otherwise the request forgery protection fails. It's only really necessary
    # when you cross quarantine (logged-out to logged-in).
    def logout_killing_session!
      logout_keeping_session!
      reset_session
    end

    def logout_keeping_session!
      @current_user = false
      kill_remember_cookie!     # Kill client-side auth cookie
      session[:id] = nil
      session[:student] = nil
      session[:samples_today] = nil
      session[:samples_updated_at] = nil
      session[:last_search] = nil
      session[:unplaced_order] = nil
      # explicitly kill any other session variables you set
    end

    # Admin Sessions

    def admin_logged_in?
      unless current_admin
        return false
      else
        return true
      end
    end

    def current_admin
      unless @current_admin == false
        @current_admin ||= (login_admin_from_session || login_admin_from_cookie)
      end
    end

    def current_admin=(admin)
      session[:admin_id] = admin ? admin.id : nil
      @current_admin = admin || false
    end

    def login_admin_from_session
      return unless session[:admin_id]
      admin = Admin.find_by_id(session[:admin_id])
      if admin
        if admin.enabled?
          self.current_admin = admin
          return current_admin
        else
          # they may have been disabled mid-session
          logout_admin_keeping_session!
        end
      end
    end

    def login_admin_from_cookie
      admin = Admin.find_by_remember_token(cookies[:auth_token]) if cookies[:auth_token]
      if admin && admin.remember_token?
        if admin.enabled?
          self.current_admin = admin
          handle_remember_cookie!(admin, false) # freshen cookie token (keeping date)
          return current_admin
        else
          # they may have been disabled mid-session
          logout_admin_keeping_session!
        end
      end
    end

    def logout_admin_killing_session!
      logout_admin_keeping_session!
      reset_session
    end

    def logout_admin_keeping_session!
      @current_admin.forget_me if @current_admin  # Kill server-side auth cookie
      @current_admin = false
      kill_remember_cookie!     # Kill client-side auth cookie
      session[:admin_id] = nil
    end

    # Remember cookie methods

    def kill_remember_cookie!
      cookies.delete :auth_token
    end

    def valid_remember_cookie?(user)
      return nil unless user
      (user.remember_token?) && (cookies[:auth_token] == user.remember_token)
    end

    def send_remember_cookie!(user)
      cookies[:auth_token] = {
        :value => user.remember_token,
        :expires => user.remember_token_expires_at
      }
    end

    # Refresh the cookie auth token if it exists, create it otherwise
    def handle_remember_cookie!(user, new_cookie_flag)
      return unless user
      case
      when valid_remember_cookie?(user) then user.refresh_token # keeping same expiry date
      when new_cookie_flag              then user.remember_me
      else                                   user.forget_me
      end
      send_remember_cookie!(user)
    end

end


text:
from lightning import Lightning
from numpy import random

lgn = Lightning()

x = random.randn(10)
y = random.randn(10)
mat = random.rand(10,10)
mat[mat>0.75] = 0
group = (random.rand(10) * 5).astype('int')

lgn.graph(x, y, mat, group=group)


text:
from twilio.rest.resources import (
    NextGenInstanceResource,
    NextGenListResource,
    transform_params,
)

class PhoneNumber(NextGenInstanceResource):
    """
    Represents information about a phone number.

    .. attribute:: phone_number

        The phone number in normalized E.164 format, e.g. "+14158675309"

    .. attribute:: national_format

        The phone number in localized format, e.g. "(415) 867-5309"

    .. attribute:: country_code

        The ISO 3166-1 two-letter code for this phone number's country, e.g.
        "US" for United States

    .. attribute:: carrier

        A dictionary of information about the carrier responsible for this
        number, if requested.

        Contains the following:
            - mobile_country_code: the country code of the mobile carrier.
            Only populated if the number is a mobile number.
            - mobile_network_code: the network code of the mobile carrier.
            Only populated if the number is a mobile number.
            - name: the name of the carrier.
            - type: the type of the number ("mobile", "landline", or "voip")
            - error_code: the error code of the carrier info request, if one
            occurred
    """
    id_key = "phone_number"

class PhoneNumbers(NextGenListResource):
    name = "PhoneNumbers"
    instance = PhoneNumber

    def get(self, number, include_carrier_info=False, country_code=None):
        """Look up a phone number.

        :param str number: The phone number to query.
        :param bool include_carrier_info: Whether to do a carrier lookup on
            the phone number. See twilio.com for the latest pricing.
        :param str country_code: If the number is provided in a local format
        rather than E.164, specify the two-letter code of the country to parse
        the number for.
        """

        params = {}
        if country_code is not None:
            params['country_code'] = country_code

        if include_carrier_info:
            params['type'] = 'carrier'

        params = transform_params(params)
        uri = "%s/%s" % (self.uri, number)
        _, item = self.request("GET", uri, params=params)

        return self.load_instance(item)


text:
Localization.NET
===============

A grammatically correct, flexible alternative to String.Format and .NET resource files.
Localize your MVC.NET solutions without touching code.

Please refer to [this blog post](http://kuhnel.wordpress.com/2012/03/11/an-101-on-the-localization-framework-in-umbraco-5/) and Localization.NET/Parsing/Default parser syntax.txt for syntax/grammar.


text:
<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name = "todo.model.Item" table = "item">

        <id name = "id" type = "int" column = "id">
            <generator class="identity"/>
        </id>

        <property name = "description" column = "description" type = "string"/>
        <property name = "created" column = "created" type = "timestamp"/>
        <property name = "done" column = "done" type = "boolean"/>

    </class>
</hibernate-mapping>


text:
import { Arr, Obj, Optional } from '@ephox/katamari';
import { Attribute, Compare, Css, CursorPosition, Insert, Replication, SelectorFilter, SugarElement, SugarNode } from '@ephox/sugar';

import { CellElement } from '../util/TableTypes';
import { CellData, Generators, SimpleGenerators } from './Generators';

const transferableAttributes: Record<string, string[]> = {
  scope: [
    'row',
    'col'
  ]
};

// NOTE: This may create a td instead of a th, but it is for irregular table handling.
const createCell = (doc: SugarElement<Document>) => () => {
  const td = SugarElement.fromTag('td', doc.dom);
  Insert.append(td, SugarElement.fromTag('br', doc.dom));
  return td;
};

const createCol = (doc: SugarElement<Document>) => () => {
  return SugarElement.fromTag('col', doc.dom);
};

const createColgroup = (doc: SugarElement<Document>) => () => {
  return SugarElement.fromTag('colgroup', doc.dom);
};

const createRow = (doc: SugarElement<Document>) => () => {
  return SugarElement.fromTag('tr', doc.dom);
};

const replace = (cell: SugarElement<HTMLTableCellElement>, tag: 'td' | 'th', attrs: Record<string, string | number | boolean | null>) => {
  const replica = Replication.copy(cell, tag);
  // TODO: Snooker passes null to indicate 'remove attribute'
  Obj.each(attrs, (v, k) => {
    if (v === null) {
      Attribute.remove(replica, k);
    } else {
      Attribute.set(replica, k, v);
    }
  });
  return replica;
};

// eslint-disable-next-line @tinymce/prefer-fun
const pasteReplace = (cell: SugarElement<HTMLTableCellElement>) => {
  // TODO: check for empty content and don't return anything
  return cell;
};

const cloneFormats = (oldCell: SugarElement<Element>, newCell: SugarElement<Element>, formats: string[]) => {
  const first = CursorPosition.first(oldCell);
  return first.map((firstText) => {
    const formatSelector = formats.join(',');
    // Find the ancestors of the first text node that match the given formats.
    const parents = SelectorFilter.ancestors(firstText, formatSelector, (element) => {
      return Compare.eq(element, oldCell);
    });
    // Add the matched ancestors to the new cell, then return the new cell.
    return Arr.foldr(parents, (last, parent) => {
      const clonedFormat = Replication.shallow(parent);
      Attribute.remove(clonedFormat, 'contenteditable');
      Insert.append(last, clonedFormat);
      return clonedFormat;
    }, newCell);
  }).getOr(newCell);
};

const cloneAppropriateAttributes = <T extends HTMLElement>(original: SugarElement<T>, clone: SugarElement<T>): void => {
  Obj.each(transferableAttributes, (validAttributes, attributeName) =>
    Attribute.getOpt(original, attributeName)
      .filter((attribute) => Arr.contains(validAttributes, attribute))
      .each((attribute) => Attribute.set(clone, attributeName, attribute))
  );
};

const cellOperations = (mutate: (e1: SugarElement<CellElement>, e2: SugarElement<CellElement>) => void, doc: SugarElement<Document>, formatsToClone: Optional<string[]>): Generators => {
  const cloneCss = <T extends HTMLElement> (prev: CellData, clone: SugarElement<T>) => {
    // inherit the style and width, dont inherit the row height
    Css.copy(prev.element, clone);
    Css.remove(clone, 'height');
    // dont inherit the width of spanning columns
    if (prev.colspan !== 1) {
      Css.remove(clone, 'width');
    }
  };

  const newCell = (prev: CellData) => {
    const td = SugarElement.fromTag(SugarNode.name(prev.element) as 'td' | 'th', doc.dom);

    const formats = formatsToClone.getOr([ 'strong', 'em', 'b', 'i', 'span', 'font', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p', 'div' ]);

    // If we aren't cloning the child formatting, we can just give back the new td immediately.
    const lastNode = formats.length > 0 ? cloneFormats(prev.element, td, formats) : td;
    Insert.append(lastNode, SugarElement.fromTag('br'));

    cloneCss(prev, td);
    cloneAppropriateAttributes(prev.element, td);
    mutate(prev.element, td);
    return td;
  };

  const newCol = (prev: CellData) => {
    const col = SugarElement.fromTag(SugarNode.name(prev.element) as 'col', doc.dom);
    cloneCss(prev, col);
    mutate(prev.element, col);
    return col;
  };

  return {
    col: newCol,
    colgroup: createColgroup(doc),
    row: createRow(doc),
    cell: newCell,
    replace,
    colGap: createCol(doc),
    gap: createCell(doc)
  };
};

const paste = (doc: SugarElement<Document>): SimpleGenerators => {
  return {
    col: createCol(doc),
    colgroup: createColgroup(doc),
    row: createRow(doc),
    cell: createCell(doc),
    replace: pasteReplace,
    colGap: createCol(doc),
    gap: createCell(doc)
  };
};

export {
  cellOperations,
  paste
};


text:
<?php if(!empty($records)) { ?>
<form role="form" name="myForm" class="form-horizontal" enctype="multipart/form-data" action="" method="post" autocomplete="off" >
<input type="hidden" name="<?php echo $this->security->get_csrf_token_name(); ?>" value="<?php echo $this->security->get_csrf_hash(); ?>">

     Brand

            <input type="text" class="form-control"  name="brand_name" id="brand_name" value="<?php echo $records[0]["brand_name"]; ?>" >

       <?php echo form_error("brand_name"); ?>

   <input type="hidden" name="brand_id" value="<?php echo $records[0]['brand_id']; ?>" >

           <a href="<?php echo base_url();?>brand/show" class="btn btn-success">Go Back
           <button type="submit" class="btn btn-default">Submit</button>

</form><?php } else {echo "No Records Found" ;}?>


text:
// Copyright (c) Microsoft.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.ErrorReporting;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.Diagnostics.EngineV2
{
    internal partial class DiagnosticIncrementalAnalyzer
    {
        /// <summary>
        /// This cache CompilationWithAnalyzer for active/open files.
        /// This will aggressively let go cached compilationWithAnalyzers to not hold them into memory too long.
        /// </summary>
        private class CompilationManager
        {
            private readonly DiagnosticIncrementalAnalyzer _owner;
            private ConditionalWeakTable<Project, CompilationWithAnalyzers> _map;

            public CompilationManager(DiagnosticIncrementalAnalyzer owner)
            {
                _owner = owner;
                _map = new ConditionalWeakTable<Project, CompilationWithAnalyzers>();
            }

            /// <summary>
            /// Return CompilationWithAnalyzer for given project with given stateSets
            /// </summary>
            public async Task<CompilationWithAnalyzers> GetAnalyzerDriverAsync(Project project, IEnumerable<StateSet> stateSets, CancellationToken cancellationToken)
            {
                if (!project.SupportsCompilation)
                {
                    return null;
                }

                CompilationWithAnalyzers analyzerDriverOpt;
                if (_map.TryGetValue(project, out analyzerDriverOpt))
                {
                    // we have cached one, return that.
                    AssertAnalyzers(analyzerDriverOpt, stateSets);
                    return analyzerDriverOpt;
                }

                // Create driver that holds onto compilation and associated analyzers
                var includeSuppressedDiagnostics = true;
                var newAnalyzerDriverOpt = await CreateAnalyzerDriverAsync(project, stateSets, includeSuppressedDiagnostics, cancellationToken).ConfigureAwait(false);

                // Add new analyzer driver to the map
                analyzerDriverOpt = _map.GetValue(project, _ => newAnalyzerDriverOpt);

                // if somebody has beat us, make sure analyzers are good.
                if (analyzerDriverOpt != newAnalyzerDriverOpt)
                {
                    AssertAnalyzers(analyzerDriverOpt, stateSets);
                }

                // return driver
                return analyzerDriverOpt;
            }

            public Task<CompilationWithAnalyzers> CreateAnalyzerDriverAsync(Project project, IEnumerable<StateSet> stateSets, bool includeSuppressedDiagnostics, CancellationToken cancellationToken)
            {
                var analyzers = stateSets.Select(s => s.Analyzer).ToImmutableArrayOrEmpty();
                return CreateAnalyzerDriverAsync(project, analyzers, includeSuppressedDiagnostics, cancellationToken);
            }

            public async Task<CompilationWithAnalyzers> CreateAnalyzerDriverAsync(
                Project project, ImmutableArray<DiagnosticAnalyzer> analyzers, bool includeSuppressedDiagnostics, CancellationToken cancellationToken)
            {
                if (!project.SupportsCompilation)
                {
                    return null;
                }

                var compilation = await project.GetCompilationAsync(cancellationToken).ConfigureAwait(false);

                // Create driver that holds onto compilation and associated analyzers
                return CreateAnalyzerDriver(
                    project, compilation, analyzers, logAnalyzerExecutionTime: false, reportSuppressedDiagnostics: includeSuppressedDiagnostics);
            }

            private CompilationWithAnalyzers CreateAnalyzerDriver(
                Project project,
                Compilation compilation,
                ImmutableArray<DiagnosticAnalyzer> analyzers,
                bool logAnalyzerExecutionTime,
                bool reportSuppressedDiagnostics)
            {
                // PERF: there is no analyzers for this compilation.
                //       compilationWithAnalyzer will throw if it is created with no analyzers which is perf optimization.
                if (analyzers.IsEmpty)
                {
                    return null;
                }

                Contract.ThrowIfFalse(project.SupportsCompilation);
                AssertCompilation(project, compilation);

                var analysisOptions = GetAnalyzerOptions(project, logAnalyzerExecutionTime, reportSuppressedDiagnostics);

                // Create driver that holds onto compilation and associated analyzers
                return compilation.WithAnalyzers(analyzers, analysisOptions);
            }

            private CompilationWithAnalyzersOptions GetAnalyzerOptions(
                Project project,
                bool logAnalyzerExecutionTime,
                bool reportSuppressedDiagnostics)
            {
                // in IDE, we always set concurrentAnalysis == false otherwise, we can get into thread starvation due to
                // async being used with syncronous blocking concurrency.
                return new CompilationWithAnalyzersOptions(
                    options: new WorkspaceAnalyzerOptions(project.AnalyzerOptions, project.Solution.Workspace),
                    onAnalyzerException: GetOnAnalyzerException(project.Id),
                    analyzerExceptionFilter: GetAnalyzerExceptionFilter(project),
                    concurrentAnalysis: false,
                    logAnalyzerExecutionTime: logAnalyzerExecutionTime,
                    reportSuppressedDiagnostics: reportSuppressedDiagnostics);
            }

            private Func<Exception, bool> GetAnalyzerExceptionFilter(Project project)
            {
                return ex =>
                {
                    if (project.Solution.Workspace.Options.GetOption(InternalDiagnosticsOptions.CrashOnAnalyzerException))
                    {
                        // if option is on, crash the host to get crash dump.
                        FatalError.ReportUnlessCanceled(ex);
                    }

                    return true;
                };
            }

            private Action<Exception, DiagnosticAnalyzer, Diagnostic> GetOnAnalyzerException(ProjectId projectId)
            {
                return _owner.Owner.GetOnAnalyzerException(projectId, _owner.DiagnosticLogAggregator);
            }

            private void ResetAnalyzerDriverMap()
            {
                // we basically eagarly clear the cache on some known changes
                // to let CompilationWithAnalyzer go.

                // we create new conditional weak table every time, it turns out
                // only way to clear ConditionalWeakTable is re-creating it.
                // also, conditional weak table has a leak - https://github.com/dotnet/coreclr/issues/665
                _map = new ConditionalWeakTable<Project, CompilationWithAnalyzers>();
            }

            [Conditional("DEBUG")]
            private void AssertAnalyzers(CompilationWithAnalyzers analyzerDriver, IEnumerable<StateSet> stateSets)
            {
                if (analyzerDriver == null)
                {
                    // this can happen if project doesn't support compilation or no stateSets are given.
                    return;
                }

                // make sure analyzers are same.
                Contract.ThrowIfFalse(analyzerDriver.Analyzers.SetEquals(stateSets.Select(s => s.Analyzer)));
            }

            [Conditional("DEBUG")]
            private void AssertCompilation(Project project, Compilation compilation1)
            {
                // given compilation must be from given project.
                Compilation compilation2;
                Contract.ThrowIfFalse(project.TryGetCompilation(out compilation2));
                Contract.ThrowIfFalse(compilation1 == compilation2);
            }

            #region state changed
            public void OnActiveDocumentChanged()
            {
                ResetAnalyzerDriverMap();
            }

            public void OnDocumentOpened()
            {
                ResetAnalyzerDriverMap();
            }

            public void OnDocumentClosed()
            {
                ResetAnalyzerDriverMap();
            }

            public void OnDocumentReset()
            {
                ResetAnalyzerDriverMap();
            }

            public void OnDocumentRemoved()
            {
                ResetAnalyzerDriverMap();
            }

            public void OnProjectRemoved()
            {
                ResetAnalyzerDriverMap();
            }

            public void OnNewSolution()
            {
                ResetAnalyzerDriverMap();
            }
            #endregion
        }
    }
}