<commit_before>// sdl_main.cpp

#ifdef _WIN32
#  define WINDOWS_LEAN_AND_MEAN
#  define NOMINMAX
#  include <windows.h>
#endif

#include <GL/glew.h>
#include <SDL.h>
#include <SDL_syswm.h>
#undef main

#include "ShaderFunctions.h"
#include "Timer.h"

Timer g_timer;
int winw = 800;
int winh = 600;

struct Shadertoy {
    GLuint prog;
	GLuint progsound;
	GLint uloc_iResolution;
	GLint uloc_iGlobalTime;
	GLint uloc_iBlockOffset;
	GLint uloc_iSampleRate;
};

Shadertoy g_toy;

void keyboard(const SDL_Event& event, int key, int codes, int action, int mods)
{
    (void)codes;
    (void)mods;

    if (action == SDL_KEYDOWN)
    {
        switch (key)
        {
        default:
            break;

        case SDLK_ESCAPE:
            SDL_Quit();
            exit(0);
            break;
        }
    }
}

void PollEvents()
{
    SDL_Event event;
    while (SDL_PollEvent(&event))
    {
        switch(event.type)
        {
        case SDL_KEYDOWN:
        case SDL_KEYUP:
            keyboard(event, event.key.keysym.sym, 0, event.key.type, 0);
            break;

        case SDL_MOUSEBUTTONDOWN:
        case SDL_MOUSEBUTTONUP:
            break;

        case SDL_MOUSEMOTION:
            break;

        case SDL_MOUSEWHEEL:
            break;

        case SDL_WINDOWEVENT:
            break;

        case SDL_QUIT:
            exit(0);
            break;

        default:
            break;
        }
    }
}

void display()
{
    glUseProgram(g_toy.prog);
    if (g_toy.uloc_iResolution > -1) glUniform3f(g_toy.uloc_iResolution, (float)winw, (float)winh, 1.f);
    if (g_toy.uloc_iGlobalTime > -1) glUniform1f(g_toy.uloc_iGlobalTime, g_timer.seconds());
    glRecti(-1,-1,1,1);
}

//
// Audio
//

struct
{
    SDL_AudioSpec spec;
    Uint8 *sound;               /* Pointer to wave data */
    Uint32 soundlen;            /* Length of wave data */
    int soundpos;               /* Current play position */
} wave;

void SDLCALL fillerup(void *unused, Uint8 * stream, int len)
{
    Uint8 *waveptr;
    int waveleft;

    waveptr = wave.sound + wave.soundpos;
    waveleft = wave.soundlen - wave.soundpos;

    while (waveleft <= len) { // wrap condition
        SDL_memcpy(stream, waveptr, waveleft);
        stream += waveleft;
        len -= waveleft;
        waveptr = wave.sound;
        waveleft = wave.soundlen;
        wave.soundpos = 0;
    }
    SDL_memcpy(stream, waveptr, len);
    wave.soundpos += len;
}

void play_audio()
{
    // init buffer
    wave.soundlen = 128*1024;
    //wav_spec.samples = samps;
    wave.sound = new Uint8[wave.soundlen];
    // Fill with noise
    for (int i=0; i<wave.soundlen/2; ++i)
    {
        wave.sound[i] = rand() % 255;
    }

    wave.spec.freq = 44100;
    wave.spec.format = AUDIO_U8; //AUDIO_S16LSB;
    wave.spec.channels = 2;
    wave.spec.callback = fillerup;

	// shadertoy effect.js 135
	//this.mSampleRate = 44100;
	//this.mPlayTime = 60;
	//this.mPlaySamples = this.mPlayTime*this.mSampleRate;
	//this.mBuffer = wa.createBuffer(2, this.mPlaySamples, this.mSampleRate);
	//var l2   = gl.getUniformLocation( this.mProgram, "iBlockOffset" );

	// render effect.js 983
#if 0
	// bufL: Float32Array[2646000]
	// numBlocks: 10.09368896484375
	var bufL = this.mBuffer.getChannelData(0); // Float32Array
	var bufR = this.mBuffer.getChannelData(1); // Float32Array
	var numBlocks = this.mPlaySamples / this.mTmpBufferSamples;
	for (var j = 0; j<numBlocks; j++)
	{
		var off = j*this.mTmpBufferSamples; // mTmpBufferSamples: 262144

		gl.uniform1f(l2, off / this.mSampleRate);
		gl.drawArrays(gl.TRIANGLES, 0, 6);
		// mTextureDimensions: 512
		gl.readPixels(0, 0, this.mTextureDimensions, this.mTextureDimensions, gl.RGBA, gl.UNSIGNED_BYTE, this.mData);

		for (var i = 0; i<this.mTmpBufferSamples; i++)
		{
			bufL[off + i] = -1.0 + 2.0*(this.mData[4 * i + 0] + 256.0*this.mData[4 * i + 1]) / 65535.0;
			bufR[off + i] = -1.0 + 2.0*(this.mData[4 * i + 2] + 256.0*this.mData[4 * i + 3]) / 65535.0;
		}
	}

	gl.disableVertexAttribArray(l1);
	gl.useProgram(null);
	gl.bindFramebuffer(gl.FRAMEBUFFER, null);
#endif

    if (SDL_OpenAudio(&wave.spec, NULL) < 0)
    {
        SDL_FreeWAV(wave.sound);
        SDL_Quit();
        exit(2);
    }

    SDL_PauseAudio(0); // Start playing
}

int main(void)
{
    ///@todo cmd line aargs

    if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
    {
        return false;
    }

    SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_COMPATIBILITY);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);

    int winw = 800;
    int winh = 600;

    SDL_Window* pWindow = SDL_CreateWindow(
        "kinderegg",
        100,100,
        winw, winh,
        SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL);

    // thank you http://www.brandonfoltz.com/2013/12/example-using-opengl-3-0-with-sdl2-and-glew/
    SDL_GLContext glContext = SDL_GL_CreateContext(pWindow);
    if (glContext == NULL)
    {
        printf("There was an error creating the OpenGL context!\n");
        return 0;
    }

    const unsigned char *version = glGetString(GL_VERSION);
    if (version == NULL)
    {
        printf("There was an error creating the OpenGL context!\n");
        return 1;
    }

    SDL_GL_MakeCurrent(pWindow, glContext);

    // Don't forget to initialize Glew, turn glewExperimental on to
    // avoid problems fetching function pointers...
    glewExperimental = GL_TRUE;
    const GLenum l_Result = glewInit();
    if (l_Result != GLEW_OK)
    {
        exit(EXIT_FAILURE);
    }

    g_toy.prog = makeShaderByName("basic");
    g_toy.uloc_iResolution = glGetUniformLocation(g_toy.prog, "iResolution");
    g_toy.uloc_iGlobalTime = glGetUniformLocation(g_toy.prog, "iGlobalTime");
	g_toy.progsound = makeShaderByName("basicsound");
	g_toy.uloc_iBlockOffset = glGetUniformLocation(g_toy.progsound, "iBlockOffset");
	g_toy.uloc_iSampleRate = glGetUniformLocation(g_toy.progsound, "iSampleRate");

	//var l2   = gl.getUniformLocation( this.mProgram, "" );

    play_audio();

    int quit = 0;
    while (quit == 0)
    {
        PollEvents();
        display();
        SDL_GL_SwapWindow(pWindow);
    }

    SDL_GL_DeleteContext(glContext);
    SDL_DestroyWindow(pWindow);
    SDL_CloseAudio();
    SDL_FreeWAV(wave.sound);
    SDL_Quit();
}
<commit_msg>Render audio to framebuffer and push it to speakers via glReadPixels. Works for the default audio shader.<commit_after>// sdl_main.cpp

#ifdef _WIN32
#  define WINDOWS_LEAN_AND_MEAN
#  define NOMINMAX
#  include <windows.h>
#endif

#include <GL/glew.h>
#include <SDL.h>
#include <SDL_syswm.h>
#undef main

#include "ShaderFunctions.h"
#include "Timer.h"

Timer g_timer;
int winw = 800;
int winh = 600;

struct Shadertoy {
    GLuint prog;
	GLuint progsound;
	GLint uloc_iResolution;
	GLint uloc_iGlobalTime;
	GLint uloc_iBlockOffset;
	GLint uloc_iSampleRate;
};

Shadertoy g_toy;

void keyboard(const SDL_Event& event, int key, int codes, int action, int mods)
{
    (void)codes;
    (void)mods;

    if (action == SDL_KEYDOWN)
    {
        switch (key)
        {
        default:
            break;

        case SDLK_ESCAPE:
            SDL_Quit();
            exit(0);
            break;
        }
    }
}

void PollEvents()
{
    SDL_Event event;
    while (SDL_PollEvent(&event))
    {
        switch(event.type)
        {
        case SDL_KEYDOWN:
        case SDL_KEYUP:
            keyboard(event, event.key.keysym.sym, 0, event.key.type, 0);
            break;

        case SDL_MOUSEBUTTONDOWN:
        case SDL_MOUSEBUTTONUP:
            break;

        case SDL_MOUSEMOTION:
            break;

        case SDL_MOUSEWHEEL:
            break;

        case SDL_WINDOWEVENT:
            break;

        case SDL_QUIT:
            exit(0);
            break;

        default:
            break;
        }
    }
}

void display()
{
    glUseProgram(g_toy.prog);
    if (g_toy.uloc_iResolution > -1) glUniform3f(g_toy.uloc_iResolution, (float)winw, (float)winh, 1.f);
    if (g_toy.uloc_iGlobalTime > -1) glUniform1f(g_toy.uloc_iGlobalTime, g_timer.seconds());
    glRecti(-1,-1,1,1);
}

//
// Audio
//

struct
{
    SDL_AudioSpec spec;
    Uint8 *sound;               /* Pointer to wave data */
    Uint32 soundlen;            /* Length of wave data */
    int soundpos;               /* Current play position */
} wave;

void SDLCALL fillerup(void *unused, Uint8 * stream, int len)
{
    Uint8 *waveptr;
    int waveleft;

    waveptr = wave.sound + wave.soundpos;
    waveleft = wave.soundlen - wave.soundpos;

    while (waveleft <= len) { // wrap condition
        SDL_memcpy(stream, waveptr, waveleft);
        stream += waveleft;
        len -= waveleft;
        waveptr = wave.sound;
        waveleft = wave.soundlen;
        wave.soundpos = 0;
    }
    SDL_memcpy(stream, waveptr, len);
    wave.soundpos += len;
}

void play_audio()
{
    wave.spec.freq = 44100;
    wave.spec.format = AUDIO_U8; //AUDIO_S16LSB;
    wave.spec.channels = 2;
    wave.spec.callback = fillerup;

    const int mPlayTime = 60; // Shadertoy gives 60 seconds of audio
    wave.soundlen = mPlayTime * wave.spec.freq;
    wave.sound = new Uint8[2*wave.soundlen];
    glViewport(0,0,512,512);
    glUseProgram(g_toy.progsound);

    unsigned char mData[512*512*4];
    int mTmpBufferSamples = 262144;
    int mPlaySamples = wave.soundlen;
    int numBlocks = mPlaySamples / mTmpBufferSamples;
    for (int j=0; j<numBlocks; ++j)
    {
        int off = j * mTmpBufferSamples;
        if (g_toy.uloc_iBlockOffset > -1) glUniform1f(g_toy.uloc_iBlockOffset, (float)off / (float)wave.spec.freq);

        glRecti(-1,-1,1,1);
        // mData: Uint8Array[1048576]
        glReadPixels(0,0,512,512, GL_RGBA, GL_UNSIGNED_BYTE, mData);
        for (int i = 0; i<mTmpBufferSamples; ++i)
        {
            const float aL = -1.0f + 2.0f*((float)mData[4 * i + 0] + 256.0f*(float)mData[4 * i + 1]) / 65535.0f;
            const float aR = -1.0f + 2.0f*((float)mData[4 * i + 2] + 256.0f*(float)mData[4 * i + 3]) / 65535.0f;
            wave.sound[2*(off + i)  ] = (unsigned char)(.5f*(1.f+aL) * 255.f);
            wave.sound[2*(off + i)+1] = (unsigned char)(.5f*(1.f+aR) * 255.f);
        }
    }

    if (SDL_OpenAudio(&wave.spec, NULL) < 0)
    {
        SDL_FreeWAV(wave.sound);
        SDL_Quit();
        exit(2);
    }

    SDL_PauseAudio(0); // Start playing
}

int main(void)
{
    ///@todo cmd line aargs

    if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
    {
        return false;
    }

    SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_COMPATIBILITY);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);

    int winw = 800;
    int winh = 600;

    SDL_Window* pWindow = SDL_CreateWindow(
        "kinderegg",
        100,100,
        winw, winh,
        SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL);

    // thank you http://www.brandonfoltz.com/2013/12/example-using-opengl-3-0-with-sdl2-and-glew/
    SDL_GLContext glContext = SDL_GL_CreateContext(pWindow);
    if (glContext == NULL)
    {
        printf("There was an error creating the OpenGL context!\n");
        return 0;
    }

    const unsigned char *version = glGetString(GL_VERSION);
    if (version == NULL)
    {
        printf("There was an error creating the OpenGL context!\n");
        return 1;
    }

    SDL_GL_MakeCurrent(pWindow, glContext);

    // Don't forget to initialize Glew, turn glewExperimental on to
    // avoid problems fetching function pointers...
    glewExperimental = GL_TRUE;
    const GLenum l_Result = glewInit();
    if (l_Result != GLEW_OK)
    {
        exit(EXIT_FAILURE);
    }

    g_toy.prog = makeShaderByName("basic");
    g_toy.uloc_iResolution = glGetUniformLocation(g_toy.prog, "iResolution");
    g_toy.uloc_iGlobalTime = glGetUniformLocation(g_toy.prog, "iGlobalTime");
    g_toy.progsound = makeShaderByName("basicsound");
    g_toy.uloc_iBlockOffset = glGetUniformLocation(g_toy.progsound, "iBlockOffset");
    g_toy.uloc_iSampleRate = glGetUniformLocation(g_toy.progsound, "iSampleRate");

    play_audio();
    glViewport(0,0, winw, winh);

    int quit = 0;
    while (quit == 0)
    {
        PollEvents();
        display();
        SDL_GL_SwapWindow(pWindow);
    }

    SDL_GL_DeleteContext(glContext);
    SDL_DestroyWindow(pWindow);
    SDL_CloseAudio();
    SDL_FreeWAV(wave.sound);
    SDL_Quit();
}
<|endoftext|>


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

#define LOG_TAG "LatinIME: jni"

#include "jni_common.h"

#include "com_android_inputmethod_keyboard_ProximityInfo.h"
#include "com_android_inputmethod_latin_BinaryDictionary.h"
#include "com_android_inputmethod_latin_DicTraverseSession.h"
#include "defines.h"

/*
 * Returns the JNI version on success, JNI_ERR on failure.
 */
jint JNI_OnLoad(JavaVM *vm, void *reserved) {
    JNIEnv *env = 0;

    if (vm->GetEnv(reinterpret_cast<void **>(&env), JNI_VERSION_1_6) != JNI_OK) {
        AKLOGE("ERROR: GetEnv failed");
        return JNI_ERR;
    }
    ASSERT(env);
    if (!env) {
        AKLOGE("ERROR: JNIEnv is invalid");
        return JNI_ERR;
    }
    if (!latinime::register_BinaryDictionary(env)) {
        AKLOGE("ERROR: BinaryDictionary native registration failed");
        return JNI_ERR;
    }
    if (!latinime::register_DicTraverseSession(env)) {
        AKLOGE("ERROR: DicTraverseSession native registration failed");
        return JNI_ERR;
    }
    if (!latinime::register_ProximityInfo(env)) {
        AKLOGE("ERROR: ProximityInfo native registration failed");
        return JNI_ERR;
    }
    /* success -- return valid version number */
    return JNI_VERSION_1_6;
}

namespace latinime {
int registerNativeMethods(JNIEnv *env, const char *className, JNINativeMethod *methods,
        int numMethods) {
    jclass clazz = env->FindClass(className);
    if (!clazz) {
        AKLOGE("Native registration unable to find class '%s'", className);
        return JNI_FALSE;
    }
    if (env->RegisterNatives(clazz, methods, numMethods) < 0) {
        AKLOGE("RegisterNatives failed for '%s'", className);
        env->DeleteLocalRef(clazz);
        return JNI_FALSE;
    }
    env->DeleteLocalRef(clazz);
    return JNI_TRUE;
}
} // namespace latinime
<commit_msg>Extra log line for debugging.<commit_after>/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define LOG_TAG "LatinIME: jni"

#include "jni_common.h"

#include "com_android_inputmethod_keyboard_ProximityInfo.h"
#include "com_android_inputmethod_latin_BinaryDictionary.h"
#include "com_android_inputmethod_latin_DicTraverseSession.h"
#include "defines.h"

/*
 * Returns the JNI version on success, JNI_ERR on failure.
 */
jint JNI_OnLoad(JavaVM *vm, void *reserved) {
    JNIEnv *env = 0;

    AKLOGE("Entered JNI_OnLoad");

    if (vm->GetEnv(reinterpret_cast<void **>(&env), JNI_VERSION_1_6) != JNI_OK) {
        AKLOGE("ERROR: GetEnv failed");
        return JNI_ERR;
    }
    ASSERT(env);
    if (!env) {
        AKLOGE("ERROR: JNIEnv is invalid");
        return JNI_ERR;
    }
    if (!latinime::register_BinaryDictionary(env)) {
        AKLOGE("ERROR: BinaryDictionary native registration failed");
        return JNI_ERR;
    }
    if (!latinime::register_DicTraverseSession(env)) {
        AKLOGE("ERROR: DicTraverseSession native registration failed");
        return JNI_ERR;
    }
    if (!latinime::register_ProximityInfo(env)) {
        AKLOGE("ERROR: ProximityInfo native registration failed");
        return JNI_ERR;
    }
    /* success -- return valid version number */
    return JNI_VERSION_1_6;
}

namespace latinime {
int registerNativeMethods(JNIEnv *env, const char *className, JNINativeMethod *methods,
        int numMethods) {
    jclass clazz = env->FindClass(className);
    if (!clazz) {
        AKLOGE("Native registration unable to find class '%s'", className);
        return JNI_FALSE;
    }
    if (env->RegisterNatives(clazz, methods, numMethods) < 0) {
        AKLOGE("RegisterNatives failed for '%s'", className);
        env->DeleteLocalRef(clazz);
        return JNI_FALSE;
    }
    env->DeleteLocalRef(clazz);
    return JNI_TRUE;
}
} // namespace latinime
<|endoftext|>


<commit_before>/*************************************************************************
 *
 *  OpenOffice.org - a multi-platform office productivity suite
 *
 *  $RCSfile: SchemaSimpleTypeContext.cxx,v $
 *
 *  $Revision: 1.3 $
 *
 *  last change: $Author: rt $ $Date: 2005-09-09 16:01:26 $
 *
 *  The Contents of this file are made available subject to
 *  the terms of GNU Lesser General Public License Version 2.1.
 *
 *
 *    GNU Lesser General Public License Version 2.1
 *    =============================================
 *    Copyright 2005 by Sun Microsystems, Inc.
 *    901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License version 2.1, as published by the Free Software Foundation.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public
 *    License along with this library; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *    MA  02111-1307  USA
 *
 ************************************************************************/

#include "SchemaSimpleTypeContext.hxx"

#include "SchemaRestrictionContext.hxx"
#include <xmltoken.hxx>
#include <nmspmap.hxx>
#include <xmlnmspe.hxx>
#include <xmltkmap.hxx>
#include <xmluconv.hxx>

#include <com/sun/star/beans/XPropertySet.hpp>
#include <com/sun/star/xsd/WhiteSpaceTreatment.hpp>

#include <tools/debug.hxx>

using rtl::OUString;
using com::sun::star::uno::Reference;
using com::sun::star::uno::Any;
using com::sun::star::xml::sax::XAttributeList;
using com::sun::star::beans::XPropertySet;
using com::sun::star::xforms::XDataTypeRepository;
using namespace xmloff::token;

static SvXMLTokenMapEntry aAttributes[] =
{
    TOKEN_MAP_ENTRY( NONE, NAME ),
    XML_TOKEN_MAP_END
};

static SvXMLTokenMapEntry aChildren[] =
{
    TOKEN_MAP_ENTRY( XSD, RESTRICTION ),
    XML_TOKEN_MAP_END
};

SchemaSimpleTypeContext::SchemaSimpleTypeContext(
    SvXMLImport& rImport,
    USHORT nPrefix,
    const OUString& rLocalName,
    const Reference<XDataTypeRepository>& rRepository ) :
        TokenContext( rImport, nPrefix, rLocalName, aAttributes, aChildren ),
        mxRepository( rRepository )
{
}

SchemaSimpleTypeContext::~SchemaSimpleTypeContext()
{
}

void SchemaSimpleTypeContext::HandleAttribute(
    sal_uInt16 nToken,
    const OUString& rValue )
{
    if( nToken == XML_NAME )
    {
        msTypeName = rValue;
    }
}

SvXMLImportContext* SchemaSimpleTypeContext::HandleChild(
    sal_uInt16 nToken,
    sal_uInt16 nPrefix,
    const OUString& rLocalName,
    const Reference<XAttributeList>& xAttrList )
{
    SvXMLImportContext* pContext = NULL;
    switch( nToken )
    {
    case XML_RESTRICTION:
        pContext = new SchemaRestrictionContext( GetImport(),
                                                 nPrefix, rLocalName,
                                                 mxRepository, msTypeName );
        break;
    default:
        DBG_ERROR( "Booo!" );
    }

    return ( pContext != NULL )
        ? pContext
        : new SvXMLImportContext( GetImport(), nPrefix, rLocalName );
}
<commit_msg>INTEGRATION: CWS warnings01 (1.3.34); FILE MERGED 2005/11/17 16:41:26 pl 1.3.34.1: #i55991# removed warnings<commit_after>/*************************************************************************
 *
 *  OpenOffice.org - a multi-platform office productivity suite
 *
 *  $RCSfile: SchemaSimpleTypeContext.cxx,v $
 *
 *  $Revision: 1.4 $
 *
 *  last change: $Author: hr $ $Date: 2006-06-19 18:56:55 $
 *
 *  The Contents of this file are made available subject to
 *  the terms of GNU Lesser General Public License Version 2.1.
 *
 *
 *    GNU Lesser General Public License Version 2.1
 *    =============================================
 *    Copyright 2005 by Sun Microsystems, Inc.
 *    901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License version 2.1, as published by the Free Software Foundation.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public
 *    License along with this library; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *    MA  02111-1307  USA
 *
 ************************************************************************/

#include "SchemaSimpleTypeContext.hxx"

#include "SchemaRestrictionContext.hxx"
#include <xmltoken.hxx>
#include <nmspmap.hxx>
#include <xmlnmspe.hxx>
#include <xmltkmap.hxx>
#include <xmluconv.hxx>

#include <com/sun/star/beans/XPropertySet.hpp>
#include <com/sun/star/xsd/WhiteSpaceTreatment.hpp>

#include <tools/debug.hxx>

using rtl::OUString;
using com::sun::star::uno::Reference;
using com::sun::star::uno::Any;
using com::sun::star::xml::sax::XAttributeList;
using com::sun::star::beans::XPropertySet;
using com::sun::star::xforms::XDataTypeRepository;
using namespace xmloff::token;

static SvXMLTokenMapEntry aAttributes[] =
{
    TOKEN_MAP_ENTRY( NONE, NAME ),
    XML_TOKEN_MAP_END
};

static SvXMLTokenMapEntry aChildren[] =
{
    TOKEN_MAP_ENTRY( XSD, RESTRICTION ),
    XML_TOKEN_MAP_END
};

SchemaSimpleTypeContext::SchemaSimpleTypeContext(
    SvXMLImport& rImport,
    USHORT nPrefix,
    const OUString& rLocalName,
    const Reference<XDataTypeRepository>& rRepository ) :
        TokenContext( rImport, nPrefix, rLocalName, aAttributes, aChildren ),
        mxRepository( rRepository )
{
}

SchemaSimpleTypeContext::~SchemaSimpleTypeContext()
{
}

void SchemaSimpleTypeContext::HandleAttribute(
    sal_uInt16 nToken,
    const OUString& rValue )
{
    if( nToken == XML_NAME )
    {
        msTypeName = rValue;
    }
}

SvXMLImportContext* SchemaSimpleTypeContext::HandleChild(
    sal_uInt16 nToken,
    sal_uInt16 nPrefix,
    const OUString& rLocalName,
    const Reference<XAttributeList>& )
{
    SvXMLImportContext* pContext = NULL;
    switch( nToken )
    {
    case XML_RESTRICTION:
        pContext = new SchemaRestrictionContext( GetImport(),
                                                 nPrefix, rLocalName,
                                                 mxRepository, msTypeName );
        break;
    default:
        DBG_ERROR( "Booo!" );
    }

    return ( pContext != NULL )
        ? pContext
        : new SvXMLImportContext( GetImport(), nPrefix, rLocalName );
}
<|endoftext|>


<commit_before>//
// Created by Dennis Goldfarb on 9/28/16.
//

#include <iostream>
#include <fstream>
#include <random>
#include <string>
#include <algorithm>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>

#include <OpenMS/CHEMISTRY/Element.h>
#include <OpenMS/CHEMISTRY/AASequence.h>
#include <OpenMS/CHEMISTRY/ResidueDB.h>
#include <OpenMS/CHEMISTRY/ElementDB.h>

static const OpenMS::ResidueDB* residueDB = OpenMS::ResidueDB::getInstance();
static const OpenMS::ElementDB* elementDB = OpenMS::ElementDB::getInstance();

static std::string AMINO_ACIDS = "ADEFGHIKLNPQRSTVWY";
static std::string AMINO_ACIDS_SULFUR = "CM";
static std::string AMINO_ACIDS_SELENIUM = "U";

std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis_AA(0, AMINO_ACIDS.length()-1);
std::uniform_int_distribution<> dis_S(0, AMINO_ACIDS_SULFUR.length()-1);

int max_depth;

OpenMS::AASequence create_random_peptide_sequence(int peptide_length, int num_sulfurs, int num_c_sulfurs, int num_selenium, int num_c_selenium) {
    OpenMS::AASequence random_peptide;

    // for insertion of sulfur containing amino acids in fragment
    for (int i = 0; i < num_sulfurs; ++i) {
        random_peptide += residueDB->getResidue(AMINO_ACIDS_SULFUR[dis_S(gen)]);
    }

    // for insertion of selenocysteines in fragment
    for (int i = 0; i < num_selenium; ++i) {
        random_peptide += residueDB->getResidue(AMINO_ACIDS_SELENIUM[0]);
    }

    // random amino acid insertion (non Sulfur and Selenium amino acids)
    for (int aa_index = 0; aa_index < peptide_length; ++aa_index) {
        random_peptide += residueDB->getResidue(AMINO_ACIDS[dis_AA(gen)]);
    }

    // for insertion of sulfur containing amino acids in fragment
    for (int i = 0; i < num_c_sulfurs; ++i) {
        random_peptide += residueDB->getResidue(AMINO_ACIDS_SULFUR[dis_S(gen)]);
    }

    // for insertion of selenocysteines in fragment
    for (int i = 0; i < num_c_selenium; ++i) {
        random_peptide += residueDB->getResidue(AMINO_ACIDS_SELENIUM[0]);
    }

    return random_peptide;
}

/*OpenMS::IsotopeDistribution getIsotopeDistribution(OpenMS::EmpiricalFormula &formula, OpenMS::UInt max_depth)
{
    OpenMS::IsotopeDistribution result(max_depth);
    for (auto it = formula.begin(); it != formula.end(); ++it)
    {
        OpenMS::IsotopeDistribution tmp = it->first->getIsotopeDistribution();
        tmp.setMaxIsotope(max_depth);
        result += tmp * it->second;
    }
    return result;
}

OpenMS::IsotopeDistribution getFragmentDistribution(OpenMS::EmpiricalFormula &precursor_ef, OpenMS::EmpiricalFormula &fragment_ef, std::vector<OpenMS::UInt> &isolated_isotopes)
{
    // A fragment's isotopes can only be as high as the largest isolated precursor isotope.
    OpenMS::UInt max_depth = *std::max_element(isolated_isotopes.begin(), isolated_isotopes.end())+1;

    // Treat *this as the fragment molecule
    OpenMS::EmpiricalFormula complementary_fragment = precursor_ef-fragment_ef;

    OpenMS::IsotopeDistribution fragment_isotope_dist = getIsotopeDistribution(fragment_ef, max_depth);
    OpenMS::IsotopeDistribution comp_fragment_isotope_dist = getIsotopeDistribution(complementary_fragment, max_depth);

    OpenMS::IsotopeDistribution result;
    result.calcFragmentIsotopeDist(fragment_isotope_dist, comp_fragment_isotope_dist, isolated_isotopes);

    return result;
}*/

void create_fragments(OpenMS::AASequence &p, std::ofstream** outfiles, int num_sulfurs, int num_c_sulfurs, int num_selenium, int num_c_selenium) {
    int num_fragments = p.size()-1;

    int tot_left_SSe = std::max(num_sulfurs + num_selenium,1);
    int tot_right_SSe = std::max(num_c_sulfurs + num_c_selenium,1);

    OpenMS::EmpiricalFormula precursor_ef = p.getFormula();
    OpenMS::IsotopeDistribution precursor_id = precursor_ef.getIsotopeDistribution(30);

    for (int index = tot_left_SSe; index < num_fragments-tot_right_SSe; ++index)
    {
        OpenMS::EmpiricalFormula b_ion = p.getPrefix(index).getFormula(OpenMS::Residue::ResidueType::BIon);
        OpenMS::EmpiricalFormula y_ion = p.getPrefix(index).getFormula(OpenMS::Residue::ResidueType::YIon);

        for (int precursor_isotope = 0; precursor_isotope < max_depth; ++precursor_isotope)
        {
            std::vector<OpenMS::UInt> isolated_isotopes;
            isolated_isotopes.push_back(precursor_isotope);

            OpenMS::IsotopeDistribution b_id = b_ion.getConditionalFragmentIsotopeDist(precursor_ef, isolated_isotopes);
            OpenMS::IsotopeDistribution y_id = y_ion.getConditionalFragmentIsotopeDist(precursor_ef, isolated_isotopes);
            //OpenMS::IsotopeDistribution b_id = getFragmentDistribution(precursor_ef, b_ion, isolated_isotopes);
            //OpenMS::IsotopeDistribution y_id = getFragmentDistribution(precursor_ef, y_ion, isolated_isotopes);

            for (int fragment_isotope = 0; fragment_isotope <= precursor_isotope && fragment_isotope < b_id.size(); ++fragment_isotope)
            {
                outfiles[precursor_isotope][fragment_isotope] << b_id.getContainer()[fragment_isotope].second * precursor_id.getContainer()[precursor_isotope].second
                                                              << "\t" << b_ion.getMonoWeight()
                                                              << "\t" << p.getMonoWeight() << std::endl;
                outfiles[precursor_isotope][fragment_isotope] << y_id.getContainer()[fragment_isotope].second * precursor_id.getContainer()[precursor_isotope].second
                                                              << "\t" << y_ion.getMonoWeight()
                                                              << "\t" << p.getMonoWeight() << std::endl;
            }
        }
    }
}

void sample_fragment_isotopic_distributions(std::string base_path, float max_mass, int num_samples, int num_sulfurs, int num_c_sulfurs, int num_selenium, int num_c_selenium, bool append) {

    // create all output files and write header to each
    std::ofstream** outfiles = new std::ofstream*[max_depth];
    for (int precursor_isotope = 0; precursor_isotope < max_depth; ++precursor_isotope) {
        outfiles[precursor_isotope] = new std::ofstream[max_depth];
        for (int fragment_isotope = 0; fragment_isotope <= precursor_isotope; ++fragment_isotope) {
            std::string filename = "Precursor" + std::to_string(precursor_isotope) + "_" +
                                   "Fragment" + std::to_string(fragment_isotope) + ".tab";

            if (append)
            {
                outfiles[precursor_isotope][fragment_isotope].open(base_path + filename, std::ofstream::out | std::ofstream::app);
            } else
            {
                outfiles[precursor_isotope][fragment_isotope].open(base_path + filename);
            }

            // Only add the header if we're creating a new file
            // This happens if we're not appending, or if we're appending and it's our first time in the loop
            if (!append) {
                outfiles[precursor_isotope][fragment_isotope] << "probability" << "\tfrag.mass" << "\tprecursor.mass"
                                                              << std::endl; //"\tfrag.a.mass" << "\tprecursor.a.mass" << std::endl;
            }
        }
    }

    int max_length = max_mass/100;

    for (int peptide_length = 0; peptide_length <= max_length; ++peptide_length) {

        for (int sample = 0; sample < num_samples; ++sample) {

            OpenMS::AASequence random_sequence = create_random_peptide_sequence(peptide_length, num_sulfurs, num_c_sulfurs,
                                                                         num_selenium, num_c_selenium);

            if (random_sequence.size() > 0 && random_sequence.getMonoWeight() <= max_mass) {
                create_fragments(random_sequence, outfiles, num_sulfurs, num_c_sulfurs, num_selenium, num_c_selenium);
            }
        }

    }

    // close all output files
    for (int precursor_isotope = 0; precursor_isotope < max_depth; ++precursor_isotope) {
        for (int fragment_isotope = 0; fragment_isotope <= precursor_isotope; ++fragment_isotope) {
            outfiles[precursor_isotope][fragment_isotope].close();
        }
        delete[] outfiles[precursor_isotope];
    }
    delete[] outfiles;
}

void sample_average_fragment_isotopic_distribution(std::string distribution_path, std::string base_path, float max_mass, float min_percentage)
{
    std::map<std::pair<int,int>, int> sulfurs2count;

    std::ifstream sulfur_dist_in(distribution_path);
    std::string input;
    int S, CS, count, i=0;
    while (sulfur_dist_in >> input)
    {
        if (i == 0) S = atoi(input.c_str());
        else if (i == 1) CS = atoi(input.c_str());
        else {
            count = atoi(input.c_str());
            sulfurs2count[std::make_pair(S,CS)] = count;
        }
        i = (i+1) % 3;
    }

    int max_count = 0;
    for (auto itr : sulfurs2count)
    {
        max_count = std::max(max_count, itr.second);
    }

    bool append = false;
    for (auto itr : sulfurs2count)
    {
        double percentage = (double) itr.second / max_count;
        if (percentage >= min_percentage) {
            int num_samples = std::floor(percentage / min_percentage);
            sample_fragment_isotopic_distributions(base_path, max_mass, num_samples, itr.first.first, itr.first.second, 0, 0, append);
            append = false;
        }
    }
}

void usage()
{
    std::cout << "PeptideFragmentSampler out_path max_mass num_samples S CS Se CSe max_isotope" << std::endl;
    std::cout << "out_path: The path to the directory that will store the training data, e.g. ~/data/" << std::endl;
    std::cout << "max_mass: maximum mass allowed for sampled peptides, e.g. 8500" << std::endl;
    std::cout << "num_samples: number of random peptides to generate for each peptide length, e.g 100" << std::endl;
    std::cout << "S: number of sulfurs that should be in the fragment ion, e.g. 0" << std::endl;
    std::cout << "CS: number of sulfurs that should be in the complementary fragment ion, e.g. 0" << std::endl;
    std::cout << "Se: number of seleniums that should be in the fragment ion, e.g. 0" << std::endl;
    std::cout << "CSe: number of seleniums that should be in the complementary fragment ion, e.g. 0" << std::endl;
    std::cout << "max_isotope: The maximum isotope to generate training data for, e.g. 5" << std::endl;
    std::cout << std::endl;

    std::cout << "PeptideFragmentSampler sulfur_dist_path out_path max_mass min_percentage max_isotope" << std::endl;
    std::cout << "sulfur_dist_path: file path to the results of GetSulfurDistribution, e.g. ~/data/sulfur_distribution.tab" << std::endl;
    std::cout << "out_path: The path to the directory that will store the training data, e.g. ~/data/" << std::endl;
    std::cout << "max_mass: maximum mass allowed for sampled peptides, e.g. 8500" << std::endl;
    std::cout << "min_percentage: the min abundance of a sulfur distribution necessary to be included in the training data (relative to most abundant case), e.g. .001" << std::endl;
    std::cout << "max_isotope: The maximum isotope to generate training data for, e.g. 5" << std::endl;
}

int main(int argc, const char ** argv) {

    if (argc != 9 && argc != 6)
    {
        usage();
    }

    // Increase the maximum number of open files for this process. Was necessary for me.
    struct rlimit rlp;
    rlp.rlim_cur = 600;
    setrlimit(RLIMIT_NOFILE, &rlp);

    if (argc == 9) {
        std::string out_path = argv[1];
        float max_mass = atof(argv[2]);
        int num_samples = atoi(argv[3]);
        int S = atoi(argv[4]);
        int CS = atoi(argv[5]);
        int Se = atoi(argv[6]);
        int CSe = atoi(argv[7]);

        max_depth = atoi(argv[8]) + 1;

        sample_fragment_isotopic_distributions(out_path, max_mass, num_samples, S, CS, Se, CSe, false);
    }
    else if (argc == 6)
    {
        std::string dist_path = argv[1];
        std::string out_path = argv[2];
        float max_mass = atof(argv[3]);
        float min_percentage = atof(argv[4]);

        max_depth = atoi(argv[5]) + 1;

        sample_average_fragment_isotopic_distribution(dist_path, out_path, max_mass, min_percentage);
    }

    return 0;
}<commit_msg>fixed bug<commit_after>//
// Created by Dennis Goldfarb on 9/28/16.
//

#include <iostream>
#include <fstream>
#include <random>
#include <string>
#include <algorithm>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>

#include <OpenMS/CHEMISTRY/Element.h>
#include <OpenMS/CHEMISTRY/AASequence.h>
#include <OpenMS/CHEMISTRY/ResidueDB.h>
#include <OpenMS/CHEMISTRY/ElementDB.h>

static const OpenMS::ResidueDB* residueDB = OpenMS::ResidueDB::getInstance();
static const OpenMS::ElementDB* elementDB = OpenMS::ElementDB::getInstance();

static std::string AMINO_ACIDS = "ADEFGHIKLNPQRSTVWY";
static std::string AMINO_ACIDS_SULFUR = "CM";
static std::string AMINO_ACIDS_SELENIUM = "U";

std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis_AA(0, AMINO_ACIDS.length()-1);
std::uniform_int_distribution<> dis_S(0, AMINO_ACIDS_SULFUR.length()-1);

int max_depth;

OpenMS::AASequence create_random_peptide_sequence(int peptide_length, int num_sulfurs, int num_c_sulfurs, int num_selenium, int num_c_selenium) {
    OpenMS::AASequence random_peptide;

    // for insertion of sulfur containing amino acids in fragment
    for (int i = 0; i < num_sulfurs; ++i) {
        random_peptide += residueDB->getResidue(AMINO_ACIDS_SULFUR[dis_S(gen)]);
    }

    // for insertion of selenocysteines in fragment
    for (int i = 0; i < num_selenium; ++i) {
        random_peptide += residueDB->getResidue(AMINO_ACIDS_SELENIUM[0]);
    }

    // random amino acid insertion (non Sulfur and Selenium amino acids)
    for (int aa_index = 0; aa_index < peptide_length; ++aa_index) {
        random_peptide += residueDB->getResidue(AMINO_ACIDS[dis_AA(gen)]);
    }

    // for insertion of sulfur containing amino acids in fragment
    for (int i = 0; i < num_c_sulfurs; ++i) {
        random_peptide += residueDB->getResidue(AMINO_ACIDS_SULFUR[dis_S(gen)]);
    }

    // for insertion of selenocysteines in fragment
    for (int i = 0; i < num_c_selenium; ++i) {
        random_peptide += residueDB->getResidue(AMINO_ACIDS_SELENIUM[0]);
    }

    return random_peptide;
}

/*OpenMS::IsotopeDistribution getIsotopeDistribution(OpenMS::EmpiricalFormula &formula, OpenMS::UInt max_depth)
{
    OpenMS::IsotopeDistribution result(max_depth);
    for (auto it = formula.begin(); it != formula.end(); ++it)
    {
        OpenMS::IsotopeDistribution tmp = it->first->getIsotopeDistribution();
        tmp.setMaxIsotope(max_depth);
        result += tmp * it->second;
    }
    return result;
}

OpenMS::IsotopeDistribution getFragmentDistribution(OpenMS::EmpiricalFormula &precursor_ef, OpenMS::EmpiricalFormula &fragment_ef, std::vector<OpenMS::UInt> &isolated_isotopes)
{
    // A fragment's isotopes can only be as high as the largest isolated precursor isotope.
    OpenMS::UInt max_depth = *std::max_element(isolated_isotopes.begin(), isolated_isotopes.end())+1;

    // Treat *this as the fragment molecule
    OpenMS::EmpiricalFormula complementary_fragment = precursor_ef-fragment_ef;

    OpenMS::IsotopeDistribution fragment_isotope_dist = getIsotopeDistribution(fragment_ef, max_depth);
    OpenMS::IsotopeDistribution comp_fragment_isotope_dist = getIsotopeDistribution(complementary_fragment, max_depth);

    OpenMS::IsotopeDistribution result;
    result.calcFragmentIsotopeDist(fragment_isotope_dist, comp_fragment_isotope_dist, isolated_isotopes);

    return result;
}*/

void create_fragments(OpenMS::AASequence &p, std::ofstream** outfiles, int num_sulfurs, int num_c_sulfurs, int num_selenium, int num_c_selenium) {
    int num_fragments = p.size()-1;

    int tot_left_SSe = std::max(num_sulfurs + num_selenium,1);
    int tot_right_SSe = std::max(num_c_sulfurs + num_c_selenium,1);

    OpenMS::EmpiricalFormula precursor_ef = p.getFormula();
    OpenMS::IsotopeDistribution precursor_id = precursor_ef.getIsotopeDistribution(30);

    for (int index = tot_left_SSe; index < num_fragments-tot_right_SSe; ++index)
    {
        OpenMS::EmpiricalFormula b_ion = p.getPrefix(index).getFormula(OpenMS::Residue::ResidueType::BIon);
        OpenMS::EmpiricalFormula y_ion = p.getPrefix(index).getFormula(OpenMS::Residue::ResidueType::YIon);

        for (int precursor_isotope = 0; precursor_isotope < max_depth; ++precursor_isotope)
        {
            std::vector<OpenMS::UInt> isolated_isotopes;
            isolated_isotopes.push_back(precursor_isotope);

            OpenMS::IsotopeDistribution b_id = b_ion.getConditionalFragmentIsotopeDist(precursor_ef, isolated_isotopes);
            OpenMS::IsotopeDistribution y_id = y_ion.getConditionalFragmentIsotopeDist(precursor_ef, isolated_isotopes);
            //OpenMS::IsotopeDistribution b_id = getFragmentDistribution(precursor_ef, b_ion, isolated_isotopes);
            //OpenMS::IsotopeDistribution y_id = getFragmentDistribution(precursor_ef, y_ion, isolated_isotopes);

            for (int fragment_isotope = 0; fragment_isotope <= precursor_isotope && fragment_isotope < b_id.size(); ++fragment_isotope)
            {
                outfiles[precursor_isotope][fragment_isotope] << b_id.getContainer()[fragment_isotope].second * precursor_id.getContainer()[precursor_isotope].second
                                                              << "\t" << b_ion.getMonoWeight()
                                                              << "\t" << p.getMonoWeight() << std::endl;
                outfiles[precursor_isotope][fragment_isotope] << y_id.getContainer()[fragment_isotope].second * precursor_id.getContainer()[precursor_isotope].second
                                                              << "\t" << y_ion.getMonoWeight()
                                                              << "\t" << p.getMonoWeight() << std::endl;
            }
        }
    }
}

void sample_fragment_isotopic_distributions(std::string base_path, float max_mass, int num_samples, int num_sulfurs, int num_c_sulfurs, int num_selenium, int num_c_selenium, bool append) {

    // create all output files and write header to each
    std::ofstream** outfiles = new std::ofstream*[max_depth];
    for (int precursor_isotope = 0; precursor_isotope < max_depth; ++precursor_isotope) {
        outfiles[precursor_isotope] = new std::ofstream[max_depth];
        for (int fragment_isotope = 0; fragment_isotope <= precursor_isotope; ++fragment_isotope) {
            std::string filename = "Precursor" + std::to_string(precursor_isotope) + "_" +
                                   "Fragment" + std::to_string(fragment_isotope) + ".tab";

            if (append)
            {
                outfiles[precursor_isotope][fragment_isotope].open(base_path + filename, std::ofstream::out | std::ofstream::app);
            } else
            {
                outfiles[precursor_isotope][fragment_isotope].open(base_path + filename);
            }

            // Only add the header if we're creating a new file
            // This happens if we're not appending, or if we're appending and it's our first time in the loop
            if (!append) {
                outfiles[precursor_isotope][fragment_isotope] << "probability" << "\tfrag.mass" << "\tprecursor.mass"
                                                              << std::endl; //"\tfrag.a.mass" << "\tprecursor.a.mass" << std::endl;
            }
        }
    }

    int max_length = max_mass/100;

    for (int peptide_length = 0; peptide_length <= max_length; ++peptide_length) {

        for (int sample = 0; sample < num_samples; ++sample) {

            OpenMS::AASequence random_sequence = create_random_peptide_sequence(peptide_length, num_sulfurs, num_c_sulfurs,
                                                                         num_selenium, num_c_selenium);

            if (random_sequence.size() > 0 && random_sequence.getMonoWeight() <= max_mass) {
                create_fragments(random_sequence, outfiles, num_sulfurs, num_c_sulfurs, num_selenium, num_c_selenium);
            }
        }

    }

    // close all output files
    for (int precursor_isotope = 0; precursor_isotope < max_depth; ++precursor_isotope) {
        for (int fragment_isotope = 0; fragment_isotope <= precursor_isotope; ++fragment_isotope) {
            outfiles[precursor_isotope][fragment_isotope].close();
        }
        delete[] outfiles[precursor_isotope];
    }
    delete[] outfiles;
}

void sample_average_fragment_isotopic_distribution(std::string distribution_path, std::string base_path, float max_mass, float min_percentage)
{
    std::map<std::pair<int,int>, int> sulfurs2count;

    std::ifstream sulfur_dist_in(distribution_path);
    std::string input;
    int S, CS, count, i=0;
    while (sulfur_dist_in >> input)
    {
        if (i == 0) S = atoi(input.c_str());
        else if (i == 1) CS = atoi(input.c_str());
        else {
            count = atoi(input.c_str());
            sulfurs2count[std::make_pair(S,CS)] = count;
        }
        i = (i+1) % 3;
    }

    int max_count = 0;
    for (auto itr : sulfurs2count)
    {
        max_count = std::max(max_count, itr.second);
    }

    bool append = false;
    for (auto itr : sulfurs2count)
    {
        double percentage = (double) itr.second / max_count;
        if (percentage >= min_percentage) {
            int num_samples = std::floor(percentage / min_percentage);
            sample_fragment_isotopic_distributions(base_path, max_mass, num_samples, itr.first.first, itr.first.second, 0, 0, append);
            append = true;
        }
    }
}

void usage()
{
    std::cout << "PeptideFragmentSampler out_path max_mass num_samples S CS Se CSe max_isotope" << std::endl;
    std::cout << "out_path: The path to the directory that will store the training data, e.g. ~/data/" << std::endl;
    std::cout << "max_mass: maximum mass allowed for sampled peptides, e.g. 8500" << std::endl;
    std::cout << "num_samples: number of random peptides to generate for each peptide length, e.g 100" << std::endl;
    std::cout << "S: number of sulfurs that should be in the fragment ion, e.g. 0" << std::endl;
    std::cout << "CS: number of sulfurs that should be in the complementary fragment ion, e.g. 0" << std::endl;
    std::cout << "Se: number of seleniums that should be in the fragment ion, e.g. 0" << std::endl;
    std::cout << "CSe: number of seleniums that should be in the complementary fragment ion, e.g. 0" << std::endl;
    std::cout << "max_isotope: The maximum isotope to generate training data for, e.g. 5" << std::endl;
    std::cout << std::endl;

    std::cout << "PeptideFragmentSampler sulfur_dist_path out_path max_mass min_percentage max_isotope" << std::endl;
    std::cout << "sulfur_dist_path: file path to the results of GetSulfurDistribution, e.g. ~/data/sulfur_distribution.tab" << std::endl;
    std::cout << "out_path: The path to the directory that will store the training data, e.g. ~/data/" << std::endl;
    std::cout << "max_mass: maximum mass allowed for sampled peptides, e.g. 8500" << std::endl;
    std::cout << "min_percentage: the min abundance of a sulfur distribution necessary to be included in the training data (relative to most abundant case), e.g. .001" << std::endl;
    std::cout << "max_isotope: The maximum isotope to generate training data for, e.g. 5" << std::endl;
}

int main(int argc, const char ** argv) {

    if (argc != 9 && argc != 6)
    {
        usage();
    }

    // Increase the maximum number of open files for this process. Was necessary for me.
    struct rlimit rlp;
    rlp.rlim_cur = 600;
    setrlimit(RLIMIT_NOFILE, &rlp);

    if (argc == 9) {
        std::string out_path = argv[1];
        float max_mass = atof(argv[2]);
        int num_samples = atoi(argv[3]);
        int S = atoi(argv[4]);
        int CS = atoi(argv[5]);
        int Se = atoi(argv[6]);
        int CSe = atoi(argv[7]);

        max_depth = atoi(argv[8]) + 1;

        sample_fragment_isotopic_distributions(out_path, max_mass, num_samples, S, CS, Se, CSe, false);
    }
    else if (argc == 6)
    {
        std::string dist_path = argv[1];
        std::string out_path = argv[2];
        float max_mass = atof(argv[3]);
        float min_percentage = atof(argv[4]);

        max_depth = atoi(argv[5]) + 1;

        sample_average_fragment_isotopic_distribution(dist_path, out_path, max_mass, min_percentage);
    }

    return 0;
}<|endoftext|>


<commit_before>// Copyright (c) 2014-2017 Michael J. Sullivan
// Use of this source code is governed by an MIT-style license that can be
// found in the LICENSE file.

// This is a version of of rculist_user_rmc that open codes all the
// list manipulation crap in instead of abstracting it away, in order
// to simplify it as an example.

#include "rculist_user_rmc_simple.hpp"
#include "epoch.hpp"
#include <mutex>

namespace rmclib {

#include "rculist_user_rmc_simple_macro.cpp"

/// BEGIN SNIP
// Perform a lookup in an RCU-protected widgetlist, with
// execution edges drawn to the LGIVE return action.
// Must be done in an Epoch read-side critical section.
widget *widget_find_fine(widgetlist *list, unsigned key) noexcept {
    XEDGE_HERE(load, load);
    XEDGE_HERE(load, use);
    widget *node;
    widget *head = &list->head;
    for (node = L(load, head->next); node != head; node = L(load, node->next)) {
        if (L(use, node->key) == key) {
            return LGIVE(use, node);
        }
    }
    return nullptr;
}

// Perform a lookup in an RCU-protected widgetlist with a traditional
// post edge.
// Must be done in an Epoch read-side critical section.
widget *widget_find(widgetlist *list, unsigned key) noexcept {
    XEDGE(find, post);
    return L(find, widget_find_fine(list, key));
}

static void insert_between(widget *n, widget *n1, widget *n2) {
    VEDGE(pre, link);
    n->prev = n1;
    n2->prev = n;
    n->next = n2;
    L(link, n1->next = n);
}
static void insert_before(widget *n, widget *n_old) {
    insert_between(n, n_old->prev, n_old);
}
static void replace(widget *n_old, widget *n_new) {
    insert_between(n_new, n_old->prev, n_old->next);
}

// Insert an object into a widgetlist, replacing an old object with
// the same key, if necessary.
// Must *not* be called from an Epoch read-side critical section.
void widget_insert(widgetlist *list, widget *obj) noexcept {
    // Acquires write_lock and automatically drops it when we leave scope.
    std::unique_lock<std::mutex> lock(list->write_lock);
    // We needn't give any constraints on the node lookup here.  Since
    // insertions always happen under the lock, any list modifications
    // are already visible to us.
    widget *old = widget_find_fine(list, obj->key);

    // If nothing to replace we just insert it normally
    if (!old) {
        insert_before(obj, &list->head);
        return;
    }

    replace(old, obj);
    lock.unlock();

    // Wait until any readers that may be using the old node are gone
    // and then delete it.
    Epoch::rcuSynchronize();
    delete old;
}
/// END SNIP

}
<commit_msg>don't use unique_lock for rculist rmc_simple<commit_after>// Copyright (c) 2014-2017 Michael J. Sullivan
// Use of this source code is governed by an MIT-style license that can be
// found in the LICENSE file.

// This is a version of of rculist_user_rmc that open codes all the
// list manipulation crap in instead of abstracting it away, in order
// to simplify it as an example.

#include "rculist_user_rmc_simple.hpp"
#include "epoch.hpp"
#include <mutex>

namespace rmclib {

#include "rculist_user_rmc_simple_macro.cpp"

/// BEGIN SNIP
// Perform a lookup in an RCU-protected widgetlist, with
// execution edges drawn to the LGIVE return action.
// Must be done in an Epoch read-side critical section.
widget *widget_find_fine(widgetlist *list, unsigned key) noexcept {
    XEDGE_HERE(load, load);
    XEDGE_HERE(load, use);
    widget *node;
    widget *head = &list->head;
    for (node = L(load, head->next); node != head; node = L(load, node->next)) {
        if (L(use, node->key) == key) {
            return LGIVE(use, node);
        }
    }
    return nullptr;
}

// Perform a lookup in an RCU-protected widgetlist with a traditional
// post edge.
// Must be done in an Epoch read-side critical section.
widget *widget_find(widgetlist *list, unsigned key) noexcept {
    XEDGE(find, post);
    return L(find, widget_find_fine(list, key));
}

static void insert_between(widget *n, widget *n1, widget *n2) {
    VEDGE(pre, link);
    n->prev = n1;
    n2->prev = n;
    n->next = n2;
    L(link, n1->next = n);
}
static void insert_before(widget *n, widget *n_old) {
    insert_between(n, n_old->prev, n_old);
}
static void replace(widget *n_old, widget *n_new) {
    insert_between(n_new, n_old->prev, n_old->next);
}

// Insert an object into a widgetlist, replacing an old object with
// the same key, if necessary.
// Must *not* be called from an Epoch read-side critical section.
void widget_insert(widgetlist *list, widget *obj) noexcept {
    list->write_lock.lock();
    // We needn't give any constraints on the node lookup here.  Since
    // insertions always happen under the lock, any list modifications
    // are already visible to us.
    widget *old = widget_find_fine(list, obj->key);

    // If nothing to replace we just insert it normally
    if (!old) {
        insert_before(obj, &list->head);
        list->write_lock.unlock();
        return;
    }

    replace(old, obj);
    list->write_lock.unlock();

    // Wait until any readers that may be using the old node are gone
    // and then delete it.
    Epoch::rcuSynchronize();
    delete old;
}
/// END SNIP

}
<|endoftext|>


<commit_before>/*****************************************************************************
 * VLC backend for the Phonon library                                        *
 * Copyright (C) 2007-2008 Tanguy Krotoff <tkrotoff@gmail.com>               *
 * Copyright (C) 2008 Lukas Durfina <lukas.durfina@gmail.com>                *
 * Copyright (C) 2009 Fathi Boudra <fabo@kde.org>                            *
 *                                                                           *
 * This program is free software; you can redistribute it and/or             *
 * modify it under the terms of the GNU Lesser General Public                *
 * License as published by the Free Software Foundation; either              *
 * version 3 of the License, or (at your option) any later version.          *
 *                                                                           *
 * This program is distributed in the hope that it will be useful,           *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
 * Lesser General Public License for more details.                           *
 *                                                                           *
 * You should have received a copy of the GNU Lesser General Public          *
 * License along with this package; if not, write to the Free Software       *
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA *
 *****************************************************************************/

#include "vlcloader.h"

#include <QtCore/QDebug>
#include <QtCore/QDir>
#include <QtCore/QLibrary>
#include <QtCore/QSettings>
#include <QtCore/QString>
#include <QtCore/QStringList>

// Global variables
libvlc_instance_t *vlc_instance = 0;
libvlc_media_player_t *vlc_current_media_player = 0;

namespace Phonon
{
namespace VLC {

bool vlcInit(int debugLevl)
{
    // Global variables
    vlc_instance = 0;

    QString path = vlcPath();
    if (!path.isEmpty()) {
        QString pluginsPath = QString("--plugin-path=") + QDir::toNativeSeparators(QFileInfo(vlcPath()).dir().path());
#if defined(Q_OS_UNIX)
        pluginsPath.append("/vlc");
#elif defined(Q_OS_WIN)
        pluginsPath.append("\\plugins");
#endif
        QByteArray p = path.toLatin1();
        QByteArray pp = pluginsPath.toLatin1();

        QByteArray log;
        QByteArray logFile;
        QByteArray verboseLevl;
        if(debugLevl>0){
            log="--extraintf=logger";
#ifdef Q_WS_WIN
	    logFile=QString("--logfile=").append(qgetenv("APPDATA")).append("\\vlc\\vlc-log.txt").toLatin1();
#else
	    logFile=QString("--logfile=").append(QDir::homePath()).append("/.vlc/vlc-log.txt").toLatin1();
#endif //Q_WS_WIN
            verboseLevl=QString("--verbose=").append(QString::number(debugLevl)).toLatin1();
        }

        // VLC command line options. See vlc --full-help
        const char *vlcArgs[] = {
            p.constData(),
            pp.constData(),
            log.constData(),
            logFile.constData(),
            verboseLevl.constData(),
            "--intf=dummy",
            "--ignore-config",
            "--reset-plugins-cache",
            "--no-media-library",
            "--no-one-instance",
            "--no-osd",
            "--no-stats",
            "--no-video-title-show",
            "--album-art=0"
        };

        // Create and initialize a libvlc instance (it should be done only once)
        vlc_instance = libvlc_new(sizeof(vlcArgs) / sizeof(*vlcArgs), vlcArgs);
        if(!vlc_instance)
            qDebug() << "libvlc exception:" << libvlc_errmsg();

        return true;
    } else {
        return false;
    }
}

void vlcRelease()
{
    libvlc_release(vlc_instance);
    vlcUnload();
}

#if defined(Q_OS_UNIX)
static bool libGreaterThan(const QString &lhs, const QString &rhs)
{
    QStringList lhsparts = lhs.split(QLatin1Char('.'));
    QStringList rhsparts = rhs.split(QLatin1Char('.'));
    Q_ASSERT(lhsparts.count() > 1 && rhsparts.count() > 1);

    for (int i = 1; i < rhsparts.count(); ++i) {
        if (lhsparts.count() <= i)
            // left hand side is shorter, so it's less than rhs
            return false;

        bool ok = false;
        int b = 0;
        int a = lhsparts.at(i).toInt(&ok);
        if (ok)
            b = rhsparts.at(i).toInt(&ok);
        if (ok) {
            // both toInt succeeded
            if (a == b)
                continue;
            return a > b;
        } else {
            // compare as strings;
            if (lhsparts.at(i) == rhsparts.at(i))
                continue;
            return lhsparts.at(i) > rhsparts.at(i);
        }
    }

    // they compared strictly equally so far
    // lhs cannot be less than rhs
    return true;
}
#endif

static QStringList findAllLibVlc()
{
    QStringList paths;
#if defined(Q_OS_UNIX)
    paths = QString::fromLatin1(qgetenv("LD_LIBRARY_PATH"))
            .split(QLatin1Char(':'), QString::SkipEmptyParts);
    paths << QLatin1String(PHONON_LIB_INSTALL_DIR) << QLatin1String("/usr/lib") << QLatin1String("/usr/local/lib");

    QStringList foundVlcs;
    foreach (const QString &path, paths) {
        QDir dir = QDir(path);
        QStringList entryList = dir.entryList(QStringList() << QLatin1String("libvlc.*"), QDir::Files);

        qSort(entryList.begin(), entryList.end(), libGreaterThan);
        foreach (const QString &entry, entryList)
            foundVlcs << path + QLatin1Char('/') + entry;
    }

    return foundVlcs;
#elif defined(Q_OS_WIN)
     // Read VLC version and installation directory from Windows registry
    QSettings settings(QSettings::SystemScope, "VideoLAN", "VLC");
    QString vlcVersion = settings.value("Version").toString();
    QString vlcInstallDir = settings.value("InstallDir").toString();
    if (vlcVersion.startsWith("1.1") && !vlcInstallDir.isEmpty()) {
        paths << vlcInstallDir + QLatin1Char('\\') + "libvlc.dll";
        return paths;
    }else{
        //If nothing is found in the registry try %PATH%
    QStringList searchPaths = QString::fromLatin1(qgetenv("PATH"))
            .split(QLatin1Char(';'), QString::SkipEmptyParts);
    QStringList foundVlcs;
    foreach (const QString &sp, searchPaths) {
        QDir dir = QDir(sp);
        QStringList entryList = dir.entryList(QStringList() << QLatin1String("libvlc.dll"), QDir::Files);
        foreach (const QString &entry, entryList)
            foundVlcs << sp + QLatin1Char('\\') + entry;
    }
    paths<<foundVlcs;
    return paths;
  }
#endif
}

static QLibrary *vlcLibrary = 0;

QString vlcPath()
{
    static QString path;
    if (!path.isEmpty()) {
        return path;
    }

    vlcLibrary = new QLibrary();
    QStringList paths = findAllLibVlc();
    foreach(path, paths) {
        vlcLibrary->setFileName(path);

        if (!vlcLibrary->resolve("libvlc_exception_init")) {//"libvlc_exception_init" not contained in 1.1+
            return path;
        } else {
            qDebug("Cannot resolve the symbol or load VLC library");
        }
        qWarning() << vlcLibrary->errorString();
    }

    vlcUnload();

    return QString();
}

void vlcUnload()
{
    vlcLibrary->unload();
    delete vlcLibrary;
    vlcLibrary = 0;
}

}
} // Namespace Phonon::VLC
<commit_msg>use pid in logfile name, use QFile::encode for pathes<commit_after>/*****************************************************************************
 * VLC backend for the Phonon library                                        *
 * Copyright (C) 2007-2008 Tanguy Krotoff <tkrotoff@gmail.com>               *
 * Copyright (C) 2008 Lukas Durfina <lukas.durfina@gmail.com>                *
 * Copyright (C) 2009 Fathi Boudra <fabo@kde.org>                            *
 *                                                                           *
 * This program is free software; you can redistribute it and/or             *
 * modify it under the terms of the GNU Lesser General Public                *
 * License as published by the Free Software Foundation; either              *
 * version 3 of the License, or (at your option) any later version.          *
 *                                                                           *
 * This program is distributed in the hope that it will be useful,           *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
 * Lesser General Public License for more details.                           *
 *                                                                           *
 * You should have received a copy of the GNU Lesser General Public          *
 * License along with this package; if not, write to the Free Software       *
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA *
 *****************************************************************************/

#include "vlcloader.h"

#include <QtCore/QDebug>
#include <QtCore/QDir>
#include <QtCore/QFile>
#include <QtCore/QLibrary>
#include <QtCore/QSettings>
#include <QtCore/QString>
#include <QtCore/QStringList>
#include <QtCore/QCoreApplication>

// Global variables
libvlc_instance_t *vlc_instance = 0;
libvlc_media_player_t *vlc_current_media_player = 0;

namespace Phonon
{
namespace VLC {

bool vlcInit(int debugLevl)
{
    // Global variables
    vlc_instance = 0;

    QString path = vlcPath();
    if (!path.isEmpty()) {
        QString pluginsPath = QString("--plugin-path=") + QDir::toNativeSeparators(QFileInfo(vlcPath()).dir().path());
#if defined(Q_OS_UNIX)
        pluginsPath.append("/vlc");
#elif defined(Q_OS_WIN)
        pluginsPath.append("\\plugins");
#endif
        QByteArray p = QFile::encodeName(path);
        QByteArray pp = QFile::encodeName(pluginsPath);

        QByteArray log;
        QByteArray logFile;
        QByteArray verboseLevl;
        if(debugLevl>0){
            verboseLevl=QString("--verbose=").append(QString::number(debugLevl)).toLatin1();
            log="--extraintf=logger";
            logFile="--logfile=";
#ifdef Q_WS_WIN
            QDir logFilePath(QString(qgetenv("APPDATA")).append("/vlc"));
#else
            QDir logFilePath(QDir::homePath().append("/.vlc"));
#endif //Q_WS_WIN
            logFilePath.mkdir("log");
            logFile.append(QFile::encodeName(QDir::toNativeSeparators(logFilePath.path().append("/log/vlc-log-").append(QString::number(qApp->applicationPid())).append(".txt"))));
        }
        // VLC command line options. See vlc --full-help
        const char *vlcArgs[] = {
            p.constData(),
            pp.constData(),
            log.constData(),
            logFile.constData(),
            verboseLevl.constData(),
            "--intf=dummy",
            "--ignore-config",
            "--reset-plugins-cache",
            "--no-media-library",
            "--no-one-instance",
            "--no-osd",
            "--no-stats",
            "--no-video-title-show",
            "--album-art=0"
        };

        // Create and initialize a libvlc instance (it should be done only once)
        vlc_instance = libvlc_new(sizeof(vlcArgs) / sizeof(*vlcArgs), vlcArgs);
        if(!vlc_instance)
            qDebug() << "libvlc exception:" << libvlc_errmsg();

        return true;
    } else {
        return false;
    }
}

void vlcRelease()
{
    libvlc_release(vlc_instance);
    vlcUnload();
}

#if defined(Q_OS_UNIX)
static bool libGreaterThan(const QString &lhs, const QString &rhs)
{
    QStringList lhsparts = lhs.split(QLatin1Char('.'));
    QStringList rhsparts = rhs.split(QLatin1Char('.'));
    Q_ASSERT(lhsparts.count() > 1 && rhsparts.count() > 1);

    for (int i = 1; i < rhsparts.count(); ++i) {
        if (lhsparts.count() <= i)
            // left hand side is shorter, so it's less than rhs
            return false;

        bool ok = false;
        int b = 0;
        int a = lhsparts.at(i).toInt(&ok);
        if (ok)
            b = rhsparts.at(i).toInt(&ok);
        if (ok) {
            // both toInt succeeded
            if (a == b)
                continue;
            return a > b;
        } else {
            // compare as strings;
            if (lhsparts.at(i) == rhsparts.at(i))
                continue;
            return lhsparts.at(i) > rhsparts.at(i);
        }
    }

    // they compared strictly equally so far
    // lhs cannot be less than rhs
    return true;
}
#endif

static QStringList findAllLibVlc()
{
    QStringList paths;
#if defined(Q_OS_UNIX)
    paths = QString::fromLatin1(qgetenv("LD_LIBRARY_PATH"))
            .split(QLatin1Char(':'), QString::SkipEmptyParts);
    paths << QLatin1String(PHONON_LIB_INSTALL_DIR) << QLatin1String("/usr/lib") << QLatin1String("/usr/local/lib");

    QStringList foundVlcs;
    foreach (const QString &path, paths) {
        QDir dir = QDir(path);
        QStringList entryList = dir.entryList(QStringList() << QLatin1String("libvlc.*"), QDir::Files);

        qSort(entryList.begin(), entryList.end(), libGreaterThan);
        foreach (const QString &entry, entryList)
            foundVlcs << path + QLatin1Char('/') + entry;
    }

    return foundVlcs;
#elif defined(Q_OS_WIN)
     // Read VLC version and installation directory from Windows registry
    QSettings settings(QSettings::SystemScope, "VideoLAN", "VLC");
    QString vlcVersion = settings.value("Version").toString();
    QString vlcInstallDir = settings.value("InstallDir").toString();
    if (vlcVersion.startsWith("1.1") && !vlcInstallDir.isEmpty()) {
        paths << vlcInstallDir + QLatin1Char('\\') + "libvlc.dll";
        return paths;
    }else{
        //If nothing is found in the registry try %PATH%
    QStringList searchPaths = QString::fromLatin1(qgetenv("PATH"))
            .split(QLatin1Char(';'), QString::SkipEmptyParts);
    QStringList foundVlcs;
    foreach (const QString &sp, searchPaths) {
        QDir dir = QDir(sp);
        QStringList entryList = dir.entryList(QStringList() << QLatin1String("libvlc.dll"), QDir::Files);
        foreach (const QString &entry, entryList)
            foundVlcs << sp + QLatin1Char('\\') + entry;
    }
    paths<<foundVlcs;
    return paths;
  }
#endif
}

static QLibrary *vlcLibrary = 0;

QString vlcPath()
{
    static QString path;
    if (!path.isEmpty()) {
        return path;
    }

    vlcLibrary = new QLibrary();
    QStringList paths = findAllLibVlc();
    foreach(path, paths) {
        vlcLibrary->setFileName(path);

        if (!vlcLibrary->resolve("libvlc_exception_init")) {//"libvlc_exception_init" not contained in 1.1+
            return path;
        } else {
            qDebug("Cannot resolve the symbol or load VLC library");
        }
        qWarning() << vlcLibrary->errorString();
    }

    vlcUnload();

    return QString();
}

void vlcUnload()
{
    vlcLibrary->unload();
    delete vlcLibrary;
    vlcLibrary = 0;
}

}
} // Namespace Phonon::VLC
<|endoftext|>


<commit_before>#include <stdio.h>

main()
{
	std::cout << "Hello World";
}

<commit_msg>edit hello world<commit_after>#include <stdio.h>
#include <iostream>

int main()
{
	std::cout << "Hello World";
}

<|endoftext|>


<commit_before>//===--- Stubs.cpp - Swift Language ABI Runtime Stubs ---------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// Misc stubs for functions which should be in swift.swift, but are difficult
// or impossible to write in swift at the moment.
//
//===----------------------------------------------------------------------===//

#include <mach/mach_time.h>
#include <sys/resource.h>
#include <sys/errno.h>
#include <cstring>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include "llvm/ADT/StringExtras.h"

// FIXME: We shouldn't be writing implemenetations for functions in the swift
// module in C, and this isn't really an ideal place to put those
// implementations.
extern "C" void _TSs5printFT3valNSs5Int64_T_(int64_t l) {
  printf("%lld", l);
}

extern "C" void _TSs5printFT3valNSs6UInt64_T_(uint64_t l) {
  printf("%llu", l);
}

extern "C" void _TSs5printFT3valNSs6Double_T_(double l) {
  char Buffer[256];
  sprintf(Buffer, "%g", l);
  if (strchr(Buffer, 'e') == nullptr && strchr(Buffer, '.') == nullptr)
    strcat(Buffer, ".0");
  printf("%s", Buffer);
}

// static func String(v : Int128, radix : Int) -> String
extern "C"
unsigned long long
print_int(char* TmpBuffer, __int64_t buf_len, __int128_t X, uint64_t Radix) {
  assert(Radix != 0 && Radix <= 36 && "Invalid radix for string conversion");
  char *P = TmpBuffer;

  bool WasNeg = X < 0;
  __uint128_t Y = WasNeg ? -X : X;

  if (Y == 0) {
    *P++ = '0';  // Special case.
  } else if (Radix == 10) {  // Special case for 10, since we care so much about performance right now.
    while (Y) {
      *P++ = '0' + char(Y % 10);
      Y /= 10;
    }
  } else {
    unsigned Radix32 = Radix;
    while (Y) {
      *P++ = llvm::hexdigit(Y % Radix32);
      Y /= Radix32;
    }
  }

  if (WasNeg) *P++ = '-';
  std::reverse(TmpBuffer, P);
  return size_t(P - TmpBuffer);
}

// static func String(v : Double) -> String
extern "C"
unsigned long long
print_double(char* Buffer, double X) {
  long long i = sprintf(Buffer, "%g", X);
  if (strchr(Buffer, 'e') == nullptr && strchr(Buffer, '.') == nullptr) {
    Buffer[i++] = '.';
    Buffer[i++] = '0';
  }
  if (i < 0) {
    __builtin_trap();
  }
  return i;
}

extern "C" bool _TNSs4Bool13getLogicValuefRS_FT_i1(bool* b) {
  return *b;
}

// FIXME: load_protocol and store_protocol are extremely ugly hacks.
struct protocol {
  void **witness_table;
  char buffer[24];
};

extern "C"
void
load_protocol(protocol *retval, protocol *p) {
  retval->witness_table = p->witness_table;
  typedef void* (*copyTy)(void*, void*, void**);
  copyTy initializeBufferWithCopyOfBuffer = (copyTy)(size_t)retval->witness_table[1];
  initializeBufferWithCopyOfBuffer(&retval->buffer,&p->buffer,retval->witness_table);
}

extern "C"
void
store_protocol(protocol *value, protocol *p) {
  p->witness_table = value->witness_table;
  typedef void* (*copyTy)(void*, void*, void**);
  copyTy initializeBufferWithCopyOfBuffer = (copyTy)(size_t)value->witness_table[1];
  initializeBufferWithCopyOfBuffer(&p->buffer,&value->buffer,value->witness_table);
}

static bool
_swift_replOutputIsUTF8(void) {
  const char *lang = getenv("LANG");
  return lang && strstr(lang, "UTF-8");
}

extern "C"
uint32_t
swift_replOutputIsUTF8(void) {
  static auto rval = _swift_replOutputIsUTF8();
  return rval;
}

#if defined(__i386__) || defined(__x86_64__)
static inline uint64_t
rdtsc() {
  uint32_t lo, hi;
  asm("rdtsc" : "=a" (lo), "=d" (hi));
  return uint64_t(hi) << 32 | lo;
}
#else
#error "not supported"
#endif

static double interruptOverhead;
static double loopOverhead;

static uint64_t
_swift_startBenchmark(void) {
  return rdtsc();
}

extern "C"
void
swift_printBenchmark(uint64_t start, uint64_t laps, char *buffer, int64_t len) {
  double val = rdtsc() - start;
  val /= laps;
  val /= interruptOverhead;
  val -= loopOverhead;
  printf("%12.2f  %*s\n", val, (int)len, buffer);
}

extern "C"
__attribute__((noinline,used))
__typeof__(&_swift_startBenchmark)
_swift_initBenchmark() asm("_swift_startBenchmark");

__typeof__(&_swift_startBenchmark)
_swift_initBenchmark() {
  asm(".symbol_resolver _swift_startBenchmark");
  union {
    unsigned reg[4*3];
    char brand[48];
  } u;
  char brand[48];
  char *s2 = u.brand;
  char *s1 = brand;
  unsigned eax, ebx, ecx, edx;
  memset(&u, 0, sizeof(u));

  eax = 0x80000002;
  asm("cpuid" : "+a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx));
  u.reg[0] = eax;
  u.reg[1] = ebx;
  u.reg[2] = ecx;
  u.reg[3] = edx;

  eax = 0x80000003;
  asm("cpuid" : "+a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx));
  u.reg[4] = eax;
  u.reg[5] = ebx;
  u.reg[6] = ecx;
  u.reg[7] = edx;

  eax = 0x80000004;
  asm("cpuid" : "+a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx));
  u.reg[8] = eax;
  u.reg[9] = ebx;
  u.reg[10] = ecx;
  u.reg[11] = edx;

  while (*s2 == ' ') {
    s2++;
  }
  do {
    while (s2[0] == ' ' && s2[1] == ' ') {
      s2++;
    }
  } while ((*s1++ = *s2++));
  printf("Processor: %s\n\n", brand);

  uint64_t start = rdtsc();
  for (unsigned long i = 0; i < 1000000000ull; i++) {
    asm("");
  }
  double delta = (rdtsc() - start) / 1000000000.0;
  assert((delta >= 1.0 && delta < 1.05) || (delta >= 2.0 && delta < 2.05));
  if (delta >= 2.0) {
    loopOverhead = 2.0;
    interruptOverhead = delta / 2.0;
  } else {
    loopOverhead = 1.0;
    interruptOverhead = delta / 1.0;
  }
  assert((interruptOverhead - 1.0) < 0.01);

  eax = 6;
  asm("cpuid" : "+a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx));

  if (eax & 2) {
    fprintf(stderr, "WARNING: TurboBoost. Results will be less reliable.\n");
    fprintf(stderr, "         Consider: sudo /usr/local/bin/pstates -D\n\n");
  }

  // Sigh... getpriority() can legitimately return -1
  errno = 0;
  int pri = getpriority(PRIO_PROCESS, 0);
  assert(errno == 0);
  if (pri >= 0) {
    fprintf(stderr, "WARNING: Non-elevated priority. Results will be less reliable.\n");
    fprintf(stderr, "         Consider: sudo nice -n -15 ./myBench\n\n");
  }

  return _swift_startBenchmark;
}
<commit_msg>Delay I/O to avoid OS-vs-benchmark CPU contention<commit_after>//===--- Stubs.cpp - Swift Language ABI Runtime Stubs ---------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// Misc stubs for functions which should be in swift.swift, but are difficult
// or impossible to write in swift at the moment.
//
//===----------------------------------------------------------------------===//

#include <mach/mach_time.h>
#include <sys/resource.h>
#include <sys/errno.h>
#include <cstring>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include "llvm/ADT/StringExtras.h"

// FIXME: We shouldn't be writing implemenetations for functions in the swift
// module in C, and this isn't really an ideal place to put those
// implementations.
extern "C" void _TSs5printFT3valNSs5Int64_T_(int64_t l) {
  printf("%lld", l);
}

extern "C" void _TSs5printFT3valNSs6UInt64_T_(uint64_t l) {
  printf("%llu", l);
}

extern "C" void _TSs5printFT3valNSs6Double_T_(double l) {
  char Buffer[256];
  sprintf(Buffer, "%g", l);
  if (strchr(Buffer, 'e') == nullptr && strchr(Buffer, '.') == nullptr)
    strcat(Buffer, ".0");
  printf("%s", Buffer);
}

// static func String(v : Int128, radix : Int) -> String
extern "C"
unsigned long long
print_int(char* TmpBuffer, __int64_t buf_len, __int128_t X, uint64_t Radix) {
  assert(Radix != 0 && Radix <= 36 && "Invalid radix for string conversion");
  char *P = TmpBuffer;

  bool WasNeg = X < 0;
  __uint128_t Y = WasNeg ? -X : X;

  if (Y == 0) {
    *P++ = '0';  // Special case.
  } else if (Radix == 10) {  // Special case for 10, since we care so much about performance right now.
    while (Y) {
      *P++ = '0' + char(Y % 10);
      Y /= 10;
    }
  } else {
    unsigned Radix32 = Radix;
    while (Y) {
      *P++ = llvm::hexdigit(Y % Radix32);
      Y /= Radix32;
    }
  }

  if (WasNeg) *P++ = '-';
  std::reverse(TmpBuffer, P);
  return size_t(P - TmpBuffer);
}

// static func String(v : Double) -> String
extern "C"
unsigned long long
print_double(char* Buffer, double X) {
  long long i = sprintf(Buffer, "%g", X);
  if (strchr(Buffer, 'e') == nullptr && strchr(Buffer, '.') == nullptr) {
    Buffer[i++] = '.';
    Buffer[i++] = '0';
  }
  if (i < 0) {
    __builtin_trap();
  }
  return i;
}

extern "C" bool _TNSs4Bool13getLogicValuefRS_FT_i1(bool* b) {
  return *b;
}

// FIXME: load_protocol and store_protocol are extremely ugly hacks.
struct protocol {
  void **witness_table;
  char buffer[24];
};

extern "C"
void
load_protocol(protocol *retval, protocol *p) {
  retval->witness_table = p->witness_table;
  typedef void* (*copyTy)(void*, void*, void**);
  copyTy initializeBufferWithCopyOfBuffer = (copyTy)(size_t)retval->witness_table[1];
  initializeBufferWithCopyOfBuffer(&retval->buffer,&p->buffer,retval->witness_table);
}

extern "C"
void
store_protocol(protocol *value, protocol *p) {
  p->witness_table = value->witness_table;
  typedef void* (*copyTy)(void*, void*, void**);
  copyTy initializeBufferWithCopyOfBuffer = (copyTy)(size_t)value->witness_table[1];
  initializeBufferWithCopyOfBuffer(&p->buffer,&value->buffer,value->witness_table);
}

static bool
_swift_replOutputIsUTF8(void) {
  const char *lang = getenv("LANG");
  return lang && strstr(lang, "UTF-8");
}

extern "C"
uint32_t
swift_replOutputIsUTF8(void) {
  static auto rval = _swift_replOutputIsUTF8();
  return rval;
}

#if defined(__i386__) || defined(__x86_64__)
static inline uint64_t
rdtsc() {
  uint32_t lo, hi;
  asm("rdtsc" : "=a" (lo), "=d" (hi));
  return uint64_t(hi) << 32 | lo;
}
#else
#error "not supported"
#endif

static double interruptOverhead;
static double loopOverhead;

static uint64_t
_swift_startBenchmark(void) {
  return rdtsc();
}

extern "C"
void
swift_printBenchmark(uint64_t start, uint64_t laps, char *buffer, int64_t len) {
  double val = rdtsc() - start;
  val /= laps;
  val /= interruptOverhead;
  val -= loopOverhead;
  printf("%12.2f  %*s\n", val, (int)len, buffer);
}

extern "C"
__attribute__((noinline,used))
__typeof__(&_swift_startBenchmark)
_swift_initBenchmark() asm("_swift_startBenchmark");

__typeof__(&_swift_startBenchmark)
_swift_initBenchmark() {
  asm(".symbol_resolver _swift_startBenchmark");
  union {
    unsigned reg[4*3];
    char brand[48];
  } u;
  char brand[48];
  char *s2 = u.brand;
  char *s1 = brand;
  unsigned eax, ebx, ecx, edx;
  memset(&u, 0, sizeof(u));
  int r;

  // Let's not have the OS compete with our CPU time if we can avoid it
  r = setvbuf(stdout, 0, _IOFBF, 0);
  assert(r == 0);

  eax = 0x80000002;
  asm("cpuid" : "+a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx));
  u.reg[0] = eax;
  u.reg[1] = ebx;
  u.reg[2] = ecx;
  u.reg[3] = edx;

  eax = 0x80000003;
  asm("cpuid" : "+a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx));
  u.reg[4] = eax;
  u.reg[5] = ebx;
  u.reg[6] = ecx;
  u.reg[7] = edx;

  eax = 0x80000004;
  asm("cpuid" : "+a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx));
  u.reg[8] = eax;
  u.reg[9] = ebx;
  u.reg[10] = ecx;
  u.reg[11] = edx;

  while (*s2 == ' ') {
    s2++;
  }
  do {
    while (s2[0] == ' ' && s2[1] == ' ') {
      s2++;
    }
  } while ((*s1++ = *s2++));
  printf("Processor: %s\n\n", brand);

  uint64_t start = rdtsc();
  for (unsigned long i = 0; i < 1000000000ull; i++) {
    asm("");
  }
  double delta = (rdtsc() - start) / 1000000000.0;
  assert((delta >= 1.0 && delta < 1.05) || (delta >= 2.0 && delta < 2.05));
  if (delta >= 2.0) {
    loopOverhead = 2.0;
    interruptOverhead = delta / 2.0;
  } else {
    loopOverhead = 1.0;
    interruptOverhead = delta / 1.0;
  }
  assert((interruptOverhead - 1.0) < 0.01);

  eax = 6;
  asm("cpuid" : "+a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx));

  if (eax & 2) {
    fprintf(stderr, "WARNING: TurboBoost. Results will be less reliable.\n");
    fprintf(stderr, "         Consider: sudo /usr/local/bin/pstates -D\n\n");
  }

  // Sigh... getpriority() can legitimately return -1
  errno = 0;
  int pri = getpriority(PRIO_PROCESS, 0);
  assert(errno == 0);
  if (pri >= 0) {
    fprintf(stderr, "WARNING: Non-elevated priority. Results will be less reliable.\n");
    fprintf(stderr, "         Consider: sudo nice -n -15 ./myBench\n\n");
  }

  return _swift_startBenchmark;
}
<|endoftext|>


<commit_before>//
//  gunzip.cpp
//  RhoLib
//
//  Created by Alex Epifanoff on 23.09.14.
//
//

#include "gunzip.h"
#include "zlib.h"
#include <stdio.h>

namespace gunzip
{

  using namespace zlib;

  int UnzipGzip(const char* inputFilename, const char* outputFilename)
  {
    static const int LENGTH = 0x1000;

    gzFile input;
    FILE* output;

    input = gzopen( inputFilename, "r" );
    if ( 0 == input )
    {
      return -1;
    }

    output = fopen( outputFilename, "w" );
    if ( 0 == output )
    {
      gzclose(input);
      return -1;
    }

    int status = 0;
    unsigned char buffer[LENGTH];

    while (true)
    {
      int bytes_read = gzread(input, buffer, LENGTH);

      if (fwrite(buffer,sizeof(char),bytes_read,output) != bytes_read )
      {
        status = -1;
        break;
      }

      if (bytes_read < LENGTH )
      {
        if (!gzeof (input))
        {
            status = -1;
        }
        break;
      }
    }

    gzclose(input);
    fclose(output);

    return status;
  }

/*
  int UnzipGzip(const char* inputFilename, const char* outputFilename)
  {
      FILE* input;
      FILE* output;

      static const int CHUNK_SIZE = 0x4000;

      z_stream strm = {0};
      unsigned char in[CHUNK_SIZE];
      unsigned char out[CHUNK_SIZE];

      strm.zalloc = Z_NULL;
      strm.zfree = Z_NULL;
      strm.opaque = Z_NULL;
      strm.next_in = in;
      strm.avail_in = 0;
      strm.next_out = out;

      int status = 0;

      input = fopen(inputFilename, "rb" );
      if ( 0 == input ) goto unzip_gzip_error;

      output = fopen(outputFilename, "wb" );
      if ( 0 == output ) goto unzip_gzip_error;

      if ( inflateInit2(&strm) < 0 ) goto unzip_gzip_error;

      while (true) {
          int bytes_read;

          bytes_read = fread (in, sizeof (char), sizeof (in), input);
          if (ferror(input)) goto unzip_gzip_error;

          strm.avail_in = bytes_read;
          do {
              strm.avail_out = CHUNK_SIZE;
              if (inflate(&strm, Z_NO_FLUSH) < 0) goto unzip_gzip_error;

              fwrite(out, sizeof(char), strm.avail_out, output);

              if (ferror(output)) goto unzip_gzip_error;
          }

          while (strm.avail_out == 0);
          if (feof (input)) {
              inflateEnd (& strm);
              break;
          }
      }

      status |= fclose(input);
      status |= fclose(output);

      return (0==status?0:-1);

  unzip_gzip_error:

      inflateEnd (& strm);

      fclose(input);
      fclose(output);

      return -1;
  }
  */

}<commit_msg>gzip: change file open modes<commit_after>//
//  gunzip.cpp
//  RhoLib
//
//  Created by Alex Epifanoff on 23.09.14.
//
//

#include "gunzip.h"
#include "zlib.h"
#include <stdio.h>

namespace gunzip
{

  using namespace zlib;

  int UnzipGzip(const char* inputFilename, const char* outputFilename)
  {
    static const int LENGTH = 0x1000;

    gzFile input;
    FILE* output;

    input = gzopen( inputFilename, "rb" );
    if ( 0 == input )
    {
      return -1;
    }

    output = fopen( outputFilename, "wb" );
    if ( 0 == output )
    {
      gzclose(input);
      return -1;
    }

    int status = 0;
    unsigned char buffer[LENGTH];

    while (true)
    {
      int bytes_read = gzread(input, buffer, LENGTH);

      if (fwrite(buffer,sizeof(char),bytes_read,output) != bytes_read )
      {
        status = -1;
        break;
      }

      if (bytes_read < LENGTH )
      {
        if (!gzeof (input))
        {
            status = -1;
        }
        break;
      }
    }

    gzclose(input);
    fclose(output);

    return status;
  }

/*
  int UnzipGzip(const char* inputFilename, const char* outputFilename)
  {
      FILE* input;
      FILE* output;

      static const int CHUNK_SIZE = 0x4000;

      z_stream strm = {0};
      unsigned char in[CHUNK_SIZE];
      unsigned char out[CHUNK_SIZE];

      strm.zalloc = Z_NULL;
      strm.zfree = Z_NULL;
      strm.opaque = Z_NULL;
      strm.next_in = in;
      strm.avail_in = 0;
      strm.next_out = out;

      int status = 0;

      input = fopen(inputFilename, "rb" );
      if ( 0 == input ) goto unzip_gzip_error;

      output = fopen(outputFilename, "wb" );
      if ( 0 == output ) goto unzip_gzip_error;

      if ( inflateInit2(&strm) < 0 ) goto unzip_gzip_error;

      while (true) {
          int bytes_read;

          bytes_read = fread (in, sizeof (char), sizeof (in), input);
          if (ferror(input)) goto unzip_gzip_error;

          strm.avail_in = bytes_read;
          do {
              strm.avail_out = CHUNK_SIZE;
              if (inflate(&strm, Z_NO_FLUSH) < 0) goto unzip_gzip_error;

              fwrite(out, sizeof(char), strm.avail_out, output);

              if (ferror(output)) goto unzip_gzip_error;
          }

          while (strm.avail_out == 0);
          if (feof (input)) {
              inflateEnd (& strm);
              break;
          }
      }

      status |= fclose(input);
      status |= fclose(output);

      return (0==status?0:-1);

  unzip_gzip_error:

      inflateEnd (& strm);

      fclose(input);
      fclose(output);

      return -1;
  }
  */

}<|endoftext|>