text:
from typing import Optional, Callable, Tuple, Dict, Any, List, NamedTuple
import functools
import pytest
import eagerpy as ep

import foolbox
import foolbox as fbn

ModelAndData = Tuple[fbn.Model, ep.Tensor, ep.Tensor]
CallableModelAndDataAndDescription = NamedTuple(
    "CallableModelAndDataAndDescription",
    [
        ("model_fn", Callable[..., ModelAndData]),
        ("real", bool),
        ("low_dimensional_input", bool),
    ],
)
ModeAndDataAndDescription = NamedTuple(
    "ModeAndDataAndDescription",
    [("model_and_data", ModelAndData), ("real", bool), ("low_dimensional_input", bool)],
)

models: Dict[str, CallableModelAndDataAndDescription] = {}
models_for_attacks: List[str] = []

def pytest_addoption(parser: Any) -> None:
    parser.addoption("--backend")
    parser.addoption("--skipslow", action="store_true")

@pytest.fixture(scope="session")
def dummy(request: Any) -> ep.Tensor:
    backend: Optional[str] = request.config.option.backend
    if backend is None or backend == "none":
        pytest.skip()
        assert False
    return ep.utils.get_dummy(backend)

def register(
    backend: str,
    *,
    real: bool = False,
    low_dimensional_input: bool = False,
    attack: bool = True,
) -> Callable[[Callable], Callable]:
    def decorator(f: Callable[[Any], ModelAndData]) -> Callable[[Any], ModelAndData]:
        @functools.wraps(f)
        def model(request: Any) -> ModelAndData:
            if request.config.option.backend != backend:
                pytest.skip()
            return f(request)

        global models
        global real_models

        models[model.__name__] = CallableModelAndDataAndDescription(
            model_fn=model, real=real, low_dimensional_input=low_dimensional_input
        )
        if attack:
            models_for_attacks.append(model.__name__)
        return model

    return decorator

def pytorch_simple_model(
    device: Any = None, preprocessing: fbn.types.Preprocessing = None
) -> ModelAndData:
    import torch

    class Model(torch.nn.Module):
        def forward(self, x: torch.Tensor) -> torch.Tensor:
            x = torch.mean(x, 3)
            x = torch.mean(x, 2)
            return x

    model = Model().eval()
    bounds = (0, 1)
    fmodel = fbn.PyTorchModel(
        model, bounds=bounds, device=device, preprocessing=preprocessing
    )

    x, _ = fbn.samples(fmodel, dataset="imagenet", batchsize=8)
    x = ep.astensor(x)
    y = fmodel(x).argmax(axis=-1)
    return fmodel, x, y

@register("pytorch", low_dimensional_input=True)
def pytorch_simple_model_default(request: Any) -> ModelAndData:
    return pytorch_simple_model()

@register("pytorch", low_dimensional_input=True)
def pytorch_simple_model_default_flip(request: Any) -> ModelAndData:
    return pytorch_simple_model(preprocessing=dict(flip_axis=-3))

@register("pytorch", attack=False, low_dimensional_input=True)
def pytorch_simple_model_default_cpu_native_tensor(request: Any) -> ModelAndData:
    import torch

    mean = 0.05 * torch.arange(3).float()
    std = torch.ones(3) * 2
    return pytorch_simple_model("cpu", preprocessing=dict(mean=mean, std=std, axis=-3))

@register("pytorch", attack=False, low_dimensional_input=True)
def pytorch_simple_model_default_cpu_eagerpy_tensor(request: Any) -> ModelAndData:
    mean = 0.05 * ep.torch.arange(3).float32()
    std = ep.torch.ones(3) * 2
    return pytorch_simple_model("cpu", preprocessing=dict(mean=mean, std=std, axis=-3))

@register("pytorch", attack=False, low_dimensional_input=True)
def pytorch_simple_model_string(request: Any) -> ModelAndData:
    return pytorch_simple_model("cpu")

@register("pytorch", attack=False, low_dimensional_input=True)
def pytorch_simple_model_object(request: Any) -> ModelAndData:
    import torch

    return pytorch_simple_model(torch.device("cpu"))

@register("pytorch", real=True, low_dimensional_input=True)
def pytorch_mnist(request: Any) -> ModelAndData:
    fmodel = fbn.zoo.ModelLoader.get().load(
        "examples/zoo/mnist/", module_name="foolbox_model"
    )
    x, y = fbn.samples(fmodel, dataset="mnist", batchsize=8)
    x = ep.astensor(x)
    y = ep.astensor(y)
    return fmodel, x, y

@register("pytorch", real=True)
def pytorch_shufflenetv2(request: Any) -> ModelAndData:
    if request.config.option.skipslow:
        pytest.skip()

    import torchvision.models as models

    model = models.shufflenet_v2_x0_5(pretrained=True).eval()
    preprocessing = dict(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225], axis=-3)
    fmodel = fbn.PyTorchModel(model, bounds=(0, 1), preprocessing=preprocessing)

    x, y = fbn.samples(fmodel, dataset="imagenet", batchsize=8)
    x = ep.astensor(x)
    y = ep.astensor(y)
    return fmodel, x, y

def tensorflow_simple_sequential(
    device: Optional[str] = None, preprocessing: fbn.types.Preprocessing = None
) -> ModelAndData:
    import tensorflow as tf

    with tf.device(device):
        model = tf.keras.Sequential()
        model.add(tf.keras.layers.GlobalAveragePooling2D())
    bounds = (0, 1)
    fmodel = fbn.TensorFlowModel(
        model, bounds=bounds, device=device, preprocessing=preprocessing
    )

    x, _ = fbn.samples(fmodel, dataset="cifar10", batchsize=8)
    x = ep.astensor(x)
    y = fmodel(x).argmax(axis=-1)
    return fmodel, x, y

@register("tensorflow", low_dimensional_input=True)
def tensorflow_simple_sequential_cpu(request: Any) -> ModelAndData:
    return tensorflow_simple_sequential("cpu", None)

@register("tensorflow", low_dimensional_input=True)
def tensorflow_simple_sequential_native_tensors(request: Any) -> ModelAndData:
    import tensorflow as tf

    mean = tf.zeros(1)
    std = tf.ones(1) * 255.0
    return tensorflow_simple_sequential("cpu", dict(mean=mean, std=std))

@register("tensorflow", low_dimensional_input=True)
def tensorflow_simple_sequential_eagerpy_tensors(request: Any) -> ModelAndData:
    mean = ep.tensorflow.zeros(1)
    std = ep.tensorflow.ones(1) * 255.0
    return tensorflow_simple_sequential("cpu", dict(mean=mean, std=std))

@register("tensorflow", low_dimensional_input=True)
def tensorflow_simple_subclassing(request: Any) -> ModelAndData:
    import tensorflow as tf

    class Model(tf.keras.Model):  # type: ignore
        def __init__(self) -> None:
            super().__init__()
            self.pool = tf.keras.layers.GlobalAveragePooling2D()

        def call(self, x: tf.Tensor) -> tf.Tensor:  # type: ignore
            x = self.pool(x)
            return x

    model = Model()
    bounds = (0, 1)
    fmodel = fbn.TensorFlowModel(model, bounds=bounds)

    x, _ = fbn.samples(fmodel, dataset="cifar10", batchsize=8)
    x = ep.astensor(x)
    y = fmodel(x).argmax(axis=-1)
    return fmodel, x, y

@register("tensorflow")
def tensorflow_simple_functional(request: Any) -> ModelAndData:
    import tensorflow as tf

    channels = 3
    h = w = 224
    data_format = tf.keras.backend.image_data_format()
    shape = (channels, h, w) if data_format == "channels_first" else (h, w, channels)
    x = x_ = tf.keras.Input(shape=shape)
    x = tf.keras.layers.GlobalAveragePooling2D()(x)
    model = tf.keras.Model(inputs=x_, outputs=x)
    bounds = (0, 1)
    fmodel = fbn.TensorFlowModel(model, bounds=bounds)

    x, _ = fbn.samples(fmodel, dataset="imagenet", batchsize=8)
    x = ep.astensor(x)
    y = fmodel(x).argmax(axis=-1)
    return fmodel, x, y

@register("tensorflow", real=True)
def tensorflow_mobilenetv3(request: Any) -> ModelAndData:
    if request.config.option.skipslow:
        pytest.skip()

    import tensorflow as tf

    model = tf.keras.applications.MobileNetV3Small(
        weights="imagenet", minimalistic=True
    )
    fmodel = fbn.TensorFlowModel(
        model,
        bounds=(0, 255),
        preprocessing=None,
    )

    x, y = fbn.samples(fmodel, dataset="imagenet", batchsize=8)
    x = ep.astensor(x)
    y = ep.astensor(y)
    return fmodel, x, y

@register("tensorflow", real=True)
def tensorflow_resnet50(request: Any) -> ModelAndData:
    if request.config.option.skipslow:
        pytest.skip()

    import tensorflow as tf

    if not tf.test.is_gpu_available():
        pytest.skip("ResNet50 test too slow without GPU")

    model = tf.keras.applications.ResNet50(weights="imagenet")
    preprocessing = dict(flip_axis=-1, mean=[104.0, 116.0, 123.0])  # RGB to BGR
    fmodel = fbn.TensorFlowModel(model, bounds=(0, 255), preprocessing=preprocessing)

    x, y = fbn.samples(fmodel, dataset="imagenet", batchsize=8)
    x = ep.astensor(x)
    y = ep.astensor(y)
    return fmodel, x, y

@register("jax", low_dimensional_input=True)
def jax_simple_model(request: Any) -> ModelAndData:
    import jax

    def model(x: Any) -> Any:
        return jax.numpy.mean(x, axis=(1, 2))

    bounds = (0, 1)
    fmodel = fbn.JAXModel(model, bounds=bounds)

    x, _ = fbn.samples(
        fmodel, dataset="cifar10", batchsize=8, data_format="channels_last"
    )
    x = ep.astensor(x)
    y = fmodel(x).argmax(axis=-1)
    return fmodel, x, y

@register("numpy")
def numpy_simple_model(request: Any) -> ModelAndData:
    class Model:
        def __call__(self, inputs: Any) -> Any:
            return inputs.mean(axis=(2, 3))

    model = Model()
    with pytest.raises(ValueError):
        fbn.NumPyModel(model, bounds=(0, 1), data_format="foo")

    fmodel = fbn.NumPyModel(model, bounds=(0, 1))
    with pytest.raises(ValueError, match="data_format"):
        x, _ = fbn.samples(fmodel, dataset="imagenet", batchsize=8)

    fmodel = fbn.NumPyModel(model, bounds=(0, 1), data_format="channels_first")
    with pytest.warns(UserWarning, match="returning NumPy arrays"):
        x, _ = fbn.samples(fmodel, dataset="imagenet", batchsize=8)

    x = ep.astensor(x)
    y = fmodel(x).argmax(axis=-1)
    return fmodel, x, y

@pytest.fixture(scope="session", params=list(models.keys()))
def fmodel_and_data_ext(request: Any) -> ModeAndDataAndDescription:
    global models
    model_description = models[request.param]
    model_and_data = model_description.model_fn(request)
    return ModeAndDataAndDescription(model_and_data, *model_description[1:])

@pytest.fixture(scope="session", params=models_for_attacks)
def fmodel_and_data_ext_for_attacks(request: Any) -> ModeAndDataAndDescription:
    global models
    model_description = models[request.param]
    model_and_data = model_description.model_fn(request)
    return ModeAndDataAndDescription(model_and_data, *model_description[1:])

@pytest.fixture(scope="session")
def fmodel_and_data(fmodel_and_data_ext: ModeAndDataAndDescription) -> ModelAndData:
    return fmodel_and_data_ext.model_and_data


text:
<!DOCTYPE html>

    <meta charset="utf-8">
    <meta http-equiv="Content-Security-Policy"/>
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
    <title></title>

    <link rel="stylesheet" href="lib/angular-chart.js/dist/angular-chart.css">
    <link href="lib/ionic/css/ionic.css" rel="stylesheet">

    <link href="css/ionicons.min.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet">

        <ion-nav-bar class="bar-self">

        </ion-nav-bar>
        <ion-nav-view></ion-nav-view>

redpajama_set_name: RedPajamaGithub


text:
package no.knowit.demo.ougn.p78;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;

import static org.hamcrest.Matchers.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class ApplicationTests {

	@Autowired
	private MockMvc mockMvc;

	@Autowired
	private PersonRepository personRepository;

	@Before
	public void deleteAllBeforeTests() throws Exception {
		personRepository.deleteAll();
	}

	@Test
	public void shouldReturnRepositoryIndex() throws Exception {

		mockMvc.perform(get("/")).andDo(print()).andExpect(status().isOk()).andExpect(
				jsonPath("$._links.people").exists());
	}

	@Test
	public void shouldCreateEntity() throws Exception {

		mockMvc.perform(post("/people").content(
				"{\"firstName\": \"Frodo\", \"lastName\":\"Baggins\"}")).andExpect(
						status().isCreated()).andExpect(
								header().string("Location", containsString("people/")));
	}

	@Test
	public void shouldRetrieveEntity() throws Exception {

		MvcResult mvcResult = mockMvc.perform(post("/people").content(
				"{\"firstName\": \"Frodo\", \"lastName\":\"Baggins\"}")).andExpect(
						status().isCreated()).andReturn();

		String location = mvcResult.getResponse().getHeader("Location");
		mockMvc.perform(get(location)).andExpect(status().isOk()).andExpect(
				jsonPath("$.firstName").value("Frodo")).andExpect(
						jsonPath("$.lastName").value("Baggins"));
	}

	@Test
	public void shouldQueryEntity() throws Exception {

		mockMvc.perform(post("/people").content(
				"{ \"firstName\": \"Frodo\", \"lastName\":\"Baggins\"}")).andExpect(
						status().isCreated());

		mockMvc.perform(
				get("/people/search/findByLastName?name={name}", "Baggins")).andExpect(
						status().isOk()).andExpect(
								jsonPath("$._embedded.people[0].firstName").value(
										"Frodo"));
	}

	@Test
	public void shouldUpdateEntity() throws Exception {

		MvcResult mvcResult = mockMvc.perform(post("/people").content(
				"{\"firstName\": \"Frodo\", \"lastName\":\"Baggins\"}")).andExpect(
						status().isCreated()).andReturn();

		String location = mvcResult.getResponse().getHeader("Location");

		mockMvc.perform(put(location).content(
				"{\"firstName\": \"Bilbo\", \"lastName\":\"Baggins\"}")).andExpect(
						status().isNoContent());

		mockMvc.perform(get(location)).andExpect(status().isOk()).andExpect(
				jsonPath("$.firstName").value("Bilbo")).andExpect(
						jsonPath("$.lastName").value("Baggins"));
	}

	@Test
	public void shouldPartiallyUpdateEntity() throws Exception {

		MvcResult mvcResult = mockMvc.perform(post("/people").content(
				"{\"firstName\": \"Frodo\", \"lastName\":\"Baggins\"}")).andExpect(
						status().isCreated()).andReturn();

		String location = mvcResult.getResponse().getHeader("Location");

		mockMvc.perform(
				patch(location).content("{\"firstName\": \"Bilbo Jr.\"}")).andExpect(
						status().isNoContent());

		mockMvc.perform(get(location)).andExpect(status().isOk()).andExpect(
				jsonPath("$.firstName").value("Bilbo Jr.")).andExpect(
						jsonPath("$.lastName").value("Baggins"));
	}

	@Test
	public void shouldDeleteEntity() throws Exception {

		MvcResult mvcResult = mockMvc.perform(post("/people").content(
				"{ \"firstName\": \"Bilbo\", \"lastName\":\"Baggins\"}")).andExpect(
						status().isCreated()).andReturn();

		String location = mvcResult.getResponse().getHeader("Location");
		mockMvc.perform(delete(location)).andExpect(status().isNoContent());

		mockMvc.perform(get(location)).andExpect(status().isNotFound());
	}
}


text:
package org.archive.wayback.exception;

import java.util.Iterator;

import org.archive.wayback.core.CaptureSearchResult;
import org.archive.wayback.core.CaptureSearchResults;

public abstract class SpecificCaptureReplayException extends WaybackException {
	protected CaptureSearchResults results = null;
	protected CaptureSearchResult result = null;
	protected CaptureSearchResult previous = null;
	protected CaptureSearchResult next = null;

	protected Exception origException;

	public SpecificCaptureReplayException(String message) {
		super(message);
	}

	public SpecificCaptureReplayException(String message, String title,
			String details) {
		super(message, title, details);
	}

	public SpecificCaptureReplayException(String message, String title) {
		super(message, title);
	}
	public void setCaptureContext(CaptureSearchResults results, CaptureSearchResult result) {

        Iterator<CaptureSearchResult> itr = results.iterator();
        previous = null;
        next = null;
        this.result = result;
        while(itr.hasNext()) {
                CaptureSearchResult cur = itr.next();
                if(cur.isClosest()) {
                        break;
                }
                previous = cur;
        }
        if(itr.hasNext()) {
                next = itr.next();
        }

	}
	public CaptureSearchResults getCaptureSearchResults() {
		return results;
	}
	public CaptureSearchResult getCaptureSearchResult() {
		return result;
	}
	public CaptureSearchResult getNextResult() {
		return next;
	}
	public CaptureSearchResult getPreviousResult() {
		return previous;
	}
	public Exception getOrigException()
	{
		return origException;
	}
}


text:
package com.cellarhq;

import com.google.common.collect.ImmutableMap;
import ratpack.func.Action;
import ratpack.handling.Chain;

import static ratpack.handlebars.Template.handlebarsTemplate;

public class StaticPageChain implements Action<Chain> {
  @Override
  public void execute(Chain chain) throws Exception {
    chain
      .get("about", ctx -> ctx.render(handlebarsTemplate("about.html", ImmutableMap.of("title", "About", "pageId", "about"))))
      .get("privacy-policy", ctx -> ctx.render(handlebarsTemplate("privacy.html", ImmutableMap.of("title", "Privacy Policy", "pageId", "privacy"))))
      .get("", ctx -> ctx.render(handlebarsTemplate("terms-of-service.html", ImmutableMap.of("title", "Terms Of Service", "pageId", "term"))))
      .fileSystem("public", f -> f.files());
  }
}


text:
namespace Nz
{
	class AbstractHash;
	class ByteArray;

	template<typename F, typename Tuple> decltype(auto) Apply(F&& fn, Tuple&& t);
	template<typename O, typename F, typename Tuple> decltype(auto) Apply(O& object, F&& fn, Tuple&& t);
	template<typename T> constexpr std::size_t BitCount();
	template<typename T> ByteArray ComputeHash(HashType hash, const T& v);
	template<typename T> ByteArray ComputeHash(AbstractHash* hash, const T& v);
	template<typename T, std::size_t N> constexpr std::size_t CountOf(T(&name)[N]) noexcept;
	template<typename T> std::size_t CountOf(const T& c);
	template<typename T> void HashCombine(std::size_t& seed, const T& v);
	template<typename T> T ReverseBits(T integer);

	template<typename T>
	struct PointedType
	{
		using type = void; //< FIXME: I can't make SFINAE work
	};

	template<typename T>
	struct TypeTag {};

	inline bool Serialize(SerializationContext& context, bool value);
	inline bool Serialize(SerializationContext& context, const std::string& value);

	template<typename T>
	std::enable_if_t<std::is_arithmetic<T>::value, bool> Serialize(SerializationContext& context, T value);

	inline bool Unserialize(SerializationContext& context, bool* value);
	inline bool Unserialize(SerializationContext& context, std::string* value);

	template<typename T>
	std::enable_if_t<std::is_arithmetic<T>::value, bool> Unserialize(SerializationContext& context, T* value);
}

#include <Nazara/Core/Algorithm.inl>

#endif // NAZARA_ALGORITHM_CORE_HPP


text:
Wrapper for React Native gestureState that provides information about the type of gesture using a simple plain english API

## Install

`npm install react-native-simple-gesture --save`

## Write Code

```
import React from 'react-native';
import SimpleGesture from 'react-native-simple-gesture';
let { PanResponder, View, Text } = React;

class SuperAwesomeComponent extends React.Component {

  componentWillMount() {
    this._panResponder = PanResponder.create({
      // Only respond to movements if the gesture is a swipe up
      onMoveShouldSetPanResponder: (e, gs) => {
        let sgs = new SimpleGesture(e,gs);
        return sgs.isSwipeUp();
      }
    });
  }

  render() {
    return(
      <View { ...this._panResponder.panHandlers }>
        <Text>I'm gonna respond to swipes that go up</Text>
      </View>
    )
  }

}
```

## What You Can Do

### Relative Gesture Distance

Get the gesture distance relative to device screen size

```
let sgs = new SimpleGesture(e, gs);
console.log('Swiped ', sgs.relativeGestureDistance.x*100, '% of the screen horizontally');
console.log('Swiped ', sgs.relativeGestureDistance.y*100, '% of the screen vertically');
```

So far I've only written swipe tests. Gestures beyond swipes are next.

### Vertical Gesture Tests

* isVertical()
* isSimpleSwipeUp()
* isSwipeUp()
* isLongSwipeUp()
* isFastSwipeUp()
* isMovingUp()
* isSimpleSwipeDown()
* isSwipeDown()
* isLongSwipeDown()
* isFastSwipeDown()
* isMovingDown()

### Horizontal Gesture Tests

* isHorizontal()
* isSimpleSwipeLeft()
* isSwipeLeft()
* isLongSwipeLeft()
* isFastSwipeLeft()
* isMovingLeft()
* isSimpleSwipeRight()
* isSwipeRight()
* isLongSwipeRight()
* isFastSwipeRight()
* isMovingRight()


text:
package com.cloudera.oryx.als.computation.merge;

import com.cloudera.oryx.als.common.NumericIDValue;
import com.cloudera.oryx.common.collection.LongFloatMap;
import com.cloudera.oryx.common.collection.LongSet;
import com.cloudera.oryx.common.random.RandomManager;
import com.cloudera.oryx.common.settings.ConfigUtils;
import com.cloudera.oryx.computation.common.fn.OryxReduceDoFn;
import com.google.common.base.Preconditions;
import com.typesafe.config.Config;
import org.apache.commons.math3.random.RandomGenerator;
import org.apache.crunch.Emitter;
import org.apache.crunch.Pair;

public final class MergeNewOldValuesFn extends OryxReduceDoFn<Pair<Long, Integer>, Iterable<NumericIDValue>,
    Pair<Long, NumericIDValue>> {

  static final int BEFORE = 0;
  static final int AFTER = 1;

  private float decayFactor;
  private float zeroThreshold;
  private Long previousUserID;
  private LongFloatMap previousUserPrefs;
  private double testSetFraction;
  private RandomGenerator random;

  @Override
  public void initialize() {
    super.initialize();
    Config config = ConfigUtils.getDefaultConfig();
    decayFactor = (float) config.getDouble("model.decay.factor");
    zeroThreshold = (float) config.getDouble("model.decay.zeroThreshold");
    Preconditions.checkArgument(decayFactor >= 0.0f && decayFactor <= 1.0f,
                                "Decay factor must be in [0,1]: %s", decayFactor);
    Preconditions.checkArgument(zeroThreshold >= 0.0f,
                                "Zero threshold must be nonnegative: %s", zeroThreshold);

    testSetFraction = config.getDouble("model.test-set-fraction");
    Preconditions.checkArgument(testSetFraction >= 0.0 && testSetFraction <= 1.0);
    if (testSetFraction > 0.0) {
      random = RandomManager.getRandom();
    }
  }

  @Override
  public void process(Pair<Pair<Long, Integer>, Iterable<NumericIDValue>> input,
                      Emitter<Pair<Long, NumericIDValue>> emitter) {
    Pair<Long, Integer> key = input.first();
    long currentUserID = key.first();

    if (key.second() == BEFORE) {

      // Should only be mapping historical data if decay factor is positive
      Preconditions.checkState(decayFactor > 0.0f);

      // Last old data had no match, just output it
      if (previousUserPrefs != null) {
        Preconditions.checkNotNull(previousUserID);
        output(previousUserID, previousUserPrefs, null, null, emitter);
        previousUserPrefs = null;
        previousUserID = null;
      }

      LongFloatMap oldPrefs = new LongFloatMap();
      for (NumericIDValue itemPref : input.second()) {
        float oldPrefValue = itemPref.getValue();
        Preconditions.checkState(!Float.isNaN(oldPrefValue), "No prior pref value?");
        // Apply decay factor here, if applicable:
        oldPrefs.increment(itemPref.getID(), oldPrefValue * decayFactor);
      }

      previousUserPrefs = oldPrefs;
      previousUserID = currentUserID;

    } else {
      // Last old data had no match, just output it
      if (previousUserPrefs != null && currentUserID != previousUserID) {
        Preconditions.checkNotNull(previousUserID);
        output(previousUserID, previousUserPrefs, null, null, emitter);
        previousUserPrefs = null;
        previousUserID = null;
      }

      LongFloatMap newPrefs = new LongFloatMap();
      LongSet removedItemIDs = new LongSet();
      LongFloatMap testSetPrefs = new LongFloatMap();

      for (NumericIDValue itemPref : input.second()) {
        long itemID = itemPref.getID();
        float newPrefValue = itemPref.getValue();

        // First: do we want to save off this data as test set data?
        if (testSetFraction > 0.0 && random.nextDouble() < testSetFraction) {
          testSetPrefs.increment(itemID, newPrefValue);
        } else {
          // Normal handling
          if (Float.isNaN(newPrefValue)) {
            removedItemIDs.add(itemID);
          } else {
            newPrefs.increment(itemID, newPrefValue);
          }
        }
      }

      output(currentUserID, previousUserPrefs, newPrefs, removedItemIDs, emitter);
      // TODO other emitter
      output(currentUserID, null, testSetPrefs, null, emitter);

      previousUserPrefs = null;
      previousUserID = null;
    }
  }

  @Override
  public void cleanup(Emitter<Pair<Long, NumericIDValue>> emitter) {
    if (previousUserPrefs != null) {
      Preconditions.checkNotNull(previousUserID);
      output(previousUserID, previousUserPrefs, null, null, emitter);
    }
    super.cleanup(emitter);
  }

  private void output(long userID,
                      LongFloatMap oldPrefs,
                      LongFloatMap newPrefs,
                      LongSet removedItemIDs,
                      Emitter<Pair<Long, NumericIDValue>> emitter) {
    // Old prefs may be null when there is no previous generation, for example, or the user is new.
    // First, write out existing prefs, possibly updated by new values
    if (oldPrefs != null && !oldPrefs.isEmpty()) {
      for (LongFloatMap.MapEntry entry : oldPrefs.entrySet()) {
        long itemID = entry.getKey();
        float oldPrefValue = entry.getValue();
        Preconditions.checkState(!Float.isNaN(oldPrefValue), "No prior pref value?");

        // May be NaN if no new data at all, or new data has no update:
        float sum = oldPrefValue;
        if (newPrefs != null) {
          float newPrefValue = newPrefs.get(itemID);
          if (!Float.isNaN(newPrefValue)) {
            sum += newPrefValue;
          }
        }

        if ((removedItemIDs == null || !removedItemIDs.contains(itemID)) &&
            Math.abs(sum) > zeroThreshold) {
          emitter.emit(Pair.of(userID, new NumericIDValue(itemID, sum)));
        }
      }
    }

    // Now output new data, that didn't exist in old prefs
    if (newPrefs != null && !newPrefs.isEmpty()) {
      for (LongFloatMap.MapEntry entry : newPrefs.entrySet()) {
        long itemID = entry.getKey();
        if (oldPrefs == null || !oldPrefs.containsKey(itemID)) {
          // It wasn't already written. If it exists in newPrefs, it's also not removed
          float newPrefValue = entry.getValue();
          if (Math.abs(newPrefValue) > zeroThreshold) {
            emitter.emit(Pair.of(userID, new NumericIDValue(itemID, newPrefValue)));
          }
        }
      }
    }
  }
}


text:
"""
A set of programs for dealing with and manipulating the log files recevied from edX.

Most of these programs are used to convert the log files, received from edX as logs from
each server organized by day, into a form that is more conducive to the research work
being done; the organization is to have the log entries for each class in a separate
file. Some of these files will cover a week, others can cover the history of an entire
course.

There are also some simple programs to extract basic data from the logs, such as the list of
users and ip addresses for those users, or the number of entries for each user in some set
of logs.
"""


text:
#include <openssl/lhash.h>

#include <assert.h>
#include <limits.h>
#include <string.h>

#include <openssl/mem.h>

#include "../internal.h"

// kMinNumBuckets is the minimum size of the buckets array in an |_LHASH|.
static const size_t kMinNumBuckets = 16;

// kMaxAverageChainLength contains the maximum, average chain length. When the
// average chain length exceeds this value, the hash table will be resized.
static const size_t kMaxAverageChainLength = 2;
static const size_t kMinAverageChainLength = 1;

struct lhash_st {
  // num_items contains the total number of items in the hash table.
  size_t num_items;
  // buckets is an array of |num_buckets| pointers. Each points to the head of
  // a chain of LHASH_ITEM objects that have the same hash value, mod
  // |num_buckets|.
  LHASH_ITEM **buckets;
  // num_buckets contains the length of |buckets|. This value is always >=
  // kMinNumBuckets.
  size_t num_buckets;
  // callback_depth contains the current depth of |lh_doall| or |lh_doall_arg|
  // calls. If non-zero then this suppresses resizing of the |buckets| array,
  // which would otherwise disrupt the iteration.
  unsigned callback_depth;

  lhash_cmp_func comp;
  lhash_hash_func hash;
};

_LHASH *lh_new(lhash_hash_func hash, lhash_cmp_func comp) {
  _LHASH *ret = OPENSSL_malloc(sizeof(_LHASH));
  if (ret == NULL) {
    return NULL;
  }
  OPENSSL_memset(ret, 0, sizeof(_LHASH));

  ret->num_buckets = kMinNumBuckets;
  ret->buckets = OPENSSL_malloc(sizeof(LHASH_ITEM *) * ret->num_buckets);
  if (ret->buckets == NULL) {
    OPENSSL_free(ret);
    return NULL;
  }
  OPENSSL_memset(ret->buckets, 0, sizeof(LHASH_ITEM *) * ret->num_buckets);

  ret->comp = comp;
  ret->hash = hash;
  return ret;
}

void lh_free(_LHASH *lh) {
  if (lh == NULL) {
    return;
  }

  for (size_t i = 0; i < lh->num_buckets; i++) {
    LHASH_ITEM *next;
    for (LHASH_ITEM *n = lh->buckets[i]; n != NULL; n = next) {
      next = n->next;
      OPENSSL_free(n);
    }
  }

  OPENSSL_free(lh->buckets);
  OPENSSL_free(lh);
}

size_t lh_num_items(const _LHASH *lh) { return lh->num_items; }

// get_next_ptr_and_hash returns a pointer to the pointer that points to the
// item equal to |data|. In other words, it searches for an item equal to |data|
// and, if it's at the start of a chain, then it returns a pointer to an
// element of |lh->buckets|, otherwise it returns a pointer to the |next|
// element of the previous item in the chain. If an element equal to |data| is
// not found, it returns a pointer that points to a NULL pointer. If |out_hash|
// is not NULL, then it also puts the hash value of |data| in |*out_hash|.
static LHASH_ITEM **get_next_ptr_and_hash(const _LHASH *lh, uint32_t *out_hash,
                                          const void *data,
                                          lhash_hash_func_helper call_hash_func,
                                          lhash_cmp_func_helper call_cmp_func) {
  const uint32_t hash = call_hash_func(lh->hash, data);
  if (out_hash != NULL) {
    *out_hash = hash;
  }

  LHASH_ITEM **ret = &lh->buckets[hash % lh->num_buckets];
  for (LHASH_ITEM *cur = *ret; cur != NULL; cur = *ret) {
    if (call_cmp_func(lh->comp, cur->data, data) == 0) {
      break;
    }
    ret = &cur->next;
  }

  return ret;
}

// get_next_ptr_by_key behaves like |get_next_ptr_and_hash| but takes a key
// which may be a different type from the values stored in |lh|.
static LHASH_ITEM **get_next_ptr_by_key(const _LHASH *lh, const void *key,
                                        uint32_t key_hash,
                                        int (*cmp_key)(const void *key,
                                                       const void *value)) {
  LHASH_ITEM **ret = &lh->buckets[key_hash % lh->num_buckets];
  for (LHASH_ITEM *cur = *ret; cur != NULL; cur = *ret) {
    if (cmp_key(key, cur->data) == 0) {
      break;
    }
    ret = &cur->next;
  }

  return ret;
}

void *lh_retrieve(const _LHASH *lh, const void *data,
                  lhash_hash_func_helper call_hash_func,
                  lhash_cmp_func_helper call_cmp_func) {
  LHASH_ITEM **next_ptr =
      get_next_ptr_and_hash(lh, NULL, data, call_hash_func, call_cmp_func);
  return *next_ptr == NULL ? NULL : (*next_ptr)->data;
}

void *lh_retrieve_key(const _LHASH *lh, const void *key, uint32_t key_hash,
                      int (*cmp_key)(const void *key, const void *value)) {
  LHASH_ITEM **next_ptr = get_next_ptr_by_key(lh, key, key_hash, cmp_key);
  return *next_ptr == NULL ? NULL : (*next_ptr)->data;
}

// lh_rebucket allocates a new array of |new_num_buckets| pointers and
// redistributes the existing items into it before making it |lh->buckets| and
// freeing the old array.
static void lh_rebucket(_LHASH *lh, const size_t new_num_buckets) {
  LHASH_ITEM **new_buckets, *cur, *next;
  size_t i, alloc_size;

  alloc_size = sizeof(LHASH_ITEM *) * new_num_buckets;
  if (alloc_size / sizeof(LHASH_ITEM*) != new_num_buckets) {
    return;
  }

  new_buckets = OPENSSL_malloc(alloc_size);
  if (new_buckets == NULL) {
    return;
  }
  OPENSSL_memset(new_buckets, 0, alloc_size);

  for (i = 0; i < lh->num_buckets; i++) {
    for (cur = lh->buckets[i]; cur != NULL; cur = next) {
      const size_t new_bucket = cur->hash % new_num_buckets;
      next = cur->next;
      cur->next = new_buckets[new_bucket];
      new_buckets[new_bucket] = cur;
    }
  }

  OPENSSL_free(lh->buckets);

  lh->num_buckets = new_num_buckets;
  lh->buckets = new_buckets;
}

// lh_maybe_resize resizes the |buckets| array if needed.
static void lh_maybe_resize(_LHASH *lh) {
  size_t avg_chain_length;

  if (lh->callback_depth > 0) {
    // Don't resize the hash if we are currently iterating over it.
    return;
  }

  assert(lh->num_buckets >= kMinNumBuckets);
  avg_chain_length = lh->num_items / lh->num_buckets;

  if (avg_chain_length > kMaxAverageChainLength) {
    const size_t new_num_buckets = lh->num_buckets * 2;

    if (new_num_buckets > lh->num_buckets) {
      lh_rebucket(lh, new_num_buckets);
    }
  } else if (avg_chain_length < kMinAverageChainLength &&
             lh->num_buckets > kMinNumBuckets) {
    size_t new_num_buckets = lh->num_buckets / 2;

    if (new_num_buckets < kMinNumBuckets) {
      new_num_buckets = kMinNumBuckets;
    }

    lh_rebucket(lh, new_num_buckets);
  }
}

int lh_insert(_LHASH *lh, void **old_data, void *data,
              lhash_hash_func_helper call_hash_func,
              lhash_cmp_func_helper call_cmp_func) {
  uint32_t hash;
  LHASH_ITEM **next_ptr, *item;

  *old_data = NULL;
  next_ptr =
      get_next_ptr_and_hash(lh, &hash, data, call_hash_func, call_cmp_func);

  if (*next_ptr != NULL) {
    // An element equal to |data| already exists in the hash table. It will be
    // replaced.
    *old_data = (*next_ptr)->data;
    (*next_ptr)->data = data;
    return 1;
  }

  // An element equal to |data| doesn't exist in the hash table yet.
  item = OPENSSL_malloc(sizeof(LHASH_ITEM));
  if (item == NULL) {
    return 0;
  }

  item->data = data;
  item->hash = hash;
  item->next = NULL;
  *next_ptr = item;
  lh->num_items++;
  lh_maybe_resize(lh);

  return 1;
}

void *lh_delete(_LHASH *lh, const void *data,
                lhash_hash_func_helper call_hash_func,
                lhash_cmp_func_helper call_cmp_func) {
  LHASH_ITEM **next_ptr, *item, *ret;

  next_ptr =
      get_next_ptr_and_hash(lh, NULL, data, call_hash_func, call_cmp_func);

  if (*next_ptr == NULL) {
    // No such element.
    return NULL;
  }

  item = *next_ptr;
  *next_ptr = item->next;
  ret = item->data;
  OPENSSL_free(item);

  lh->num_items--;
  lh_maybe_resize(lh);

  return ret;
}

void lh_doall_arg(_LHASH *lh, void (*func)(void *, void *), void *arg) {
  if (lh == NULL) {
    return;
  }

  if (lh->callback_depth < UINT_MAX) {
    // |callback_depth| is a saturating counter.
    lh->callback_depth++;
  }

  for (size_t i = 0; i < lh->num_buckets; i++) {
    LHASH_ITEM *next;
    for (LHASH_ITEM *cur = lh->buckets[i]; cur != NULL; cur = next) {
      next = cur->next;
      func(cur->data, arg);
    }
  }

  if (lh->callback_depth < UINT_MAX) {
    lh->callback_depth--;
  }

  // The callback may have added or removed elements and the non-zero value of
  // |callback_depth| will have suppressed any resizing. Thus any needed
  // resizing is done here.
  lh_maybe_resize(lh);
}

uint32_t lh_strhash(const char *c) {
  if (c == NULL) {
    return 0;
  }

  return OPENSSL_hash32(c, strlen(c));
}


text:
@class UICountingLabel;
typedef NS_ENUM(NSInteger, UILabelCountingMethod) {
    UILabelCountingMethodEaseInOut,
    UILabelCountingMethodEaseIn,
    UILabelCountingMethodEaseOut,
    UILabelCountingMethodLinear
};

typedef NSString* (^UICountingLabelFormatBlock)(CGFloat value);
typedef NSAttributedString* (^UICountingLabelAttributedFormatBlock)(CGFloat value);
typedef void(^UICountingLabelCompletionBlock)(UICountingLabel *countingLabel);
typedef void(^UICountingLabelCurrentValueBlock)(UICountingLabel *countingLabel, CGFloat currentValue);

@interface UICountingLabel : UILabel

@property (nonatomic, strong) NSString *format;
@property (nonatomic, strong) NSString *positiveFormat;//如果浮点数需要千分位分隔符,须使用@"###,##0.00"进行控制样式

@property (nonatomic, assign) UILabelCountingMethod method;
@property (nonatomic, assign) NSTimeInterval animationDuration;

@property (nonatomic, copy) UICountingLabelFormatBlock formatBlock;
@property (nonatomic, copy) UICountingLabelAttributedFormatBlock attributedFormatBlock;
@property (nonatomic, copy) UICountingLabelCompletionBlock completionBlock;
@property (nonatomic, copy) UICountingLabelCurrentValueBlock currentValueBlock;

- (void)countFrom:(CGFloat)startValue to:(CGFloat)endValue;
- (void)countFrom:(CGFloat)startValue to:(CGFloat)endValue duration:(NSTimeInterval)duration;

- (void)countFromCurrentValueTo:(CGFloat)endValue;
- (void)countFromCurrentValueTo:(CGFloat)endValue duration:(NSTimeInterval)duration;

- (void)countFromZeroTo:(CGFloat)endValue;
- (void)countFromZeroTo:(CGFloat)endValue duration:(NSTimeInterval)duration;

- (CGFloat)currentValue;

@end


text:
%% |phasewrap| documentation
% The |phasewrap| function wraps values to the range -pi to pi or -180 to 180.
%
%% Syntax
%
%  Zw = phasewrap(Z)
%  Zw = phasewrap(Z,'deg')
%
%% Description
%
% |Zw = phasewrap(Z)| wraps angle |Z| in radians to the range -pi to pi.
%
% |Zw = phasewrap(Z,'deg')| wraps angle |Z| in degree to the range -180 to 180.
%
%% Example

Z = 2*peaks(900);
Zw = phasewrap(Z);
imagesc(Zw)
phasemap
cb = colorbar;
ylabel(cb,' phase ')

%% Author Info
% This function was written by Chad A. Greene of the University of Texas at Austin
% Institute for Geophysics (UTIG), May 2016.
% <http://www.chadagreene.com>


text:
<?php

// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc.  All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//     * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

namespace Google\Protobuf\Internal;

use Google\Protobuf\Internal\GPBType;
use Google\Protobuf\Internal\MessageOptions;

class FileDescriptor
{

    private $package;
    private $message_type = [];
    private $enum_type = [];

    public function setPackage($package)
    {
        $this->package = $package;
    }

    public function getPackage()
    {
        return $this->package;
    }

    public function getMessageType()
    {
        return $this->message_type;
    }

    public function addMessageType($desc)
    {
        $this->message_type[] = $desc;
    }

    public function getEnumType()
    {
        return $this->enum_type;
    }

    public function addEnumType($desc)
    {
        $this->enum_type[]= $desc;
    }

    public static function buildFromProto($proto)
    {
        $file = new FileDescriptor();
        $file->setPackage($proto->getPackage());
        foreach ($proto->getMessageType() as $message_proto) {
            $file->addMessageType(Descriptor::buildFromProto(
                $message_proto, $proto, ""));
        }
        foreach ($proto->getEnumType() as $enum_proto) {
            $file->getEnumType()[] =
                $file->addEnumType(
                    EnumDescriptor::buildFromProto(
                        $enum_proto,
                        $proto,
                        ""));
        }
        return $file;
    }
}

class Descriptor
{

    private $full_name;
    private $field = [];
    private $nested_type = [];
    private $enum_type = [];
    private $klass;
    private $options;
    private $oneof_decl = [];

    public function addOneofDecl($oneof)
    {
        $this->oneof_decl[] = $oneof;
    }

    public function getOneofDecl()
    {
        return $this->oneof_decl;
    }

    public function setFullName($full_name)
    {
        $this->full_name = $full_name;
    }

    public function getFullName()
    {
        return $this->full_name;
    }

    public function addField($field)
    {
        $this->field[$field->getNumber()] = $field;
    }

    public function getField()
    {
        return $this->field;
    }

    public function addNestedType($desc)
    {
        $this->nested_type[] = $desc;
    }

    public function getNestedType()
    {
        return $this->nested_type;
    }

    public function addEnumType($desc)
    {
        $this->enum_type[] = $desc;
    }

    public function getEnumType()
    {
        return $this->enum_type;
    }

    public function getFieldByNumber($number)
    {
      if (!isset($this->field[$number])) {
        return NULL;
      } else {
        return $this->field[$number];
      }
    }

    public function setClass($klass)
    {
        $this->klass = $klass;
    }

    public function getClass()
    {
        return $this->klass;
    }

    public function setOptions($options)
    {
        $this->options = $options;
    }

    public function getOptions()
    {
        return $this->options;
    }

    public static function buildFromProto($proto, $file_proto, $containing)
    {
        $desc = new Descriptor();

        $message_name_without_package  = "";
        $classname = "";
        $fullname = "";
        getFullClassName(
            $proto,
            $containing,
            $file_proto,
            $message_name_without_package,
            $classname,
            $fullname);
        $desc->setFullName($fullname);
        $desc->setClass($classname);
        $desc->setOptions($proto->getOptions());

        foreach ($proto->getField() as $field_proto) {
            $desc->addField(FieldDescriptor::buildFromProto($field_proto));
        }

        // Handle nested types.
        foreach ($proto->getNestedType() as $nested_proto) {
            $desc->addNestedType(Descriptor::buildFromProto(
              $nested_proto, $file_proto, $message_name_without_package));
        }

        // Handle oneof fields.
        foreach ($proto->getOneofDecl() as $oneof_proto) {
            $desc->addOneofDecl(
                OneofDescriptor::buildFromProto($oneof_proto, $desc));
        }

        return $desc;
    }
}

function getClassNameWithoutPackage(
    $name,
    $file_proto)
{
    if ($name === "Empty" && $file_proto->getPackage() === "google.protobuf") {
        return "GPBEmpty";
    } else {
        $option = $file_proto->getOptions();
        $prefix = is_null($option) ? "" : $option->getPhpClassPrefix();
        // Nested message class names are seperated by '_', and package names
        // are seperated by '\'.
        return $prefix . implode('_', array_map('ucwords',
                               explode('.', $name)));
    }
}

function getFullClassName(
    $proto,
    $containing,
    $file_proto,
    &$message_name_without_package,
    &$classname,
    &$fullname)
{
    // Full name needs to start with '.'.
    $message_name_without_package = $proto->getName();
    if ($containing !== "") {
        $message_name_without_package =
            $containing . "." . $message_name_without_package;
    }

    $package = $file_proto->getPackage();
    if ($package === "") {
        $fullname = "." . $message_name_without_package;
    } else {
        $fullname = "." . $package . "." . $message_name_without_package;
    }

    $class_name_without_package =
        getClassNameWithoutPackage($message_name_without_package, $file_proto);
    if ($package === "") {
        $classname = $class_name_without_package;
    } else {
        $classname =
            implode('\\', array_map('ucwords', explode('.', $package))).
            "\\".$class_name_without_package;
    }
}

class OneofDescriptor
{

    private $name;
    private $fields;

    public function setName($name)
    {
        $this->name = $name;
    }

    public function getName()
    {
        return $this->name;
    }

    public function addField(&$field)
    {
        $this->fields[] = $field;
    }

    public function getFields()
    {
        return $this->fields;
    }

    public static function buildFromProto($oneof_proto)
    {
        $oneof = new OneofDescriptor();
        $oneof->setName($oneof_proto->getName());
        return $oneof;
    }
}

class EnumDescriptor
{

    private $klass;
    private $full_name;
    private $value;

    public function setFullName($full_name)
    {
        $this->full_name = $full_name;
    }

    public function getFullName()
    {
        return $this->full_name;
    }

    public function addValue($number, $value)
    {
        $this->value[$number] = $value;
    }

    public function setClass($klass)
    {
        $this->klass = $klass;
    }

    public function getClass()
    {
        return $this->klass;
    }

    public static function buildFromProto($proto, $file_proto, $containing)
    {
        $desc = new EnumDescriptor();

        $enum_name_without_package  = "";
        $classname = "";
        $fullname = "";
        getFullClassName(
            $proto,
            $containing,
            $file_proto,
            $enum_name_without_package,
            $classname,
            $fullname);
        $desc->setFullName($fullname);
        $desc->setClass($classname);

        return $desc;
    }
}

class EnumValueDescriptor
{
}

class FieldDescriptor
{

    private $name;
    private $setter;
    private $getter;
    private $number;
    private $label;
    private $type;
    private $message_type;
    private $enum_type;
    private $packed;
    private $is_map;
    private $oneof_index = -1;

    public function setOneofIndex($index)
    {
        $this->oneof_index = $index;
    }

    public function getOneofIndex()
    {
        return $this->oneof_index;
    }

    public function setName($name)
    {
        $this->name = $name;
    }

    public function getName()
    {
        return $this->name;
    }

    public function setSetter($setter)
    {
        $this->setter = $setter;
    }

    public function getSetter()
    {
        return $this->setter;
    }

    public function setGetter($getter)
    {
        $this->getter = $getter;
    }

    public function getGetter()
    {
        return $this->getter;
    }

    public function setNumber($number)
    {
        $this->number = $number;
    }

    public function getNumber()
    {
        return $this->number;
    }

    public function setLabel($label)
    {
        $this->label = $label;
    }

    public function getLabel()
    {
        return $this->label;
    }

    public function isRepeated()
    {
        return $this->label === GPBLabel::REPEATED;
    }

    public function setType($type)
    {
        $this->type = $type;
    }

    public function getType()
    {
        return $this->type;
    }

    public function setMessageType($message_type)
    {
        $this->message_type = $message_type;
    }

    public function getMessageType()
    {
        return $this->message_type;
    }

    public function setEnumType($enum_type)
    {
        $this->enum_type = $enum_type;
    }

    public function getEnumType()
    {
        return $this->enum_type;
    }

    public function setPacked($packed)
    {
        $this->packed = $packed;
    }

    public function getPacked()
    {
        return $this->packed;
    }

    public function isPackable()
    {
        return $this->isRepeated() && self::isTypePackable($this->type);
    }

    public function isMap()
    {
        return $this->getType() == GPBType::MESSAGE &&
               !is_null($this->getMessageType()->getOptions()) &&
               $this->getMessageType()->getOptions()->getMapEntry();
    }

    private static function isTypePackable($field_type)
    {
        return ($field_type !== GPBType::STRING  &&
            $field_type !== GPBType::GROUP   &&
            $field_type !== GPBType::MESSAGE &&
            $field_type !== GPBType::BYTES);
    }

    public static function getFieldDescriptor(
        $name,
        $label,
        $type,
        $number,
        $oneof_index,
        $packed,
        $type_name = null)
    {
        $field = new FieldDescriptor();
        $field->setName($name);
        $camel_name = implode('', array_map('ucwords', explode('_', $name)));
        $field->setGetter('get' . $camel_name);
        $field->setSetter('set' . $camel_name);
        $field->setType($type);
        $field->setNumber($number);
        $field->setLabel($label);
        $field->setPacked($packed);
        $field->setOneofIndex($oneof_index);

        // At this time, the message/enum type may have not been added to pool.
        // So we use the type name as place holder and will replace it with the
        // actual descriptor in cross building.
        switch ($type) {
            case GPBType::MESSAGE:
                $field->setMessageType($type_name);
                break;
            case GPBType::ENUM:
                $field->setEnumType($type_name);
                break;
            default:
                break;
        }

        return $field;
    }

    public static function buildFromProto($proto)
    {
        $type_name = null;
        switch ($proto->getType()) {
            case GPBType::MESSAGE:
            case GPBType::GROUP:
            case GPBType::ENUM:
                $type_name = $proto->getTypeName();
                break;
            default:
                break;
        }

        $oneof_index = $proto->hasOneofIndex() ? $proto->getOneofIndex() : -1;
        $packed = false;
        $options = $proto->getOptions();
        if ($options !== null) {
            $packed = $options->getPacked();
        }

        return FieldDescriptor::getFieldDescriptor(
            $proto->getName(), $proto->getLabel(), $proto->getType(),
            $proto->getNumber(), $oneof_index, $packed, $type_name);
    }
}


text:
layout: docs
title: Add-on Board
---

# Add-on Board

Soldering the resistor to between the power and data lines of the sensor cable can be a bit tricky. Especially if you want to connect more than one sensor to your Pi (as you'd solder the resistor to two power and two data lines!).

To make things easier, I designed a (very) simple board. It lets you connect up to five sensors, all you need to do is crimp the sensor cables with the appropriate connector.

The board is available as a [Fritzing file](https://github.com/tobyweston/temperature-machine/blob/master/pizero.fzz) on the Github site. You can get PCBs manufactured pretty cheaply online, I recommend [JCL PCB](https://jlcpcb.com/).

Failing that, [HenrysCat](https://github.com/HenrysCat) has kindly made the boards available on his [storefront](https://coolstuff.fws.store/Temperature_Machine_add-on_board/p7655610_20870085.aspx) for purchase. This storefront isn't associated with temperature-machine but head over to [Gitter](https://gitter.im/temperature-machine/Lobby#) to talk to [HenrysCat](https://github.com/HenrysCat) direct.

Basic PCB

Add up to five sensors with simple push-on type connectors. You just need to crimp them

Adding Headers

Using right angled headers on the Pi means you can still use the remaining GPIO pins

Sandwich!

Soldering the three pins directly to the add-on board and using the right angled headers keeps the whole thing small

Just add Sensors

The finished article ready for sensors to be attached

Go go go!

A single sensor connected

For the temperature-machine in my lounge, I mounted it behind my TV and added some [Pimoroni Blinkt!](https://shop.pimoroni.com/products/blinkt) LEDs for ambient lighting.

Blinky Lights

Adding Pimoroni's Blinkt! for some ambient lighting


text:
#include "sani/platform/graphics/screen_shader_source.hpp"
#include "sani/platform/graphics/descriptions.hpp"
#include "sani/platform/graphics/render_target_2d.hpp"
#include "sani/platform/graphics/graphics_device.hpp"
#include "sani/platform/graphics/viewport.hpp"
#include "sani/debug.hpp"

#include <sstream>
#include <vector>

// Contains WindowsGL and LinuxGL implementations of the graphics device.

#if SANI_TARGET_PLATFORM == SANI_PLATFORM_WINDOWS

#include "GL/wglew.h"
#include "GL/glew.h"

/*
	Reminder...
		Func call ->
			1) GL/Device calls
			2) State WRITE
				ret
*/

#define PUSH_ERROR(message)			pushError(message, __FUNCTION__, __LINE__)
#define CHECK_FOR_API_ERRORS		checkForAPIErrors(__FUNCTION__, __LINE__)

#if _DEBUG
#define CHECKED_API_CALL(fnc)		fnc; CHECK_FOR_API_ERRORS; if (hasErrors()) return
#else
#define CHECKED_API_CALL(fnc)		fnc
#endif

// Common impl of the graphics device.
namespace sani {

	namespace graphics {

		const uint32 ArrayBufferIndex = 0;
		const uint32 ElementArrayBufferIndex = 1;

		struct VertexAttributePointerState {
			bool valid { false };

			VertexAttributePointerDescription desc;

			VertexAttributePointerState() = default;

			~VertexAttributePointerState() = default;
		};

		struct DeviceState final {
			Viewport viewport;

			std::vector<std::pair<bool, VertexAttributePointerDescription>> vertexPointers;

			uint32 windowedLocationX	{ 0 };
			uint32 windowedLocationY	{ 0 };

			uint32 buffers[2];
			uint32 effect				{ 0 };
			uint32 texture				{ 0 };

			RenderTarget2D* renderTarget	{ nullptr };

			DeviceState() {
				vertexPointers.resize(4);

				buffers[0] = NULL;
				buffers[1] = NULL;
			}

			void invalidateBuffer(const uint32 id) {
				if (buffers[ArrayBufferIndex] == id)		buffers[ArrayBufferIndex]			= NULL;
				if (buffers[ElementArrayBufferIndex] == id) buffers[ElementArrayBufferIndex]	= NULL;
			}
			void invalidateEffect(const uint32 id) {
				if (effect == id) effect = NULL;
			}
			void invalidateRenderTarget(const uint32 fbid) {
				if (renderTarget == nullptr) return;

				if (renderTarget->getFramebuffer() == fbid) renderTarget = nullptr;
			}
			void invalidateTexture(const uint32 id) {
				if (texture == id) texture = NULL;
			}

			~DeviceState() = default;
		};

		struct Impl final {
			uint32 backbufferWidth				{ 0 };
			uint32 backbufferHeight				{ 0 };
			uint32 samples						{ 0 };

			RenderTarget2D* backbuffer			{ nullptr };
			RenderTarget2D* msaBackbuffer		{ nullptr };
			uint32 backbufferEffect;

			std::vector<DeviceState> states;
			DeviceState* currentState			{ nullptr };

			HGLRC renderingContext;
			HDC deviceContext;

			HWND hwnd;
			HINSTANCE hInstance;

			uint32 screenBuffer					{ 0 };

			bool fullscreen						{ false };

			Impl() = default;

			~Impl() {
				delete msaBackbuffer;
			}
		};

		GraphicsDevice::GraphicsDevice(const HWND hWnd, const HINSTANCE hInstance) : impl(new Impl()) {
			impl->hwnd = hWnd;
			impl->hInstance = hInstance;
			impl->deviceContext = GetDC(hWnd);

			impl->states.push_back(DeviceState());
			impl->currentState = &impl->states.back();

			SANI_INIT_EVENT(backbufferSizeChanged, void(const uint32, const uint32, const uint32, const uint32));
			SANI_INIT_EVENT(viewportSizeChanged, void(const Viewport&, const Viewport&));
		}

		void GraphicsDevice::pushError(const String& message, const char* const fnc, const uint32 ln) {
			errorBuffer.push(GraphicsError(message, fnc, ln));
		}
		void GraphicsDevice::checkForAPIErrors(const char* const fnc, const uint32 ln) {
			auto errorCode = glGetError();

			while (errorCode != 0) {
				errorBuffer.push(GraphicsError(errorCode, fnc, ln));

				errorCode = glGetError();
			}
		}

		uint32 GraphicsDevice::compileShader(const char* const src, const ShaderType type, String& error) {
			const auto shader = type == ShaderType::Vertex ? glCreateShader(GL_VERTEX_SHADER) : glCreateShader(GL_FRAGMENT_SHADER);

			auto status = 0;

			glShaderSource(shader, 1, &src, nullptr);
			glCompileShader(shader);
			glGetShaderiv(shader, GL_COMPILE_STATUS, &status);

			if (status == GL_FALSE)  {
				auto loglen = 0;

				auto buff = new char[loglen];

				glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &loglen);
				glGetShaderInfoLog(shader, loglen, &loglen, buff);

				error = String(buff);
				delete buff;

				return NULL;
			}

			return shader;
		}

		bool GraphicsDevice::createContext() {
			// Create context.
			const auto context = wglCreateContext(impl->deviceContext);

			if (context == NULL) {
				PUSH_ERROR("could not create context");

				return false;
			}

			wglMakeCurrent(impl->deviceContext, context);

			impl->renderingContext = context;

			return true;
		}
		bool GraphicsDevice::initializeGlew() {
			const auto glewState = glewInit();

			if (glewState != GLEW_OK) {
				PUSH_ERROR("could not initialize glew!");

				return false;
			}

			return true;
		}
		bool GraphicsDevice::initializeDevice() {
			RECT clntRect;
			GetClientRect(impl->hwnd, &clntRect);

			const Viewport viewport(0, 0, clntRect.right - clntRect.left, clntRect.bottom - clntRect.top);

			CHECKED_API_CALL(glEnable(GL_BLEND))								false;
			CHECKED_API_CALL(glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)) false;

			glViewport(0, 0, viewport.width, viewport.height);

			impl->msaBackbuffer = impl->samples == NULL ? nullptr : new RenderTarget2D(this, impl->backbufferWidth, impl->backbufferHeight, impl->samples);
			impl->backbuffer = new RenderTarget2D(this, impl->backbufferWidth, impl->backbufferHeight);
			impl->currentState->viewport = viewport;

			return !hasErrors();
		}

		void GraphicsDevice::setupScreenShader() {
			const GLfloat quadVertices[] = {
				// Positions   // TexCoords
				-1.0f, 1.0f, 0.0f, 1.0f,
				-1.0f, -1.0f, 0.0f, 0.0f,
				1.0f, -1.0f, 1.0f, 0.0f,

				-1.0f, 1.0f, 0.0f, 1.0f,
				1.0f, -1.0f, 1.0f, 0.0f,
				1.0f, 1.0f, 1.0f, 1.0f
			};

			BufferDescription desc;
			desc.type = BufferType::ArrayBuffer;
			desc.usage = BufferUsage::Static;
			desc.bytes = sizeof(quadVertices) * sizeof(float);
			desc.data = (void*)quadVertices;

			const auto buffer = createBuffer(&desc);
			if (hasErrors()) return;

			const auto effect = compileEffect(ScreenShaderVertexSource, ScreenShaderFragmentSource);
			if (hasErrors()) return;

			impl->screenBuffer = buffer;
			impl->backbufferEffect = effect;
		}

		void GraphicsDevice::initialize(const uint32 backBufferWidth, const uint32 backBufferHeight, const uint32 samples) {
			SANI_ASSERT(backBufferWidth != 0);
			SANI_ASSERT(backBufferHeight != 0);

			impl->backbufferWidth = backBufferWidth;
			impl->backbufferHeight = backBufferHeight;
			impl->samples = samples;

			if (!createContext()) return;

			if (!initializeGlew()) return;

			if (!initializeDevice()) return;

			setupScreenShader();
		}
		void GraphicsDevice::dispose() {
			wglDeleteContext(impl->renderingContext);
			wglMakeCurrent(impl->deviceContext, NULL);
		}

		GraphicsError GraphicsDevice::nextError() {
			const auto next = errorBuffer.top();
			errorBuffer.pop();

			return next;
		}
		bool GraphicsDevice::hasErrors() {
			return !errorBuffer.empty();
		}

		bool GraphicsDevice::isFullscreen() const {
			return impl->fullscreen;
		}
		void GraphicsDevice::fullscreen() {
			RECT wndRect;

			GetClientRect(impl->hwnd, &wndRect);

			// Set style to fullscreen.
			SetWindowLongPtr(impl->hwnd, GWL_STYLE, WS_SYSMENU | WS_POPUP | WS_CLIPCHILDREN | WS_VISIBLE);

			// Get current monitors size.
			const uint32 width = GetSystemMetrics(SM_CXSCREEN);
			const uint32 height = GetSystemMetrics(SM_CYSCREEN);

			MoveWindow(impl->hwnd, 0, 0, width, height, TRUE);

			// Store the location of the window.
			impl->currentState->windowedLocationX = wndRect.left;
			impl->currentState->windowedLocationY = wndRect.top;
		}
		void GraphicsDevice::windowed() {
			RECT wndRect;
			RECT rect;

			GetWindowRect(impl->hwnd, &wndRect);

			const uint32 width = wndRect.right - wndRect.left;
			const uint32 height = wndRect.bottom - wndRect.top;

			rect.left = impl->currentState->windowedLocationX;
			rect.top = impl->currentState->windowedLocationY;
			rect.bottom = height;
			rect.right = width;

			SetWindowLongPtr(impl->hwnd, GWL_STYLE, WS_OVERLAPPEDWINDOW | WS_VISIBLE);
			AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, FALSE);
			MoveWindow(impl->hwnd, impl->currentState->windowedLocationX, impl->currentState->windowedLocationY, width, height, TRUE);
		}

		void GraphicsDevice::setBackbufferSize(const uint32 width, const uint32 height) {
			impl->backbufferWidth = width;
			impl->backbufferHeight = height;
		}
		uint32 GraphicsDevice::getBackbufferWidth() const {
			return impl->backbufferWidth;
		}
		uint32 GraphicsDevice::getBackbufferHeight() const {
			return impl->backbufferHeight;
		}

		void GraphicsDevice::setSamplesCount(const uint32 samples) {
			impl->samples = samples;
		}
		uint32 GraphicsDevice::getSamplesCount() const {
			return impl->samples;
		}

		void GraphicsDevice::applyBackbufferChanges() {
			const auto oldWidth = getBackbufferWidth();
			const auto oldHeight = getBackbufferHeight();

			auto* oldMsaBackbuffer = impl->msaBackbuffer;
			auto* oldBackbuffer = impl->backbuffer;

			// Delete last msa buffer.
			if (oldMsaBackbuffer != nullptr) {
				oldMsaBackbuffer->dispose();

				delete oldMsaBackbuffer;

				oldMsaBackbuffer = nullptr;
			}

			oldBackbuffer->dispose();

			delete oldBackbuffer;

			const auto newWidth = impl->backbufferWidth;
			const auto newHeight = impl->backbufferHeight;

			impl->msaBackbuffer = impl->samples == NULL ? nullptr : new RenderTarget2D(this, newWidth, newHeight, impl->samples);
			impl->backbuffer = new RenderTarget2D(this, newWidth, newHeight);

			SANI_TRIGGER_EVENT(backbufferSizeChanged,
							   void(const uint32, const uint32, const uint32, const uint32),
							   oldWidth, oldHeight, newHeight, newWidth);

			if (impl->currentState->renderTarget == nullptr) setRenderTarget(impl->msaBackbuffer);
		}

		void GraphicsDevice::setRenderTarget(RenderTarget2D* const renderTarget) {
			RenderTarget2D* const defaultBackbuffer = impl->msaBackbuffer == nullptr ? impl->backbuffer : impl->msaBackbuffer;

			impl->currentState->renderTarget = renderTarget == nullptr ? defaultBackbuffer : renderTarget;

			CHECKED_API_CALL(glBindFramebuffer(GL_FRAMEBUFFER, impl->currentState->renderTarget->getFramebuffer()));

			const auto r = impl->currentState->renderTarget->getClearRed();
			const auto g = impl->currentState->renderTarget->getClearGreen();
			const auto b = impl->currentState->renderTarget->getClearBlue();
			const auto a = impl->currentState->renderTarget->getClearAlpha();

			glClearColor(r, g, b, a);

			CHECKED_API_CALL(glClear(GL_COLOR_BUFFER_BIT));
		}

		void GraphicsDevice::setViewport(const Viewport& viewport) {
			glViewport(viewport.x, viewport.y, viewport.width, viewport.height);

			impl->currentState->viewport = viewport;
		}
		const Viewport& GraphicsDevice::getViewport() const {
			return impl->currentState->viewport;
		}

		// Buffer related.
		uint32 GraphicsDevice::createBuffer(const BufferDescription* const desc) {
			SANI_ASSERT(desc != nullptr);

			uint32 id = 0;

			glGenBuffers(1, &id);
			bindBuffer(desc->type, id);

			CHECKED_API_CALL(glBufferData(static_cast<GLenum>(desc->type), desc->bytes, desc->data, static_cast<GLenum>(desc->usage))) NULL;
			bindBuffer(desc->type, NULL);

			return id;
		}
		void GraphicsDevice::deleteBuffer(const uint32 id) {
			SANI_ASSERT(id != NULL);

			glDeleteBuffers(1, &id);

			for (auto& state : impl->states) state.invalidateBuffer(id);
		}
		void GraphicsDevice::bindBuffer(const BufferType target, const uint32 id) {
			CHECKED_API_CALL(glBindBuffer(static_cast<GLenum>(target), id));

			impl->currentState->buffers[static_cast<uint32>(target)] = id;
		}
		void GraphicsDevice::setBufferData(const BufferType target, const void* const data, const uint32 bytes, const uint32 offset) {
			const uint32 buffer = impl->currentState->buffers[static_cast<uint32>(target)];

			if (buffer == NULL) {
				PUSH_ERROR("buffer type not binded");

				return;
			}

			CHECKED_API_CALL(glBufferSubData(static_cast<GLenum>(target), offset, bytes, data));
		}

		// Texture related.
		void GraphicsDevice::bindTexture(const TextureTarget target, const uint32 id) {
			CHECKED_API_CALL(glBindTexture(static_cast<GLenum>(target), id));

			impl->currentState->texture = id;
		}
		uint32 GraphicsDevice::createTexture(const TextureDescription* desc) {
			SANI_ASSERT(desc != nullptr);

			uint32 id = 0;

			glGenTextures(1, &id);
			glBindTexture(GL_TEXTURE_2D, id);

			setTextureData(TextureTarget::Texture2D, 0, desc->format, desc->width, desc->height, desc->format, nullptr);

			glBindTexture(GL_TEXTURE_2D, NULL);

			return id;
		}

		static bool asd = false;

		void GraphicsDevice::createRendertarget(uint32& texid, uint32& fbid, const uint32 width, const uint32 height, const uint32 samples) {
			glGenFramebuffers(1, &fbid);
			glBindFramebuffer(GL_FRAMEBUFFER, fbid);

			// Generate texture.
			glGenTextures(1, &texid);

			GLenum target = samples == NULL ? GL_TEXTURE_2D : GL_TEXTURE_2D_MULTISAMPLE;

			if (target == GL_TEXTURE_2D) {
				glBindTexture(GL_TEXTURE_2D, texid);

				CHECKED_API_CALL(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL));
				CHECKED_API_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
				CHECKED_API_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
			} else {
				glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, texid);

				CHECKED_API_CALL(glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, samples, GL_RGBA, width, height, GL_TRUE));
			}

			CHECKED_API_CALL(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, target, texid, NULL));

			if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) errorBuffer.push(GraphicsError("Could not create render target 2D", __FUNCTION__, __LINE__));

			glBindFramebuffer(GL_FRAMEBUFFER, NULL);
			glBindTexture(target, NULL);
		}
		void GraphicsDevice::deleteRenderTarget(const uint32 fbid) {
			CHECKED_API_CALL(glDeleteFramebuffers(1, &fbid));

			for (auto& state : impl->states) state.invalidateRenderTarget(fbid);
		}

		void GraphicsDevice::setTextureParameter(const TextureTarget target, const TextureParameterName field, const int32 value) {
			CHECKED_API_CALL(glTexParameteri(static_cast<GLenum>(target), static_cast<GLenum>(field), static_cast<GLuint>(value)));
		}
		void GraphicsDevice::getTextureData(const TextureTarget target, const int32 level, const SurfaceFormat format, unsigned char* data) {
			CHECKED_API_CALL(glGetTexImage(static_cast<GLenum>(target),
										   level,
										   static_cast<GLint>(format),
										   GL_UNSIGNED_BYTE,
										   data));
		}
		void GraphicsDevice::setTextureData(const TextureTarget target, const int32 level, const SurfaceFormat internalFormat, const int32 width, const int32 height, const SurfaceFormat format, const unsigned char* data) {
			CHECKED_API_CALL(glTexImage2D(static_cast<GLenum>(target),
										  level,
										  static_cast<GLint>(internalFormat),
										  width,
										  height,
										  0,
										  static_cast<GLint>(format),
										  GL_UNSIGNED_BYTE,
										  data));
		}

		void GraphicsDevice::deleteTexture(const uint32 texid) {
			glDeleteTextures(1, &texid);

			for (auto& state : impl->states) state.invalidateTexture(texid);
		}

		// Effect related.
		void GraphicsDevice::bindEffect(const uint32 id) {
			CHECKED_API_CALL(glUseProgram(id));

			impl->currentState->effect = id;
		}

		int32 GraphicsDevice::getUniformsCount() {
			int32 count = 0;

			CHECKED_API_CALL(glGetProgramiv(impl->currentState->effect, GL_ACTIVE_UNIFORMS, &count)) NULL;

			return count;
		}
		int32 GraphicsDevice::getUniformLocation(const String& name) {
			return CHECKED_API_CALL(glGetUniformLocation(impl->currentState->effect, name.c_str())) NULL;
		}

		void GraphicsDevice::getUniformInformation(const uint32 index, uint32& location, String& name, uint32& type, int32& valuesCount) {
			char namebuff[128];

			int32 namelen = -1;

			// Get name len, values, count, type and name.
			CHECKED_API_CALL(glGetActiveUniform(impl->currentState->effect, index, sizeof(namebuff)-1, &namelen, &valuesCount, &type, namebuff));

			// Copy name if we got the info.
			if (namelen != -1) name = String(std::begin(namebuff), std::begin(namebuff) + namelen);

			// Get location of the uniform.
			location = getUniformLocation(name);
		}
		void GraphicsDevice::setUniformValue(const String& name, const void* const data, const UniformType type) {
			const auto location = getUniformLocation(name);

			if (location == -1) {
				PUSH_ERROR("invalid uniform name");

				return;
			}

			if		(type == UniformType::Float32)		{ CHECKED_API_CALL(glUniform1f(location, *static_cast<const GLfloat* const>(data))); }
			else if (type == UniformType::Mat3F)		{ CHECKED_API_CALL(glUniformMatrix3fv(location, 1, GL_FALSE, static_cast<const GLfloat* const>(data))); }
			else if (type == UniformType::Mat4F)		{ CHECKED_API_CALL(glUniformMatrix4fv(location, 1, GL_FALSE, static_cast<const GLfloat* const>(data))); }
			else										{ PUSH_ERROR("invalid uniform type"); }
		}

		uint32 GraphicsDevice::compileEffect(const char* const vscr, const char* const fscr, String& errors) {
			String vserror;
			const auto vs = compileShader(vscr, ShaderType::Vertex, vserror);

			String fserror;
			const auto fs = compileShader(fscr, ShaderType::Fragment, fserror);

			if (vs == NULL || fs == NULL) {
				std::stringstream ss;

				if (!vserror.empty()) {
					ss << "vs err: ";
					ss << vserror;
				}

				if (fserror.empty()) {
					ss << " - ";
					ss << "fs err: ";
					ss << fserror;
				}

				errors = ss.str();

				return NULL;
			}

			// Shaders compile ok, return.
			const auto pid = glCreateProgram();
			glAttachShader(pid, vs);
			glAttachShader(pid, fs);
			glLinkProgram(pid);

			auto status = 0;
			glGetProgramiv(pid, GL_LINK_STATUS, &status);

			if (status == GL_FALSE) {
				PUSH_ERROR("could not link program");

				return NULL;
			}

			return pid;
		}
		uint32 GraphicsDevice::compileEffect(const char* const vscr, const char* const fscr) {
			String errors; // Not used.

			return compileEffect(vscr, fscr, errors);
		}
		void GraphicsDevice::deleteEffect(const uint32 id) {
			CHECKED_API_CALL(glDeleteProgram(id));

			for (auto& state : impl->states) state.invalidateEffect(id);
		}

		// Vertex pointer related.
		void GraphicsDevice::createVertexPointer(const VertexAttributePointerDescription* const desc) {
			CHECKED_API_CALL(glEnableVertexAttribArray(desc->location));

			CHECKED_API_CALL(glVertexAttribPointer(desc->location,
												   desc->size,
												   static_cast<GLenum>(desc->type),
												   desc->normalized,
												   desc->stride,
												   (void*)desc->offset));

			// Register.
			if (impl->currentState->vertexPointers.size() < desc->location) impl->currentState->vertexPointers.resize(desc->location);

			impl->currentState->vertexPointers[desc->location].first = true;
			impl->currentState->vertexPointers[desc->location].second = *desc;
		}
		void GraphicsDevice::disableVertexPointer(const uint32 location) {
			CHECKED_API_CALL(glDisableVertexAttribArray(location));

			if (impl->currentState->vertexPointers.size() < location) return;

			impl->currentState->vertexPointers[location].first = false;
		}

		void GraphicsDevice::clear(const float32 r, const float32 g, const float32 b, const float32 a) {
			setRenderTarget(nullptr);

			glClearColor(r, g, b, a);

			SwapBuffers(impl->deviceContext);

			glClear(GL_COLOR_BUFFER_BIT);

			CHECK_FOR_API_ERRORS;
		}
		void GraphicsDevice::present(const uint32 effect) {
			const auto width = impl->backbuffer->getWidth();
			const auto height = impl->backbuffer->getHeight();

			// Check if using sampling.
			if (impl->msaBackbuffer != nullptr) {
				// Copy from MSA backbuffer to "draw" backbuffer. Using sampling.
				CHECKED_API_CALL(glBindFramebuffer(GL_READ_FRAMEBUFFER, impl->msaBackbuffer->getFramebuffer()));
				CHECKED_API_CALL(glBindFramebuffer(GL_DRAW_FRAMEBUFFER, impl->backbuffer->getFramebuffer()));
				CHECKED_API_CALL(glBlitFramebuffer(0, 0, width, height, 0, 0, width, height, GL_COLOR_BUFFER_BIT, GL_NEAREST));
			}

			// Go back to default backbuffer.
			glBindFramebuffer(GL_FRAMEBUFFER, NULL);
			glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
			glClear(GL_COLOR_BUFFER_BIT);

			// Bind back buffer texture.
			// Using sampling or not. Does not matter at this point.
			glBindTexture(GL_TEXTURE_2D, impl->backbuffer->getID());

			const auto postproceff = effect == NULL ? impl->backbufferEffect : effect;

			CHECKED_API_CALL(glUseProgram(postproceff));
			CHECKED_API_CALL(glBindBuffer(GL_ARRAY_BUFFER, impl->screenBuffer));

			const auto VtxDescLocation = 0;
			const auto TexDescLocation = 1;
			const auto ScreenVertexElementsCount = 4;

			static VertexAttributePointerDescription vtxDesc;
			vtxDesc.location = VtxDescLocation;
			vtxDesc.size = 2;
			vtxDesc.type = PrimitiveType::Float;
			vtxDesc.normalized = false;
			vtxDesc.stride = sizeof(float32) * ScreenVertexElementsCount;
			vtxDesc.offset = 0;

			static VertexAttributePointerDescription texDesc;
			texDesc.location = TexDescLocation;
			texDesc.size = 2;
			texDesc.type = PrimitiveType::Float;
			texDesc.normalized = false;
			texDesc.stride = sizeof(float32) * ScreenVertexElementsCount;
			texDesc.offset = sizeof(float32) * 2;

			createVertexPointer(&vtxDesc);
			createVertexPointer(&texDesc);

			drawArrays(RenderMode::Triangles, 0, 6);

			disableVertexPointer(VtxDescLocation);
			disableVertexPointer(TexDescLocation);
		}
		void GraphicsDevice::drawArrays(const RenderMode mode, const uint32 first, const uint32 count) {
			CHECKED_API_CALL(glDrawArrays(static_cast<GLenum>(mode), first, count));
		}
		void GraphicsDevice::drawElements(const RenderMode mode, const PrimitiveType type, const uint32 count, const uint32 indices) {
			CHECKED_API_CALL(glDrawElements(static_cast<GLenum>(mode), count, static_cast<GLenum>(type), (void*)indices));
		}

		void GraphicsDevice::resumeState() {
			// TODO: impl.
		}
		void GraphicsDevice::saveState() {
			// TODO: impl.
		}

		GraphicsDevice::~GraphicsDevice() {
			delete impl;
		}
	}
}

#endif


text:
package org.opencb.hpg.bigdata.tools.sequence;

import hbparquet.hadoop.util.ContextUtil;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.compress.CompressionCodec;
import org.apache.hadoop.io.compress.CompressionCodecFactory;
import org.apache.hadoop.mapreduce.InputSplit;
import org.apache.hadoop.mapreduce.JobContext;
import org.apache.hadoop.mapreduce.RecordReader;
import org.apache.hadoop.mapreduce.TaskAttemptContext;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;
import org.seqdoop.hadoop_bam.*;
import org.seqdoop.hadoop_bam.FormatConstants.BaseQualityEncoding;
import org.seqdoop.hadoop_bam.util.ConfHelper;

import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by jtarraga on 21/05/15.
 * Hadoop-BAM FastqInputFormat modification
 * To avoid calling context.setStatus(...), since TaskAttemptContext became an interface (not a class) from r2.4.1
 */
public class FastqInputFormatMODIF extends FileInputFormat<Text, SequencedFragment>
{
    public static final String CONF_BASE_QUALITY_ENCODING = "hbam.fastq-input.base-quality-encoding";
    public static final String CONF_FILTER_FAILED_QC      = "hbam.fastq-input.filter-failed-qc";
    public static final String CONF_BASE_QUALITY_ENCODING_DEFAULT = "sanger";

    public static class FastqRecordReader extends RecordReader<Text, SequencedFragment> {
        /*
         * fastq format:
         * <fastq> := <block>+
         * <block := @<seqname>\n<seq>\n+[<seqname>]\n<qual>\n
         * <seqname> := [A-Za-z0-9_.:-]+
         * <seq> := [A-Za-z\n\.~]+
         * <qual> := [!-~\n]+
         *
         * LP: this format is broken, no?  You can have multi-line sequence and quality strings,
         * and the quality encoding includes '@' in its valid character range.  So how should one
         * distinguish between \n@ as a record delimiter and and \n@ as part of a multi-line
         * quality string?
         *
         * For now I'm going to assume single-line sequences.  This works for our sequencing
         * application.  We'll see if someone complains in other applications.
         */

        // start:  first valid data index
        private long start;
        // end:  first index value beyond the slice, i.e. slice is in range [start,end)
        private long end;
        // pos: current position in file
        private long pos;
        // file:  the file being read
        private Path file;

        private LineReader lineReader;
        private InputStream inputStream;
        private Text currentKey = new Text();
        private SequencedFragment currentValue = new SequencedFragment();

        /* If true, will scan the identifier for read data as specified in the Casava
         * users' guide v1.8:
         * @<instrument>:<run number>:<flowcell ID>:<lane>:<tile>:<x-pos>:<y-pos> <read>:<is filtered>:
         *            <control number>:<index sequence>
         * After the first name that doesn't match lookForIlluminaIdentifier will be
         * set to false and no further scanning will be done.
         */
        private boolean lookForIlluminaIdentifier = true;
        private static final Pattern ILLUMINA_PATTERN =
                Pattern.compile("([^:]+):(\\d+):([^:]*):(\\d+):(\\d+):(-?\\d+):(-?\\d+)\\s+([123]):([YN]):(\\d+):(.*)");

        private Text buffer = new Text();

        private BaseQualityEncoding qualityEncoding;
        private boolean filterFailedQC = false;

        // How long can a read get?
        private static final int MAX_LINE_LENGTH = 10000;

        public FastqRecordReader(Configuration conf, FileSplit split) throws IOException {
            setConf(conf);
            file = split.getPath();
            start = split.getStart();
            end = start + split.getLength();

            FileSystem fs = file.getFileSystem(conf);
            FSDataInputStream fileIn = fs.open(file);

            CompressionCodecFactory codecFactory = new CompressionCodecFactory(conf);
            CompressionCodec        codec        = codecFactory.getCodec(file);

            if (codec == null) { // no codec.  Uncompressed file.
                positionAtFirstRecord(fileIn);
                inputStream = fileIn;
            } else { // compressed file
                if (start != 0) {
                    throw new RuntimeException("Start position for compressed file is not 0! (found " + start + ")");
                }

                inputStream = codec.createInputStream(fileIn);
                end = Long.MAX_VALUE; // read until the end of the file
            }

            lineReader = new LineReader(inputStream);
        }

        protected void setConf(Configuration conf) {
            String encoding = conf.get(FastqInputFormat.CONF_BASE_QUALITY_ENCODING,
                    conf.get(
                            FormatConstants.CONF_INPUT_BASE_QUALITY_ENCODING,
                            FastqInputFormat.CONF_BASE_QUALITY_ENCODING_DEFAULT)
            );

            if ("illumina".equals(encoding)) {
                qualityEncoding = BaseQualityEncoding.Illumina;
            } else if ("sanger".equals(encoding)) {
                qualityEncoding = BaseQualityEncoding.Sanger;
            } else {
                throw new RuntimeException("Unknown input base quality encoding value " + encoding);
            }

            filterFailedQC = ConfHelper.parseBoolean(
                    conf.get(FastqInputFormat.CONF_FILTER_FAILED_QC,
                            conf.get(FormatConstants.CONF_INPUT_FILTER_FAILED_QC)),
                    false);
        }

        /*
         * Position the input stream at the start of the first record.
         */
        private void positionAtFirstRecord(FSDataInputStream stream) throws IOException {
            if (start > 0) {
                // Advance to the start of the first record
                // We use a temporary LineReader to read lines until we find the
                // position of the right one.  We then seek the file to that position.
                stream.seek(start);
                LineReader reader = new LineReader(stream);

                int bytesRead = 0;
                do {
                    bytesRead = reader.readLine(buffer, (int)Math.min(MAX_LINE_LENGTH, end - start));
                    if (bytesRead > 0 && (buffer.getLength() <= 0 || buffer.getBytes()[0] != '@')) {
                        start += bytesRead;
                    } else {
                        // line starts with @.  Read two more and verify that it starts with a +
                        //
                        // If this isn't the start of a record, we want to backtrack to its end
                        long backtrackPosition = start + bytesRead;

                        bytesRead = reader.readLine(buffer, (int)Math.min(MAX_LINE_LENGTH, end - start));
                        bytesRead = reader.readLine(buffer, (int)Math.min(MAX_LINE_LENGTH, end - start));
                        if (bytesRead > 0 && buffer.getLength() > 0 && buffer.getBytes()[0] == '+') {
                            break; // all good!
                        } else {
                            // backtrack to the end of the record we thought was the start.
                            start = backtrackPosition;
                            stream.seek(start);
                            reader = new LineReader(stream);
                        }
                    }
                } while (bytesRead > 0);

                stream.seek(start);
            }
            // else
            // if start == 0 we presume it starts with a valid fastq record
            pos = start;
        }

        /*
         * Added to use mapreduce API.
         *
         */
        public void initialize(InputSplit split, TaskAttemptContext context) throws IOException, InterruptedException {
        }

        /*
         * Added to use mapreduce API.
         */
        public Text getCurrentKey() {
            return currentKey;
        }

        /*
         * Added to use mapreduce API.
         */
        public SequencedFragment getCurrentValue() {
            return currentValue;
        }

        /*
         * Added to use mapreduce API.
         */
        public boolean nextKeyValue() throws IOException, InterruptedException {
            return next(currentKey, currentValue);
        }

        /*
         * Close this RecordReader to future operations.
         */
        public void close() throws IOException {
            inputStream.close();
        }

        /*
         * Create an object of the appropriate type to be used as a key.
         */
        public Text createKey() {
            return new Text();
        }

        /*
         * Create an object of the appropriate type to be used as a value.
         */
        public SequencedFragment createValue() {
            return new SequencedFragment();
        }

        /*
         * Returns the current position in the input.
         */
        public long getPos() {
            return pos;
        }

        /*
         * How much of the input has the RecordReader consumed i.e.
         */
        public float getProgress() {
            if (start == end) {
                return 1.0f;
            } else {
                return Math.min(1.0f, (pos - start) / (float)(end - start));
            }
        }

        public String makePositionMessage() {
            return file.toString() + ":" + pos;
        }

        protected boolean lowLevelFastqRead(Text key, SequencedFragment value) throws IOException {
            // ID line
            long skipped = lineReader.skip(1); // skip @
            pos += skipped;
            if (skipped == 0) {
                return false; // EOF
            }

            // ID
            readLineInto(key);
            // sequence
            value.clear();
            readLineInto(value.getSequence());
            readLineInto(buffer);
            if (buffer.getLength() == 0 || buffer.getBytes()[0] != '+') {
                throw new RuntimeException("unexpected fastq line separating sequence and quality at "
                        + makePositionMessage() + ". Line: " + buffer + ". \nSequence ID: " + key);
            }
            readLineInto(value.getQuality());

            // look for the Illumina-formatted name.  Once it isn't found lookForIlluminaIdentifier will be set to false
            lookForIlluminaIdentifier = lookForIlluminaIdentifier && scanIlluminaId(key, value);
            if (!lookForIlluminaIdentifier) {
                scanNameForReadNumber(key, value);
            }
            return true;
        }

        /*
         * Reads the next key/value pair from the input for processing.
         */
        public boolean next(Text key, SequencedFragment value) throws IOException {
            if (pos >= end) {
                return false; // past end of slice
            }

            try {
                boolean gotData;
                boolean goodRecord;
                do {
                    gotData = lowLevelFastqRead(key, value);
                    goodRecord = gotData
                            && (!filterFailedQC || value.getFilterPassed() == null || value.getFilterPassed());
                } while (gotData && !goodRecord);

                if (goodRecord) { // goodRecord falso also when we couldn't read any more data
                    if (qualityEncoding == BaseQualityEncoding.Illumina) {
                        try {
                            // convert illumina to sanger scale
                            SequencedFragment.convertQuality(value.getQuality(),
                                    BaseQualityEncoding.Illumina, BaseQualityEncoding.Sanger);
                        } catch (FormatException e) {
                            throw new FormatException(e.getMessage() + " Position: "
                                    + makePositionMessage() + "; Sequence ID: " + key);
                        }
                    } else { // sanger qualities.
                        int outOfRangeElement = SequencedFragment.verifyQuality(value.getQuality(),
                                BaseQualityEncoding.Sanger);
                        if (outOfRangeElement >= 0) {
                            throw new FormatException("Base quality out of range for Sanger Phred+33 format (found "
                                    + (value.getQuality().getBytes()[outOfRangeElement] - FormatConstants.SANGER_OFFSET)
                                    + ").\n"
                                    + "Maybe qualities are in Illumina Phred+64 format?\n"
                                    + "Position: " + makePositionMessage() + "; Sequence ID: " + key);
                        }
                    }
                }
                return goodRecord;
            } catch (EOFException e) {
                throw new RuntimeException("unexpected end of file in fastq record at "
                        + makePositionMessage() + ".  Id: " + key.toString());
            }
        }

        private void scanNameForReadNumber(Text name, SequencedFragment fragment) {
            // look for a /[0-9] at the end of the name
            if (name.getLength() >= 2) {
                byte[] bytes = name.getBytes();
                int last = name.getLength() - 1;

                if (bytes[last - 1] == '/' && bytes[last] >= '0' && bytes[last] <= '9') {
                    fragment.setRead(bytes[last] - '0');
                }
            }
        }

        private boolean scanIlluminaId(Text name, SequencedFragment fragment) {
            Matcher m = ILLUMINA_PATTERN.matcher(name.toString());
            boolean matches = m.matches();
            if (matches) {
                fragment.setInstrument(m.group(1));
                fragment.setRunNumber(Integer.parseInt(m.group(2)));
                fragment.setFlowcellId(m.group(3));
                fragment.setLane(Integer.parseInt(m.group(4)));
                fragment.setTile(Integer.parseInt(m.group(5)));
                fragment.setXpos(Integer.parseInt(m.group(6)));
                fragment.setYpos(Integer.parseInt(m.group(7)));
                fragment.setRead(Integer.parseInt(m.group(8)));
                fragment.setFilterPassed("N".equals(m.group(9)));
                fragment.setControlNumber(Integer.parseInt(m.group(10)));
                fragment.setIndexSequence(m.group(11));
            }
            return matches;
        }

        private int readLineInto(Text dest) throws IOException {
            int bytesRead = lineReader.readLine(dest, MAX_LINE_LENGTH);
            if (bytesRead <= 0) {
                throw new EOFException();
            }
            pos += bytesRead;
            return bytesRead;
        }
    }

    @Override
    public boolean isSplitable(JobContext context, Path path) {
        CompressionCodec codec = new CompressionCodecFactory(ContextUtil.getConfiguration(context)).getCodec(path);
        return codec == null;
    }

    public RecordReader<Text, SequencedFragment> createRecordReader(
            InputSplit genericSplit,
            TaskAttemptContext context) throws IOException, InterruptedException {
        //context.setStatus(genericSplit.toString());
        return new FastqRecordReader(ContextUtil.getConfiguration(context), (FileSplit)genericSplit);
    }
}


text:
#import <Foundation/NSObject.h>

@class NSMutableDictionary, NSMutableArray, NSDictionary;

@interface UIWebFormPasswordsEditor : NSObject {
	NSDictionary* _credentialsByProtectionSpace;
	NSMutableArray* _flattenedCredentials;
	NSMutableDictionary* _credentialToProtectionSpaceMap;
	BOOL _ignoreNotifications;
}
+(id)sharedPasswordsEditor;
-(id)protectionSpaceForCredential:(id)credential;
-(id)addressStringForCredential:(id)credential;
-(void)resetCredentials;
-(void)credentialsChanged:(id)changed;
-(void)startMonitoringCredentials;
-(void)stopMonitoringCredentials;
-(void)dealloc;
-(void)removeSelectedItems:(id)items;
-(void)removeAll;
@end


text:
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
GOBIN=${GOBIN:="$GOPATH/bin"}

function print_real_go_files {
    grep --files-without-match 'DO NOT EDIT!' $(find . -iname '*.go')
}

function generate_markdown {
    echo "Generating Github markdown"
    oldpwd=$(pwd)
    for i in $(find . -iname 'doc.go'); do
        dir=${i%/*}
        realdir=$(realpath $dir)
        package=${realdir##${GOPATH}/src/}
        echo "$package"
        cd ${dir}
        ${GOBIN}/godoc2ghmd -ex -file DOC.md ${package}
        ln -s DOC.md README.md 2> /dev/null # can fail
        cd ${oldpwd}
    done;
}

function goimports_all {
    echo "Running goimports"
    goimports -l -w $(print_real_go_files)
    return $?
}

go get github.com/davecheney/godoc2md

generate_markdown
goimports_all
echo "returning $?"


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

namespace Thinktecture.AuthorizationServer.Models
{
    public static class ClientExtensions
    {
        public static Client Get(this IEnumerable<Client> clients, string clientId)
        {
            return (from c in clients
                    where c.ClientId.Equals(clientId)
                    select c)
                   .FirstOrDefault();
        }

        public static Client ValidateClient(this IEnumerable<Client> clients, string clientId, string clientSecret)
        {
            // todo: hashing etc

            var client = clients.Get(clientId);
            if (client == null)
            {
                return null;
            }

            if (client.ClientSecret == clientSecret)
            {
                return client;
            }

            return null;
        }
    }
}


text:
package io.pivotal.labs.io;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;

public class ProcessResult {

    public static ProcessResult of(String... command) throws IOException, InterruptedException {
        ProcessBuilder processBuilder = new ProcessBuilder(command);
        return of(processBuilder);
    }

    public static ProcessResult of(ProcessBuilder processBuilder) throws IOException, InterruptedException {
        return of(processBuilder, false);
    }

    public static ProcessResult of(ProcessBuilder processBuilder, boolean inheritIo) throws IOException, InterruptedException {
        Process process = processBuilder.start();
        if (inheritIo) {
            new Thread(copy(process.getInputStream(), System.out)).start();
            new Thread(copy(process.getErrorStream(), System.err)).start();
        }
        process.waitFor();
        return of(process);
    }

    private static Runnable copy(InputStream in, PrintStream out) {
        return () -> {
            int b;
            try {
                while ((b = in.read()) != -1) out.write(b);
            } catch (IOException e) {
                out.println();
                e.printStackTrace(out);
            }
        };
    }

    public static ProcessResult of(Process process) throws IOException {
        int exitValue = process.exitValue();
        String output = readFully(process.getInputStream());
        String error = readFully(process.getErrorStream());

        return new ProcessResult(exitValue, output, error);
    }

    private static String readFully(InputStream in) throws IOException {
        ByteArrayOutputStream buf = new ByteArrayOutputStream();
        ResourceUtils.copy(in, buf);
        return buf.toString();
    }

    private final int exitValue;
    private final String output;
    private final String error;

    public ProcessResult(int exitValue, String output, String error) {
        this.exitValue = exitValue;
        this.output = output;
        this.error = error;
    }

    public int getExitValue() {
        return exitValue;
    }

    public String getOutput() {
        return output;
    }

    public String getError() {
        return error;
    }

}