text:
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Threading;
using EnvDTE;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using NuGet.Dialog.PackageManagerUI;
using NuGet.Dialog.Providers;
using NuGet.Test;
using NuGet.Test.Mocks;
using NuGet.VisualStudio;

namespace NuGet.Dialog.Test {
    [TestClass]
    public class OnlineProviderTest {
        [TestMethod]
        public void NamePropertyIsCorrect() {
            // Arrange
            var provider = CreateOnlineProvider();

            // Act & Assert
            Assert.AreEqual("Online", provider.Name);
        }

        [TestMethod]
        public void RefresOnNodeSelectionPropertyIsTrue() {
            // Arrange
            var provider = CreateOnlineProvider();

            // Act & Assert
            Assert.IsTrue(provider.RefreshOnNodeSelection);
        }

        [TestMethod]
        public void VerifySortDescriptors() {
            // Arrange
            var provider = CreateOnlineProvider();

            // Act
            var descriptors = provider.SortDescriptors.Cast<PackageSortDescriptor>().ToList();

            // Assert
            Assert.AreEqual(4, descriptors.Count);
            Assert.AreEqual("DownloadCount", descriptors[0].SortProperties.First());
            Assert.AreEqual(ListSortDirection.Descending, descriptors[0].Direction);
            Assert.AreEqual("Rating", descriptors[1].SortProperties.First());
            Assert.AreEqual(ListSortDirection.Descending, descriptors[1].Direction);
            Assert.AreEqual("Title", descriptors[2].SortProperties.First());
            Assert.AreEqual("Id", descriptors[2].SortProperties.Last());
            Assert.AreEqual(ListSortDirection.Ascending, descriptors[2].Direction);
            Assert.AreEqual("Title", descriptors[3].SortProperties.First());
            Assert.AreEqual("Id", descriptors[3].SortProperties.Last());
            Assert.AreEqual(ListSortDirection.Descending, descriptors[3].Direction);
        }

        [TestMethod]
        public void RootNodeIsPopulatedWithCorrectNumberOfNodes() {
            // Arrange
            var provider = CreateOnlineProvider();

            // Act
            var extentionsTree = provider.ExtensionsTree;

            // Assert
            Assert.AreEqual(2, extentionsTree.Nodes.Count);
            Assert.IsInstanceOfType(extentionsTree.Nodes[0], typeof(SimpleTreeNode));
            Assert.AreEqual("One", extentionsTree.Nodes[0].Name);

            Assert.IsInstanceOfType(extentionsTree.Nodes[1], typeof(SimpleTreeNode));
            Assert.AreEqual("Two", extentionsTree.Nodes[1].Name);
        }

        [TestMethod]
        public void CanExecuteReturnsCorrectResult() {
            // Arrange

            var packageA = PackageUtility.CreatePackage("A", "1.0");
            var packageB = PackageUtility.CreatePackage("B", "2.0");
            var packageC = PackageUtility.CreatePackage("C", "3.0");

            var sourceRepository = new MockPackageRepository();
            sourceRepository.AddPackage(packageA);
            sourceRepository.AddPackage(packageC);
            sourceRepository.AddPackage(packageB);

            var localRepository = new MockPackageRepository();
            localRepository.AddPackage(packageA);

            var projectManager = new Mock<IProjectManager>();
            projectManager.Setup(p => p.IsInstalled(It.IsAny<IPackage>())).Returns<IPackage>(p => localRepository.Exists(p));

            var packageManager = new Mock<IVsPackageManager>();
            packageManager.Setup(p => p.SourceRepository).Returns(sourceRepository);

            var provider = CreateOnlineProvider(packageManager.Object, projectManager.Object);

            var extensionA = new PackageItem(provider, packageA, null);
            var extensionB = new PackageItem(provider, packageB, null);
            var extensionC = new PackageItem(provider, packageC, null);

            // Act
            bool canExecuteA = provider.CanExecute(extensionA);
            bool canExecuteB = provider.CanExecute(extensionB);
            bool canExecuteC = provider.CanExecute(extensionC);

            // Assert
            Assert.IsTrue(canExecuteC);
            Assert.IsTrue(canExecuteB);
            Assert.IsFalse(canExecuteA);
        }

        [TestMethod]
        public void ExecuteMethodCallsInstallPackageMethodOnPackageManager() {
            // Arrange
            var packageA = PackageUtility.CreatePackage("A", "1.0");
            var packageB = PackageUtility.CreatePackage("B", "2.0");
            var packageC = PackageUtility.CreatePackage("C", "3.0");

            var sourceRepository = new MockPackageRepository();
            sourceRepository.AddPackage(packageA);
            sourceRepository.AddPackage(packageC);
            sourceRepository.AddPackage(packageB);

            var localRepository = new MockPackageRepository();
            localRepository.AddPackage(packageA);

            var projectManager = new Mock<IProjectManager>();
            projectManager.Setup(p => p.LocalRepository).Returns(localRepository);

            var packageManager = new Mock<IVsPackageManager>();
            packageManager.Setup(p => p.SourceRepository).Returns(sourceRepository);

            var provider = CreateOnlineProvider(packageManager.Object, projectManager.Object);
            var extensionTree = provider.ExtensionsTree;

            var firstTreeNode = (SimpleTreeNode)extensionTree.Nodes[0];
            firstTreeNode.Repository.AddPackage(packageA);
            firstTreeNode.Repository.AddPackage(packageB);
            firstTreeNode.Repository.AddPackage(packageC);

            provider.SelectedNode = firstTreeNode;
            IVsPackageManager activePackageManager = provider.GetActivePackageManager();
            Mock<IVsPackageManager> mockPackageManager = Mock.Get<IVsPackageManager>(activePackageManager);

            ManualResetEvent manualEvent = new ManualResetEvent(false);

            provider.ExecuteCompletedCallback = delegate {
                // Assert
                mockPackageManager.Verify(p => p.InstallPackage(projectManager.Object, packageB, It.IsAny<IEnumerable<PackageOperation>>(), false, provider), Times.Once());

                manualEvent.Set();
            };

            var extensionB = new PackageItem(provider, packageB, null);

            // Act
            provider.Execute(extensionB);

            // do not allow the method to return
            manualEvent.WaitOne();
        }

        [TestMethod]
        public void ExecuteMethodInvokesScript() {
            // source repo has A, B, C
            // solution repo has A
            // project repo has C

            // install B

            // Arrange
            var packageA = PackageUtility.CreatePackage("A", "1.0");
            var packageB = PackageUtility.CreatePackage("B", "2.0", content: new string[] { "hello world" }, tools: new string[] { "install.ps1", "uninstall.ps1" });
            var packageC = PackageUtility.CreatePackage("C", "3.0");

            var solutionRepository = new MockPackageRepository();
            solutionRepository.AddPackage(packageA);

            var sourceRepository = new MockPackageRepository();
            sourceRepository.AddPackage(packageA);
            sourceRepository.AddPackage(packageB);
            sourceRepository.AddPackage(packageC);

            var localRepository = new MockPackageRepository();
            localRepository.Add(packageC);

            var projectManager = CreateProjectManager(localRepository, solutionRepository);

            var project = new Mock<Project>();
            var scriptExecutor = new Mock<IScriptExecutor>();

            var packageManager = new Mock<IVsPackageManager>();
            packageManager.Setup(p => p.SourceRepository).Returns(sourceRepository);
            packageManager.Setup(p => p.LocalRepository).Returns(solutionRepository);

            packageManager.Setup(p => p.InstallPackage(
               projectManager, It.IsAny<IPackage>(), It.IsAny<IEnumerable<PackageOperation>>(), false, It.IsAny<ILogger>())).Callback(
               () => {
                   solutionRepository.AddPackage(packageB);
                   projectManager.AddPackageReference(packageB.Id, packageB.Version);
               });

            var provider = CreateOnlineProvider(packageManager.Object, projectManager, null, null, project.Object, scriptExecutor.Object);
            var extensionTree = provider.ExtensionsTree;

            var firstTreeNode = (SimpleTreeNode)extensionTree.Nodes[0];
            firstTreeNode.Repository.AddPackage(packageA);
            firstTreeNode.Repository.AddPackage(packageB);
            firstTreeNode.Repository.AddPackage(packageC);

            provider.SelectedNode = firstTreeNode;

            ManualResetEvent manualEvent = new ManualResetEvent(false);

            provider.ExecuteCompletedCallback = delegate {
                try {
                    // Assert
                    scriptExecutor.Verify(p => p.Execute(It.IsAny<string>(), PowerShellScripts.Install, packageB, project.Object, It.IsAny<ILogger>()), Times.Once());
                }
                finally {
                    manualEvent.Set();
                }
            };

            var extensionB = new PackageItem(provider, packageB, null);

            // Act
            provider.Execute(extensionB);

            // do not allow the method to return
            manualEvent.WaitOne();
        }

        [TestMethod]
        public void ExecuteMethodInstallPackagesWithInitScript() {
            // source repo has A, B, C
            // solution repo has A
            // project repo has C

            // install B

            // Arrange
            var packageA = PackageUtility.CreatePackage("A", "1.0");
            var packageB = PackageUtility.CreatePackage("B", "2.0", content: new string[] { "hello world" }, tools: new string[] { "init.ps1" });
            var packageC = PackageUtility.CreatePackage("C", "3.0");

            var solutionRepository = new MockPackageRepository();
            solutionRepository.AddPackage(packageA);

            var sourceRepository = new MockPackageRepository();
            sourceRepository.AddPackage(packageA);
            sourceRepository.AddPackage(packageB);
            sourceRepository.AddPackage(packageC);

            var localRepository = new MockPackageRepository();
            localRepository.Add(packageC);

            var projectManager = CreateProjectManager(localRepository, solutionRepository);

            var project = new Mock<Project>();
            var scriptExecutor = new Mock<IScriptExecutor>();

            var packageManager = new Mock<IVsPackageManager>();
            packageManager.Setup(p => p.SourceRepository).Returns(sourceRepository);
            packageManager.Setup(p => p.LocalRepository).Returns(solutionRepository);
            packageManager.Setup(p => p.InstallPackage(projectManager, packageB, It.IsAny<IEnumerable<PackageOperation>>(), false, It.IsAny<ILogger>())).
                Raises(p => p.PackageInstalled += null, packageManager, new PackageOperationEventArgs(packageB, ""));

            var provider = CreateOnlineProvider(packageManager.Object, projectManager, null, null, project.Object, scriptExecutor.Object);
            var extensionTree = provider.ExtensionsTree;

            var firstTreeNode = (SimpleTreeNode)extensionTree.Nodes[0];
            firstTreeNode.Repository.AddPackage(packageA);
            firstTreeNode.Repository.AddPackage(packageB);
            firstTreeNode.Repository.AddPackage(packageC);

            provider.SelectedNode = firstTreeNode;

            ManualResetEvent manualEvent = new ManualResetEvent(false);

            provider.ExecuteCompletedCallback = delegate {
                try {
                    // Assert

                    // init.ps1 should be executed
                    scriptExecutor.Verify(p => p.Execute(It.IsAny<string>(), PowerShellScripts.Init, packageB, null, It.IsAny<ILogger>()), Times.Once());

                    // InstallPackage() should get called
                    packageManager.Verify(p => p.InstallPackage(
                       projectManager, It.IsAny<IPackage>(), It.IsAny<IEnumerable<PackageOperation>>(), false, It.IsAny<ILogger>()), Times.Once());
                }
                finally {
                    manualEvent.Set();
                }
            };

            var extensionB = new PackageItem(provider, packageB, null);

            // Act
            provider.Execute(extensionB);

            // do not allow the method to return
            manualEvent.WaitOne();
        }

        private static OnlineProvider CreateOnlineProvider(
            IVsPackageManager packageManager = null,
            IProjectManager projectManager = null,
            IPackageRepositoryFactory repositoryFactory = null,
            IPackageSourceProvider packageSourceProvider = null,
            Project project = null,
            IScriptExecutor scriptExecutor = null) {

            if (packageManager == null) {
                var packageManagerMock = new Mock<IVsPackageManager>();
                var sourceRepository = new MockPackageRepository();
                packageManagerMock.Setup(p => p.SourceRepository).Returns(sourceRepository);

                packageManager = packageManagerMock.Object;
            }

            if (projectManager == null) {
                projectManager = new Mock<IProjectManager>().Object;
            }

            if (repositoryFactory == null) {
                var repositoryFactoryMock = new Mock<IPackageRepositoryFactory>();
                repositoryFactoryMock.Setup(p => p.CreateRepository(It.IsAny<PackageSource>())).Returns(new MockPackageRepository());
                repositoryFactory = repositoryFactoryMock.Object;
            }

            if (packageSourceProvider == null) {
                var packageSourceProviderMock = new Mock<IPackageSourceProvider>();
                packageSourceProviderMock.Setup(p => p.GetPackageSources()).Returns(
                        new PackageSource[2] {
                            new PackageSource("Test1", "One"),
                            new PackageSource("Test2", "Two")
                        }
                    );
                packageSourceProvider = packageSourceProviderMock.Object;
            }

            var factory = new Mock<IVsPackageManagerFactory>();
            factory.Setup(m => m.CreatePackageManager(It.IsAny<IPackageRepository>())).Returns(packageManager);

            var mockProgressWindowOpener = new Mock<IProgressWindowOpener>();
            var mockLicenseWindowOpener = new Mock<ILicenseWindowOpener>();

            if (project == null) {
                project = new Mock<Project>().Object;
            }

            if (scriptExecutor == null) {
                scriptExecutor = new Mock<IScriptExecutor>().Object;
            }

            var services = new ProviderServices(
                mockLicenseWindowOpener.Object,
                mockProgressWindowOpener.Object,
                scriptExecutor,
                new MockOutputConsoleProvider()
            );

            return new OnlineProvider(
                project,
                projectManager,
                new System.Windows.ResourceDictionary(),
                repositoryFactory,
                packageSourceProvider,
                factory.Object,
                services,
                new Mock<IProgressProvider>().Object);
        }

        private static ProjectManager CreateProjectManager(IPackageRepository localRepository, IPackageRepository sourceRepository) {
            var projectSystem = new MockProjectSystem();
            return new ProjectManager(sourceRepository, new DefaultPackagePathResolver(projectSystem), projectSystem, localRepository);
        }
    }
}


text:
package com.leacox.sandbox.security;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.Marker;
import org.slf4j.MarkerFactory;

import java.security.AccessController;
import java.security.AllPermission;
import java.security.CodeSource;
import java.security.Permission;
import java.security.PermissionCollection;
import java.security.Permissions;
import java.security.Policy;
import java.security.PrivilegedAction;
import java.security.ProtectionDomain;

/**
 * A policy that grants {@code AllPermission} to the application/runtime class loader and a limited
 * set of permissions to the {@link UserClassLoader}.
 *
 *
In order to apply different permissions to user code vs application code the classes must be
 * isolated in different class loaders.
 *
 *
Additionally, since class loaders are hierarchical it is best to have a minimal set of class
 * on the classpath when the application is bootstrapped. The bootstrap/system class loaders will be
 * available to both application and user class loaders. The best way to accomplish this is to have
 * a bootstrapping class that creates a separate isolated application classloader by loading
 * additional jars at runtime and then a separate user classloader that loaders the users jar.
 *
 * @author John Leacox
 */
public class RuntimePolicy extends Policy {
  private static final Logger logger = LoggerFactory.getLogger(RuntimePolicy.class);
  private static final Marker securityMarker = MarkerFactory.getMarker("SECURITY");

  private final ProtectionDomain providerDomain;
  private final Permissions allPermissions;

  /**
   * Creates a new instance of {@code RuntimePolicy}.
   */
  public RuntimePolicy() {
    // Determine this policies domain. This is necessary to avoid a chicken-and-egg problem.
    Policy thisPolicy = this;
    this.providerDomain = AccessController.doPrivileged(
        (PrivilegedAction<ProtectionDomain>) () -> thisPolicy.getClass().getProtectionDomain());

    Permissions permissions = new Permissions();
    permissions.add(new AllPermission());
    permissions.setReadOnly();
    this.allPermissions = permissions;
  }

  private boolean isPolicy(ProtectionDomain domain) {
    return providerDomain == domain;
  }

  private boolean isUser(ProtectionDomain domain) {
    return domain.getClassLoader() instanceof UserClassLoader;
  }

  @Override
  public PermissionCollection getPermissions(ProtectionDomain domain) {
    boolean isPolicy = isPolicy(domain);
    boolean isUser = isUser(domain);

    String sourceLocation = codeSourceLocation(domain);
    String classLoaderName = classLoaderName(domain);
    logger.info(securityMarker,
        "getPermissions, codeSource = {}, classLoader = {}, isUser = {}",
        sourceLocation, classLoaderName, isPolicy, isUser);

    Permissions permissions;
    if (isPolicy || !isUser) {
      permissions = allPermissions;
    } else {
      permissions = userPermissions();
    }

    logger
        .info(securityMarker,
            "getPermissions, codeSource = {}, classLoader = {}, isUser = {}, permissions = {}",
            sourceLocation, classLoaderName, isUser, permissions);

    return permissions;
  }

  @Override
  public boolean implies(ProtectionDomain domain, Permission permission) {
    boolean isUser = isUser(domain);

    String sourceLocation = codeSourceLocation(domain);
    String classLoaderName = classLoaderName(domain);
    logger.info(securityMarker,
        "implies, codeSource = {}, classLoader = {}, permission = {}, isUser = {}",
        sourceLocation, classLoaderName, permission, isUser);

    boolean implies;
    if (isPolicy(domain)) {
      implies = true;
    } else {
      implies = super.implies(domain, permission);
    }

    logger.info(securityMarker,
        "implies, codeSource = {}, classLoader = {}, permission = {}, isUser = {}, result = {}",
        sourceLocation, classLoaderName, permission, isUser, implies);

    return implies;
  }

  private String codeSourceLocation(ProtectionDomain domain) {
    CodeSource source = domain.getCodeSource();
    if (source == null) {
      return "null";
    }

    return source.getLocation().getPath();
  }

  private String classLoaderName(ProtectionDomain domain) {
    ClassLoader loader = domain.getClassLoader();
    if (loader == null) {
      return "null";
    }

    return loader.getClass().getName();
  }

  private Permissions userPermissions() {
    // No permissions
    return new Permissions();
  }
}

redpajama_set_name: RedPajamaGithub


text:
package com.ruinwesen.patchmanager.swing.form;

import java.awt.Event;

public class FormElementEvent extends Event {

    private static final long serialVersionUID = -3520624373295287965L;
    public static final int FORM_ELEMENT_BASE_ID = 10000;
    public static final int FORM_ELEMENT_VALUE_CHANGED = FORM_ELEMENT_BASE_ID+1;
    public static final int FORM_ELEMENT_VALUE_VALIDITY_CHANGED = FORM_ELEMENT_BASE_ID+2;

    public FormElementEvent(FormElement target, int id) {
        this(target, id, null);
    }

    public FormElementEvent(FormElement target, int id, Object arg) {
        super(target, id, arg);
        if (target == null) {
            throw new IllegalArgumentException("target == null");
        }
    }

    public FormElement getFormElement() {
        return (FormElement) target;
    }

}


text:
{{'DECISION-TABLE.POPUP.CREATE-TITLE' | translate}}

{{'DECISION-TABLE.POPUP.CREATE-DESCRIPTION' | translate}}

 0" class="alert error"
                     style="font-size: 14px; margin-top:20px">

                        {{model.errorMessage}}

                    {{'DECISION-TABLE.NAME' | translate}}
                    <input ng-disabled="model.loading" type="text" class="form-control" maxlength="400"
                           id="newDecisionTableName" ng-model="model.decisionTable.name" custom-keys
                           enter-pressed="ok()" auto-focus editor-input-check>

                    {{'DECISION-TABLE.KEY' | translate}}
                    <input ng-disabled="model.loading" type="text" class="form-control" maxlength="255"
                           id="newDecisionTableKey" ng-model="model.decisionTable.key" editor-input-check>

                    {{'DECISION-TABLE.DESCRIPTION' | translate}}
                    <textarea ng-disabled="model.loading" class="form-control" id="newDecisionTableDescription" rows="5"
                              ng-model="model.decisionTable.description"></textarea>

                    <button type="button" class="btn btn-sm btn-default" ng-click="cancel()"
                            ng-disabled="model.loading">
                        {{'GENERAL.ACTION.CANCEL' | translate}}
                    </button>
                    <button type="button" class="btn btn-sm btn-default" ng-click="ok()"
                            ng-disabled="model.loading || !model.decisionTable.name || model.decisionTable.name.length == 0 || !model.decisionTable.key || model.decisionTable.key.length == 0">
                        {{'DECISION-TABLE.ACTION.CREATE-CONFIRM' | translate}}
                    </button>


text:
\input{preamble}

\begin{document}
    \section{Subjects}
    \begin{itemize}
        \item Markov Decision Process
        \item Bellman Equations and Algorithms
    \end{itemize}

    \section{Notes}

    \subsection{Elements of reinforcement learning}

    \begin{description}
        \item[Exploration vs. exploitation] Should we explore new
        opportunities, or take advantage of the ones we have found that works?
        \item[Agent] How do we interact with the environment, who do we
        represent?
        \item[Environment] What actions can we take and what is currently
        happening?
        \item[Policy] How does the learning agent behave? A mapping from the
        perceived states of the environment to which actions to be taken when
        in those states.
        \item[Reward signal] On each time step, the environment send the agent
        a single number, a reward. The agents sole objective is to maximize the
        total reward it receives. The agent can change the outcome by the
        signal by taking actions or changing the environment, but can't change
        the function that generates the reward signal.
        \item[Value function] The value of a state is the total amount of
        reward an agent can expect to accumulate, \textit{starting from that
        state}. Rewards return the immediate desirability of a state, value
        returns the long-term desirability of a state.
        \item[Model of the environment] Something that mimics the behaviour of
        the environment, so we can infer how the environment will behave. (E.g.
        could be used to predict the next model). There are both model-based
        and model-free methods.
    \end{description}

    \subsection{Finite Markov Decision Processes}
    We can think of the interaction between an agent and the environment as the
    agent taking some action at time $t$ $A_t$, and the environment providing
    the agent with a reward $R_{t+1}$ and a new state $S_{t+1}$ which prompts a
    new action $A_{t+1}$ from the agent.

    At each time step $t$, the agent implements a mapping from states to
    probabilities of selecting each possible action $A_t \in A(S_t)$. This
    mapping is called the agent's policy and is denoted $\pi_t$ where
    $\pi_t(a|s)$ is the probability that $A_t=a$ if $S_t=s$. Reinforcement
    learning is about changing the policy as a result of experience.

    We seek to maximize the \textit{expected return} $\Ex({G_t})$, but how do
    we define $G_t$? The simplest case is simply the sum of the rewards:

    \begin{equation*}
        G_t = R_{t+1}+R_{t+2}+\cdots+R_T
    \end{equation*}
    If we are dealing with \textit{episodic tasks}, i.e. tasks that are
    independent like plays of a game or trips through a maze (we call these
    tasks ``episodes''). However, if we
    are dealing with \textit{continuing tasks}, for example a robot with a long
    life span or process-control tasks then the return, which we are trying to
    maximize is potentially infinite. Thus, we will use a definition of return
    that is slightly more complex conceptually but much simpler mathematically.
    We introduce the concept of \textit{discounting}, the agent will try to
    maximize the sum of the discounted rewards, where the rewards are
    decreasing in value so tasks that are performed immediately are worth more:
    \begin{equation*}
        G_t=R_{t+1}+\gamma R_{t+2}+ \gamma ^2 R_{t+3} + \cdots =
        \sum_{k=0}^{\infty}\gamma^k R_{t+k+1}
    \end{equation*}
    Here $0\leq \gamma \leq 1$ is called the discount rate. Then we know that
    if $\gamma < 1$ then the infinite sum has a finite value, as long as
    $R_k\neq \infty$. If $\gamma = 0$, then the agent is only concerned with
    maximizing immediate rewards. More often than not, we won't have $\infty$
    rewards, so we can simplify this to:
    \begin{equation*}
        G_t=\sum_{k=0}^{T-t-1}\gamma^k R_{t+k+1}
    \end{equation*}
    Where it is possible that $T=\infty$ or $\gamma = 1$ but both cannot be
    true (since the sum would then be $\infty$).

    \subsubsection{The Markov Property}
    We cannot expect an actor to know everything about the environment, as some
    information may be hidden. The environment will provide the actor with
    different sensations, and we might expect the actor to remember all the
    sensations it has experienced so far.

    We call a state signal which succeeds in retaining all relevant information
    to be \textit{Markov} or to have \textit{the Markov property}. For example,
    if we were playing chess, then the current configuration of all the pieces
    on the board would serve as a Markov state, because it summarizes all
    important details that led to it. Much of the information about the
    sequence is lost, but all that really matters for the future of the game is
    retained. It doesn't matter that we don't know how we ended up at that
    chess board configuration, the current configuration is all that is
    relevant.

    In order to formally define the Markov property in a mathematically simple
    way, we will assume that there are a finit number of states and reward
    values. This enables us to work in terms of sums and probabilities instead
    of integrals and probability densities, but it can easily be extended.

    Consider how an environment might respond at time $t+1$ to the action taken
    at time $t$. The most general, causal case is where the response depends on
    everything that has happened earlier:
    \begin{equation*}
        \Pr\left[S_{t+1} =s',
        R_{t+1}=r|S_0,A_0,R_1,S_1,A_1,\dots,R_{t-1},S_{t-1},A_{t-1},R_t,S_t,A_t
         \right]
    \end{equation*}
    However, if the state signal has the Markov property, then the environments
    response at $t+1$ only depends on the state and action at $t$ so we can
    define it as simply:
    \begin{equation*}
        p(s',r|s,a)=\Pr\left[S_{t+1}=s',R_{t+1}=r|S_t=s, A_t=a\right]
    \end{equation*}
    If this hold for all $s'$ and $r$ then we will be able to predict the
    result of an action having just $s$, $a$ just as good as we would be able
    to do it given the complete history of states and actions.

    It is usually useful to think of the state at each time step as an
    approximation to a Markov state even in non-Markov state signals because of
    the better performance Markov gives us in reinforcement learning, as long
    as one keeps in mind that it may not fully satisfy the Markov property.

    \subsubsection{Markov Decision Process}
    A reinforcement learning task that satisfies the Markov property is called
    a \textit{Markov decision process} or MDP. If there is a finite amount of
    states and actions, then we call it a \textit{finite MDP}. If you
    understand \textit{finite MDP} then you understand $90\%$ of modern
    reinforcement learning.

    A finite MDP, is defined by uts state and actions sets and by the one-step
    dynamics of the environment. We use the probability of going to $s'$ and
    receiving $r$ from $s$ and $a$ as previously:
    \begin{equation*}
    p(s',r|s,a)=\Pr\left[S_{t+1}=s',R_{t+1}=r|S_t=s, A_t=a\right]
    \end{equation*}
    This completely specify the dynamics of some finite MDP. We can then
    compute anything else we might want to know about the environment such as
    the expected rewards for state-action pairs:
    \begin{equation*}
        r(s,a)=\Ex\left[R_{t+1}|S_t=s, A_t=a\right]=\sum_{r\in R}r \sum_{s' \in
        S} p(s',r|s,a)
    \end{equation*}
    The state-transition probabilities:
    \begin{equation*}
        p(s'|s,a)=\Pr\left[S_{t+1}=s'|S_t=s, A_t=a\right]=\sum_{r\in
        R}p(s',r|s,a)
    \end{equation*}
    And the expected rewards for state--action--next-state:
    \begin{equation*}
        r(s,a,s')=\Ex\left[R_{t+1}|S_t=s,A_t=a,S_{t+1}=s'\right]=\frac{\sum_{r\in
         R}r\cdot p(s',r|s,a)}{p(s'|s,a)}
    \end{equation*}

    \subsubsection{Value Functions}
    We will need to estimate value functions for almost all reinforcement
    learning algorithms. The value functions are functions of states that
    estimate \textit{how good} it is to for the the agent to be in a given
    state (or rather, how good is it to perform some action in a given state).
    The notion of ``how good'' is defined in terms of what future rewards can
    we expect to receive.

    A policy $\pi$ is a mapping from each state $s \in S$ and action $a \in
    A(s)$ to the probability $\pi(a|s)$ of taking action $a$ when in state $s$.
    Informally, the following equation is the expected return when starting in
    state $s$ and following policy $\pi$ for a MDP:
    \begin{equation*}
        v_\pi(s)=\Ex_\pi\left[G_t\given
        S_t=s\right]=\Ex_\pi\left[\sum_{k=0}^{\infty}\gamma^k
        R_{t+k+1}\given S_t=s\right]
    \end{equation*}

    Similarly, we can define the value of taking action $a$ in state $s$ under
    $\pi$ as the expected return starting from $s$, taking the action $a$ and
    then following $\pi$:
    \begin{equation*}
        q_\pi(s,a)=\Ex_\pi\left[G_t\given
        S_t=s,A_t=a\right]=\Ex_\pi\left[\sum_{k=0}^{\infty}\gamma^kR_{t+k+1}\given
         S_t=s,A_t=a\right]
    \end{equation*}
    We call $q_\pi$ the action-value function for policy $\pi$. We can estimate
    $v_\pi$ and $q_\pi$ using the average of the actual returns, we will return
    to this later. For now, let's look at a fundamental property of $v_\pi$
    which is used throughout reinforcement learning. Namely, the property that
    they satisfy particular recursive relationships:

    \begin{align*}
        v_\pi(s)&= \Ex_\pi\left[G_t\given S_t=s\right]\\
            &= \Ex_\pi\left[\sum_{k=0}^{\infty}\gamma^kR_{t+k+1}\given S_t =
            s\right]\\
            &=
            \Ex_\pi\left[R_{t+1}+\gamma\sum_{k=0}^{\infty}\gamma^kR_{t+k+2}
            \given S_t=s\right]\\
            &= \sum_a \pi\left(a\given s\right) \sum_{s'} \sum_r p(s',r|s,a)
            \left[r+\gamma \Ex\left[\sum_{k=0}^{\infty}\gamma^k R_{t+k+2}
            \given S_{t+1}=s'\right]\right]\\
            &= \sum_a \pi\left(a\given s\right) \sum_{s',r} p(s',r|s,a)
            \left[r+\gamma v_\pi(s')\right]
    \end{align*}
    We can read this equation as a sum over the triples $a$, $s'$ and $r$. For
    each triple, we compute its probability $\pi\left(a\given s\right)p(s',
    r|s,a)$ weight the value in the bracket (which is the reward plus the
    expected reward from the next state $s'$), which gives us the expected
    value. This equation is called the \textit{Bellman equation for $v_\pi$}

    \subsubsection{Optimal value functions}
    In order to ``solve'' reinforcement learning, roughly means that we need to
    find a policy that achieves a lot of reward in the long run. For finite
    MDPs we can define it precisely as follows. A police $\pi$ is said to be
    better than or equals to a policy $\pi'$ if its expected return is better
    than or equal to that of $\pi'$. In other words $\pi \geq \pi' \iff
    v_\pi(s)\geq v_{\pi'}(s) \forall s\in S$. There will always be at least one
    policy which is better than or equal to all other policy which is the
    optimal policy, we denote any of the optimal policies as $\pi_*$. We can
    define their state-value function as:
    \begin{equation*}
        v_*(s)=\max_\pi v_\pi(s)
    \end{equation*}
    Which is the optimal state-value function. We can define the same for the
    optimal action-value function:
    \begin{equation*}
        q_*(s,a)=\max_\pi q_\pi(s,a)
    \end{equation*}
    For the state-action pair $(s,a)$, this function gives the expected return
    of taking action $a$ in state $s$ and then following an optimal policy. We
    can therefore write $q_*$ in terms of $v_*$ as follows:
    \begin{equation*}
        q_*(s,a)=\Ex\left[R_{t+1}+\gamma v_*(S_{t+1}) \given S_t=s, A_t=a\right]
    \end{equation*}

    \subsubsection{Dynamic programming}
    We can use dynamic programmin (DP) to compute the value functions explained
    earlier. Furthermore, we can easily obtain optimal policies once we have
    found the optimal value functions $v_*$ or $q_*$ which satisfy the Bellman
    equations:
    \begin{align*}
        v_*(s)&=\max_a \Ex\left[R_{t+1}+\gamma v_*(S_{t+1}) \given S_t=s, A_t =
        a\right]\\
            &= \max_a \sum_{s',r}p(s',r|s,a)\left[r+\gamma v_*(s')\right]
    \end{align*}
    or
    \begin{align*}
        q_*(s,a) &= \Ex\left[R_{t+1}+\gamma \max_{a'}q_*(S_{t+1},a') \given
        S_t=s, A_t=a \right]\\
            &= \sum_{s', r}p\left(s', r \given s,a\right)\left[r+\gamma
            \max_{a'}q_*(s',a')\right]
    \end{align*}
    We will see that we obtain DP algorithms by turning these Bellman equations
    into assigments, that is update rules which improve approximations of the
    desired functions.

    \subsubsection{Policy Evaluation}
    First, we will consider computing the state-value function $v_\pi$. This is
    called \textit{policy evaluation} recall that:
    \begin{align*}
    v_\pi(s) &= \Ex_\pi\left[\sum_{k=0}^{\infty}\gamma^kR_{t+k+1}\given S_t =
    s\right]\\
        &= \Ex_\pi\left[R_{t+1}+\gamma v_\pi(S_{t+1})
        \given S_t=s\right]\\
        &= \sum_a \pi\left(a\given s\right) \sum_{s',r} p(s',r|s,a)
        \left[r+\gamma v_\pi(s')\right]
    \end{align*}
    Now consider a sequence of approximate value functions $v_0,v_1,\dots$, the
    initial approximation $v_0$ is arbitrary except the terminal state has to
    be $0$. We can then obtain the approximations by using the Bellman equation
    for $v_\pi$:
    \begin{align*}
        v_{k+1}(s)&= \Ex_\pi\left[R_{t+1}+\gamma v_k(S_{t+1}) \given S_t = s
        \right]\\
            &=\sum_a \pi\left(a \given s\right)\sum_{s',r}p\left(s',r \given
            s,a\right)\left[r+ \gamma v_k(s')\right]
    \end{align*}
    This approximation does indeed converge to $v_\pi$ as $k\rightarrow \infty$

    \subsubsection{Policy Improvement}
    Now that we are able to determine $v_\pi$, we are able to evaluate our
    policies, and thus we will be able to improve upon them. For some state
    $s$, we would like to know whether or not we should change the policy such
    that we deterministically choose an action $a\neq \pi(s)$. We know the
    quality of following the current policy from $s$ ($v_\pi(s)$), so would it
    be better or worse to change to another policy? In order to answer this, we
    could select $a$ in $s$ and otherwise just follow the existing policy:
    \begin{align*}
        q_\pi(s,a)&=\Ex\left[R_{t+1}+\gamma v_\pi(S_{t+1}) \given S_t=s,
        A_t=a\right]\\
            &= \sum_{s',r}p\left(s',r \given s,a\right)\left[r+\gamma
            v_\pi(s')\right]
    \end{align*}
    If this is greater than $v_\pi(s)$ then it is better to select $a$ once in
    $s$, but then it would in fact be better to pick $s$ every time:
    \begin{equation*}
        q_\pi(s,\pi'(s)) \geq v_\pi(s) \implies v_{\pi'}(s) \geq v_\pi(s)
    \end{equation*}
    We can then use this to define a new greedy policy $\pi'$ given by:
    \begin{align*}
        \pi'(s)&=\arg\max_a q_\pi(s,a)\\
            &=\arg\max_a \Ex\left[R_{t+1} + \gamma v_\pi(S_{t+1}) \given S_t=s,
            A_t=a \right]\\
            &=\arg\max_a \sum_{s',r}p\left(s',r \given s,a\right)\left[r+\gamma
            v_\pi(s')\right]
    \end{align*}
    I.e. the greedy policy looks one step ahead and picks the action that looks
    best in the short term. Now suppose $\pi'$ is as good as, but not better
    than, the old policy $\pi$. The $v_\pi=v_{\pi'}$ and then it follows that:
    \begin{align*}
        v_{\pi'}(s)&=\max_a\Ex\left[R_{t+1}+\gamma v_{\pi'}(S_{t+1}) \given
        S_t=s, A_t=a \right]\\
            &= \max_a \sum_{s',r}p\left(s',r \given s,a \right)\left[r+\gamma
            v_{\pi'}(s')\right]
    \end{align*}
    But this was the optimal Bellman equation from earlier, therfore $v_{\pi'}$
    must be $v_*$ and thus $\pi'$ must be an optimal policy, thus this policy
    improvement algorithm must give us a strictly better policy until we hit
    the optimal one.

    So far, we have only considered deterministic policies, that is where
    $\pi(s)$ always evaluate to the same action. However, all of the ideas so
    far, easily extend to stochastic policies ($\pi(a|s)$).

    Now that we can both evaluate and improve our policies, we can just
    continue evaluating then improving and evaluating then improve and so on,
    monotonically improving untill we find a optimal solution.

    \subsubsection{Value iteration}
    This algorithm is fairly expensive, so we need to speed it up somehow. One
    way is implementation specific, where we simply make sure to pick up policy
    evaluation values from the last evaluation and not $0$.

    Value iteration provides us with a way to solve both policy evaluation and
    improvement in one step.

    \begin{align*}
    v_{k+1}(s)&=\max_a\Ex\left[R_{t+1}+\gamma v_k(S_{t+1}) \given
    S_t=s, A_t=a \right]\\
        &= \max_a \sum_{s',r}p\left(s',r \given s,a \right)\left[r+\gamma
        v_k(s')\right]
    \end{align*}
    Here, we find that $v_k$ still converges to $v_*$.

    \subsubsection{Drawback of DP}
    The drawback of DP as described here, is that it involves operations over
    the entire state set of the MDP, so if there are many states (like
    backgammon) then it is incredibly expensive.

    \subsubsection{Generalized Policy Iteration}
    In generalized policy iteration (GPI), we maintain both an approximate
    policy and an approximate value function. The value function is repeatedly
    altered to approximate the value function for the current policy, and the
    current policy is repeatedly improved with respect to the current value
    function. In a way they move against each other, as they create a moving
    target for each other, but they cause both policy and value function to
    approach optimality.

    This is a simple overview of the generalized policy iteration:
    \begin{itemize}
        \item While improving repeat
        \item Run policy evaluation for some time on some states
        \item Run policy improvement for som time on some states
    \end{itemize}
    Here we may choose not to visit all states, but in most cases we will still
    converge in polynomial time.

    \subsection{Monte Carlo algorithms}
    Up until now, we have assumed complete knowledge of the environment (finite
    states and actions). Monte Carlo algorithms, allows us to learn from just
    \textit{experience} -- we cample sequences of states, action and rewards
    without any prior knowledge of the environment's dynamics.

    In the Monte Carlo methods, we want to sovle the reinforcement learning
    problem based on averaging sample returns. Furthermore instead of computing
    the value functions (as we did in DP), we here want to learn the value
    functions instead.

    What if we looked at just the state-value function, how would we try to
    estimate that? The obvious solution here, is to average all the returns
    observed after visits to that state. As we observe more and more returns,
    the average should converge to the expected value. This is the underlying
    idea of all Monte Carlo algorithms.

    In order to ensure well-defined returns are available, we will only
    consider Monte Carlo for episodic tasks.

    \subsubsection{Estimating $v_\pi(s)$}
    $v_\pi(s)$ is the value of a state $s$ under policy $\pi$, given a set of
    episodes obtained by following $\pi$ and passing through $s$. Each
    occurrence of state $s$ in an episode is called a \textit{visit} to $s$. It
    may happen that we visit $s$ multiple times in an episode, so we will look
    at the \textit{first visit} to $s$. Let's first look at a theoretical
    algorithm which computes the value of $s$ in the first visit to $s$:
    \begin{algorithm}
        \caption{First-visit MC policy evaluation}
        \begin{algorithmic}
            \State \textit{// Initialize:}
            \State $\pi \gets$ policy to be evaluated
            \State $V \gets $ an arbitrary state-value function
            \State $Returns(s) \gets $ an empty list, for all $s \in S$
            \State
            \Loop \, forever
                \State Generate an episode using $\pi$
                \ForAll{state $s$ in the episode}
                    \State $G \gets $ return following the first occurence of
                    $s$
                    \State Append $G$ to $Returns(s)$
                    \State $V(s) \gets \average(Returns(s))$
                \EndFor
            \EndLoop
        \end{algorithmic}
    \end{algorithm}

    We could also have explored the \textit{every-visit MC method}, which has
    some different properties and extends more naturally to function
    approximation, but First-visit MC has been more widely studied.

    First-visit MC converges quadratically to $v_\pi(s)$ as the number of
    first-visits to $s$
    goes to infinity. An alternative, finite option, is to loop for some time
    or some number of episodes.

    \subsubsection{Estimation of action values}
    If a model is available, we can simply look ahead in the next possible
    states and pick the action that leads to the best state. If no model is
    available, then we have to estimate the values of the action (e.g. speed of
    vehicle) we should pick in order to get to the best state and maximize
    reward.

    In other words, one of the primary goals for Monte Carlo methods is to
    estimate $q_*$, which is the optimal value we get when we take action $a$
    from state $s$. In order to estimate this, we may run a First-visit MC
    method as before, just visiting state-action pairs instead of just states.
    Again, this will converge quadratically as before.

    The complication is that many state-action pairs may never be visited. If
    $\pi$ is a deterministic policy, then following $\pi$ one will only observe
    returns from one action from each state (as it is deterministic). Therefore
    the Monte Carlo estimates of the other actions from that state will not
    improve with experience. This is the general problem of \textit{maintaining
    exploration} i.e. we must assure that we are continually exploring other
    options in order to attempt to find a better one. One way to solve this, is
    to say that we start in a state-action pair, and every state-action pair
    has a nonzero probability of being selected as the start. We call this the
    assumption of \textit{exploring starts}.

    \textit{Exploring starts}, is not really useful when we learn by actual
    interacting with an environment as we cannot simply start from any state we
    want. In this case, the most common alternative approach is to only
    considering stochastic policies with only nonzero probabilities.

    \subsubsection{Approximating optimal policies}
    Let us consider a Monte Carlo version of classical policy iteration. We can
    perform policy evaluation as described in the previous section (estimation
    of action values). If we assume that:
    \begin{itemize}
        \item We observe an infinite number of episodes.
        \item The episodes are generated with exploring starts.
    \end{itemize}
    Then the Monte Carlo methods will compute each $q_{\pi_k}$ exactly for
    arbitrary $\pi_k$.

    Now we just need to figure out how to perform policy-improvements. We can
    improve on our policy with a simple greed policy with respect to the
    current value function. We can then compute for each $s \in S$ the policy
    $\pi$:
    \begin{equation*}
        \pi(s) = \arg\max_a q(s,a)
    \end{equation*}
    We can then construct each $\pi_{k+1}$ as the greedy policy with respect to
    $q_{\pi_k}$, and we get that the policy improvement theorem applies since:
    \begin{align*}
        q_{\pi_k}(s,\pi_{k+1}(s)) &= q_{\pi_k}(s, \arg\max_a q_{\pi_k}(s,a))
    \end{align*}
    This assures us that each $\pi_{k+1}$ is uniformly better than or just as
    good as $\pi_k$. If it is just as good as $\pi_k$ then they are both
    optimal policies.

    \subsubsection{Monte Carlo without infinite episodes}
    We made two unlikely assumptions in order to easily obtain this guarantee
    of convergence. One was that the episodes has exploring starts, the other
    was that we could run for infinitely many episodes. We will try to remove
    the latter assumption here and then return to exploring starts afterwards.

    There are two primary ways to combat the infinite episodes assumption, one
    approach is to estimate some bounds on magnitude and probability of error
    and run until these bounds are sufficiently small. This will guarantee
    correct convergence up to some level of approximation, however it is likely
    to require far too many episodes to be useful in practice.

    The second approach is to avoid trying to complete policy evaluation before
    returning for improvement and simply move the value function \textit{toward}
    $q_{\pi_k}$. One extreme example of this was the value iteration, in which
    only one iteration of iterative policy evaluation is performed between
    improvements. Or the in-place version where we alternate between
    improvement and evaluation steps for single states.

    For Monte Carlo policy evaluation it is natural to alternate between
    evaluation and improvement on an episode-by-episode basis. After each
    episode we evaluate the policy and then improve it at all the visited
    states. We can describe this algorithm as follows:

    \begin{algorithm}
        \caption{Monte Carlo ES (Exploring Starts)}
        \begin{algorithmic}
            \State \textit{// Initialize, for all } $s\in S, a\in A(s)$:
            \State $Q(s,a) \gets$ arbitrary
            \State $\pi(s) \gets$ arbitrary
            \State $Returns(s,a) \gets$ empty list
            \State
            \Loop\, forever
                \State Choose $S_0 \in S$ and $A_0 \in A(S_0)$ s.t. all pairs
                have probability $> 0$
                \State Generate an episode starting from $S_0, A_0$ following
                $\pi$
                \ForAll{pairs $s,a$ appearing in the episode}
                    \State $G \gets$ return following the first occurence of
                    $s,a$
                    \State Append $G$ to $Returns(s,a)$
                    \State $Q(s,a) \gets \average(Returns(s,a))$
                \EndFor
                \ForAll{states $s$ in the episode}
                    \State $\pi(s) \gets \arg \max_a Q(s,a)$
                \EndFor
            \EndLoop
        \end{algorithmic}
    \end{algorithm}

    \subsubsection{Monte Carlo without Exploring Starts}
    Now, how can we avoid the unlikely assumption of exploring starts? The only
    general way to ensure that all actions are selected infinitely often is for
    the agent to continue to select them. We have two approaches to ensuring
    that this happens, we call it \textit{on-policy} methods  and
    \textit{off-policy} methods. On-policy attempts to evaluate or improve the
    policy used to make decision whereas off-policy methods evaluate or improve
    a different policy. The ES method above is an example of an on-policy
    method. We will explore on-policy methods more here, and not go into
    details with off-policy.

    Here we will look at $\epsilon$-greedy policies, which with probability
    $1-\epsilon$ will simply follow the policy $\pi$, but with probability
    $\epsilon$ it will select a uniformly random action. So each action has
    probability $\frac{\epsilon}{|A(s)|}$ of being picked, except the estimated
    optimal action which also has probability $1-\epsilon$ of being picked,
    yielding the algorithm:

    \begin{algorithm}
        \caption{On-policy first-visit MC control (for $\epsilon$-soft
        policies)}
        \begin{algorithmic}
            \State \textit{// Initialize}
            \ForAll{$s \in S$ and $a \in A(s)$}
                \State $Q(s,a) \gets$ arbitrary
                \State $Returns(s,a) \gets$ empty list
                \State $\pi(a|s) \gets$ an arbitrary $\epsilon$-soft policy
            \EndFor

            \Loop\, forever
                \State Generate an episode using $\pi$
                \ForAll{pairs $s,a$ appearing in the episode}
                    \State $G \gets$ return following the first occurence of
                    $s,a$
                    \State Append $G$ to $Returns(s,a)$
                    \State $Q(s,a) \gets \average(Returns(s,a))$
                \EndFor
                \ForAll{visited states $s$ in the episode}
                    \State $A^* \gets \arg\max_a Q(s,a)$
                    \ForAll{$a \in A(s)$}
                        \State $\pi(a|s) \gets \begin{cases}
                        1-\epsilon + \frac{\epsilon}{|A(s)|},&\text{ if
                        }a=A^*\\
                        \frac{\epsilon}{|A(s)|},&\text{ if } a \neq A^*
                        \end{cases}$
                    \EndFor
                \EndFor
            \EndLoop
        \end{algorithmic}
    \end{algorithm}

    This holds under the policy improvement theorem, although I will not prove
    this here.
\end{document}


text:
/*
 * This file contains the subroutines that support the Target threads.
 */
#include "xint.h"

/*----------------------------------------------------------------------------*/
/* xdd_target_pass() - This subroutine will perform a single pass
 */
int32_t
xdd_target_pass(xdd_plan_t* planp, target_data_t *tdp) {

	/* Before we get started, check to see if we need to reset the
	 * run_status in case we are using the start trigger.
	 */
	if (tdp->td_target_options & TO_WAITFORSTART) {
		if (tdp->td_trigp) tdp->td_trigp->run_status = 0;
	}

	// This barrier is to ensure that all TARGETS start at same time

	xdd_barrier(&planp->results_targets_startpass_barrier,&tdp->td_occupant,0);

	/* Check to see if any of the other threads have aborted */
	if (xgp->abort) {
		fprintf(xgp->errout,"%s: ERROR: xdd_target_pass: Target number %d name '%s' Aborting due to failure with another target\n",
			xgp->progname,
			tdp->td_target_number,
			tdp->td_target_full_pathname);
		fflush(xgp->errout);
		return(-1);
	}

 	// This will wait for the interactive command processor to say go if we are in interactive mode
	if (planp->plan_options & PLAN_INTERACTIVE)
		xdd_barrier(&planp->interactive_barrier,&tdp->td_occupant,0);
	if (xgp->abort)
		return(0);

	// If this run has completed or we've been canceled, then exit this loop
	if (xgp->run_complete)
		return(0);

	// Things to do before this pass is started
	xdd_target_ttd_before_pass(tdp);

/////////////////////////////// PSEUDO-Loop Starts Here ////////////////////////
// The PSEUDO-Loop just means that the actual "looping" in done in
// the xdd_target_pass_loop() subroutine (or the e2e equivalent).
	// The pass loops are handled by one of two subroutines depending on
	// whether this is the Destination Side of an E2E operation or not.
	tdp->td_current_state &= ~TARGET_CURRENT_STATE_PASS_COMPLETE;
	if (tdp->td_target_options & TO_ENDTOEND) { // E2E operations are *different*
		if (tdp->td_target_options & TO_E2E_SOURCE)
		    xdd_targetpass_e2e_loop_src(planp, tdp);
		else xdd_targetpass_e2e_loop_dst(planp, tdp);
	} else { // Normal operations (other than E2E)
	    xdd_target_pass_loop(planp, tdp);
	}
	tdp->td_current_state |= TARGET_CURRENT_STATE_PASS_COMPLETE;
/////////////////////////////// PSEUDO-Loop Ends  Here /////////////////////////
	// If this is an E2E operation and we had gotten canceled - just return
	if ((tdp->td_target_options & TO_ENDTOEND) && (xgp->canceled))
		return(-1);

	// Things that the Target Thread needs to do after a pass has completed
	xdd_target_ttd_after_pass(tdp);

	// Release the results_manager() to process/display the
	// results for this pass
	xdd_barrier(&planp->results_targets_endpass_barrier,&tdp->td_occupant,0);

	// At this point all the Target Threads have completed their pass and
	// have passed thru the previous barrier releasing the results_manager()
	// to process/display the results for this pass.

	// Wait at this barrier for the results_manager() to process/display the
	// results for this last pass
	xdd_barrier(&planp->results_targets_display_barrier,&tdp->td_occupant,0);

	// This pass is complete - return to the Target Thread
	return(0);
} // End of xdd_target_pass()

/*----------------------------------------------------------------------------*/
/* xdd_target_pass_loop() - This subroutine will assign tasks to Worker Threads
 * until all bytes have been processed.
 *
 * This subroutine is called by xdd_target_pass().
 */
void
xdd_target_pass_loop(xdd_plan_t* planp, target_data_t *tdp) {
	worker_data_t	*wdp;
	int		q;
	int32_t	status;	// Return status from various subroutines

/////////////////////////////// Loop Starts Here ///////////////////////////////
// This loop will transfer all data for a target until it runs out of
// bytes or if we get canceled.
// This loop will block/wait in xdd_get_and_available_worker_thread() until a
// worker thread becomes available for use. The number of worker threads available to do
// work is determined by the queue_depth for this target. Therefore, if the
// queue_depth is 1 then there is only 1 worker thread available and this loop will
// only ever be able to issue one I/O operation at a time and will have to wait
// for an I/O operation to complete before moving on to the next.
//
	while (tdp->td_current_bytes_remaining) {
		// Lock Step Processing (located in lockstep.c)
		// When the -lockstep option is specified, the xdd_lockstep()subroutine
		// will perform all I/O operations for a pass. Thus, when xdd_lockstep()
		// returns, it is necessary to set the "bytes_remaining" to zero so that
		// it looks like a normal completion.
		if (tdp->td_lsp) {
			status = xdd_lockstep(tdp);
			tdp->td_current_bytes_remaining = 0;
			break;
		}

		// Get pointer to next Worker Thread to issue a task to
		wdp = xdd_get_any_available_worker_thread(tdp);

		// Things to do before an I/O is issued
		status = xdd_target_ttd_before_io_op(tdp, wdp);
		if (status != XDD_RC_GOOD) {
			// Mark this worker thread NOT BUSY and break out of this loop
			pthread_mutex_lock(&wdp->wd_worker_thread_target_sync_mutex);
			wdp->wd_worker_thread_target_sync &= ~WTSYNC_BUSY; // Mark this Worker Thread NOT Busy
			pthread_mutex_unlock(&wdp->wd_worker_thread_target_sync_mutex);
			break;
		}

		// Set up the task for the Worker Thread
		xdd_target_pass_task_setup(wdp);

		// Release the Worker Thread to let it start working on this task.
		// This effectively causes the I/O operation to be issued.
		xdd_barrier(&wdp->wd_thread_targetpass_wait_for_task_barrier,&tdp->td_occupant,0);

	} // End of WHILE loop that transfers data for a single pass
//
/////////////////////////////// Loop Ends Here /////////////////////////////////

	// Check to see if we've been canceled - if so, we need to leave
	if (xgp->canceled) {
		fprintf(xgp->errout,"\n%s: xdd_target_pass_loop: Target %d: ERROR: Canceled!\n",
			xgp->progname,
			tdp->td_target_number);
		return;
	}
	// Wait for all Worker Threads to complete their most recent task
	// The easiest way to do this is to get the Worker Thread pointer for each
	// Worker Thread specifically and then reset it's "busy" bit to 0.
	for (q = 0; q < tdp->td_queue_depth; q++) {
		wdp = xdd_get_specific_worker_thread(tdp,q);
		pthread_mutex_lock(&wdp->wd_worker_thread_target_sync_mutex);
		wdp->wd_worker_thread_target_sync &= ~WTSYNC_BUSY; // Mark this Worker Thread NOT Busy
		tdp->td_any_worker_thread_available++;
		pthread_mutex_unlock(&wdp->wd_worker_thread_target_sync_mutex);
	}
	if (tdp->td_counters.tc_current_io_status != 0)
		planp->target_errno[tdp->td_target_number] = XDD_RETURN_VALUE_IOERROR;

	return;
} // End of xdd_target_pass_loop()

/*----------------------------------------------------------------------------*/
/* xdd_target_pass_task_setup() - This subroutine will set up the task info for an I/O
 */
void
xdd_target_pass_task_setup(worker_data_t *wdp) {
	target_data_t	*tdp;
	xdd_ts_tte_t	*ttep;

	tdp = wdp->wd_tdp;
	// Assign an IO task to this worker thread
	wdp->wd_task.task_request = TASK_REQ_IO;

	// Get the most recent File Descriptor in case it changed...
	wdp->wd_task.task_file_desc = tdp->td_file_desc;

	// Set the Operation Type
	if (tdp->td_seekhdr.seeks[tdp->td_counters.tc_current_op_number].operation == SO_OP_WRITE) { // Write Operation
		wdp->wd_task.task_op_type = TASK_OP_TYPE_WRITE;
		wdp->wd_task.task_op_string = "WRITE";
	} else if (tdp->td_seekhdr.seeks[tdp->td_counters.tc_current_op_number].operation == SO_OP_READ) { // READ Operation
		wdp->wd_task.task_op_type = TASK_OP_TYPE_READ;
		wdp->wd_task.task_op_string = "READ";
	} else {
		wdp->wd_task.task_op_type = TASK_OP_TYPE_NOOP;
		wdp->wd_task.task_op_string = "NOOP";
	}

	// Figure out the transfer size to use for this I/O
	if (tdp->td_current_bytes_remaining < (uint64_t)tdp->td_xfer_size)
		wdp->wd_task.task_xfer_size = tdp->td_current_bytes_remaining;
	else wdp->wd_task.task_xfer_size = tdp->td_xfer_size;

	// Set the location to seek to
	wdp->wd_task.task_byte_offset = tdp->td_counters.tc_current_byte_offset;

	// Remember the operation number for this target
	wdp->wd_task.task_op_number = tdp->td_counters.tc_current_op_number;

   	// If time stamping is on then assign a time stamp entry to this Worker Thread
   	if ((tdp->td_ts_table.ts_options & (TS_ON|TS_TRIGGERED))) {
		wdp->wd_ts_entry = tdp->td_ts_table.ts_current_entry;
		ttep = &tdp->td_ts_table.ts_hdrp->tsh_tte[wdp->wd_ts_entry];
		tdp->td_ts_table.ts_current_entry++;
		if (tdp->td_ts_table.ts_options & TS_ONESHOT) { // Check to see if we are at the end of the ts buffer
			if (tdp->td_ts_table.ts_current_entry == tdp->td_ts_table.ts_size)
				tdp->td_ts_table.ts_options &= ~TS_ON; // Turn off Time Stamping now that we are at the end of the time stamp buffer
		} else if (tdp->td_ts_table.ts_options & TS_WRAP) {
			tdp->td_ts_table.ts_current_entry = 0; // Wrap to the beginning of the time stamp buffer
		}
		ttep->tte_pass_number = tdp->td_counters.tc_pass_number;
		ttep->tte_worker_thread_number = wdp->wd_worker_number;
		ttep->tte_thread_id = wdp->wd_thread_id;
		ttep->tte_op_type = wdp->wd_task.task_op_type;
		ttep->tte_op_number = wdp->wd_task.task_op_number;
		ttep->tte_byte_offset = wdp->wd_task.task_byte_offset;
	}
if (xgp->global_options & GO_DEBUG_TASK) fprintf(stderr,"DEBUG_TASK: %lld: xdd_target_pass_task_setup_src: Target: %d: Worker: %d: task_request: 0x%x: file_desc: %d: datap: %p: op_type: %d, op_string: %s: op_number: %lld: xfer_size: %d, byte_offset: %lld\n ", (long long int)pclk_now(),tdp->td_target_number,wdp->wd_worker_number,wdp->wd_task.task_request,wdp->wd_task.task_file_desc,wdp->wd_task.task_datap,wdp->wd_task.task_op_type,wdp->wd_task.task_op_string,(unsigned long long int)wdp->wd_task.task_op_number,(int)wdp->wd_task.task_xfer_size,(long long int)wdp->wd_task.task_byte_offset);
	// Update the pointers/counters in the Target Data Struct to get
	// ready for the next I/O operation
	tdp->td_counters.tc_current_byte_offset += wdp->wd_task.task_xfer_size;
	tdp->td_counters.tc_current_op_number++;
	tdp->td_current_bytes_issued += wdp->wd_task.task_xfer_size;
	tdp->td_current_bytes_remaining -= wdp->wd_task.task_xfer_size;

} // End of xdd_target_pass_task_setup()

/*
 * Local variables:
 *  indent-tabs-mode: t
 *  default-tab-width: 4
 *  c-indent-level: 4
 *  c-basic-offset: 4
 * End:
 *
 * vim: ts=4 sts=4 sw=4 noexpandtab
 */


text:
@implementation NSIndexPath (Signedness)

- (NSUInteger)unsignedSection {
    return (NSUInteger)self.section;
}

- (NSUInteger)unsignedRow {
    return (NSUInteger)self.row;
}

@end


text:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<meta name="collection" content="api">

<TITLE>
JobMediaSheetsCompleted (Java 2 Platform SE 5.0)
</TITLE>

<META NAME="keywords" CONTENT="javax.print.attribute.standard.JobMediaSheetsCompleted class">
<META NAME="keywords" CONTENT="equals()">
<META NAME="keywords" CONTENT="getCategory()">
<META NAME="keywords" CONTENT="getName()">

<LINK REL ="stylesheet" TYPE="text/css" HREF="../../../../stylesheet.css" TITLE="Style">

      Overview
      Package
    Class
      Use
      Tree
      Deprecated
      Index
      Help

JavaTM 2 Platform
Standard Ed. 5.0

 PREV CLASS
 NEXT CLASS

  FRAMES
 NO FRAMES

  All Classes

  SUMMARY: NESTED | FIELD | CONSTR | METHOD

DETAIL: FIELD | CONSTR | METHOD

javax.print.attribute.standard

Class JobMediaSheetsCompleted

java.lang.Object
  javax.print.attribute.IntegerSyntax
      javax.print.attribute.standard.JobMediaSheetsCompleted

All Implemented Interfaces:
Serializable, Cloneable, Attribute, PrintJobAttribute

public final class JobMediaSheetsCompleted
extends IntegerSyntax
implements PrintJobAttribute

Class JobMediaSheetsCompleted is an integer valued printing attribute class
 that specifies the number of media sheets which have completed marking and
 stacking for the entire job so far, whether those sheets have been processed
 on one side or on both.

 The JobMediaSheetsCompleted attribute describes the progress of the job. This
 attribute is intended to be a counter. That is, the JobMediaSheetsCompleted
 value for a job that has not started processing must be 0. When the job's
 JobState is PROCESSING or PROCESSING_STOPPED, the
 JobMediaSheetsCompleted value is intended to increase as the job is
 processed; it indicates the amount of the job that has been processed at the
 time the Print Job's attribute set is queried or at the time a print job
 event is reported. When the job enters the COMPLETED, CANCELED, or ABORTED
 states, the JobMediaSheetsCompleted value is the final value for the job.

 IPP Compatibility: The integer value gives the IPP integer value. The
 category name returned by getName() gives the IPP attribute
 name.

See Also:
JobMediaSheets,
JobMediaSheetsSupported,
JobKOctetsProcessed,
JobImpressionsCompleted,
Serialized Form

Constructor Summary

JobMediaSheetsCompleted(int value)

          Construct a new job media sheets completed attribute with the given
 integer value.

Method Summary

 boolean
equals(Object object)

          Returns whether this job media sheets completed attribute is equivalent
 to the passed in object.

 Class<? extends Attribute>
getCategory()

          Get the printing attribute class which is to be used as the "category"
 for this printing attribute value.

 String
getName()

          Get the name of the category of which this attribute value is an
 instance.

Methods inherited from class javax.print.attribute.IntegerSyntax

getValue, hashCode, toString

Methods inherited from class java.lang.Object

clone, finalize, getClass, notify, notifyAll, wait, wait, wait

Constructor Detail

JobMediaSheetsCompleted

public JobMediaSheetsCompleted(int value)

Construct a new job media sheets completed attribute with the given
 integer value.

Parameters:
value - Integer value.

Throws:

IllegalArgumentException - (Unchecked exception) Thrown if value is less than 0.

Method Detail

equals

public boolean equals(Object object)

Returns whether this job media sheets completed attribute is equivalent
 to the passed in object. To be equivalent, all of the following
 conditions must be true:

 object is not null.

 object is an instance of class JobMediaSheetsCompleted.

 This job media sheets completed attribute's value and
 object's value are equal.

Overrides:
equals in class IntegerSyntax

Parameters:
object - Object to compare to.

Returns:
True if object is equivalent to this job media
          sheets completed attribute, false otherwise.
See Also:
Object.hashCode(),
Hashtable

getCategory

public final Class<? extends Attribute> getCategory()

Get the printing attribute class which is to be used as the "category"
 for this printing attribute value.

 For class JobMediaSheetsCompleted, the category is class
 JobMediaSheetsCompleted itself.

Specified by:
getCategory in interface Attribute

Returns:
Printing attribute class (category), an instance of class
          java.lang.Class.

getName

public final String getName()

Get the name of the category of which this attribute value is an
 instance.

 For class JobMediaSheetsCompleted, the category name is
 "job-media-sheets-completed".

Specified by:
getName in interface Attribute

Returns:
Attribute category name.

      Overview
      Package
    Class
      Use
      Tree
      Deprecated
      Index
      Help

JavaTM 2 Platform
Standard Ed. 5.0

 PREV CLASS
 NEXT CLASS

  FRAMES
 NO FRAMES

  All Classes

  SUMMARY: NESTED | FIELD | CONSTR | METHOD

DETAIL: FIELD | CONSTR | METHOD

Submit a bug or feature
For further API reference and developer documentation, see Java 2 SDK SE Developer Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 2004, 2010 Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.


text:
> tl;dr Make sure that you understand the code before using this.  Use at your
> own risk!

Use `sensors` or `cat /proc/acpi/ibm/thermal` to see your systems current
thermal state.

Make sure that

    cat /proc/acpi/ibm/

lists the `watchdog` and `level` command.  If it does not, enable them with:

    sudo -c 'echo options thinkpad_acpi experimental=1 fan_control=1 > /etc/modprobe.d/thinkpad_acpi.conf'
    subo reboot


text:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SharpTools.Convert
{
    /// <summary>
    /// 支持类型之间的安全转换,以及DataTable,Xml,Json之间的互相转换
    /// </summary>
    public class ConvertManager
    {

    }
}


text:
using System;

namespace BattleField
{
    class Battlefield
    {
        //poLeto za biTka

        private char[,] gameField;
        public Battlefield()
        {
            gameField = null;
        }
        public void Start()
        {

            Console.WriteLine(@"Welcome to ""Battle Field"" game. ");
            int size = 0;
            string readBuffer = null;

            Console.Write("Enter battle field size: n=");
            readBuffer = Console.ReadLine();

            while (!int.TryParse(readBuffer, out size))
            {
                Console.WriteLine("Wrong format!");
                Console.Write("Enter battle field size: n=");

            }

            if (size > 10 || size <= 0)
            { Start(); }
            else{

                gameField = GameServices.GenerateField(size);
                StartInteraction();
            }
        }

        private void StartInteraction()
        {
            string readBuffer = null;
            int blownMines = 0;
            for (int i = 0; i < 50; i++)
                Console.WriteLine();

            while (GameServices.ContainsMines(gameField))
            {
                GameServices.PokajiMiRezultata(gameField);
                Console.Write("Please enter coordinates: ");
                readBuffer = Console.ReadLine();
                Mine mineToBlow =

                    GameServices.ExtractMineFromString(readBuffer);

                while (mineToBlow == null)
                {
                    Console.Write("Please enter coordinates: ");
                    readBuffer = Console.ReadLine();
                    mineToBlow = GameServices.ExtractMineFromString(readBuffer);
                }

                if (!GameServices.IsValidMove(gameField, mineToBlow.X, mineToBlow.Y))
                {
                    Console.WriteLine("Invalid move!");
                    continue;
                }

                GameServices.Гърми(gameField, mineToBlow);
                blownMines++;
            }

            GameServices.PokajiMiRezultata(gameField);
            Console.WriteLine("Game over. Detonated mines: {0}", blownMines);
        }
    }
}


text:
ACCEPTED

#### According to
The Catalogue of Life, 3rd January 2011

#### Published in
null

#### Original name
Stellaria muscorum Fassett

### Remarks
null


text:
<!DOCTYPE html>

  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>B2B Email Course</title>

  <link rel="stylesheet" href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
  <link rel="stylesheet" href="css/EmailCourse.css"/>

5 Conversion Tricks You're Missing

Explain in the Subheadline something that Supports the Headline

Get the Free
 5-day Email Course

            <form>

                First Name *
                <input type="text" name="first_name" class="form-control"/>

                Email *
                <input type="text" name="email" class="form-control"/>

              <button class="btn btn-lg btn-block">Send the eCourse</button>

Tell them you're not going to spam them and point them to your privacy policy.

            </form>

Each Day You will Learn Something New

Intro description for Web App goes here. It tells people how this Web App is going to change their business. For the better of course. Not the worse. People wouldn't buy a Web App like that.

              1

Here's what they Learn in Lesson 1

This is the first point about your report that's going to get them itching to download. If it doesn't, be more pursuasive.

              2

A Bit About What they Learn in Lesson 2

This is the first point about your report that's going to get them itching to download. If it doesn't, be more pursuasive.

              3

And Then There's Lesson 3

This is the first point about your report that's going to get them itching to download. If it doesn't, be more pursuasive.

              4

Why Not Move onto Lesson 4

This is the first point about your report that's going to get them itching to download. If it doesn't, be more pursuasive.

              5

By this time They're Brilliant Lesson 5

This is the first point about your report that's going to get them itching to download. If it doesn't, be more pursuasive.

©2014 Your Company, Inc.


text:
module MudPie

  def self.build_render_chain(source_path)
    url = source_path.to_s.sub(%r{^[^/]+}, '')
    chain = SourceLink.new(url, source_path)
    while r = Renderer.for_extname(File.extname(url))
      url = url.chomp(File.extname(url))
      chain = RenderLink.new(url, r, chain)
    end
    chain
  end

  class ChainLink

    def static?
      false
    end

    def meta
      context = PageContext.new(nil)
      read_meta(context)
      context
    end

    def render(context)
    end

  end

  class SourceLink < ChainLink

    attr_reader :url

    def initialize(url, path)
      @url = url
      @path = path
    end

    def static?
      true
    end

    def read_meta(context)
    end

    def open
      f = File.open(@path, 'r')
      yield f
    ensure
      f.close
    end

    def render(context)
      @path.read
    end

    def to_s
      "[SOURCE:#{@path}]"
    end

  end

  class RenderLink < ChainLink

    attr_reader :url

    def initialize(url, renderer, link)
      @url = url
      @renderer = renderer
      @link = link
    end

    def read_meta(context)
      @link.read_meta(context)
      if @link.respond_to?(:open)
        @link.open do |f|
          @renderer.read_meta(context, f)
        end
      end
    end

    def render(context)
      input = StringIO.new @link.render(context)
      output = StringIO.new
      @renderer.render(context, input, output)
      output.rewind
      output.read
    end

    def to_s
      "[RENDER:#{@renderer.class}:#{@link}]"
    end

  end

  class LayoutLink < ChainLink

    def initialize(layout, link)
      @layout = layout
      @link = link
      @renderer = MudPie::Renderer.for_extname(@layout.source_path.extname)
      raise "No renderer for layout #{@layout.source_path}" unless @renderer
      @template = @renderer.load_template(@layout.source_path.to_s)
    end

    def url
      @link.url
    end

    def render(context)
      content = @link.render(context)
      b = context.get_binding { content }
      @renderer.render_template(@template, b)
    end

    def to_s
      "[LAYOUT:#{@layout.url}:#{@link}]"
    end

  end

end


text:
package com.google.cloud.spark.bigquery;

import com.google.common.base.Preconditions;
import org.apache.avro.Conversions;
import org.apache.avro.LogicalTypes;
import org.apache.avro.Schema;
import org.apache.avro.SchemaBuilder;
import org.apache.avro.generic.GenericData;
import org.apache.avro.util.Utf8;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSqlUtils;
import org.apache.spark.sql.catalyst.InternalRow;
import org.apache.spark.sql.catalyst.expressions.SpecializedGetters;
import org.apache.spark.sql.catalyst.expressions.UnsafeArrayData;
import org.apache.spark.sql.catalyst.expressions.UnsafeRow;
import org.apache.spark.sql.catalyst.util.ArrayData;
import org.apache.spark.sql.types.ArrayType;
import org.apache.spark.sql.types.BinaryType;
import org.apache.spark.sql.types.BooleanType;
import org.apache.spark.sql.types.ByteType;
import org.apache.spark.sql.types.DataType;
import org.apache.spark.sql.types.DateType;
import org.apache.spark.sql.types.Decimal;
import org.apache.spark.sql.types.DecimalType;
import org.apache.spark.sql.types.DoubleType;
import org.apache.spark.sql.types.FloatType;
import org.apache.spark.sql.types.IntegerType;
import org.apache.spark.sql.types.LongType;
import org.apache.spark.sql.types.MapType;
import org.apache.spark.sql.types.NullType;
import org.apache.spark.sql.types.ShortType;
import org.apache.spark.sql.types.StringType;
import org.apache.spark.sql.types.StructField;
import org.apache.spark.sql.types.StructType;
import org.apache.spark.sql.types.TimestampType;
import org.apache.spark.sql.types.UserDefinedType;
import scala.collection.mutable.IndexedSeq;

import java.math.BigDecimal;
import java.nio.ByteBuffer;
import java.util.List;
import java.util.Optional;

public class AvroSchemaConverter {

  private static final Schema NULL = Schema.create(Schema.Type.NULL);
  private static final Conversions.DecimalConversion DECIMAL_CONVERSIONS =
      new Conversions.DecimalConversion();

  public static Schema sparkSchemaToAvroSchema(StructType sparkSchema) {
    return sparkTypeToRawAvroType(sparkSchema, false, "root");
  }

  static Schema sparkTypeToRawAvroType(DataType dataType, boolean nullable, String recordName) {
    SchemaBuilder.TypeBuilder<Schema> builder = SchemaBuilder.builder();
    Schema avroType = sparkTypeToRawAvroType(dataType, recordName, builder);

    if (nullable) {
      avroType = Schema.createUnion(avroType, NULL);
    }

    return avroType;
  }

  static Schema sparkTypeToRawAvroType(
      DataType dataType, String recordName, SchemaBuilder.TypeBuilder<Schema> builder) {
    if (dataType instanceof BinaryType) {
      return builder.bytesType();
    }
    if (dataType instanceof ByteType
        || dataType instanceof ShortType
        || dataType instanceof IntegerType
        || dataType instanceof LongType) {
      return builder.longType();
    }
    if (dataType instanceof BooleanType) {
      return builder.booleanType();
    }
    if (dataType instanceof FloatType || dataType instanceof DoubleType) {
      return builder.doubleType();
    }
    if (dataType instanceof DecimalType) {
      DecimalType decimalType = (DecimalType) dataType;
      if (decimalType.precision() <= SchemaConverters.BQ_NUMERIC_PRECISION
          && decimalType.scale() <= SchemaConverters.BQ_NUMERIC_SCALE) {
        return LogicalTypes.decimal(decimalType.precision(), decimalType.scale())
            .addToSchema(builder.bytesType());
      } else {
        throw new IllegalArgumentException(
            "Decimal type is too wide to fit in BigQuery Numeric format");
      }
    }
    if (dataType instanceof StringType) {
      return builder.stringType();
    }
    if (dataType instanceof TimestampType) {
      // return builder.TIMESTAMP; FIXME: Restore this correct conversion when the Vortex
      // team adds microsecond support to their backend
      return LogicalTypes.timestampMicros().addToSchema(builder.longType());
    }
    if (dataType instanceof DateType) {
      return LogicalTypes.date().addToSchema(builder.intType());
    }
    if (dataType instanceof ArrayType) {
      return builder
          .array()
          .items(
              sparkTypeToRawAvroType(
                  ((ArrayType) dataType).elementType(),
                  ((ArrayType) dataType).containsNull(),
                  recordName));
    }
    if (dataType instanceof StructType) {
      SchemaBuilder.FieldAssembler<Schema> fieldsAssembler = builder.record(recordName).fields();
      for (StructField field : ((StructType) dataType).fields()) {
        Schema avroType = sparkTypeToRawAvroType(field.dataType(), field.nullable(), field.name());
        fieldsAssembler.name(field.name()).type(avroType).noDefault();
      }
      return fieldsAssembler.endRecord();
    }
    if (dataType instanceof UserDefinedType) {
      DataType userDefinedType = ((UserDefinedType) dataType).sqlType();
      return sparkTypeToRawAvroType(userDefinedType, recordName, builder);
    }
    if (dataType instanceof MapType) {
      throw new IllegalArgumentException(SchemaConverters.MAPTYPE_ERROR_MESSAGE);
    } else {
      throw new IllegalArgumentException("Data type not supported: " + dataType.simpleString());
    }
  }

  public static GenericData.Record sparkRowToAvroGenericData(
      InternalRow row, StructType sparkSchema, Schema avroSchema) {
    StructConverter structConverter = new StructConverter(sparkSchema, avroSchema);
    return structConverter.convert(row);
  }

  static Schema resolveNullableType(Schema avroType, boolean nullable) {
    if (nullable && avroType.getType() != Schema.Type.NULL) {
      // avro uses union to represent nullable type.
      List<Schema> fields = avroType.getTypes();
      Preconditions.checkArgument(
          fields.size() == 2, "Avro nullable filed should be represented by a union of size 2");
      Optional<Schema> actualType =
          fields.stream().filter(field -> field.getType() != Schema.Type.NULL).findFirst();
      return actualType.orElseThrow(
          () -> new IllegalArgumentException("No actual type has been found in " + avroType));
    } else {
      return avroType;
    }
  }

  static Converter createConverterFor(DataType sparkType, Schema avroType) {
    if (sparkType instanceof NullType && avroType.getType() == Schema.Type.NULL) {
      return (getter, ordinal) -> null;
    }
    if (sparkType instanceof BooleanType && avroType.getType() == Schema.Type.BOOLEAN) {
      return (getter, ordinal) -> getter.getBoolean(ordinal);
    }
    if (sparkType instanceof ByteType && avroType.getType() == Schema.Type.LONG) {
      return (getter, ordinal) -> Long.valueOf(getter.getByte(ordinal));
    }
    if (sparkType instanceof ShortType && avroType.getType() == Schema.Type.LONG) {
      return (getter, ordinal) -> Long.valueOf(getter.getShort(ordinal));
    }
    if (sparkType instanceof IntegerType && avroType.getType() == Schema.Type.LONG) {
      return (getter, ordinal) -> Long.valueOf(getter.getInt(ordinal));
    }
    if (sparkType instanceof LongType && avroType.getType() == Schema.Type.LONG) {
      return (getter, ordinal) -> getter.getLong(ordinal);
    }
    if (sparkType instanceof FloatType && avroType.getType() == Schema.Type.DOUBLE) {
      return (getter, ordinal) -> Double.valueOf(getter.getFloat(ordinal));
    }
    if (sparkType instanceof DoubleType && avroType.getType() == Schema.Type.DOUBLE) {
      return (getter, ordinal) -> getter.getDouble(ordinal);
    }
    if (sparkType instanceof DecimalType && avroType.getType() == Schema.Type.BYTES) {
      DecimalType decimalType = (DecimalType) sparkType;
      return (getter, ordinal) -> {
        BigDecimal bigDecimal = null;
        if (getter instanceof UnsafeRow || getter instanceof UnsafeArrayData) {
          // UnsafeRow mandates Datatype
          Decimal decimal = getter.getDecimal(ordinal, decimalType.precision(), decimalType.scale());
          bigDecimal = decimal.toJavaBigDecimal();
        } else {
          Object decimal = getter.get(ordinal, /* unused */ null);
          bigDecimal =
              decimal instanceof Decimal
                  ? ((Decimal) decimal).toJavaBigDecimal()
                  : (BigDecimal) decimal;
        }
        return DECIMAL_CONVERSIONS.toBytes(
            bigDecimal,
            avroType,
            LogicalTypes.decimal(decimalType.precision(), decimalType.scale()));
      };
    }
    if (sparkType instanceof StringType && avroType.getType() == Schema.Type.STRING) {
      return (getter, ordinal) -> {
        String str =
                // UnsafeRow mandates Datatype
                getter instanceof UnsafeRow || getter instanceof UnsafeArrayData
                        ? getter.getUTF8String(ordinal).toString()
                        : getter.get(ordinal, /* unused */ null).toString();
        return new Utf8(str);
      };
    }
    if (sparkType instanceof BinaryType && avroType.getType() == Schema.Type.FIXED) {
      int size = avroType.getFixedSize();
      return (getter, ordinal) -> {
        byte[] data = getter.getBinary(ordinal);
        if (data.length != size) {
          throw new IllegalArgumentException(
              String.format(
                  "Cannot write %s bytes of binary data into FIXED Type with size of %s bytes",
                  data.length, size));
        }
        return new GenericData.Fixed(avroType, data);
      };
    }
    if (sparkType instanceof BinaryType && avroType.getType() == Schema.Type.BYTES) {
      return (getter, ordinal) -> ByteBuffer.wrap(getter.getBinary(ordinal));
    }

    if (sparkType instanceof DateType && avroType.getType() == Schema.Type.INT) {
      return (getter, ordinal) -> {
        Object sparkValue =
            // UnsafeRow mandates Datatype
            getter instanceof UnsafeRow || getter instanceof UnsafeArrayData
                ? getter.getInt(ordinal)
                : getter.get(ordinal, /* unused */ null);
        return SparkBigQueryUtil.sparkDateToBigQuery(sparkValue);
      };
    }

    if (sparkType instanceof TimestampType && avroType.getType() == Schema.Type.LONG) {
      return (getter, ordinal) -> {
        Object sparkValue =
            // UnsafeRow mandates Datatype
            getter instanceof UnsafeRow || getter instanceof UnsafeArrayData
                ? getter.getLong(ordinal)
                : getter.get(ordinal, /* unused */ null);
        return SparkBigQueryUtil.sparkTimestampToBigQuery(sparkValue);
      };
    }

    if (sparkType instanceof ArrayType && avroType.getType() == Schema.Type.ARRAY) {
      DataType et = ((ArrayType) sparkType).elementType();
      boolean containsNull = ((ArrayType) sparkType).containsNull();

      Converter elementConverter =
          createConverterFor(et, resolveNullableType(avroType.getElementType(), containsNull));
      return (getter, ordinal) -> {
        ArrayData arrayData = null;
        if (getter instanceof UnsafeRow || getter instanceof UnsafeArrayData) {
          // UnsafeRow mandates DataType
          arrayData = getter.getArray(ordinal);
        } else {
          Object array = getter.get(ordinal, /* unused */ null);
          if (array instanceof IndexedSeq) {
            arrayData = ArrayData.toArrayData(array);
          } else {
            arrayData = (ArrayData) array;
          }
        }
        int len = arrayData.numElements();
        Object[] result = new Object[len];
        for (int i = 0; i < len; i++) {
          if (containsNull && arrayData.isNullAt(i)) {
            result[i] = null;
          } else {
            result[i] = elementConverter.convert(arrayData, i);
          }
        }
        // avro writer is expecting a Java Collection, so we convert it into
        // `ArrayList` backed by the specified array without data copying.
        return java.util.Arrays.asList(result);
      };
    }
    if (sparkType instanceof StructType && avroType.getType() == Schema.Type.RECORD) {
      StructType sparkStruct = (StructType) sparkType;

      StructConverter structConverter = new StructConverter(sparkStruct, avroType);
      int numFields = sparkStruct.length();
      return (getter, ordinal) -> {
        InternalRow internalRow = null;
        if (getter instanceof UnsafeRow || getter instanceof UnsafeArrayData) {
          // UnsafeRow mandates Datatype
          internalRow = getter.getStruct(ordinal, numFields);
        } else {
          Object obj = getter.get(ordinal, /* unused */ null);
          if (obj instanceof Row) {
            internalRow = SparkSqlUtils.getInstance().rowToInternalRow((Row) obj);
          } else {
            internalRow = (InternalRow) obj;
          }
        }
        return structConverter.convert(internalRow);
      };
    }
    if (sparkType instanceof UserDefinedType) {
      UserDefinedType userDefinedType = (UserDefinedType) sparkType;
      return createConverterFor(userDefinedType.sqlType(), avroType);
    }
    throw new IllegalArgumentException(
        String.format("Cannot convert Catalyst type %s to Avro type %s", sparkType, avroType));
  }

  @FunctionalInterface
  interface Converter {
    Object convert(SpecializedGetters getters, int ordinal);
  }

  static class StructConverter {
    private final StructType sparkStruct;
    private final Schema avroStruct;

    StructConverter(StructType sparkStruct, Schema avroStruct) {
      this.sparkStruct = sparkStruct;
      this.avroStruct = avroStruct;
      Preconditions.checkArgument(
          avroStruct.getType() == Schema.Type.RECORD
              && avroStruct.getFields().size() == sparkStruct.length(),
          "Cannot convert Catalyst type %s to Avro type %s.",
          sparkStruct,
          avroStruct);
    }

    GenericData.Record convert(InternalRow row) {
      int numFields = sparkStruct.length();
      Converter[] fieldConverters = new Converter[numFields];
      StructField[] sparkFields = sparkStruct.fields();
      Schema.Field[] avroFields = avroStruct.getFields().toArray(new Schema.Field[numFields]);

      GenericData.Record result = new GenericData.Record(avroStruct);

      for (int i = 0; i < numFields; i++) {
        if (row.isNullAt(i)) {
          result.put(i, null);
        } else {
          Converter fieldConverter =
              AvroSchemaConverter.createConverterFor(
                  sparkFields[i].dataType(),
                  AvroSchemaConverter.resolveNullableType(
                      avroFields[i].schema(), sparkFields[i].nullable()));
          result.put(i, fieldConverter.convert(row, i));
        }
      }
      return result;
    }
  }
}


text:
@class SamplePlugin;

static SamplePlugin *sharedPlugin;

@interface SamplePlugin : NSObject

+ (instancetype)sharedPlugin;
- (id)initWithBundle:(NSBundle *)plugin;

@property (nonatomic, strong, readonly) NSBundle* bundle;
@end


text:
from typing import Iterator, TYPE_CHECKING

from cirq import ops
from cirq.interop.quirk.cells.cell import CellMaker

if TYPE_CHECKING:
    import cirq

def generate_all_scalar_cell_makers() -> Iterator[CellMaker]:
    yield _scalar("NeGate", ops.global_phase_operation(-1))
    yield _scalar("i", ops.global_phase_operation(1j))
    yield _scalar("-i", ops.global_phase_operation(-1j))
    yield _scalar("√i", ops.global_phase_operation(1j**0.5))
    yield _scalar("√-i", ops.global_phase_operation((-1j) ** 0.5))

def _scalar(identifier: str, operation: 'cirq.Operation') -> CellMaker:
    return CellMaker(identifier, size=1, maker=lambda _: operation)


text:
class CasmSubmitPage : public TabPage
{
public:
	CasmSubmitPage(TWidget *parent);
	~CasmSubmitPage();

	void configureNotify(const TDimension &size);

	void onActivate();
	void onDeactivate();

private:
	class Data;
	Data *m_data;
};

#endif


text:
package org.uberfire.workbench.events;

import org.jboss.errai.common.client.api.annotations.MapsTo;
import org.jboss.errai.common.client.api.annotations.Portable;
import org.uberfire.backend.vfs.Path;
import org.uberfire.rpc.SessionInfo;

import static org.uberfire.commons.validation.PortablePreconditions.checkNotNull;

/**
 * An Event indicating a Resource has been updated
 */
@Portable
public class ResourceUpdatedEvent extends ResourceUpdated implements ResourceEvent {

    private Path path;
    private SessionInfo sessionInfo;

    public ResourceUpdatedEvent(@MapsTo("path") final Path path,
                                @MapsTo("message") final String message,
                                @MapsTo("sessionInfo") final SessionInfo sessionInfo) {
        super(message);
        this.path = checkNotNull("path",
                                 path);
        this.sessionInfo = checkNotNull("sessionInfo",
                                        sessionInfo);
    }

    @Override
    public Path getPath() {
        return this.path;
    }

    public SessionInfo getSessionInfo() {
        return sessionInfo;
    }

    @Override
    public String toString() {
        return "ResourceUpdatedEvent [path=" + path + ", sessionInfo=" + sessionInfo + "]";
    }
}


text:
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Runtime.CompilerServices;

namespace System.Diagnostics
{
    internal static class Precondition
    {
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static void Require(bool condition)
        {
            if (!condition)
            {
                Fail();
            }
        }

        private static void Fail()
        {
            if (Debugger.IsAttached)
            {
                Debugger.Break();
            }
            throw new Failure();
        }

        public sealed class Failure : Exception
        {
            static string s_message = "precondition failed";
            internal Failure() : base(s_message) { }
        }
    }
}


text:
#import <Foundation/Foundation.h>
#import "TTransport.h"

NS_ASSUME_NONNULL_BEGIN

@interface TMemoryBuffer : NSObject <TTransport>

-(NSData *) buffer;

-(id) initWithData:(NSData *)data;

-(id) initWithDataNoCopy:(NSMutableData *)data;

@end

NS_ASSUME_NONNULL_END


text:
@interface ApiStop : ApiData

@property (  copy, nonatomic) NSNumber *order;
@property (  copy, nonatomic) NSNumber *sequence;
@property (  copy, nonatomic) NSString *ID;
@property (  copy, nonatomic) NSString *name;
@property (  copy, nonatomic) NSString *station;
@property (  copy, nonatomic) NSString *station_name;
// for stopsbylocation - stop's distance in miles
// from location given in original request
@property (  copy, nonatomic) NSNumber *distance;

@property (assign, nonatomic, readonly) CLLocationCoordinate2D location;
//- (CLLocationCoordinate2D) location;

@end

// ----------------------------------------------------------------------

@interface ApiStopsByRoute : ApiData

@property (  copy, nonatomic) NSString *routeID; // from request
@property (strong, nonatomic) NSArray *directions;

+ (void)get4route:(NSString *)routeID
		  success:(void(^)(ApiStopsByRoute *stops))success
		  failure:(void(^)(NSError *error))failure;
- (void)update_success:(void(^)(ApiStopsByRoute *stops))success
			   failure:(void(^)(NSError *error))failure;
@end

// ----------------------------------------------------------------------

@interface ApiStopsByLocation : ApiData
@property (assign, nonatomic) CLLocationCoordinate2D location; // from request
@property (strong, nonatomic) NSArray *stops;

+ (void)get4location:(CLLocationCoordinate2D)location
			 success:(void(^)(ApiStopsByLocation *stops))success
			 failure:(void(^)(NSError *error))failure;
// a real app would never 'update' an existing stops-by-location object
// as a user's location is infinitely variable in two dimensions
- (void)update_success:(void(^)(ApiStopsByLocation *stops))success
			   failure:(void(^)(NSError *error))failure;
@end

// ----------------------------------------------------------------------