text:
'use strict';

const ResourceBudgetAudit = require('../../audits/performance-budget.js');
const networkRecordsToDevtoolsLog = require('../network-records-to-devtools-log.js');

/* eslint-env jest */

describe('Performance: Resource budgets audit', () => {
  let artifacts;
  let context;
  beforeEach(() => {
    artifacts = {
      devtoolsLogs: {
        defaultPass: networkRecordsToDevtoolsLog([
          {url: 'http://example.com/file.html', resourceType: 'Document', transferSize: 30},
          {url: 'http://example.com/app.js', resourceType: 'Script', transferSize: 10},
          {url: 'http://third-party.com/script.js', resourceType: 'Script', transferSize: 50},
          {url: 'http://third-party.com/file.jpg', resourceType: 'Image', transferSize: 70},
        ]),
      },
      URL: {requestedUrl: 'http://example.com', finalUrl: 'http://example.com'},
    };
    context = {computedCache: new Map(), settings: {}};
  });

  describe('with a budget.json', () => {
    beforeEach(() => {
      context.settings.budgets = [{
        path: '/',
        resourceSizes: [
          {
            resourceType: 'script',
            budget: 0,
          },
          {
            resourceType: 'image',
            budget: 1000,
          },
        ],
        resourceCounts: [
          {
            resourceType: 'script',
            budget: 0,
          },
          {
            resourceType: 'image',
            budget: 1000,
          },
        ],
      }];
    });

    it('includes table columns for requet & file size overages', async () => {
      const result = await ResourceBudgetAudit.audit(artifacts, context);
      expect(result.details.headings).toHaveLength(5);
    });

    it('table item information is correct', async () => {
      const result = await ResourceBudgetAudit.audit(artifacts, context);
      const item = result.details.items[0];
      expect(item.label).toBeDisplayString('Script');
      expect(item.requestCount).toBe(2);
      expect(item.size).toBe(60);
      expect(item.sizeOverBudget).toBe(60);
      expect(item.countOverBudget).toBeDisplayString('2 requests');
    });

    describe('request & transfer size overage', () => {
      it('are displayed', async () => {
        const result = await ResourceBudgetAudit.audit(artifacts, context);
        const scriptRow = result.details.items.find(r => r.resourceType === 'script');
        expect(scriptRow.sizeOverBudget).toBe(60);
        expect(scriptRow.countOverBudget).toBeDisplayString('2 requests');
      });

      it('are empty for passing budgets', async () => {
        const result = await ResourceBudgetAudit.audit(artifacts, context);
        const imageRow = result.details.items.find(r => r.resourceType === 'image');
        expect(imageRow.sizeOverBudget).toBeUndefined();
        expect(imageRow.countOverBudget).toBeUndefined();
      });

      it('convert budgets from kilobytes to bytes during calculations', async () => {
        context.settings.budgets = [{
          path: '/',
          resourceSizes: [
            {
              resourceType: 'document',
              budget: 20,
            },
          ],
        }];
        const result = await ResourceBudgetAudit.audit(artifacts, context);
        expect(result.details.items[0].siveOverBudget).toBeUndefined();
      });
    });

    it('only includes rows for resource types with budgets', async () => {
      const result = await ResourceBudgetAudit.audit(artifacts, context);
      expect(result.details.items).toHaveLength(2);
    });

    it('sorts rows by descending file size overage', async () => {
      context.settings.budgets = [{
        path: '/',
        resourceSizes: [
          {
            resourceType: 'document',
            budget: 0,
          },
          {
            resourceType: 'script',
            budget: 0,
          },
          {
            resourceType: 'image',
            budget: 0,
          },
        ],
      }];
      const result = await ResourceBudgetAudit.audit(artifacts, context);
      const items = result.details.items;
      items.slice(0, -1).forEach((item, index) => {
        expect(item.size).toBeGreaterThanOrEqual(items[index + 1].size);
      });
    });
  });

  describe('budget selection', () => {
    describe('with a matching budget', () => {
      it('applies the correct budget', async () => {
        context.settings.budgets = [{
          path: '/',
          resourceSizes: [
            {
              resourceType: 'script',
              budget: 0,
            },
          ],
        },
        {
          path: '/file.html',
          resourceSizes: [
            {
              resourceType: 'image',
              budget: 0,
            },
          ],
        },
        {
          path: '/not-a-match',
          resourceSizes: [
            {
              resourceType: 'document',
              budget: 0,
            },
          ],
        },
        ];
        const result = await ResourceBudgetAudit.audit(artifacts, context);
        expect(result.details.items[0].resourceType).toBe('image');
      });
    });

    describe('without a matching budget', () => {
      it('returns "audit does not apply"', async () => {
        context.settings.budgets = [{
          path: '/not-a-match',
          resourceSizes: [
            {
              resourceType: 'script',
              budget: 0,
            },
          ],
        },
        ];
        const result = await ResourceBudgetAudit.audit(artifacts, context);
        expect(result.details).toBeUndefined();
        expect(result.notApplicable).toBe(true);
      });
    });

    describe('without a budget.json', () => {
      beforeEach(() => {
        context.settings.budgets = null;
      });

      it('returns "audit does not apply"', async () => {
        const result = await ResourceBudgetAudit.audit(artifacts, context);
        expect(result.details).toBeUndefined();
        expect(result.notApplicable).toBe(true);
      });
    });
  });
});


text:
namespace AonWeb.FluentHttp.Mocks
{
    public class NullMockLogger : IMockLogger
    {
        public void WriteLine(string message) { }
        public void WriteLine(string format, params object[] args) { }
    }
}

redpajama_set_name: RedPajamaGithub


text:
package net.floodlightcontroller.cachemanager;

import java.util.Comparator;

class HTTPRequestComparator implements Comparator<Object>{
	public int compare(Object o1, Object o2) {
		HTTPRequest p1 = (HTTPRequest) o1;
		HTTPRequest p2 = (HTTPRequest) o2;
        return p1.getTimestamp().compareTo(p2.getTimestamp());
    }
}


text:
/**
 * Generated bundle index. Do not edit.
 */
export * from './public-api';
export { MatMenuItemBase as ɵa22, _MatMenuItemMixinBase as ɵb22 } from './menu-item';
export { MAT_MENU_SCROLL_STRATEGY_PROVIDER as ɵd22, MAT_MENU_SCROLL_STRATEGY_PROVIDER_FACTORY as ɵc22 } from './menu-trigger';


text:
<DOMSymbolItem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://ns.adobe.com/xfl/2008/" name="Facet Boxes/ElasticList/elasticListBox" itemID="47012205-00000a3b" linkageExportForAS="true" linkageBaseClass="flash.display.Sprite" linkageClassName="eu.stefaner.elasticlists.ui.facetboxes.elasticlist.ElasticListBox" sourceFlashFilepath="../../../../../../working copy/demos/archiProjects/fla/ArchitecturalProjectsBrowser.fla" sourceLibraryItemHRef="Facet Boxes/ElasticList/elasticListBox" sourceLastModified="1223977037" lastModified="1277385218" lastUniqueIdentifier="15" isSpriteSubclass="true">
  <timeline>
    <DOMTimeline name="elasticListBox">
      <layers>
        <DOMLayer name="Layer 4" color="#FF4FFF">
          <frames>
            <DOMFrame index="0" keyMode="9728">
              <elements/>
            </DOMFrame>
          </frames>
        </DOMLayer>
        <DOMLayer name="Ebene 2" color="#9933CC" current="true" isSelected="true">
          <frames>
            <DOMFrame index="0" keyMode="9728">
              <elements>
                <DOMSymbolInstance libraryItemName="Facet Boxes/facetBox_bg" name="bg" centerPoint3DX="90" centerPoint3DY="295">
                  <matrix>
                    <Matrix a="0.899993896484375" d="0.999862670898438"/>
                  </matrix>
                  <transformationPoint>
                    <Point x="100" y="295.05"/>
                  </transformationPoint>
                </DOMSymbolInstance>
              </elements>
            </DOMFrame>
          </frames>
        </DOMLayer>
      </layers>
    </DOMTimeline>
  </timeline>
</DOMSymbolItem>


text:
<refentry id="author.othername.in.middle">
<refmeta>
<refentrytitle>author.othername.in.middle</refentrytitle>
<refmiscinfo role="type">boolean</refmiscinfo>
</refmeta>
<refnamediv>
<refname>author.othername.in.middle</refname>
<refpurpose>Is <sgmltag>othername</sgmltag> in <sgmltag>author</sgmltag> a
middle name?</refpurpose>
</refnamediv>

<refsynopsisdiv>
<src:fragment id='author.othername.in.middle.frag'><xsl:param name="author.othername.in.middle" select="1"/></src:fragment>
</refsynopsisdiv>

<refsect1><title>Description</title>

<para>If true (non-zero), the <sgmltag>othername</sgmltag> of an <sgmltag>author</sgmltag>
appears between the <sgmltag>firstname</sgmltag> and
<sgmltag>surname</sgmltag>.  Otherwise, <sgmltag>othername</sgmltag>
is suppressed.
</para>

</refsect1>
</refentry>


text:
Slang Project Coding Conventions
================================

Principles
----------

This document attempts to establish conventions to be used in the Slang codebase.
We have two goals for this convention.

The first goal is to make the code look relatively consistent so that it is easy to navigate and understand for contributors.
Having varying styles across different modules, files, functions, or lines of code makes the overall design and intention of the codebase harder to follow.

The second goal is to minimize the scope complexity of diffs when multiple maintainers work together on the codebase.
In the absence of an enforced style, developers tend to "clean up" code they encoutner to match their personal preferences, and in so doing create additional diffs that increase the chances of merge conflicts and pain down the line.

Because the Slang codebase has passed through many hands and evolved without a pre-existing convention, these two goals can come into conflict.
We encourage developers to err on the side of leaving well enough alone (favoring the second goal).
Don't rewrite or refactor code to match these conventions unless you were already going to have to touch all of those lines of code anyway.

Note that external code that is incorporated into the project is excluded from all of these conventions.

Languages
---------

### C++

Most code in the Slang project is implemented in C++.
We currently assume support for some C++11 idioms, but have explicitly avoided adding dependencies on later versions.

As a general rule, be skeptical of "modern C++" ideas unless they are clearly better to simpler alternatives.
We are not quite in the realm of "Orthodox C++", but some of the same guidelines apply:

* Don't use exceptions for non-fatal errors (and even then support a build flag to opt out of exceptions)
* Don't the built-in C++ RTTI system (home-grown is okay)
* Don't use the C++ variants of C headers (e.g., `<cstdio>` instead of `<stdio.h>`)
* Don't use the STL containers
* Don't use iostreams

The compiler implementation does not follow some of these guidelines at present; that should not be taken as an excuse to further the proliferation of stuff like `dynamic_cast`.
Do as we say, not as we do.

Some relatively recent C++ features that are okay to use:

* Rvalue references for "move semantics," but only if you are implementing performance-critical containers or other code where this really matters.

* `auto` on local variables, if the expected type is clear in context

* Lambdas are allowed, but think carefully about whether just declaring a subroutine would also work.

* Using `>>` to close multiple levels of templates, instead of `> >` (but did you really need all those templates?)

* `nullptr`

* `enum class`

* Range-based `for` loops

* `override`

* Default member initializers in `class`/`struct` bodies

Templates are suitable in cases where they improve clarity and type safety.
As a general rule, it is best when templated code is kept minimal, and forwards to a non-templated function that does the real work, to avoid code bloat.

Any use of template metaprogramming would need to prove itself exceptionally useful to pay for the increase in cognitive complexity.
We don't want to be in the business of maintaining "clever" code.

As a general rule, `const` should be used sparingly and only with things that are logically "value types."
If you find yourself having to `const`-qualify a lot of member function in type that you expect to be used as a heap-allocated object, then something has probably gone wrong.

As a general rule, default to making the implementation of a type `public`, and only encapsulate state or operations with `private` when you find that there are complex semantics or invariants that can't be provided without a heavier hand.

### Slang

The Slang project codebase also includes `.slang` files implementing the Slang standard library, as well as various test cases and examples.
The conventions described here are thus the "official" recommendations for how users should format Slang code.

To the extent possible, we will try to apply the same basic conventions to both C++ and Slang.
In places where we decide that the two languages merit different rules, we will point it out.

Files and Includes
------------------

### File Names

All files and directories that are added to codebase should have names that contain only ASCII lower-case letters, digits, dots (`.`) and dashes (`-`).
Operating systems still vary greatly in their handling of case sensitivity for file names, and non-ASCII code points are handled with even less consistency; sticking to a restricted subset of ASCII helps avoids some messy interactions between case-insensitive file systems and case-sensitive source-control systems like Git.
As with all these conventions, files from external projects are exempted from these restrictions.

### Naming of Source and Header Files

In general the C++ codebase should be organized around logical features/modules/subsystem, each of which has a single `.h` file and zero or more `.cpp` files to implement it.

If there is a single `.cpp` file, its name should match the header: e.g., `parser.h` and `parser.cpp`.

If there is more than one `.cpp` file, their names should start with the header name: e.g., `parser.h` and `parser-decls.cpp` and `parser-exprs.cpp`.
If there are declarations that need to be shared by the `.cpp` files, but shouldn't appear in the public interface, then can go in a `*-impl.h` header (e.g., `parser-impl.h`).

Use best judgement when deciding what counts as a "feature." One class per file is almost always overkill, but the codebase currently leans too far in the other direction, with some oversized source files.

### Headers

Every header file should have an include guard.
Within the implementation we can use `#pragma once`, but exported API headers (`slang.h`) should use traditional `#ifdef` style guards (and they should be consumable as both C and C++).

A header should include or forward-declare everything it needs in order to compile.
It is *not* up to the programmer who `#include`s a header to sort out the dependencies.

Avoid umbrella or "catch-all" headers.

### Source Files

Every source file should start by including the header for its feature/module, before any other includes (this helps ensure that the header correctly includes its dependencies).

Functions that are only needed within that one source file can be marked `static`, but we should avoid using the same name for functions in different files (in order to support lumped/unified builds).

### Includes

In general, includes should be grouped as follows:

* First, the correspodning feature/module header, if we are in a source file
* Next, any `<>`-enlosed includes for system/OS headers
* Next, any `""`-enclosed includes for external/third-part code that is stored in the project repository
* Finally, any includes for other features in the project

Within each group, includes should be sorted alphabetically.
If this breaks because of ordering issues for system/OS/third-party headers (e.g., `<Windows.h>` must be included before `<GL/GL.h>`), then ideally those includes should be mediated by a Slang-project-internal header that features can include.

Namespaces
----------

Favor fewer namespaces when possible.
Small programs may not need any.

All library code that a Slang user might link against should go in the `Slang` namespace for now, to avoid any possibility of clashes in a static linking scenario.
The public C API is obviously an exception to this.

Indenting, Spacing, and Braces
------------------------------

Indent by four spaces. Don't use tabs except in files that require them (e.g., `Makefile`s).

Matching opening/closing curly braces should be indented to the same column, with an exception for empty braces (`{}`).
An opening curly brace should always be on a new line.

```c++
void someFunc(int a)
{
    doThings(a);
}

void emptyFunc()
{}

struct Helper
{
    void help()
    {
        emptyFunc();
    }
}
```

Extremely short "accessor" style functions can violate these rules if the whole definition fits on a line.

```c++
struct Vec1
{
    float x;

    float getX() { return x; }
}
```

There is no hard limit on line length, but if you are going past 80-100 columns quite often, maybe think about breaking lines.

When you decide to break lines for a paramter or argument list, always break after the opening `(`, and put each argument/parameter on its own line:

```c++
float bigFunc(
    int     a,
    float   b,
    void*   c)
{
    ...
}

float gVar = bigFunc(
    0,
    1.0f,
    data);
```

You can vertically align succesive lines of code to emphasize common structure, but this is not required:

```c++
case A_AND_B: doA(); doB(); break;
case JUST_A:  doA();        break;
case JUST_B:         doB(); break;
```

Don't put space between a control-flow keyword and a following `(`.
Examples of how to format the major C++ control-flow constructs follow:

```c++
void example()
{
    for(int ii = 0; ii < N; ++ii)
    {
        if(ii == 0)
        {
        }
        else
        {
        }
    }

    int x = 0;
    while(x < 100)
    {
        x++;
    }

    int mode = 0;
    do
    {
        switch(mode)
        {
        case 0:
            x /= 2;
            /* fallthrough */
        case 1:
            x--;
            mode = 3;

        case 2:
        default:
            x++;
            mode--;
            break;
        }
    } while(x > 0);
}
```

Don't put space between a unary operator and its operand.
Always put space between an assignment (including compound assignment) operator and its operands.
For other binary operators, use your best judgement.

If you have to line break a binary expression, put the line break after the operator for an assignment, and before the operator before all others.

Intializer lists for constructors get some special-case formatting rules.
If everything fits on one line, then great:

```c++
SomeClass::SomeClass()
    : a(0), b(1), c(2)
{}
```

Otherwise, put the line break *before* the comma:

```c++
SomeClass::SomeClass()
    : a(0)
    , b(1)
    , c(2)
{}
```

When working with `static const` arrays, put the outer `{}` for the array on their own line, and then put each element on its own line and aim for vertical alignment.

```c++
struct Mapping
{
    char const* key;
    char const* val;
} kMapping[] =
{
    { "a", "aardvark" },
    { "b", "bat" },
};
```

A trailing comma should always be used for array initializer lists like this, as well as for `enum` tags.

When writing multi-line macros, the backslash escapes should align vertically.
For consistentcy, a trailing `\` can be used, followed by a comment to mark the end of the definition:

```c++
#define FOREACH_ANIMAL(X)   \
    X(Aardvark)             \
    X(Bat)                  \
    /* end */
```

Naming
------

### Casing

Types should in general use `UpperCamelCase`. This includes `struct`s, `class`es, `enum`s and `typedef`s.

Values should in general use `lowerCamelCase`. This includes functions, methods, local variables, global variables, parameters, fields, etc.

Macros should in general use `SCREAMING_SNAKE_CASE`.
It is important to prefix all macros (e.g., with `SLANG_`) to avoid collisions, since `namespace`s don't affect macros).

In names using camel case, acronyms and initialisms should appear eniterly in either upper or lower case (e.g., `D3DThing d3dThing`) and not be capitalized as if they were ordinary words (e.g., `D3dThing d3dThing`).
Note that this also applies to uses of "ID" as an abbreviation for "identifier" (e.g., use `nodeID` instead of `nodeId`).

### Prefixes

Prefixes based on types (e.g., `p` for pointers) should never be used.

Global variables should have a `g` prefix, e.g. `gCounter`.
Non-`const` `static` class members can have an `s` prefix if that suits your fancy.
Of course, both of these should be avoided, so this shouldn't come up often.

Constant data (in the sense of `static const`) should have a `k` prefix.

In contexts where "information hiding" is relevant/important, such as when a type has both `public` and `private` members, or just has certain operations/fields that are considered "implementation details" that most clients should not be using, an `m_` prefix on member variables and a `_` prefix on member functions is allowed (but not required).

In function parameter lists, an `in`, `out`, or `io` prefix can be added to a parameter name to indicate whether a pointer/reference/buffer is intended to be used for input, output, or both input and output.
For example:

```c++
void copyData(void* outBuffer, void const* inBuffer, size_t size);

Result lookupThing(Key k, Thing& outThing);

void maybeAppendExtraNames(std::vector<Name>& ioNames);
```

Public C APIs will prefix all symbol names while following the casing convention (e.g. `SlangModule`, `slangLoadModule`, etc.).

### Enums

C-style `enum` should use the following convention:

```c++
enum Color
{
    kColor_Red,
    kColor_Green,
    kColor_Blue,

    kColorCount,
};
```

When using `enum class`, drop the `k` and type name as prefix, but retain the `UpperCamelCase` tag names:

```c++
enum class Color
{
    Red,
    Green,
    Blue,

    Count,
};
```

When defining a set of flags, separate the type definition from the `enum`:

```c++
typedef unsigned int Axes;
enum
{
    kAxes_None = 0,

    kAxis_X = 1 << 0,
    kAxis_Y = 1 << 1,
    kAxis_Z = 1 << 2,

    kAxes_All = kAxis_X | kAxis_Y | kAxis_Z,
};
```

Note that the type name reflects the plural case, while the cases that represent individual bits are named with a singular prefix.

In public APIs, all `enum`s should use the style of separating the type defintion from the `enum`, and all cases should use `SCREAMING_SNAKE_CASE`:

```c++
typedef unsigned int SlangAxes;
enum
{
    SLANG_AXES_NONE = 0,

    SLANG_AXIS_X = 1 << 0,
    SLANG_AXIS_Y = 1 << 1,
    SLANG_AXIS_Z = 1 << 2,

    SLANG_AXES_ALL = SLANG_AXIS_X | SLANG_AXIS_Y | SLANG_AXIS_Z,
};
```

### General

Names should default to the English language and US spellings, to match the dominant conventiosn of contemporary open-source projects.

Function names should either be named with action verbs (`get`, `set`, `create`, `emit`, `parse`, etc.) or read as questions (`isEnabled`, `shouldEmit`, etc.).

Whenever possible, compiler concepts should be named using the most widely-understood term available: e.g., we use `Token` over `Lexeme`, and `Lexer` over `Scanner` simply because they appear to be the more common names.

Avoid abbreviations and initialisms unless they are already widely established across the codebase; a longer name may be cumbersome to write in the moment, but the code will probably be read many more times than it is written, so clarity should be preferred.
An important exception to this is common compiler concepts or techqniues which may have laboriously long names: e.g., Static Single Assignment (SSA), Sparse Conditional Copy Propagation (SCCP), etc.

One gotcha particular to compiler front-ends is that almost every synonym for "type" has some kind of established technical meaning; most notably the term "kind" has a precise meaning that is relevant in our domain.
It is common practice in C and C++ to define tagged union types with a selector field called a "type" or "kind," which does not usually match this technical definition.
If a developer wants to avoid confusion, they are encouraged to use the term "flavor" instead of "type" or "kind" since this term (while a bit silly) is less commonly used in the literature.

Comments and Documentation
--------------------------

You probably know the drill: comments are good, but an out-of-date comment can be worse than no comment at all.
Try to write comments that explain the "why" of your code more than the "what."
When implementing a textbook algorithm or technique, it may help to imagine giving the reviewer of your code a brief tutorial on the topic.

In cases where comments would benefit from formatting, use Markdown syntax.
We do not currently have a setup for extracting documentation from comments, but if we add one we will ensure that it works with Markdown.

When writing comments, please be aware that your words could be read by many people, from a variety of cultures and backgrounds.
Default to a plain-spoken and professional tone and avoid using slang, idiom, profanity, etc.


text:
@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    [MigrationViewController setUpWithWindow:self.window];

    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
}

- (void)applicationWillTerminate:(UIApplication *)application {
}

@end


text:
A custom `StyleSheet` implementation that prevents errors and is open for optimizations.
## Methods
- [create](#createstyles)
- [combineStyles](#combinestylesstyles)

## `create(styles)`
On React Native, `StyleSheet.create` right now is just a dumb container which returns the exact same styles that come into. It just helps to reuse existing React Native Components.
```javascript
import { StyleSheet } from 'react-look-native'

const styles = StyleSheet.create({
	box: {
		color: 'red',
		'@media (min-height: 300px)': {
			color: 'blue',
			// They can also be nested multiple times
			'@media (min-width: 500px)': {
				color: 'gray'
			}
		}
	}
})
```

## `combineStyles(...styles)`
Styles can be combined using the `combineStyles` helper.
It simply deep assigns all styles objects.
```javascript
import { StyleSheet } from 'react-look-native'
// You will most likely use a shortcut
const c = StyleSheet.combineStyles

const styles = StyleSheet.create({
	box: { color: 'red' },
	container: { fontSize: 14 }
})

c(styles.box, styles.container) // => {color: 'red', fontSize: 14}
```


text:
<?php
class ModelModuleMessage extends Model {

	public function insertMessageText($SenderId,$text)
	{
		$sql = "INSERT INTO " . DB_PREFIX . "messageText SET SenderId = '" . $SenderId . "', MessageText =  '" . $text . "'";
		$this->db->query($sql);
		return $this->db->getLastId();
	}

	public function insertMessage($SenderId,$ReceiverId,$MessageTextId)
	{
		$sql = "INSERT INTO " . DB_PREFIX . "message SET SenderId = '" . $SenderId . "',ReceiverId =  '" . $ReceiverId . "' , MessageTextId =  '" . $MessageTextId . "',SendTime =  '" . date('Y-m-d H:i:s',time()) . "'";
		$this->db->query($sql);
	}

	public function getMessages($page = 1)
	{
		$limit = ($page - 1) * 20;

		$sql = "SELECT msg.id,msg.SenderId,msg.ReceiverId,msg.SendTime,msg.ReadFlag,msgt.messageText FROM " . DB_PREFIX . "message msg INNER JOIN " . DB_PREFIX . "messageText msgt ON msg.MessageTextId=msgt.Id WHERE msg.ReceiverId=0 order by msg.ReceiverId asc,msg.id desc limit " . $limit . ",20";
		$query = $this->db->query($sql);
		return $query->rows;
	}

	public function getMessagestation()
	{
		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "message where ReceiverId=0");

		return $query->row['total'];
	}

	public function getUnreadMessagestation()
	{
		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "message where ReadFlag = 0 and ReceiverId=0");

		return $query->row['total'];
	}

	public function setMessageStatus($id,$status)
	{
		return $this->db->query("UPDATE ".DB_PREFIX . "message set ReadFlag = ".(int)$status." where id = ".(int)$id);
	}

	public function getMessageItem($SenderId,$ReceiverId)
	{
		$sql = "select msg.id,msg.SenderId,msg.ReceiverId,msg.SendTime,msg.ReadFlag,msgt.MessageText from " . DB_PREFIX . "message msg," . DB_PREFIX . "messageText msgt where msg.MessageTextId = msgt.id and ((msg.SenderId = ". (int)$SenderId ." and msg.ReceiverId = ". (int)$ReceiverId .") or (msg.ReceiverId = ". (int)$SenderId ." and msg.SenderId = ". (int)$ReceiverId ."))";

		$query = $this->db->query($sql);
		foreach ($query->rows as $key => $value) {
			if($value['ReceiverId'] == $ReceiverId){
				$this->setMessageStatus($value['id'],1);
			}
		}
		return $query->rows;
	}
}
?>


text:
test:
	`which django-admin.py` test --pythonpath=. --settings=assetfiles.tests.settings --with-xunit -s
coverage:
	`which django-admin.py` test --pythonpath=. --settings=assetfiles.tests.settings --with-coverage --cover-package=assetfiles --cover-min-percentage=50


text:
package com.dotosoft.dot4command.testutils;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import org.hamcrest.StringDescription;
import org.junit.Before;
import org.junit.Test;

import com.dotosoft.dot4command.chain.Catalog;
import com.dotosoft.dot4command.chain.Context;
import com.dotosoft.dot4command.testutils.AddingCommand;
import com.dotosoft.dot4command.testutils.DelegatingCommand;
import com.dotosoft.dot4command.testutils.HasCommandCount;

/**
 * @version $Id$
 */
public class HasCommandCountTestCase {

    private Catalog<String, Object, Context<String, Object>> catalog;
    private HasCommandCount matcher;

    @Before
    public void setUp() throws Exception {
        catalog = new TestCatalog<String, Object, Context<String, Object>>();
        matcher = new HasCommandCount(2);
    }

    @Test
    public void wrongCountFails() throws Exception {
        assertFalse(matcher.matchesSafely(catalog, new StringDescription()));
    }

    @Test
    public void correctCount() throws Exception {
        catalog.addCommand("addingCMD", new AddingCommand());
        catalog.addCommand("DelegatingCMD", new DelegatingCommand());

        assertTrue(matcher.matchesSafely(catalog, new StringDescription()));
    }

    @Test(expected = IllegalStateException.class)
    public void inconsistentCatalogThrowsException() throws Exception {
        catalog.addCommand("key for null", null);

        matcher.matchesSafely(catalog, new StringDescription());
    }
}


text:
<!DOCTYPE html>

<meta charset="utf-8"/>
<title>EUC-JP decoding errors</title>
<meta name="timeout" content="long">

<link rel="author" title="Richard Ishida" href="mailto:ishida@w3.org">
<link rel="help" href="https://encoding.spec.whatwg.org/#euc-jp">
<meta name="assert" content="The browser decodes characters that are not recognised from the euc-jp encoding as replacement characters.">


text:
#ifndef VR_SEURAT_INGEST_POINT_CLOUD_ASSEMBLER_H_
#define VR_SEURAT_INGEST_POINT_CLOUD_ASSEMBLER_H_

#include <memory>

#include "ion/math/vector.h"
#include "json/json.h"
#include "seurat/geometry/point_cloud_builder.h"
#include "seurat/image/ldi.h"
#include "seurat/ingest/view_group_loader.h"

namespace seurat {
namespace ingest {

// PointCloudAssembler converts structured view data into a point cloud in
// parallel.
class PointCloudAssembler {
 public:
  PointCloudAssembler(int thread_count,
                      std::unique_ptr<ViewGroupLoader> view_group_loader,
                      std::unique_ptr<geometry::PointCloudBuilder> builder)
      : thread_count_(thread_count),
        view_group_loader_(std::move(view_group_loader)),
        builder_(std::move(builder)) {}

  // Creates the vector of world-space positions.
  base::Status Build(std::vector<ion::math::Point3f>* positions,
                     std::vector<float>* weights) const;

  // Computes and returns a vector with the world-space positions of all points
  // in the view group represented by |cameras| and |ldis|.
  static std::vector<ion::math::Point3f> PositionsFromView(
      int thread_count, const base::Camera& camera, const image::Ldi4f& ldi);

 private:
  // Maximum number of threads to use.
  int thread_count_;

  // The source of color, depth, and camera data.
  std::unique_ptr<ViewGroupLoader> view_group_loader_;

  // The point cloud builder used for the conversion.
  std::unique_ptr<geometry::PointCloudBuilder> builder_;
};

}  // namespace ingest
}  // namespace seurat

#endif  // VR_SEURAT_INGEST_POINT_CLOUD_ASSEMBLER_H_


text:
.. _memory_pools_v2:

Memory Pools
############

A :dfn:`memory pool` is a kernel object that allows memory blocks
to be dynamically allocated from a designated memory region.
The memory blocks in a memory pool can be of any size,
thereby reducing the amount of wasted memory when an application
needs to allocate storage for data structures of different sizes.
The memory pool uses a "buddy memory allocation" algorithm
to efficiently partition larger blocks into smaller ones,
allowing blocks of different sizes to be allocated and released efficiently
while limiting memory fragmentation concerns.

.. contents::
    :local:
    :depth: 2

Concepts
********

Any number of memory pools can be defined. Each memory pool is referenced
by its memory address.

A memory pool has the following key properties:

* A **minimum block size**, measured in bytes.
  It must be at least 4X bytes long, where X is greater than 0.

* A **maximum block size**, measured in bytes.
  This should be a power of 4 times larger than the minimum block size.
  That is, "maximum block size" must equal "minimum block size" times 4^Y,
  where Y is greater than or equal to zero.

* The **number of maximum-size blocks** initially available.
  This must be greater than zero.

* A **buffer** that provides the memory for the memory pool's blocks.
  This must be at least "maximum block size" times
  "number of maximum-size blocks" bytes long.

The memory pool's buffer must be aligned to an N-byte boundary, where
N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...). To ensure that
all memory blocks in the buffer are similarly aligned to this boundary,
the minimum block size must also be a multiple of N.

A thread that needs to use a memory block simply allocates it from a memory
pool. Following a successful allocation, the :c:data:`data` field
of the block descriptor supplied by the thread indicates the starting address
of the memory block. When the thread is finished with a memory block,
it must release the block back to the memory pool so the block can be reused.

If a block of the desired size is unavailable, a thread can optionally wait
for one to become available.
Any number of threads may wait on a memory pool simultaneously;
when a suitable memory block becomes available, it is given to
the highest-priority thread that has waited the longest.

Unlike a heap, more than one memory pool can be defined, if needed. For
example, different applications can utilize different memory pools; this
can help prevent one application from hijacking resources to allocate all
of the available blocks.

Internal Operation
==================

A memory pool's buffer is an array of maximum-size blocks,
with no wasted space between the blocks.
Each of these "level 0" blocks is a *quad-block* that can be
partitioned into four smaller "level 1" blocks of equal size, if needed.
Likewise, each level 1 block is itself a quad-block that can be partitioned
into four smaller "level 2" blocks in a similar way, and so on.
Thus, memory pool blocks can be recursively partitioned into quarters
until blocks of the minimum size are obtained,
at which point no further partitioning can occur.

A memory pool keeps track of how its buffer space has been partitioned
using an array of *block set* data structures. There is one block set
for each partitioning level supported by the pool, or (to put it another way)
for each block size. A block set keeps track of all free blocks of its
associated size using an array of *quad-block status* data structures.

When an application issues a request for a memory block,
the memory pool first determines the size of the smallest block
that will satisfy the request, and examines the corresponding block set.
If the block set contains a free block, the block is marked as used
and the allocation process is complete.
If the block set does not contain a free block,
the memory pool attempts to create one automatically by splitting a free block
of a larger size or by merging free blocks of smaller sizes;
if a suitable block can't be created, the allocation request fails.

The memory pool's merging algorithm cannot combine adjacent free
blocks of different sizes, nor can it merge adjacent free blocks of
the same size if they belong to different parent quad-blocks. As a
consequence, memory fragmentation issues can still be encountered when
using a memory pool.

When an application releases a previously allocated memory block it is
combined synchronously with its three "partner" blocks if possible,
and recursively so up through the levels.  This is done in constant
time, and quickly, so no manual "defragmentation" management is
needed.

Implementation
**************

Defining a Memory Pool
======================

A memory pool is defined using a variable of type :c:type:`struct k_mem_pool`.
However, since a memory pool also requires a number of variable-size data
structures to represent its block sets and the status of its quad-blocks,
the kernel does not support the runtime definition of a memory pool.
A memory pool can only be defined and initialized at compile time
by calling :c:macro:`K_MEM_POOL_DEFINE`.

The following code defines and initializes a memory pool that has 3 blocks
of 4096 bytes each, which can be partitioned into blocks as small as 64 bytes
and is aligned to a 4-byte boundary.
(That is, the memory pool supports block sizes of 4096, 1024, 256,
and 64 bytes.)
Observe that the macro defines all of the memory pool data structures,
as well as its buffer.

.. code-block:: c

    K_MEM_POOL_DEFINE(my_pool, 64, 4096, 3, 4);

Allocating a Memory Block
=========================

A memory block is allocated by calling :cpp:func:`k_mem_pool_alloc()`.

The following code builds on the example above, and waits up to 100 milliseconds
for a 200 byte memory block to become available, then fills it with zeroes.
A warning is issued if a suitable block is not obtained.

Note that the application will actually receive a 256 byte memory block,
since that is the closest matching size supported by the memory pool.

.. code-block:: c

    struct k_mem_block block;

    if (k_mem_pool_alloc(&my_pool, &block, 200, 100) == 0)) {
        memset(block.data, 0, 200);
	...
    } else {
        printf("Memory allocation time-out");
    }

Memory blocks may also be allocated with :cpp:func:`malloc()`-like semantics
using :cpp:func:`k_mem_pool_malloc()`. Such allocations must be freed with
:cpp:func:`k_free()`.

Releasing a Memory Block
========================

A memory block is released by calling either :cpp:func:`k_mem_pool_free()`
or :cpp:func:`k_free()`, depending on how it was allocated.

The following code builds on the example above, and allocates a 75 byte
memory block, then releases it once it is no longer needed. (A 256 byte
memory block is actually used to satisfy the request.)

.. code-block:: c

    struct k_mem_block block;

    k_mem_pool_alloc(&my_pool, &block, 75, K_FOREVER);
    ... /* use memory block */
    k_mem_pool_free(&block);

Thread Resource Pools
*********************

Certain kernel APIs may need to make heap allocations on behalf of the
calling thread. For example, some initialization APIs for objects like
pipes and message queues may need to allocate a private kernel-side buffer,
or objects like queues may temporarily allocate kernel data structures
as items are placed in the queue.

Such memory allocations are drawn from memory pools that are assigned to
a thread. By default, a thread in the system has no resource pool and
any allocations made on its behalf will fail. The supervisor-mode only
:cpp:func:`k_thread_resource_pool_assign()` will associate any implicit
kernel-side allocations to the target thread with the provided memory pool,
and any children of that thread will inherit this assignment.

If a system heap exists, threads may alternatively have their resources
drawn from it using the :cpp:func:`k_thread_system_pool_assign()` API.

Suggested Uses
**************

Use a memory pool to allocate memory in variable-size blocks.

Use memory pool blocks when sending large amounts of data from one thread
to another, to avoid unnecessary copying of the data.

APIs
****

The following memory pool APIs are provided by :file:`kernel.h`:

* :c:macro:`K_MEM_POOL_DEFINE`
* :cpp:func:`k_mem_pool_alloc()`
* :cpp:func:`k_mem_pool_free()`
* :cpp:func:`k_mem_pool_malloc()`
* :cpp:func:`k_free()`
* :cpp:func:`k_thread_resource_pool_assign()`
* :cpp:func:`k_thread_system_pool_assign()`


text:
namespace hobbes {

// the file header
struct filehead {
  uint32_t magic;    // trivial sanity check for file-type
  uint16_t pagesize; // how large does this file assume that one memory page is?
  uint16_t version;  // a file format version number (incremented when incompatibilities are introduced)
};
#define CURRENT_FILE_FORMAT_VERSION ((uint16_t)1)

// a single system page record
//   this will be stored as two bytes, the upper 2 bits used for storing a page type and the lower 14 used for free size in that page
struct pagetype {
  enum code {
    null        = 0, // a "null" page (used to mark the end of system page data)
    toc         = 1, // a "toc" page (a page of data about other pages)
    environment = 2, // an "environment" page (describing variable bindings)
    data        = 3  // a "data" page (raw data for bound variables)
  };

  static uint8_t encode(code    x) { return (uint8_t)x; }
  static code    decode(uint8_t x) { return (code)   x; }
};

struct pagedata {
  uint16_t ptfd;

  pagedata(pagetype::code ty = pagetype::toc, uint16_t sz = 0) : ptfd(encode(ty, sz)) { }
  static uint16_t encode(pagetype::code ty, uint16_t sz) { return ((pagetype::encode(ty) & 3) << 14) | (sz & 0x3FFF); }
  pagetype::code type() const { return pagetype::decode(this->ptfd >> 14); }
  uint16_t size() const { return this->ptfd & 0x3FFF; }
  void type(pagetype::code x) { this->ptfd = encode(x, size()); }
  void size(uint16_t x) { this->ptfd = encode(type(), x); }
};
typedef std::vector<pagedata> pagetable;

static const size_t   minPageSize     = 256;
static const size_t   maxPageSize     = ((1 << 14) - 1); // we take the upper 2 bits to store page type
static const uint32_t filePrefixBytes = 0x10a1db0d;
static const size_t   pageDataOffset  = sizeof(filehead); // page data begins just past the header

// the index of a page of data in the underlying file
typedef uint64_t file_pageindex_t;

// convenience for raising errors out of errno
void dbglog(const std::string&);
static void raiseSysError(const std::string& msg, const std::string& fname) {
  std::ostringstream ss;
  ss << fname << ": " << msg << " (" << strerror(errno) << ")" << std::flush;
  dbglog(ss.str());
  throw std::runtime_error(ss.str());
}

// a stored name/value binding
struct binding {
  binding(const MonoTypePtr& type = MonoTypePtr(), size_t offset = 0, size_t boffset = 0) : type(type), offset(offset), boffset(boffset) { } // happy Josh?

  MonoTypePtr type;    // what type of data is stored here?
  size_t      offset;  // where is it stored?
  size_t      boffset; // where is this binding data stored in the file?
};

typedef std::map<std::string, binding> bindingset;

// a mem-mapped file region
struct fregion {
  file_pageindex_t base_page; // the base file page for this mapping
  size_t           pages;     // the number of mapped pages
  char*            base;      // the base address of this mapped region
  size_t           used;      // the number of bytes actually used out of this mapped region (when decremented to 0 we can safely unmap)
};

// account for mappings by absolute page
typedef std::map<file_pageindex_t, fregion> fmappings;

// remember which mapped sections correspond to which pages
struct falloc {
  file_pageindex_t page; // the page that this was allocated out of
  size_t           size; // the number of bytes allocated to this thing
};

typedef std::map<char*, falloc> fallocs;

// fast page searches (to avoid linear searches of the entire page table)
typedef std::vector<file_pageindex_t>     pageseq;
typedef std::map<pagetype::code, pageseq> ptyorder;

// an image file, opened either for reading or writing
struct imagefile {
  imagefile() : fd(-1) { }

  // stable open file properties
  std::string path;
  bool        readonly;
  int         fd;
  uint16_t    page_size;
  uint16_t    version;

  // mutable incremental read/write state
  size_t file_size;
  size_t head_toc_pos; // the head position for writing TOC entries

  // the fewest number of pages to mmap at one time
  // (this prevents us from making too many tiny mmap regions and hitting the OS map limit)
  size_t mmapPageMultiple;

  // toc page -> absolute page
  pageseq tocpages;

  // system, environment, data
  pagetable  pages;
  ptyorder   freespace;
  bindingset bindings;
  fmappings  mappings;
  fallocs    allocs;
};

// how many bytes are remaining in the page for a given index?
uint16_t restInPage(const imagefile* f, size_t idx) {
  return f->page_size - (idx % f->page_size);
}

// how many pages are covered by a sequence of bytes?
size_t pageCount(const imagefile* f, size_t sz) {
  return (sz / f->page_size) + ((sz % f->page_size) > 0 ? 1 : 0);
}

static void closeFile(imagefile* f) {
  if (f->fd > -1) {
    close(f->fd);
  }
  delete f;
}

static bool loadFileSize(imagefile* f) {
  struct stat sb;
  if (fstat(f->fd, &sb) < 0) {
    return false;
  }
  f->file_size = sb.st_size;
  return true;
}

static void seekAbs(const imagefile* f, size_t pos) {
  if (lseek(f->fd, pos, SEEK_SET) == -1) {
    raiseSysError("Can't seek to offset=" + str::from(pos) + " in file with size=" + str::from(f->file_size), f->path);
  }
}

static off_t filePosition(const imagefile* f) {
  off_t r = lseek(f->fd, 0, SEEK_CUR);
  if (r == (off_t)-1) {
    raiseSysError("Can't query position in file", f->path);
  }
  return r;
}

static void allocPages(imagefile* f, size_t pages) {
  size_t nsz = f->file_size + pages * f->page_size;
  if (ftruncate(f->fd, nsz) == -1) {
    raiseSysError("Can't resize file", f->path);
  }
  f->file_size = nsz;
}

static void allocPage(imagefile* f) {
  allocPages(f, 1);
}

// trivial read and write to files, assuming type T is POD
template <typename T>
  void write(imagefile* f, const T& x) {
    ssize_t wr = ::write(f->fd, (const void*)&x, sizeof(T));
    if ((wr == (ssize_t)-1) || (wr != sizeof(T))) {
      raiseSysError("Failed to write " + str::demangle<T>() + " to file", f->path);
    }
  }

template <typename TIter>
  void writes(imagefile* f, TIter begin, TIter end) {
    size_t  bsz = sizeof(*begin) * (end - begin);
    ssize_t wr  = ::write(f->fd, (const void*)&(*(begin)), bsz);
    if ((wr == (ssize_t)-1) || (wr != bsz)) {
      raiseSysError("Failed to write " + str::demangle< decltype(*begin) >() + " sequence to file", f->path);
    }
  }

void write(imagefile* f, const std::string& x) {
  write(f, x.size());
  writes(f, x.data(), x.data() + x.size());
}

void write(imagefile* f, const std::vector<unsigned char>& xs) {
  write(f, xs.size());
  writes(f, xs.begin(), xs.end());
}

template <typename T>
  void read(const imagefile* f, T* x) {
    ssize_t rr = ::read(f->fd, (void*)x, sizeof(T));
    if ((rr == (ssize_t)-1) || (rr != sizeof(T))) {
      raiseSysError("Failed to read " + str::demangle<T>() + " from file", f->path);
    }
  }

template <typename T>
  void reads(const imagefile* f, size_t sz, T* x) {
    size_t  bsz = sz * sizeof(T);
    ssize_t rr  = ::read(f->fd, (void*)x, bsz);
    if ((rr == (ssize_t)-1) || (rr != bsz)) {
      raiseSysError("Failed to read " + str::demangle<T>() + " sequence from file", f->path);
    }
  }

void read(imagefile* f, std::string* x) {
  size_t sz = 0;
  read(f, &sz);

  x->resize(sz);
  if (sz > 0) {
    reads(f, sz, &((*x)[0]));
  }
}

void read(imagefile* f, std::vector<unsigned char>* xs) {
  size_t sz = 0;
  read(f, &sz);

  xs->resize(sz);
  if (sz > 0) {
    reads(f, sz, &((*xs)[0]));
  }
}

// the absolute position of an indexed page
inline size_t pageOffset(const imagefile* f, file_pageindex_t page) {
  return f->page_size * page;
}

// the page index implied by an absolute position
inline file_pageindex_t pageIndex(const imagefile* f, uint64_t fpos) {
  return fpos / f->page_size;
}

// an absolute file position from a page and relative position
static size_t position(const imagefile* f, file_pageindex_t page, uint16_t offset) {
  return pageOffset(f, page) + ((uint64_t)offset);
}

// find the file page for a given TOC page
file_pageindex_t tocPageToFilePage(const imagefile* f, uint64_t tpage) {
  assert(tpage < f->tocpages.size());
  return f->tocpages[tpage];
}

// the absolute position for some TOC data
static uint64_t tocPosToFilePos(const imagefile* f, uint64_t spos) {
  uint64_t page   = spos / f->page_size;
  uint64_t offset = spos % f->page_size;

  return (tocPageToFilePage(f, page) * f->page_size) + offset;
}

// the absolute file position of a page's entry in the page table
static size_t pageTOCPosition(const imagefile* f, file_pageindex_t page) {
  // this is how many page entries fit in the first TOC page (which includes the file header)
  const uint64_t firstTPCount = (f->page_size - sizeof(filehead) - sizeof(file_pageindex_t)) / sizeof(pagedata);

  // is the given page in the first TOC page?
  if (page < firstTPCount) {
    // if so, its TOC entry offset is just straight past the header
    return sizeof(filehead) + (page * sizeof(pagedata));
  } else {
    // ok, let's forget about the TOC entries from the first page
    page -= firstTPCount;

    // this is how many page entries fit in every subsequent TOC page
    const uint64_t restTPCount = (f->page_size - sizeof(file_pageindex_t)) / sizeof(pagedata);

    // from this we can find the TOC page that we belong on and the offset within that page
    const uint64_t tpage  = (page / restTPCount) + 1; // +1 because we've covered the first page already
    const uint64_t offset = (page % restTPCount) * sizeof(pagedata);

    // and that's all we need for the file position of this TOC entry
    return (tocPageToFilePage(f, tpage) * f->page_size) + offset;
  }
}

// rearrange our free list to accomodate this page resize
static void updatePageSizeIndex(imagefile* f, file_pageindex_t page) {
  const pagedata& pd = f->pages[page];
  pageseq& pord = f->freespace[pd.type()];

  for (size_t k = 0; k < pord.size(); ++k) {
    if (pord[k] == page) {
      for (size_t i = k; i < pord.size() - 1; ++i) {
        if (f->pages[pord[i]].size() < f->pages[pord[i+1]].size()) {
          std::swap(pord[i], pord[i+1]);
        } else {
          break;
        }
      }
      break;
    }
  }
}

// insert this page (assumed new) into our size-index
//  (if it's too small to bother with, don't bother remembering it)
static void insertPageSizeIndex(imagefile* f, file_pageindex_t page) {
  static const size_t minPageSize   = 30;
  static const size_t maxPageMemory = 200;

  const pagedata& pd = f->pages[page];
  if (pd.size() < minPageSize) return;

  // figure out where to put this page
  pageseq& pord = f->freespace[pd.type()];
  size_t k = pord.size();

  for (size_t i = 0; i < pord.size(); ++i) {
    if (pd.size() > f->pages[i].size()) {
      k = i;
      break;
    }
  }

  pord.insert(pord.begin() + k, page);
  if (pord.size() > maxPageMemory) { pord.resize(maxPageMemory); }
}

// try to find a page with as much free space as requested
static bool findPageWithSpace(imagefile* f, pagetype::code pt, size_t datalen, size_t alignment, file_pageindex_t* idx) {
  const pageseq& pord = f->freespace[pt];
  if (pord.size() == 0) {
    return false;
  }

  const auto& pd  = f->pages[pord[0]];
  size_t      pad = (f->page_size - pd.size()) % alignment;

  if ((datalen + pad) > pd.size()) {
    return false;
  } else {
    *idx = pord[0];
    return true;
  }
}

// update the TOC entry for a given page
static void updateTOCData(imagefile* f, file_pageindex_t page, const pagedata& pd) {
  // pages never suddenly get free space
  pagedata& opd = f->pages[page];
  assert(pd.size() <= opd.size());

  // update the page table data in memory and on disk
  opd = pd;
  seekAbs(f, pageTOCPosition(f, page));
  write(f, pd);

  // re-evaluate where this page belongs in the ordering of pages with free space
  updatePageSizeIndex(f, page);
}

// append a sequence of TOC entries (representing allocated pages)
static void appendTOCData(imagefile* f, const pagetable& newpages) {
  // add these new pages to the TOC
  for (const auto& newpage : newpages) {
    f->pages.push_back(newpage);

    // maintain an index of pages by size
    insertPageSizeIndex(f, f->pages.size() - 1);

    // keep track of where system pages are
    if (newpage.type() == pagetype::toc) {
      f->tocpages.push_back(f->pages.size() - 1);
    }
  }

  // we may allocate new TOC entry pages as we go about writing these entries
  pagetable newtocpages;

  // now let's write the new pages to the TOC, allocating new pages for the TOC as necessary
  size_t idx = 0;
  while (idx < newpages.size()) {
    // the write position within this page
    uint16_t relpos = f->head_toc_pos % f->page_size;

    // the number of bytes available to write in this page
    //  (account for the last bit of the page used to link to the next system page)
    size_t bytes_avail = (f->page_size - relpos) - sizeof(file_pageindex_t);

    // the number of pagedata slots available to write in this page
    size_t slots_avail = bytes_avail / sizeof(pagedata);

    // the number of pagedata slots that we have left to write
    size_t slots_left = newpages.size() - idx;

    // the number of pagedata slots that we will fill on this cycle
    size_t slots_write = std::min(slots_avail, slots_left);

    // the number of bytes written
    size_t bytes_written = slots_write * sizeof(pagedata);

    // move to this page offset, and write all of the pagedata entries that we can
    seekAbs(f, f->head_toc_pos);
    writes(f, newpages.begin() + idx, newpages.begin() + idx + slots_write);

    // advance our read heads as far as we've just written
    idx += slots_write;
    f->head_toc_pos += slots_write * sizeof(pagedata);

    // have we written up to the link section of the page?
    // if so, we need to make a new system page and link to it
    file_pageindex_t nextpage = -1;

    if (restInPage(f, f->head_toc_pos) == sizeof(file_pageindex_t)) {
      // make a new TOC page and move the TOC write head to it
      f->head_toc_pos = f->file_size;
      newtocpages.push_back(pagedata(pagetype::toc, 0));
      allocPage(f);

      // link this finished TOC page to the new TOC page
      nextpage = f->pages.size() + newtocpages.size() - 1;
      write(f, nextpage);
    }
  }

  // if we added TOC pages while writing the input TOC entries, then TOC entries for those TOC pages need to be added too
  if (newtocpages.size() > 0) {
    appendTOCData(f, newtocpages);
  }
}

// find a location within pages of a given type where we can put a value with this length/alignment
// (if no such space can be found, allocate new page(s) as necessary)
size_t findSpace(imagefile* f, pagetype::code pt, size_t datalen, size_t alignment) {
  assert(datalen > 0 && alignment > 0);

  // can we find an existing page with the space that we need?
  file_pageindex_t fpage = -1;
  if (findPageWithSpace(f, pt, datalen, alignment, &fpage)) {
    pagedata& pd = f->pages[fpage];

    uint64_t offset = f->page_size - pd.size();
    uint64_t pad    = offset % alignment;

    updateTOCData(f, fpage, pagedata(pt, pd.size() - (datalen+pad)));
    return position(f, fpage, offset+pad);
  }

  // if we got here, then we need to allocate one or more contiguous pages for this data
  // all allocated pages will have 0 free size, except the last one which will leave the remainder
  size_t    pages = pageCount(f, datalen);
  size_t    result = f->file_size;
  pagetable tocdata;

  // every intermediate page in this allocation will be fully exhausted
  for (size_t p = 1; p < pages; ++p) {
    tocdata.push_back(pagedata(pt, 0));
  }

  // the last page of the allocation is only fully exhausted if an exact multiple of the page size has been allocated
  // otherwise claim only the remainder as allocated
  uint64_t lpallocd = datalen % f->page_size;

  if (lpallocd == 0) {
    tocdata.push_back(pagedata(pt, 0));
  } else {
    tocdata.push_back(pagedata(pt, f->page_size - lpallocd));
  }
  allocPages(f, pages);

  // keep track of these pages that we've added
  appendTOCData(f, tocdata);

  return result;
}

// add a variable binding (name, type, size, and file location)
static void addBinding(imagefile* f, const std::string& vname, const MonoTypePtr& type, size_t offset) {
  // prepare the binding data to save
  std::vector<unsigned char> etype;
  encode(type, &etype);

  // determine how much space we'll need to store it
  size_t bsz = sizeof(size_t)                // the stored data offset
             + sizeof(size_t) + vname.size() // the variable name
             + sizeof(size_t) + etype.size() // the variable's type
             ;

  // find the best place to put this data
  size_t boffset = findSpace(f, pagetype::environment, bsz, 1);

  // now just write the data there
  seekAbs(f, boffset);
  write(f, offset);
  write(f, vname);
  write(f, etype);

  // oh and we'll want to keep track of it too
  f->bindings[vname] = binding(type, offset, boffset);
}

// mmap a region out of this file
fregion& createFileRegionMap(imagefile* f, file_pageindex_t page, size_t pages) {
  // leave no gaps in page mappings
  if (f->mappings.size() > 0) {
    fregion& mr = f->mappings.rbegin()->second;

    size_t pend = mr.base_page + mr.pages;
    if (pend < page) {
      pages += (page - pend);
      page   = pend;
    }
  }

  // adjust our map page count to match the set increment
  pages = align<size_t>(pages, f->mmapPageMultiple);

  // map the specified file region into memory
  char* d = (char*)mmap(0, pages * f->page_size, PROT_READ | (f->readonly ? 0 : PROT_WRITE), MAP_SHARED, f->fd, page * f->page_size);
  if (d == MAP_FAILED) {
    raiseSysError
    (
      "Failed to map " + str::from(pages) +
      " pages from page " + str::from(page) +
      " out of " + str::from(f->file_size) +
      " bytes with a page size of " + str::from(f->page_size) +
      " bytes",
      f->path
    );
  }

  fregion& r = f->mappings[page];
  r.base_page = page;
  r.pages     = pages;
  r.base      = d;
  r.used      = 0;
  return r;
}

// munmap a region out of this file
void releaseFileRegionMap(imagefile* f, const fregion& fr) {
  if (munmap(fr.base, fr.pages * f->page_size) != 0) {
    raiseSysError("Failed to unmap page " + str::from(fr.base_page) + " from file", f->path);
  }
}

// the greatest map position <= a point
template <typename K, typename V>
  typename std::map<K, V>::iterator gleb(std::map<K, V>& m, const K& x) {
    auto r = m.lower_bound(x);

    if (r == m.end()) {
      if (m.size() > 0) {
        --r;
      }
    } else if (r->first != x) {
      --r;
    }
    return r;
  }

// find the mapping data for a region, or create it if necessary
fregion& mappedFileRegion(imagefile* f, file_pageindex_t page, size_t pages) {
  // try to find the nearest possible mapping for this page
  fmappings::iterator fm = gleb(f->mappings, page);

  // if we couldn't find a mapping, then we have to make one
  if (fm == f->mappings.end()) {
    return createFileRegionMap(f, page, pages);
  }
  fregion& r = fm->second;

  // if we found a mapping, and the requested region is in it, ship it!
  if (page >= r.base_page && (page + pages) <= (r.base_page + r.pages)) {
    return r;
  }

  // otherwise, we just need to make a new one
  return createFileRegionMap(f, page, pages);
}

// allocate a region of this file as mapped memory
static char* mapFileData(imagefile* f, size_t fpos, size_t sz) {
  file_pageindex_t page   = fpos / f->page_size;
  uint16_t         offset = fpos % f->page_size;

  // get the mapped region where this data lives
  // and increment its use count
  fregion& r = mappedFileRegion(f, page, pageCount(f, sz));
  r.used += sz;

  // the result will be offset from the base of the mapped page
  // (plus any intervening pages from the base of the mapping to the page for this data)
  char* result = r.base + (f->page_size * (page - r.base_page)) + offset;

  // remember where this allocated data came from (in case we want to release it later)
  falloc& fa = f->allocs[result];
  fa.page = r.base_page;
  fa.size = sz;

  return result;
}

// deallocate memory mapped out of this file
// (if this means that there are no outstanding references to the mapping, then the mapping itself is released)
static void unmapFileData(imagefile* f, void* p, size_t sz) {
  fallocs::iterator fa = f->allocs.find((char*)p);
  if (fa == f->allocs.end()) {
    return;
  }

  // remember what page mapping this allocation was out of, but forget the mapping
  file_pageindex_t dpage = fa->second.page;
  f->allocs.erase(fa);

  // dereference these bytes from the page mapping
  // if the page mapping has no references, we can remove the page mapping too
  fmappings::iterator fm = f->mappings.find(dpage);
  if (fm == f->mappings.end()) {
    throw std::runtime_error("Internal error, inconsistent file mapping state");
  }

  if (fm->second.used > sz) {
    fm->second.used -= sz;
  } else {
    releaseFileRegionMap(f, fm->second);
    f->mappings.erase(fm);
  }
}

// we shouldn't ever work with files that have invalid page sizes
uint16_t assertValidPageSize(const imagefile* f, size_t psize) {
  if (psize < minPageSize) {
    throw std::runtime_error(f->path + ": System page size too small for db support (" + str::from(psize) + ")");
  } else if (psize > maxPageSize) {
    throw std::runtime_error(f->path + ": System page size too large for db support (" + str::from(psize) + ")");
  } else if ((psize % sizeof(pagedata)) != 0) {
    throw std::runtime_error(f->path + ": System page size must be a multiple of " + str::from(sizeof(pagedata)) + " (" + str::from(psize) + ")");
  } else if ((sizeof(filehead) % sizeof(pagedata)) != 0) {
    // should be a static assert :T
    throw std::runtime_error("No page size is valid, file format internally inconsistent");
  }
  return (uint16_t)psize;
}

// put a new file into a valid empty state
static void createFile(imagefile* f) {
  if (f->readonly) {
    throw std::runtime_error("Can't initialize empty file for read: " + f->path);
  }
  f->page_size = assertValidPageSize(f, sysconf(_SC_PAGESIZE));
  f->version   = CURRENT_FILE_FORMAT_VERSION;

  // start the first page!
  allocPage(f);
  seekAbs(f, 0);

  // the first page of the file begins with a standard header
  filehead fh;
  memset(&fh, 0, sizeof(fh));
  fh.magic    = filePrefixBytes;
  fh.pagesize = f->page_size;
  fh.version  = f->version;
  write(f, fh);

  // now begin the page table, starting with a description of this page
  f->pages.push_back(pagedata(pagetype::toc, 0));
  write(f, f->pages.back());
  f->tocpages.push_back(0);

  // and point the TOC head here
  f->head_toc_pos = sizeof(filehead) + sizeof(pagedata);
}

// read all of the page data entries possible from the current file position
void readPageData(imagefile* f) {
  // we stop reading page data when we hit the 0 page
  // and if we get to the end of a page, there's a link to the next page
  while (true) {
    pagedata pd;
    read(f, &pd);
    if (pd.type() == pagetype::null) {
      break;
    }
    f->pages.push_back(pd);
    f->head_toc_pos += sizeof(pagedata);

    if (restInPage(f, f->head_toc_pos) == sizeof(file_pageindex_t)) {
      file_pageindex_t nextpage = -1;
      read(f, &nextpage);
      f->head_toc_pos = pageOffset(f, nextpage);
      f->tocpages.push_back(nextpage);
      seekAbs(f, f->head_toc_pos);
    }
  }
}

// the representation of file reference types changed after v0
struct mapFileRefs : public switchTyFn {
  MonoTypePtr with(const TApp* ap) const {
    if (ap->args().size() == 2) {
      if (const Prim* f = is<Prim>(ap->fn())) {
        if (f->name() == "fileref") {
          return MonoTypePtr(TApp::make(ap->fn(), list(switchOf(ap->args()[1], *this))));
        }
      }
    }
    return MonoTypePtr(TApp::make(switchOf(ap->fn(), *this), switchOf(ap->args(), *this)));
  }
};

// read a single environment variable definition
static void readEnvironmentRecord(imagefile* f) {
  size_t                     offset;
  std::string                vname;
  std::vector<unsigned char> etype;
  size_t                     boffset = filePosition(f);

  // read the variable definition
  read(f, &offset);
  read(f, &vname);
  read(f, &etype);

  MonoTypePtr type = decode(etype);

  // keep track of it -- either by the v0 or v1 convention
  if (f->version == 0) {
    f->bindings[vname] = binding(tapp(primty("fileref"), list(switchOf(type, mapFileRefs()))), boffset, boffset);
  } else {
    f->bindings[vname] = binding(type, offset, boffset);
  }
}

// read environment data starting at some page
// (return the number of pages read to avoid erroneously double-reading environment data that might span multiple pages)
static size_t readEnvironmentPage(imagefile* f, file_pageindex_t p) {
  // remember the offset where we began reading environment data
  size_t initOffset = pageOffset(f, p);

  // go there
  seekAbs(f, initOffset);

  // we're done when we've read as much data as is reported for whatever page we're in
  while (true) {
    // we expect to read at least one environment binding
    readEnvironmentRecord(f);

    // exit when we've read to the end of whatever page we're in
    //  NOTE: we have to adjust -/+ 1 byte to account for the case when an environment
    //        page is filled up to and including the last byte (otherwise we'd mistakenly
    //        assume that we have to continue reading from the next page)
    size_t           pos   = filePosition(f) - 1;
    file_pageindex_t tpage = file_pageindex_t(pos / f->page_size);
    uint16_t         rpos  = (pos % f->page_size) + 1;

    if (rpos == (f->page_size - f->pages[tpage].size())) {
      break;
    }
  }

  // now just report on the number of pages we've read (probably just 1 or 2)
  return pageCount(f, filePosition(f) - initOffset);
}

// read the file
static void readFile(imagefile* f) {
  // start reading the first page
  seekAbs(f, 0);

  // get the header, make sure it's sound
  filehead fh;
  read(f, &fh);

  if (fh.magic != filePrefixBytes) {
    throw std::runtime_error("Not a valid structured data file: " + f->path);
  } else if (fh.version > CURRENT_FILE_FORMAT_VERSION) {
    throw std::runtime_error("Cannot read newer file format (" + str::from(fh.version) + " > " + str::from(CURRENT_FILE_FORMAT_VERSION) + ")");
  }
  f->page_size = assertValidPageSize(f, fh.pagesize);
  f->version   = fh.version;

  // load the first page descriptor
  pagedata pd;
  read(f, &pd);
  f->pages.push_back(pd);

  f->tocpages.push_back(0);
  f->head_toc_pos = sizeof(filehead) + sizeof(pagedata);

  // now read all page descriptors
  readPageData(f);

  // and then read in the environment
  for (file_pageindex_t p = 0; p < f->pages.size(); ++p) {
    if (f->pages[p].type() == pagetype::environment) {
      // read environment data starting from this page
      // (skip any contiguous pages we might have read as part of this)
      p += readEnvironmentPage(f, p) - 1;
    }
  }
}

// open a file, or create it if necessary
static imagefile* openFile(const std::string& fname, bool readonly) {
  imagefile* f        = new imagefile();
  f->path             = fname;
  f->readonly         = readonly;
  f->mmapPageMultiple = 262144;    // map in 1GB increments

  try {
    // open the file
    f->fd = open(fname.c_str(), readonly ? O_RDONLY : (O_RDWR | O_CREAT), S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);

    if (f->fd < 0) {
      raiseSysError("Unable to open for " + std::string(readonly ? "read" : "write"), fname);
    }

    // load the initial file size
    if (!loadFileSize(f)) {
      raiseSysError("Can't stat file", f->path);
    }

    // if we've just created this file, initialize it, else read it
    if (f->file_size == 0) {
      createFile(f);
    } else {
      readFile(f);
    }

    // there, we've loaded this file
    return f;
  } catch (...) {
    closeFile(f);
    throw;
  }
}

// does this file even represent a structured data file?
// we'll say yes if it can be opened, it has the magic number, and its size is a multiple of its page size
bool isDBFile(const std::string& fname) {
  imagefile* f = new imagefile();
  f->path      = fname;
  f->readonly  = true;

  try {
    // open it
    f->fd = open(fname.c_str(), O_RDONLY, 0);
    if (f->fd < 0) { closeFile(f); return false; }

    // get its size
    if (!loadFileSize(f)) { closeFile(f); return false; }

    // get the header
    seekAbs(f, 0);
    filehead fh;
    read(f, &fh);

    if (fh.magic != filePrefixBytes) { closeFile(f); return false; }
    f->page_size = assertValidPageSize(f, fh.pagesize);

    bool result = (f->page_size > 0) && (f->file_size % f->page_size) == 0;
    closeFile(f);
    return result;
  } catch (...) {
    closeFile(f);
    return false;
  }
}

// for storage, we need to make sure that size is computed slightly differently than for memory
size_t storageSizeOf(const MonoTypePtr& mty) {
  if (const Recursive* rty = is<Recursive>(mty)) {
    return sizeOf(unroll(mty));
  } else {
    return sizeOf(mty);
  }
}

// the base interface for reading data archives
reader::reader(imagefile* f) : fdata(f) {
  // just go ahead and map the whole file in
  mapFileData(this->fdata, 0, this->fdata->file_size);
}

reader::reader(const std::string& path) : fdata(openFile(path, true)) {
  // just go ahead and map the whole file in
  mapFileData(this->fdata, 0, this->fdata->file_size);
}

reader::~reader() {
  closeFile(this->fdata);
}

const std::string& reader::file() const {
  return this->fdata->path;
}

size_t reader::size() const {
  return this->fdata->file_size;
}

MonoTypeSubst reader::signature() const {
  MonoTypeSubst s;
  for (const auto& b : this->fdata->bindings) {
    s[b.first] = b.second.type;
  }
  return s;
}

void* reader::unsafeLookup(const std::string& vn, const MonoTypePtr& ty) const {
  auto b = this->fdata->bindings.find(vn);
  if (b == this->fdata->bindings.end()) {
    throw std::runtime_error("Variable undefined in database: " + vn);
  }

  if (*b->second.type == *ty) {
    return unsafeLoad(b->second.type, b->second.offset);
  } else {
    throw std::runtime_error("Database variable has unexpected type: " + hobbes::show(b->second.type) + " != " + hobbes::show(ty));
  }
}

uint64_t reader::unsafeLookupOffset(const std::string& vn, const MonoTypePtr& ty) const {
  auto b = this->fdata->bindings.find(vn);
  if (b == this->fdata->bindings.end()) {
    throw std::runtime_error("Variable undefined in database: " + vn);
  }

  if (*b->second.type == *ty) {
    return b->second.offset;
  } else {
    throw std::runtime_error("Database variable has unexpected type: " + hobbes::show(b->second.type) + " != " + hobbes::show(ty));
  }
}

uint64_t reader::unsafeOffsetOf(const MonoTypePtr& ty, void* p) const {
  return unsafeOffsetOfVal(is<Array>(ty), p);
}

uint64_t reader::unsafeOffsetOfVal(bool isArr, void* p) const {
  fallocs::const_iterator fa = gleb(this->fdata->allocs, (char*)p);
  if (fa == this->fdata->allocs.end()) {
    throw std::runtime_error("No file offset can be determined for unmapped memory");
  } else {
    fmappings::const_iterator fm = this->fdata->mappings.find(fa->second.page);
    if (fm == this->fdata->mappings.end()) {
      throw std::runtime_error("Internal error, inconsistent file mapping state");
    }

    return pageOffset(this->fdata, fm->second.base_page) + (((char*)p) - fm->second.base) - (isArr ? sizeof(long) : 0);
  }
}

bool storedAsArray(const MonoTypePtr& ty) {
  if (const Recursive* rty = is<Recursive>(ty)) {
    return storedAsArray(rty->recType());
  } else {
    return is<Array>(ty);
  }
}

void* reader::unsafeLoad(const MonoTypePtr& ty, uint64_t pos) const {
  if (pos > this->fdata->file_size) {
    // actually, this will be wrong in general since the writer might have written away from the reader
    throw std::runtime_error("Offset out of range of file");
  } else if (storedAsArray(ty)) {
    return unsafeLoadArray(pos);
  } else {
    return unsafeLoad(pos, storageSizeOf(ty));
  }
}

void* reader::unsafeLoad(uint64_t pos, size_t datasz) const {
  return mapFileData(this->fdata, pos, datasz);
}

uint64_t reader::unsafeArrayCapacity(uint64_t pos) const {
  seekAbs(this->fdata, pos);
  long datasz = 0;
  read(this->fdata, &datasz);
  return (uint64_t)datasz;
}

int reader::unsafeGetFD() const {
  return this->fdata->fd;
}

reader::PageEntries* reader::pageEntries() const {
  PageEntries* r = makeArray<PageEntry>(this->fdata->pages.size());
  for (size_t i = 0; i < r->size; ++i) {
    r->data[i].first  = this->fdata->pages[i].type();
    r->data[i].second = this->fdata->pages[i].size();
  }
  return r;
}

void* reader::unsafeLoadArray(uint64_t pos) const {
  // if we're loading an array, we actually have to read the data size first to know how much to map
  return ((unsigned char*)mapFileData(this->fdata, pos, unsafeArrayCapacity(pos))) + sizeof(long);
}

void reader::unsafeUnload(void* p, size_t sz) {
  unmapFileData(this->fdata, p, sz);
}

void reader::unsafeUnloadArray(void* p) {
  unsigned char* pxd = (((unsigned char*)p) - sizeof(long));
  unmapFileData(this->fdata, pxd, *((long*)pxd));
}

// change any file ref types to point to this reader because they must be out of this file (this is a bit of a hack)
MonoTypePtr mkFR(const MonoTypePtr& t) {
  return tapp(primty("fileref"), list(t));
}

void reader::showFileSummary(std::ostream& out) const {
  out << this->fdata->path << " : " << str::showDataSize(this->fdata->file_size) << std::endl;
}

void reader::showEnvironment(std::ostream& out) const {
  str::seqs table;
  table.resize(3);

  for (const auto& b : this->fdata->bindings) {
    static const size_t maxTypeLen = 100;
    std::string tn = hobbes::show(b.second.type);
    if (tn.size() > 100) { tn = tn.substr(0, 100) + "..."; }

    table[0].push_back(b.first);
    table[1].push_back("::");
    table[2].push_back(tn);
  }

  str::printHeadlessLeftAlignedTable(out, table);
}

void reader::showMappings(std::ostream& out, size_t pageRows) const {
  static const size_t bytesPerRow = 20;
  static const size_t bytesToShow = std::min<size_t>(bytesPerRow * pageRows, this->fdata->page_size);

  for (const auto& m : this->fdata->mappings) {
    out << "map from page " << m.first << " for " << m.second.pages << " page(s) at address " << (void*)m.second.base << std::endl;

    for (size_t i = 0; i < bytesToShow; ++i) {
      out << str::hex(*((unsigned char*)m.second.base + i)) << " ";
      if (((i+1) % bytesPerRow) == 0) {
        out << std::endl;
      }
    }
    if (bytesToShow < this->fdata->page_size) {
      out << "..." << std::endl;
    }
  }
}

std::string showPageDesc(const pagedata& pd) {
  std::ostringstream ss;
  switch (pd.type()) {
  case pagetype::toc:         ss << "T"; break;
  case pagetype::environment: ss << "E"; break;
  case pagetype::data:        ss << "D"; break;
  default:                    ss << "?"; break;
  }

  std::string sz = str::from(pd.size());
  for (size_t p = sz.size(); p < 5; ++p) {
    ss << "0";
  }
  ss << sz;
  return ss.str();
}

void reader::showPageTable(std::ostream& out, size_t maxRows) const {
  static const size_t cellSize = 6;
  static const size_t rowSize  = 10;

  // gather page descriptions
  str::seq pdescs;
  for (size_t i = 0; i < this->fdata->pages.size(); ++i) {
    if (i == rowSize * maxRows) {
      break;
    }
    pdescs.push_back(showPageDesc(this->fdata->pages[i]));
  }

  // show the page table
  out << "page table:" << std::endl;
  for (size_t i = 0; i < pdescs.size(); ++i) {
    out << pdescs[i] << "|";
    if (((i+1) % rowSize) == 0) {
      out << std::endl << std::string(rowSize * (cellSize + 1), '-') << std::endl;
    }
  }
  if (pdescs.size() < this->fdata->pages.size()) {
    out << "..." << std::endl;
  }
  out << std::endl;
}

void reader::show(std::ostream& out) const {
  showEnvironment(out);
}

// the interface for writing data archives
writer::writer(const std::string& path) : reader(openFile(path, false)) {
}

void* writer::unsafeDefine(const std::string& vn, const MonoTypePtr& ty) {
  return allocNamed(vn, ty, storageSizeOf(ty));
}

void* writer::unsafeDefine(const std::string& vn, const MonoTypePtr& ty, size_t len) {
  // allocate an extra bit of space in this array data to store the actual allocated length
  // (this allows storage of arrays with a reported length lower than capacity)
  size_t datasz = sizeof(long) + sizeof(long) + (len * storageSizeOf(ty));
  unsigned char* result = (unsigned char*)allocNamed(vn, arrayty(ty), datasz);
  *((long*)result) = datasz;
  return (void*)(result + sizeof(long));
}

void* writer::allocNamed(const std::string& vn, const MonoTypePtr& ty, size_t datasz) {
  // make sure that we're not redefining a value that's already defined
  if (this->fdata->bindings.find(vn) != this->fdata->bindings.end()) {
    throw std::runtime_error("Variable already defined in database: " + vn);
  }

  // allocate space for the data
  size_t dloc = findSpace(this->fdata, pagetype::data, datasz, alignment(ty));

  // add the environment binding
  addBinding(this->fdata, vn, ty, dloc);

  // get the mapped address of this data
  return mapFileData(this->fdata, dloc, datasz);
}

void* writer::unsafeStore(const MonoTypePtr& ty) {
  return allocAnon(storageSizeOf(ty), alignment(ty));
}

void* writer::unsafeStore(const MonoTypePtr& ty, size_t len) {
  return unsafeStoreArray(storageSizeOf(ty), len);
}

void* writer::unsafeStore(size_t sz, size_t align) {
  return allocAnon(sz, align);
}

uint64_t writer::unsafeStoreToOffset(size_t sz, size_t align) {
  return findSpace(this->fdata, pagetype::data, sz, align);
}

void* writer::unsafeStoreArray(size_t esize, size_t len) {
  // allocate an extra bit of space in this array data to store the actual allocated length
  // (this allows anonymous storage of arrays with a reported length lower than capacity)
  size_t datasz = sizeof(long) + sizeof(long) + (len * esize);
  unsigned char* result = (unsigned char*)allocAnon(datasz, sizeof(size_t));
  *((long*)result) = datasz;
  return (void*)(result + sizeof(long));
}

uint64_t writer::unsafeStoreArrayToOffset(size_t esize, size_t len) {
  void*    p = unsafeStoreArray(esize, len);
  uint64_t r = unsafeOffsetOfVal(true, p);

  unsafeUnloadArray(p);
  return r;
}

void writer::signalUpdate() {
  // write a (safe) dummy byte to the file header to trigger an update signal
  seekAbs(this->fdata, 0);
  write(this->fdata, (uint8_t)0x0d);
}

void* writer::allocAnon(size_t datasz, size_t align) {
  // allocate space for this data
  size_t dloc = findSpace(this->fdata, pagetype::data, datasz, align);

  // get the mapped address of this data
  return mapFileData(this->fdata, dloc, datasz);
}

// convenience storage methods
fileref<array<char>*> writer::store(const char* x, size_t sz) {
  return store(x, x + sz);
}

fileref<array<char>*> writer::store(const char* x) {
  return store(x, strlen(x));
}

fileref<array<char>*> writer::store(const array<char>* x) {
  return store(x->data, x->size);
}

fileref<array<char>*> writer::store(const std::string& x) {
  return store(x.begin(), x.end());
}

// create every directory in a path if it doesn't exist already
//  (equivalent to 'mkdir -p')
void ensureDirExists(const std::string& path) {
  str::seq ps = str::csplit(path, "/");
  std::ostringstream pfx;

  for (const auto& p : ps) {
    pfx << p << "/";
    if (mkdir(pfx.str().c_str(), S_IRWXU | S_IRWXG | S_IRWXO) == -1 && errno != EEXIST && errno != EISDIR) {
      throw std::runtime_error("Failed to make directory '" + pfx.str() + "' with error: " + strerror(errno));
    }
  }
}

std::string withUniqueFilenameBy(const std::string& fprefix, const std::string& fsuffix, const std::function<bool(const std::string&)>& fileOp) {
  // the directory that this file is in can be created if necessary
  ensureDirExists(str::rsplit(fprefix, "/").first);

  // we'll want the current date/time in the name
  time_t now = ::time(0);
  const tm* t = localtime(&now);

  // keep trying for new filenames until we get one that's distinct
  size_t inst = 0;
  while (true) {
    std::ostringstream ss;
    ss << fprefix << t->tm_year + 1900 << "." << (t->tm_mon < 9 ? "0" : "") << t->tm_mon + 1 << "." << t->tm_mday << "-" << inst << fsuffix;
    if (fileOp(ss.str())) {
      return ss.str();
    }
    ++inst;
  }
}

// generate a new file with a given prefix & suffix
std::string uniqueFilename(const std::string& fprefix, const std::string& fsuffix) {
  return withUniqueFilenameBy(fprefix, fsuffix, [](const std::string& newpath) {
    int fd = open(newpath.c_str(), O_CREAT | O_EXCL, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
    if (fd >= 0) {
      close(fd);
      return true;
    } else if (errno == EEXIST) {
      return false;
    } else {
      throw std::runtime_error("Failed to generate a log database file with error: " + std::string(strerror(errno)));
    }
  });
}

// move an existing file to a new file with a given prefix & suffix
std::string moveToUniqueFilename(const std::string& oldpath, const std::string& fprefix, const std::string& fsuffix) {
  return withUniqueFilenameBy(fprefix, fsuffix, [&oldpath](const std::string& newpath) {
    int rt = link(oldpath.c_str(), newpath.c_str());
    if (rt == 0) {
      unlink(oldpath.c_str());
      return true;
    } else if (errno == EEXIST) {
      return false;
    } else {
      throw std::runtime_error("Failed to move to a log database file with error: " + std::string(strerror(errno)));
    }
  });
}

}


text:
package org.apache.olingo.odata2.core.ep.aggregator;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.olingo.odata2.api.ep.EntityProviderException;

/**
 *
 */
public class EntityTypeMapping {
  private static final EntityTypeMapping ENTITY_TYPE_MAPPING = new EntityTypeMapping();
  final String propertyName;
  final Class<?> mapping;
  final List<EntityTypeMapping> mappings;

  private EntityTypeMapping() {
    this(null, Object.class);
  }

  private EntityTypeMapping(final String name, final Class<?> mappingClass) {
    propertyName = name;
    mapping = mappingClass;
    mappings = Collections.emptyList();
  }

  private EntityTypeMapping(final String name, final List<EntityTypeMapping> typeMappings) {
    propertyName = name;
    mapping = EntityTypeMapping.class;
    List<EntityTypeMapping> tmp = new ArrayList<EntityTypeMapping>();
    for (EntityTypeMapping typeMapping : typeMappings) {
      tmp.add(typeMapping);
    }
    mappings = Collections.unmodifiableList(tmp);
  }

  public static EntityTypeMapping create(final Map<String, Object> mappings) throws EntityProviderException {
    return create(null, mappings);
  }

  @SuppressWarnings("unchecked")
  public static EntityTypeMapping create(final String name, final Map<String, Object> mappings)
      throws EntityProviderException {
    if (mappings == null) {
      return ENTITY_TYPE_MAPPING;
    }
    List<EntityTypeMapping> typeMappings = new ArrayList<EntityTypeMapping>();
    Set<Entry<String, Object>> entries = mappings.entrySet();
    for (Entry<String, Object> entry : entries) {
      EntityTypeMapping typeMapping;
      Object value = entry.getValue();
      if (value instanceof Map) {
        typeMapping = create(entry.getKey(), (Map<String, Object>) value);
      } else if (value instanceof Class) {
        typeMapping = new EntityTypeMapping(entry.getKey(), (Class<?>) value);
      } else {
        throw new EntityProviderException(EntityProviderException.INVALID_MAPPING.addContent(entry.getKey()));
      }
      typeMappings.add(typeMapping);
    }

    return new EntityTypeMapping(name, typeMappings);
  }

  boolean isComplex() {
    return mappings != null && mapping == EntityTypeMapping.class;
  }

  /**
   * If this {@link EntityTypeMapping} is complex the mapping for the property
   * with the given name is returned; otherwise an empty {@link EntityTypeMapping} is returned.
   * @param name
   * @return the mapping for this entity type
   */
  public EntityTypeMapping getEntityTypeMapping(final String name) {
    if (isComplex()) {
      for (EntityTypeMapping mapping : mappings) {
        if (mapping.propertyName.equals(name)) {
          return mapping;
        }
      }
    }
    return ENTITY_TYPE_MAPPING;
  }

  /**
   * If this {@link EntityTypeMapping} is complex the mapping {@link Class} for the property
   * with the given name is returned; otherwise NULL is returned.
   * @param name
   * @return mapping {@link Class} for the property with given name or NULL.
   */
  public Class<?> getMappingClass(final String name) {
    if (isComplex()) {
      for (EntityTypeMapping mapping : mappings) {
        if (mapping.propertyName.equals(name)) {
          return mapping.mapping;
        }
      }
    }
    return null;
  }

  @Override
  public String toString() {
    if (isComplex()) {
      return "{'" + propertyName + "'->" + mappings.toString() + "}";
    }
    return "{'" + propertyName + "' as " + (mapping == null ? "NULL" : mapping.getSimpleName()) + "}";
  }

}


text:
var DEFAULT_MEASURES = 30;
var DEFAULT_TEMPO = 120;
var DEFAULT_SECONDS = 60;
var DEFAULT_MEASURECOUNT = 4;
function MTTModel(){
  var self = this;
  self.measures = ko.observable(DEFAULT_MEASURES);
  self.seconds = ko.observable(DEFAULT_SECONDS);
  self.tempo = ko.observable(DEFAULT_TEMPO);
  self.measureCount = ko.observable(DEFAULT_MEASURECOUNT);

  self.recalculateSeconds = function () {
    self.seconds(Math.ceil(((self.measures() * self.measureCount())/self.tempo())*60));
  };

  self.recalculateMeasures = function () {
      self.measures(Math.ceil(((self.tempo()/60)*self.seconds())/self.measureCount()));
  };

  self.recalculateTempo = function () {
      self.tempo(Math.ceil(((self.measures()*self.measureCount())/(self.seconds()))*60));
  };

  self.secondsCalc = ko.computed( function () {
      self.measures();
      self.tempo();
      self.measureCount();
      self.recalculateMeasures();
      self.recalculateTempo();
      });

  self.measuresCalc = ko.computed( function () {
      self.seconds();
      self.tempo();
      self.measureCount();
      self.recalculateSeconds();
      self.recalculateTempo();
      });

  self.tempoCalc = ko.computed( function () {
      self.seconds();
      self.measures();
      self.measureCount();
      self.recalculateMeasures();
      self.recalculateTempo();
      });

};

ko.applyBindings(new MTTModel());


text:
namespace counter {

// This is a base class for the counter implementation.
class CounterBase : public Factory<CounterBase> {
 public:
  virtual ~CounterBase() {}

  // Defines a map from an enum type to a specific metric.
  // TODO(otasevic,oztekin): use unique_ptr once we resolve the compilation
  // issues with gcc 4.9.
  typedef shared_ptr<metrics::MetricInterface> smart_ptr_type;
  typedef map<string, smart_ptr_type> MetricsMap;

  // Defines a set of metrics. This is used to specify the kind of metrics to be tracked
  // and/or retrieved.
  typedef vector<string> MetricSet;

  // This method is implemented by every derived class and is supposed to update all the metrics
  // with the event parameters.
  virtual void ProcessEvent(const metrics::tCountedEvent& event, int num_events = 1) = 0;

  // This method retrieves all the registered metrics for a specific time interval.
  virtual MetricsMap GetMetricsForInterval(uint64_t interval_duration) = 0;

  virtual MetricsMap GetMetricsForInterval(uint64_t interval_duration,
                                           const MetricSet& metric_set) = 0;

  virtual MetricsMap GetAllTimeMetrics() = 0;
};

}  // namespace counter

#endif  // _PUBLIC_UTIL_COUNTER_COUNTER_BASE_H_


text:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace SeafClient.Requests
{
    /// <summary>
    /// Requestto retrieve  a thumbnail for the given image
    /// </summary>
    public class GetThumbnailImageRequest : SessionRequest<byte[]>
    {
        public string LibraryId { get; set; }

        public String Path { get; set; }

        public int ThumbnailSize { get; set; }

        public override string CommandUri
        {
            get { return String.Format("api2/repos/{0}/thumbnail/?p={1}&size={2:d}", LibraryId, WebUtility.UrlEncode(Path), ThumbnailSize); }
        }

        public override async Task<byte[]> ParseResponseAsync(System.Net.Http.HttpResponseMessage msg)
        {
            return await msg.Content.ReadAsByteArrayAsync();
        }

        public GetThumbnailImageRequest(string authToken, string libraryId, string imageFilePath, int thumbnailSize)
            : base(authToken)
        {
            LibraryId = libraryId;
            Path = imageFilePath;
            ThumbnailSize = thumbnailSize;

            if (!Path.StartsWith("/"))
                Path = "/" + Path;
        }
    }
}


text:
WindowFeatures::WindowFeatures(const CefPopupFeatures& popupFeatures) {
  this->width = popupFeatures.width;
  this->height = popupFeatures.height;
  this->x = popupFeatures.x;
  this->y = popupFeatures.y;
  this->dialog = popupFeatures.dialog > 0;
  this->topmost = false;

  if (popupFeatures.additionalFeatures) {
    CefString feature;
    int size = cef_string_list_size(popupFeatures.additionalFeatures);
    for (int i = 0; i < size; i++) {
      cef_string_list_value(popupFeatures.additionalFeatures, i, feature.GetWritableStruct());
      if (feature == "topmost") {
        this->topmost = true;
      }
    }
  }
}


text:
namespace BrowserAutomationStudioFramework
{
    class ENGINESHARED_EXPORT CopyResourceController : public IResourceController
    {
        Q_OBJECT
        IResources * ResourcesExternal;
        IResources * DefaultsExternal;
        IResourceModel* CopyModelToActual(const QString& Type, IResourceModel* ModelTo, IResourceModel* ModelFrom, IResourceModel* ModelDefault, QObject* parent);
        bool IsDefaultChanged(const QString& Type,IResourceModel* ModelFrom, IResourceModel* ModelDefault);

    public:
        explicit CopyResourceController(QObject *parent = 0);

    signals:

    public slots:
        void SetResources(IResources * ResourcesExternal);
        void SetDefaults(IResources * DefaultsExternal);
        virtual void FromViewToModel(IResources * resources, bool Clear = true);
        virtual void FromModelToView(IResources * resources, bool Clear = true);
    };
}

#endif // COPYRESOURCECONTROLLER_H


text:
using Orchard.UI.Resources;

namespace TinyMce {
    public class ResourceManifest : IResourceManifestProvider {
        public void BuildManifests(ResourceManifestBuilder builder) {
            var manifest = builder.Add();
            manifest.DefineScript("TinyMce").SetUrl("tiny_mce.js", "tiny_mce_src.js");
            manifest.DefineScript("OrchardTinyMce").SetUrl("orchard-tinymce.js").SetDependencies("TinyMce");
        }
    }
}


text:
<?php

namespace Magento\User\Test\TestCase;

use Magento\Backend\Test\Page\AdminAuthLogin;
use Magento\Backend\Test\Page\Adminhtml\Dashboard;
use Magento\User\Test\Fixture\Role;
use Magento\User\Test\Fixture\User;
use Magento\User\Test\Page\Adminhtml\UserRoleEditRole;
use Magento\User\Test\Page\Adminhtml\UserRoleIndex;
use Magento\Mtf\Fixture\FixtureFactory;
use Magento\Mtf\TestCase\Injectable;

/**
 * Test Creation for DeleteUserRoleEntity
 *
 * Test Flow:
 * Preconditions:
 *  1.Create new admin user and assign it to new role.
 * Steps:
 *  1. Log in as admin user from data set.
 *  2. Go to System>Permissions>User Roles
 *  3. Open role created in precondition
 *  4. Click "Delete Role" button
 *  5. Perform all assertions
 *
 * @group ACL_(PS)
 * @ZephyrId MAGETWO-23926
 */
class DeleteUserRoleEntityTest extends Injectable
{
    /* tags */
    const MVP = 'no';
    const DOMAIN = 'PS';
    /* end tags */

    /**
     * @var UserRoleIndex
     */
    protected $userRoleIndex;

    /**
     * @var UserRoleEditRole
     */
    protected $userRoleEditRole;

    /**
     * @var AdminAuthLogin
     */
    protected $adminAuthLogin;

    /**
     * @var Dashboard
     */
    protected $dashboard;

    /**
     * Preconditions for test
     *
     * @param FixtureFactory $fixtureFactory
     * @return array
     */
    public function __prepare(FixtureFactory $fixtureFactory)
    {
        $adminUser = $fixtureFactory->createByCode(
            'user',
            ['dataset' => 'custom_admin_with_default_role']
        );
        $adminUser->persist();

        return [
            'role' => $adminUser->getDataFieldConfig('role_id')['source']->getRole(),
            'adminUser' => $adminUser
        ];
    }

    /**
     * @param UserRoleIndex $userRoleIndex
     * @param UserRoleEditRole $userRoleEditRole
     * @param AdminAuthLogin $adminAuthLogin
     * @param Dashboard $dashboard
     * @return void
     */
    public function __inject(
        UserRoleIndex $userRoleIndex,
        UserRoleEditRole $userRoleEditRole,
        AdminAuthLogin $adminAuthLogin,
        Dashboard $dashboard
    ) {
        $this->userRoleIndex = $userRoleIndex;
        $this->userRoleEditRole = $userRoleEditRole;
        $this->adminAuthLogin = $adminAuthLogin;
        $this->dashboard = $dashboard;
    }

    /**
     * Runs Delete User Role Entity test.
     *
     * @param Role $role
     * @param User $adminUser
     * @param string $isDefaultUser
     * @return void
     */
    public function testDeleteAdminUserRole(
        Role $role,
        User $adminUser,
        $isDefaultUser
    ) {
        $filter = [
            'rolename' => $role->getRoleName(),
        ];
        //Steps
        if ($isDefaultUser == 0) {
            $this->adminAuthLogin->open();
            $this->adminAuthLogin->getLoginBlock()->fill($adminUser);
            $this->adminAuthLogin->getLoginBlock()->submit();
        }
        $this->userRoleIndex->open();
        $this->userRoleIndex->getRoleGrid()->searchAndOpen($filter);
        $this->userRoleEditRole->getPageActions()->delete();
        $this->userRoleEditRole->getModalBlock()->acceptAlert();
    }

    /**
     * Logout Admin User from account
     *
     * return void
     */
    public function tearDown()
    {
        $this->dashboard->getAdminPanelHeader()->logOut();
    }
}


text:
namespace device {

class HidService;
class MockHidService;
class MockUsbService;
class UsbService;

class MockDeviceClient : device::DeviceClient {
 public:
  MockDeviceClient();
  ~MockDeviceClient() override;

  // device::DeviceClient implementation:
  UsbService* GetUsbService() override;
  HidService* GetHidService() override;

  // Accessors for the mock instances.
  MockHidService* hid_service();
  MockUsbService* usb_service();

 private:
  std::unique_ptr<MockHidService> hid_service_;
  std::unique_ptr<MockUsbService> usb_service_;
};

}  // namespace device

#endif  // DEVICE_CORE_MOCK_DEVICE_CLIENT_H_


text:
'use strict';

var EventEmitter = require('events').EventEmitter;
var feedback = new EventEmitter();
var CHANNEL = 'feedback';
var util = require('util');
var _ = require('lodash');

module.exports = {

  on: function(cb) {
    feedback.on(CHANNEL, function(feedback) {
      cb(feedback);
    });
  },

  emit: function(string) {
    if (Buffer.isBuffer(string)) { string = string.toString(); }
    if (arguments.length > 1 && _.isString(string)) {
      string = util.format.apply(this, arguments);
    }
    feedback.emit(CHANNEL, string);
  }
};


text:
describe("twig.filter.length", function () {

  it("returns the number of characters in a string", function () {
    twig.filter.length(null, "foo").should.equal(3);
  });

  it("returns the number of elements in an array", function () {
    twig.filter.length(null, []).should.equal(0);
  });

  it("returns the number of properties of an object", function () {
    twig.filter.length(null, {foo: "bar"}).should.equal(1);
  });

  it("returns 0 for null", function () {
    twig.filter.length(null, null).should.equal(0);
  });

  it("returns the number of characters in an integer", function () {
    twig.filter.length(null, 1234).should.equal(4);
  });

});