text:
//Implementation of https://tools.ietf.org/html/rfc6256
// Using Self-Delimiting Numeric Values in Protocols
#include "CborUint.h"
#include <iostream>
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/multiprecision/detail/bitscan.hpp>
#include <boost/endian/conversion.hpp>
#ifdef USE_CBOR_FAST
#include <immintrin.h>
#endif

#define CBOR_UINT8_TYPE   (24)
#define CBOR_UINT16_TYPE  (25)
#define CBOR_UINT32_TYPE  (26)
#define CBOR_UINT64_TYPE  (27)

//return output size
unsigned int CborEncodeU64(uint8_t * outputEncoded, const uint64_t valToEncodeU64, const uint64_t bufferSize) {
#ifdef USE_CBOR_FAST
    if (bufferSize >= 9) {
        return CborEncodeU64FastBufSize9(outputEncoded, valToEncodeU64);
    }
    else {
        return CborEncodeU64Fast(outputEncoded, valToEncodeU64, bufferSize);
    }
#else
    return CborEncodeU64Classic(outputEncoded, valToEncodeU64, bufferSize);
#endif // USE_CBOR_FAST
}

//return output size
unsigned int CborEncodeU64BufSize9(uint8_t * const outputEncoded, const uint64_t valToEncodeU64) {
#ifdef USE_CBOR_FAST
    return CborEncodeU64FastBufSize9(outputEncoded, valToEncodeU64);
#else
    return CborEncodeU64ClassicBufSize9(outputEncoded, valToEncodeU64);
#endif // USE_CBOR_FAST
}

//return decoded value (0 if failure), also set parameter numBytes taken to decode
uint64_t CborDecodeU64(const uint8_t * inputEncoded, uint8_t * numBytes, const uint64_t bufferSize) {
#ifdef USE_CBOR_FAST
    if (bufferSize >= 9) {
        return CborDecodeU64FastBufSize9(inputEncoded, numBytes);
    }
    else {
        return CborDecodeU64Fast(inputEncoded, numBytes, bufferSize);
    }
#else
    return CborDecodeU64Classic(inputEncoded, numBytes, bufferSize);
#endif // USE_CBOR_FAST
}

//return decoded value (0 if failure), also set parameter numBytes taken to decode
uint64_t CborDecodeU64BufSize9(const uint8_t * inputEncoded, uint8_t * numBytes) {
#ifdef USE_CBOR_FAST
    return CborDecodeU64FastBufSize9(inputEncoded, numBytes);
#else
    return CborDecodeU64ClassicBufSize9(inputEncoded, numBytes);
#endif // USE_CBOR_FAST
}

//return output size
unsigned int CborEncodeU64Classic(uint8_t * const outputEncoded, const uint64_t valToEncodeU64, const uint64_t bufferSize) {
    if (valToEncodeU64 < CBOR_UINT8_TYPE) {
        if (bufferSize < 1) {
            return 0;
        }
        outputEncoded[0] = (uint8_t)valToEncodeU64;
        return 1;
    }
    else if (valToEncodeU64 <= UINT8_MAX) {
        if (bufferSize < 2) {
            return 0;
        }
        outputEncoded[0] = CBOR_UINT8_TYPE;
        outputEncoded[1] = (uint8_t)valToEncodeU64;
        return 2;
    }
    else if (valToEncodeU64 <= UINT16_MAX) {
        if (bufferSize < 3) {
            return 0;
        }
        outputEncoded[0] = CBOR_UINT16_TYPE;
        const uint16_t be16 = boost::endian::native_to_big((uint16_t)valToEncodeU64);
        const uint8_t * const be16As8Ptr = (uint8_t*)&be16;
        outputEncoded[1] = be16As8Ptr[0];
        outputEncoded[2] = be16As8Ptr[1];
        return 3;
    }
    else if (valToEncodeU64 <= UINT32_MAX) {
        if (bufferSize < 5) {
            return 0;
        }
        outputEncoded[0] = CBOR_UINT32_TYPE;
        const uint32_t be32 = boost::endian::native_to_big((uint32_t)valToEncodeU64);
        const uint8_t * const be32As8Ptr = (uint8_t*)&be32;
        outputEncoded[1] = be32As8Ptr[0];
        outputEncoded[2] = be32As8Ptr[1];
        outputEncoded[3] = be32As8Ptr[2];
        outputEncoded[4] = be32As8Ptr[3];
        return 5;
    }
    else if (valToEncodeU64 <= UINT64_MAX) {
        if (bufferSize < 9) {
            return 0;
        }
        outputEncoded[0] = CBOR_UINT64_TYPE;
        const uint64_t be64 = boost::endian::native_to_big(valToEncodeU64);
#if 0
        * reinterpret_cast<uint64_t*>(&outputEncoded[1]) = be64;
#else
        const uint8_t * const be64As8Ptr = (uint8_t*)&be64;
        outputEncoded[1] = be64As8Ptr[0];
        outputEncoded[2] = be64As8Ptr[1];
        outputEncoded[3] = be64As8Ptr[2];
        outputEncoded[4] = be64As8Ptr[3];
        outputEncoded[5] = be64As8Ptr[4];
        outputEncoded[6] = be64As8Ptr[5];
        outputEncoded[7] = be64As8Ptr[6];
        outputEncoded[8] = be64As8Ptr[7];
#endif
        return 9;
    }
    return 0;
}

//return output size
unsigned int CborEncodeU64ClassicBufSize9(uint8_t * const outputEncoded, const uint64_t valToEncodeU64) {
    if (valToEncodeU64 < CBOR_UINT8_TYPE) {
        outputEncoded[0] = (uint8_t)valToEncodeU64;
        return 1;
    }
    else if (valToEncodeU64 <= UINT8_MAX) {
        outputEncoded[0] = CBOR_UINT8_TYPE;
        outputEncoded[1] = (uint8_t)valToEncodeU64;
        return 2;
    }
    else if (valToEncodeU64 <= UINT16_MAX) {
        outputEncoded[0] = CBOR_UINT16_TYPE;
        const uint16_t be16 = boost::endian::native_to_big((uint16_t)valToEncodeU64);
        const uint8_t * const be16As8Ptr = (uint8_t*)&be16;
        outputEncoded[1] = be16As8Ptr[0];
        outputEncoded[2] = be16As8Ptr[1];
        return 3;
    }
    else if (valToEncodeU64 <= UINT32_MAX) {
        outputEncoded[0] = CBOR_UINT32_TYPE;
        const uint32_t be32 = boost::endian::native_to_big((uint32_t)valToEncodeU64);
        const uint8_t * const be32As8Ptr = (uint8_t*)&be32;
        outputEncoded[1] = be32As8Ptr[0];
        outputEncoded[2] = be32As8Ptr[1];
        outputEncoded[3] = be32As8Ptr[2];
        outputEncoded[4] = be32As8Ptr[3];
        return 5;
    }
    else if (valToEncodeU64 <= UINT64_MAX) {
        outputEncoded[0] = CBOR_UINT64_TYPE;
        const uint64_t be64 = boost::endian::native_to_big(valToEncodeU64);
#if 0
        * reinterpret_cast<uint64_t*>(&outputEncoded[1]) = be64;
#else
        const uint8_t * const be64As8Ptr = (uint8_t*)&be64;
        outputEncoded[1] = be64As8Ptr[0];
        outputEncoded[2] = be64As8Ptr[1];
        outputEncoded[3] = be64As8Ptr[2];
        outputEncoded[4] = be64As8Ptr[3];
        outputEncoded[5] = be64As8Ptr[4];
        outputEncoded[6] = be64As8Ptr[5];
        outputEncoded[7] = be64As8Ptr[6];
        outputEncoded[8] = be64As8Ptr[7];
#endif
        return 9;
    }
    return 0;
}

//return output size
unsigned int CborGetEncodingSizeU64Classic(const uint64_t valToEncodeU64) {
    if (valToEncodeU64 < CBOR_UINT8_TYPE) {
        return 1;
    }
    else if (valToEncodeU64 <= UINT8_MAX) {
        return 2;
    }
    else if (valToEncodeU64 <= UINT16_MAX) {
        return 3;
    }
    else if (valToEncodeU64 <= UINT32_MAX) {
        return 5;
    }
    else if (valToEncodeU64 <= UINT64_MAX) {
        return 9;
    }
    else {
        return 0;
    }
}

//return decoded value (return invalid number that must be ignored on failure)
//  also sets parameter numBytes taken to decode (set to 0 on failure)
uint64_t CborDecodeU64Classic(const uint8_t * const inputEncoded, uint8_t * numBytes, const uint64_t bufferSize) {
    *numBytes = 0; //initialize to invalid
    if (bufferSize == 0) {
        return 0;
    }
    uint64_t result = 0;
    const uint8_t type = inputEncoded[0];
    if (type < CBOR_UINT8_TYPE) {
        result = type;
        *numBytes = 1;
    }
    else if (type == CBOR_UINT8_TYPE) {
        if (bufferSize >= 2) {
            result = inputEncoded[1];
            *numBytes = 2;
        }
    }
    else if (type == CBOR_UINT16_TYPE) {
        if (bufferSize >= 3) {
            uint16_t result16Be;
            uint8_t * const result16BeAs8Ptr = (uint8_t*)&result16Be;
            result16BeAs8Ptr[0] = inputEncoded[1];
            result16BeAs8Ptr[1] = inputEncoded[2];
            result = boost::endian::big_to_native(result16Be);
            *numBytes = 3;
        }
    }
    else if (type == CBOR_UINT32_TYPE) {
        if (bufferSize >= 5) {
            uint32_t result32Be;
            uint8_t * const result32BeAs8Ptr = (uint8_t*)&result32Be;
            result32BeAs8Ptr[0] = inputEncoded[1];
            result32BeAs8Ptr[1] = inputEncoded[2];
            result32BeAs8Ptr[2] = inputEncoded[3];
            result32BeAs8Ptr[3] = inputEncoded[4];
            result = boost::endian::big_to_native(result32Be);
            *numBytes = 5;
        }
    }
    else if (type == CBOR_UINT64_TYPE) {
        if (bufferSize >= 9) {
            uint64_t result64Be;
            uint8_t * const result64BeAs8Ptr = (uint8_t*)&result64Be;
            result64BeAs8Ptr[0] = inputEncoded[1];
            result64BeAs8Ptr[1] = inputEncoded[2];
            result64BeAs8Ptr[2] = inputEncoded[3];
            result64BeAs8Ptr[3] = inputEncoded[4];
            result64BeAs8Ptr[4] = inputEncoded[5];
            result64BeAs8Ptr[5] = inputEncoded[6];
            result64BeAs8Ptr[6] = inputEncoded[7];
            result64BeAs8Ptr[7] = inputEncoded[8];
            result = boost::endian::big_to_native(result64Be);
            *numBytes = 9;
        }
    }

    return result;
}

//return decoded value (return invalid number that must be ignored on failure)
//  also sets parameter numBytes taken to decode (set to 0 on failure)
uint64_t CborDecodeU64ClassicBufSize9(const uint8_t * const inputEncoded, uint8_t * numBytes) {
    *numBytes = 0; //initialize to invalid
    uint64_t result = 0;
    const uint8_t type = inputEncoded[0];
    if (type < CBOR_UINT8_TYPE) {
        result = type;
        *numBytes = 1;
    }
    else if (type == CBOR_UINT8_TYPE) {
        result = inputEncoded[1];
        *numBytes = 2;
    }
    else if (type == CBOR_UINT16_TYPE) {
        uint16_t result16Be;
        uint8_t * const result16BeAs8Ptr = (uint8_t*)&result16Be;
        result16BeAs8Ptr[0] = inputEncoded[1];
        result16BeAs8Ptr[1] = inputEncoded[2];
        result = boost::endian::big_to_native(result16Be);
        *numBytes = 3;
    }
    else if (type == CBOR_UINT32_TYPE) {
        uint32_t result32Be;
        uint8_t * const result32BeAs8Ptr = (uint8_t*)&result32Be;
        result32BeAs8Ptr[0] = inputEncoded[1];
        result32BeAs8Ptr[1] = inputEncoded[2];
        result32BeAs8Ptr[2] = inputEncoded[3];
        result32BeAs8Ptr[3] = inputEncoded[4];
        result = boost::endian::big_to_native(result32Be);
        *numBytes = 5;
    }
    else if (type == CBOR_UINT64_TYPE) {
        uint64_t result64Be;
        uint8_t * const result64BeAs8Ptr = (uint8_t*)&result64Be;
        result64BeAs8Ptr[0] = inputEncoded[1];
        result64BeAs8Ptr[1] = inputEncoded[2];
        result64BeAs8Ptr[2] = inputEncoded[3];
        result64BeAs8Ptr[3] = inputEncoded[4];
        result64BeAs8Ptr[4] = inputEncoded[5];
        result64BeAs8Ptr[5] = inputEncoded[6];
        result64BeAs8Ptr[6] = inputEncoded[7];
        result64BeAs8Ptr[7] = inputEncoded[8];
        result = boost::endian::big_to_native(result64Be);
        *numBytes = 9;
    }

    return result;
}

static const uint8_t msbToRequiredEncodingSize[64] = {
    1,1,1,1,2,2,2,2,
    3,3,3,3,3,3,3,3,
    5,5,5,5,5,5,5,5,
    5,5,5,5,5,5,5,5,
    9,9,9,9,9,9,9,9,
    9,9,9,9,9,9,9,9,
    9,9,9,9,9,9,9,9,
    9,9,9,9,9,9,9,9
};

#ifdef USE_CBOR_FAST

static const uint8_t encodingSizeToType[10] = {
    0, //size 0 = invalid
    0, //size 1 = the value itself
    CBOR_UINT8_TYPE, //size 2
    CBOR_UINT16_TYPE, //size 3
    0, //size 4 = invalid
    CBOR_UINT32_TYPE, //size 5
    0, //size 6 = invalid
    0, //size 7 = invalid
    0, //size 8 = invalid
    CBOR_UINT64_TYPE, //size 9
};
static const uint8_t encodingSizeToShifts[10] = {
    0, //size 0 = invalid
    0, //size 1 = the value itself
    56, //size 2
    48, //size 3
    0, //size 4 = invalid
    32, //size 5
    0, //size 6 = invalid
    0, //size 7 = invalid
    0, //size 8 = invalid
    0, //size 9
};
//encoding sizes with extra info to allow one lookup in a table
#define ES2 ((CBOR_UINT8_TYPE << 8) | (56U << 16))
#define ES3 ((CBOR_UINT16_TYPE << 8) | (48U << 16))
#define ES5 ((CBOR_UINT32_TYPE << 8) | (32U << 16))
#define ES9 ((CBOR_UINT64_TYPE << 8) | (0U << 16))
static const uint32_t msbToRequiredEncodingSizePlusTypePlusShifts[64] = {
    1,1,1,1,(2 | ES2),(2 | ES2),(2 | ES2),(2 | ES2),
    (3 | ES3),(3 | ES3),(3 | ES3),(3 | ES3),(3 | ES3),(3 | ES3),(3 | ES3),(3 | ES3),
    (5 | ES5),(5 | ES5),(5 | ES5),(5 | ES5),(5 | ES5),(5 | ES5),(5 | ES5),(5 | ES5),
    (5 | ES5),(5 | ES5),(5 | ES5),(5 | ES5),(5 | ES5),(5 | ES5),(5 | ES5),(5 | ES5),
    (9 | ES9),(9 | ES9),(9 | ES9),(9 | ES9),(9 | ES9),(9 | ES9),(9 | ES9),(9 | ES9),
    (9 | ES9),(9 | ES9),(9 | ES9),(9 | ES9),(9 | ES9),(9 | ES9),(9 | ES9),(9 | ES9),
    (9 | ES9),(9 | ES9),(9 | ES9),(9 | ES9),(9 | ES9),(9 | ES9),(9 | ES9),(9 | ES9),
    (9 | ES9),(9 | ES9),(9 | ES9),(9 | ES9),(9 | ES9),(9 | ES9),(9 | ES9),(9 | ES9)
};

//return output size
unsigned int CborEncodeU64Fast(uint8_t * const outputEncoded, const uint64_t valToEncodeU64, const uint64_t bufferSize) {

    //critical that the compiler optimizes this instruction using cmovne instead of imul (which is what the casts to uint8_t and bool are for)
    //bitscan seems to have undefined behavior on a value of zero
    //msb will be 0 if value to encode < 24
    const unsigned int msb = (static_cast<bool>(valToEncodeU64 >= CBOR_UINT8_TYPE)) * (static_cast<uint8_t>(boost::multiprecision::detail::find_msb<uint64_t>(valToEncodeU64)));

    const unsigned int encodingSize = msbToRequiredEncodingSize[msb];
    if (bufferSize >= 9) {
#if 0
        //const uint32_t typeAsUint32Le = boost::endian::native_to_little((uint32_t)encodingSizeToType[encodingSize]); //byte in first location
        //const uint8_t type = encodingSizeToType[encodingSize]; //byte in first location
        if (encodingSize == 1) {
            _mm_stream_si32((int32_t *)(&outputEncoded[0]), boost::endian::native_to_little((uint32_t)valToEncodeU64)); //outputEncoded[0] = (uint8_t)valToEncodeU64;
        }
        else if (encodingSize == 2) {
            uint32_t valToWrite = (uint8_t)valToEncodeU64;
            valToWrite <<= 8;
            valToWrite |= encodingSizeToType[encodingSize];
            _mm_stream_si32((int32_t *)(&outputEncoded[0]), boost::endian::native_to_little(valToWrite));
            //outputEncoded[0] = encodingSizeToType[encodingSize];
            //outputEncoded[1] = (uint8_t)valToEncodeU64;
        }
        else if (encodingSize == 3) {

            uint32_t valToWrite = boost::endian::native_to_big((uint16_t)valToEncodeU64);
            valToWrite <<= 8;
            valToWrite |= encodingSizeToType[encodingSize];
            _mm_stream_si32((int32_t *)(&outputEncoded[0]), boost::endian::native_to_little(valToWrite));
            //outputEncoded[0] = encodingSizeToType[encodingSize];
            //outputEncoded[1] = (uint8_t)(valToEncodeU64 >> 8); //big endian most significant byte first
            //outputEncoded[2] = (uint8_t)valToEncodeU64; //big endian least significant byte last
        }
        else if (encodingSize == 5) {
            uint64_t valToWrite = boost::endian::native_to_big((uint32_t)valToEncodeU64);
            valToWrite <<= 8;
            valToWrite |= encodingSizeToType[encodingSize];
            _mm_stream_si64((long long int *)(&outputEncoded[0]), boost::endian::native_to_little(valToWrite));
            //outputEncoded[0] = encodingSizeToType[encodingSize];
            //_mm_stream_si32((int32_t *)(&outputEncoded[1]), boost::endian::native_to_big((uint32_t)valToEncodeU64));
        }
        else if (encodingSize == 9) {
            _mm_stream_si32((int32_t *)(&outputEncoded[0]), boost::endian::native_to_little((uint32_t)encodingSizeToType[encodingSize])); //outputEncoded[0] = encodingSizeToType[encodingSize];
            _mm_stream_si64((long long int *)(&outputEncoded[1]), boost::endian::native_to_big(valToEncodeU64));
        }
#else
        /*
        if ((bufferSize >= 9) && (encodingSize > 1)) {
            outputEncoded[0] = encodingSizeToType[encodingSize];
            _mm_stream_si64((long long int *)(&outputEncoded[1]), boost::endian::native_to_big(valToEncodeU64 << encodingSizeToShifts[encodingSize]));
            return encodingSize;
        }*/
        const uint8_t firstByte = encodingSizeToType[encodingSize] | ((static_cast<bool>(encodingSize == 1)) * ((uint8_t)valToEncodeU64)); //byte in first location
        _mm_stream_si32((int32_t *)outputEncoded, boost::endian::native_to_little((uint32_t)firstByte)); //outputEncoded[0] = (uint8_t)valToEncodeU64;
        //_mm_stream_si64((long long int *)outputEncoded, boost::endian::native_to_little((uint64_t)firstByte)); //outputEncoded[0] = (uint8_t)valToEncodeU64;
        _mm_stream_si64((long long int *)(&outputEncoded[1]), boost::endian::native_to_big(valToEncodeU64 << encodingSizeToShifts[encodingSize]));
        /*return encodingSize;
        if (encodingSize == 1) {
            _mm_stream_si32((int32_t *)(&outputEncoded[0]), boost::endian::native_to_little((uint32_t)valToEncodeU64)); //outputEncoded[0] = (uint8_t)valToEncodeU64;
        }
        if ((encodingSize > 1)) {
            outputEncoded[0] = encodingSizeToType[encodingSize];
            const uint64_t src = boost::endian::native_to_big(valToEncodeU64 << encodingSizeToShifts[encodingSize]);
            memcpy(&outputEncoded[1], &src, encodingSize - 1);
            return encodingSize;
        }*/
#endif
    }
    else if (bufferSize < encodingSize) {
        return 0;
    }
    else {
#if 0

#endif
        outputEncoded[0] = encodingSizeToType[encodingSize];
        if (encodingSize == 1) {
            outputEncoded[0] = (uint8_t)valToEncodeU64;
        }
        else if (encodingSize == 2) {
            outputEncoded[1] = (uint8_t)valToEncodeU64;
        }
        else if (encodingSize == 3) {
            outputEncoded[1] = (uint8_t)(valToEncodeU64 >> 8); //big endian most significant byte first
            outputEncoded[2] = (uint8_t)valToEncodeU64; //big endian least significant byte last
        }
        else if (encodingSize == 5) {
            _mm_stream_si32((int32_t *)(&outputEncoded[1]), boost::endian::native_to_big((uint32_t)valToEncodeU64));
        }
        else if (encodingSize == 9) {
            _mm_stream_si64((long long int *)(&outputEncoded[1]), boost::endian::native_to_big(valToEncodeU64));
        }
    }
    return encodingSize;
}

//return output size
unsigned int CborEncodeU64FastBufSize9(uint8_t * const outputEncoded, const uint64_t valToEncodeU64) {
    //critical that the compiler optimizes this instruction using cmovne instead of imul (which is what the casts to uint8_t and bool are for)
    //bitscan seems to have undefined behavior on a value of zero
    //msb will be 0 if value to encode < 24
    const unsigned int msb = (static_cast<bool>(valToEncodeU64 >= CBOR_UINT8_TYPE)) * (static_cast<uint8_t>(boost::multiprecision::detail::find_msb<uint64_t>(valToEncodeU64)));
    const uint32_t requiredEncodingSizePlusTypePlusShifts = msbToRequiredEncodingSizePlusTypePlusShifts[msb];
    const uint8_t encodingSize = (uint8_t)requiredEncodingSizePlusTypePlusShifts;
    const uint8_t type = (uint8_t)(requiredEncodingSizePlusTypePlusShifts >> 8);
    const uint8_t shift = (uint8_t)(requiredEncodingSizePlusTypePlusShifts >> 16);
    const uint8_t firstByte = type | ((static_cast<bool>(encodingSize == 1)) * ((uint8_t)valToEncodeU64)); //byte in first location
#if 0
    outputEncoded[0] = firstByte;
    *(reinterpret_cast<uint64_t*>(&outputEncoded[1])) = boost::endian::native_to_big(valToEncodeU64 << shift);
#else
#if 1
    _mm_stream_si32((int32_t *)outputEncoded, boost::endian::native_to_little((uint32_t)firstByte)); //outputEncoded[0] = (uint8_t)valToEncodeU64;
#else
    _mm_stream_si64((long long int *)outputEncoded, boost::endian::native_to_little((uint64_t)firstByte)); //outputEncoded[0] = (uint8_t)valToEncodeU64;
#endif
    _mm_stream_si64((long long int *)(&outputEncoded[1]), boost::endian::native_to_big(valToEncodeU64 << shift));
#endif
    return encodingSize;
}

//return decoded value (return invalid number that must be ignored on failure)
//  also sets parameter numBytes taken to decode (set to 0 on failure)
uint64_t CborDecodeU64Fast(const uint8_t * const inputEncoded, uint8_t * numBytes, const uint64_t bufferSize) {
    *numBytes = 0; //initialize to invalid
    if (bufferSize == 0) {
        return 0;
    }
    uint64_t result = 0;
    const uint8_t type = inputEncoded[0];
    if (type < CBOR_UINT8_TYPE) {
        result = type;
        *numBytes = 1;
    }
    else if (type == CBOR_UINT8_TYPE) {
        if (bufferSize >= 2) {
            result = inputEncoded[1];
            *numBytes = 2;
        }
    }
    else if (type == CBOR_UINT16_TYPE) {
        if (bufferSize >= 3) {
            result = ((static_cast<uint16_t>(inputEncoded[1])) << 8) | inputEncoded[2];
            *numBytes = 3;
        }
    }
    else if (type == CBOR_UINT32_TYPE) {
        if (bufferSize >= 5) {
            //const __m128i enc = _mm_loadu_si32((__m128i const*)(&inputEncoded[1])); //SSE2 Load unaligned 32-bit integer from memory into the first element of dst.
#if 1
            const __m128i enc = _mm_castps_si128(_mm_load_ss((float const*)(&inputEncoded[1]))); //Load a single-precision (32-bit) floating-point element from memory into the lower of dst, and zero the upper 3 elements. mem_addr does not need to be aligned on any particular boundary.
            const uint32_t result32Be = _mm_cvtsi128_si32(enc); //SSE2 Copy the lower 32-bit integer in a to dst.
            result = boost::endian::big_to_native(result32Be);
#else
            result = static_cast<uint32_t>(_loadbe_i32(&inputEncoded[1]));
#endif

            *numBytes = 5;
        }
    }
    else if (type == CBOR_UINT64_TYPE) {
        if (bufferSize >= 9) {
            //const __m128i enc = _mm_loadl_epi64((__m128i const*)(&inputEncoded[1])); //_mm_loadu_si64(data); //SSE Load unaligned 64-bit integer from memory into the first element of dst.
#if 1
            const __m128i enc = _mm_castpd_si128(_mm_load_sd((double const*)(&inputEncoded[1]))); //Load a double-precision (64-bit) floating-point element from memory into the lower of dst, and zero the upper element. mem_addr does not need to be aligned on any particular boundary.
            const uint64_t result64Be = _mm_cvtsi128_si64(enc); //SSE2 Copy the lower 64-bit integer in a to dst.
            result = boost::endian::big_to_native(result64Be);
#else
            result = static_cast<uint64_t>(_loadbe_i64(&inputEncoded[1]));
#endif
            *numBytes = 9;
        }
    }

    return result;
}

//return decoded value (return invalid number that must be ignored on failure)
//  also sets parameter numBytes taken to decode (set to 0 on failure)
uint64_t CborDecodeU64FastBufSize9(const uint8_t * const inputEncoded, uint8_t * numBytes) {
#if 1
    static const uint8_t RIGHT_SHIFTS[CBOR_UINT64_TYPE + 1] = {
        56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, //type = 0..23
        56, 48, 32, 0 //type = 24..27
    };
    static const uint8_t NUM_BYTES[CBOR_UINT64_TYPE + 1] = {
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, //type = 0..23
        2, 3, 5, 9 //type = 24..27
    };

    const uint8_t type = inputEncoded[0];
    if (type > CBOR_UINT64_TYPE) { //branch prediction should never enter here
        *numBytes = 0;
        return 0;
    }
    *numBytes = NUM_BYTES[type];
    const __m128i enc = _mm_castpd_si128(_mm_load_sd((double const*)(&inputEncoded[static_cast<uint8_t>(type >= CBOR_UINT8_TYPE)]))); //Load a double-precision (64-bit) floating-point element from memory into the lower of dst, and zero the upper element. mem_addr does not need to be aligned on any particular boundary.
    const uint64_t result64Be = _mm_cvtsi128_si64(enc); //SSE2 Copy the lower 64-bit integer in a to dst.
    //const uint64_t result64Be = *(reinterpret_cast<const uint64_t*>(&inputEncoded[static_cast<uint8_t>(type >= CBOR_UINT8_TYPE)]));
    return (static_cast<uint64_t>(boost::endian::big_to_native(result64Be))) >> RIGHT_SHIFTS[type];
#else
    static constexpr uint16_t A = (1 << 8) | 56; //type = 0..23
    static constexpr uint16_t B = (2 << 8) | 56; //type = 24
    static constexpr uint16_t C = (3 << 8) | 48; //type = 25
    static constexpr uint16_t D = (5 << 8) | 32; //type = 26
    static constexpr uint16_t E = (9 << 8) | 0; //type = 27
    static const uint16_t NUM_BYTES_PLUS_RIGHT_SHIFTS[CBOR_UINT64_TYPE + 1] = {
        A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, //type = 0..23
        B, C, D, E //type = 24..27
    };

    const uint8_t type = inputEncoded[0];
    if (type > CBOR_UINT64_TYPE) { //branch prediction should never enter here
        *numBytes = 0;
        return 0;
    }
    const uint16_t rightShiftsPlusNumBytes = NUM_BYTES_PLUS_RIGHT_SHIFTS[type];
    *numBytes = rightShiftsPlusNumBytes >> 8;
    const __m128i enc = _mm_castpd_si128(_mm_load_sd((double const*)(&inputEncoded[static_cast<uint8_t>(type >= CBOR_UINT8_TYPE)]))); //Load a double-precision (64-bit) floating-point element from memory into the lower of dst, and zero the upper element. mem_addr does not need to be aligned on any particular boundary.
    const uint64_t result64Be = _mm_cvtsi128_si64(enc); //SSE2 Copy the lower 64-bit integer in a to dst.
    return (static_cast<uint64_t>(boost::endian::big_to_native(result64Be))) >> (static_cast<uint8_t>(rightShiftsPlusNumBytes));
#endif
}

#endif //#ifdef USE_CBOR_FAST

//return output size
unsigned int CborGetNumBytesRequiredToEncode(const uint64_t valToEncodeU64) {

    //critical that the compiler optimizes this instruction using cmovne instead of imul (which is what the casts to uint8_t and bool are for)
    //bitscan seems to have undefined behavior on a value of zero
    //msb will be 0 if value to encode < 24
    const unsigned int msb = (static_cast<bool>(valToEncodeU64 >= CBOR_UINT8_TYPE)) * (static_cast<uint8_t>(boost::multiprecision::detail::find_msb<uint64_t>(valToEncodeU64)));

    return msbToRequiredEncodingSize[msb];
}

uint64_t CborTwoUint64ArraySerialize(uint8_t * serialization, const uint64_t element1, const uint64_t element2) {
    uint8_t * const serializationBase = serialization;
    *serialization++ = (4U << 5) | 2; //major type 4, additional information 2
    serialization += CborEncodeU64BufSize9(serialization, element1);
    serialization += CborEncodeU64BufSize9(serialization, element2);
    return serialization - serializationBase;
}
uint64_t CborTwoUint64ArraySerialize(uint8_t * serialization, const uint64_t element1, const uint64_t element2, uint64_t bufferSize) {
    uint8_t * const serializationBase = serialization;
    uint64_t thisSerializationSize;
    if (bufferSize == 0) {
        return 0;
    }
    --bufferSize;
    *serialization++ = (4U << 5) | 2; //major type 4, additional information 2

    thisSerializationSize = CborEncodeU64(serialization, element1, bufferSize); //if zero returned on failure will only malform the bundle but not cause memory overflow
    serialization += thisSerializationSize;
    bufferSize -= thisSerializationSize;

    thisSerializationSize = CborEncodeU64(serialization, element2, bufferSize);
    serialization += thisSerializationSize;
    //bufferSize -= thisSerializationSize; //not needed

    return serialization - serializationBase;
}
uint64_t CborTwoUint64ArraySerializationSize(const uint64_t element1, const uint64_t element2) {
    uint64_t serializationSize = 1; //cbor first byte major type 4, additional information 2
    serializationSize += CborGetEncodingSizeU64(element1);
    serializationSize += CborGetEncodingSizeU64(element2);
    return serializationSize;
}
bool CborTwoUint64ArrayDeserialize(const uint8_t * serialization, uint8_t * numBytesTakenToDecode, uint64_t bufferSize, uint64_t & element1, uint64_t & element2) {
    uint8_t cborUintSize;
    const uint8_t * const serializationBase = serialization;

    if (bufferSize == 0) {
        return false;
    }
    --bufferSize;
    const uint8_t initialCborByte = *serialization++;
    if ((initialCborByte != ((4U << 5) | 2U)) && //major type 4, additional information 2 (array of length 2)
        (initialCborByte != ((4U << 5) | 31U))) { //major type 4, additional information 31 (Indefinite-Length Array)
        return false;
    }

    element1 = CborDecodeU64(serialization, &cborUintSize, bufferSize);
    if (cborUintSize == 0) {
        return false; //failure
    }
    serialization += cborUintSize;
    bufferSize -= cborUintSize;

    element2 = CborDecodeU64(serialization, &cborUintSize, bufferSize);
    if (cborUintSize == 0) {
        return false; //failure
    }
    serialization += cborUintSize;

    //An implementation of the Bundle Protocol MAY accept a sequence of
    //bytes that does not conform to the Bundle Protocol specification
    //(e.g., one that represents data elements in fixed-length arrays
    //rather than indefinite-length arrays) and transform it into
    //conformant BP structure before processing it.
    if (initialCborByte == ((4U << 5) | 31U)) { //major type 4, additional information 31 (Indefinite-Length Array)
        bufferSize -= cborUintSize; //from element 2 above
        if (bufferSize == 0) {
            return false;
        }
        const uint8_t breakStopCode = *serialization++;
        if (breakStopCode != 0xff) {
            return false;
        }
    }

    *numBytesTakenToDecode = static_cast<uint8_t>(serialization - serializationBase);
    return true;
}

uint64_t CborArbitrarySizeUint64ArraySerialize(uint8_t * serialization, const std::vector<uint64_t> & elements) {
    uint8_t * const serializationBase = serialization;

    uint8_t * const arrayHeaderStartPtr = serialization;
    serialization += CborEncodeU64BufSize9(serialization, elements.size());
    *arrayHeaderStartPtr |= (4U << 5); //change from major type 0 (unsigned integer) to major type 4 (array)

    for (std::size_t i = 0; i < elements.size(); ++i) {
        serialization += CborEncodeU64BufSize9(serialization, elements[i]);
    }
    return serialization - serializationBase;
}
uint64_t CborArbitrarySizeUint64ArraySerialize(uint8_t * serialization, const std::vector<uint64_t> & elements, uint64_t bufferSize) {
    uint8_t * const serializationBase = serialization;
    uint64_t thisSerializationSize;
    uint8_t * const arrayHeaderStartPtr = serialization;
    thisSerializationSize = CborEncodeU64(serialization, elements.size(), bufferSize); //if zero returned on failure will only malform the bundle but not cause memory overflow
    serialization += thisSerializationSize;
    bufferSize -= thisSerializationSize;
    *arrayHeaderStartPtr |= (4U << 5); //change from major type 0 (unsigned integer) to major type 4 (array)

    for (std::size_t i = 0; i < elements.size(); ++i) {
        thisSerializationSize = CborEncodeU64(serialization, elements[i], bufferSize); //if zero returned on failure will only malform the bundle but not cause memory overflow
        serialization += thisSerializationSize;
        bufferSize -= thisSerializationSize;
    }
    return serialization - serializationBase;
}
uint64_t CborArbitrarySizeUint64ArraySerializationSize(const std::vector<uint64_t> & elements) {
    uint64_t serializationSize = CborGetEncodingSizeU64(elements.size());
    for (std::size_t i = 0; i < elements.size(); ++i) {
        serializationSize += CborGetEncodingSizeU64(elements[i]);
    }
    return serializationSize;
}
bool CborArbitrarySizeUint64ArrayDeserialize(uint8_t * serialization, uint64_t & numBytesTakenToDecode, uint64_t bufferSize, std::vector<uint64_t> & elements, const uint64_t maxElements) {
    uint8_t cborUintSizeDecoded;
    const uint8_t * const serializationBase = serialization;

    if (bufferSize == 0) {
        return false;
    }
    const uint8_t initialCborByte = *serialization; //buffer size verified above
    if (initialCborByte == ((4U << 5) | 31U)) { //major type 4, additional information 31 (Indefinite-Length Array)
        //An implementation of the Bundle Protocol MAY accept a sequence of
        //bytes that does not conform to the Bundle Protocol specification
        //(e.g., one that represents data elements in fixed-length arrays
        //rather than indefinite-length arrays) and transform it into
        //conformant BP structure before processing it.
        ++serialization;
        --bufferSize;
        elements.resize(0);
        elements.reserve(maxElements);
        for (std::size_t i = 0; i < maxElements; ++i) {
            elements.push_back(CborDecodeU64(serialization, &cborUintSizeDecoded, bufferSize));
            if (cborUintSizeDecoded == 0) {
                return false; //failure
            }
            serialization += cborUintSizeDecoded;
            bufferSize -= cborUintSizeDecoded;
        }
        if (bufferSize == 0) {
            return false;
        }
        const uint8_t breakStopCode = *serialization++;
        if (breakStopCode != 0xff) {
            return false;
        }
    }
    else {
        uint8_t * const arrayHeaderStartPtr = serialization; //buffer size verified above
        const uint8_t cborMajorTypeArray = (*arrayHeaderStartPtr) >> 5;
        if (cborMajorTypeArray != 4) {
            return false; //failure
        }
        *arrayHeaderStartPtr &= 0x1f; //temporarily zero out major type to 0 to make it unsigned integer
        const uint64_t numElements = CborDecodeU64(arrayHeaderStartPtr, &cborUintSizeDecoded, bufferSize);
        *arrayHeaderStartPtr |= (4U << 5); // restore to major type to 4 (change from major type 0 (unsigned integer) to major type 4 (array))
        if (cborUintSizeDecoded == 0) {
            return false; //failure
        }
        if (numElements > maxElements) {
            return false; //failure
        }
        serialization += cborUintSizeDecoded;
        bufferSize -= cborUintSizeDecoded;

        elements.resize(numElements);
        for (std::size_t i = 0; i < numElements; ++i) {
            elements[i] = CborDecodeU64(serialization, &cborUintSizeDecoded, bufferSize);
            if (cborUintSizeDecoded == 0) {
                return false; //failure
            }
            serialization += cborUintSizeDecoded;
            bufferSize -= cborUintSizeDecoded;
        }
    }

    numBytesTakenToDecode = (serialization - serializationBase);
    return true;
}


text:
#include "Classifier.h"
#include "vw.h"
#include "../moses/Util.h"
#include <iostream>
#include <boost/algorithm/string/predicate.hpp>

using namespace boost::algorithm;

namespace Discriminative
{

ClassifierFactory::ClassifierFactory(const std::string &modelFile, const std::string &vwOptions)
  : m_vwOptions(vwOptions), m_train(false)
{
  m_VWInstance = VW::initialize(VW_DEFAULT_OPTIONS + " -i " + modelFile + vwOptions);
}

ClassifierFactory::ClassifierFactory(const std::string &modelFilePrefix)
  : m_lastId(0), m_train(true)
{
  if (ends_with(modelFilePrefix, ".gz")) {
    m_modelFilePrefix = modelFilePrefix.substr(0, modelFilePrefix.size() - 3);
    m_gzip = true;
  } else {
    m_modelFilePrefix = modelFilePrefix;
    m_gzip = false;
  }
}

ClassifierFactory::~ClassifierFactory()
{
  if (! m_train)
    VW::finish(*m_VWInstance);
}

ClassifierFactory::ClassifierPtr ClassifierFactory::operator()()
{
  if (m_train) {
    boost::unique_lock<boost::mutex> lock(m_mutex); // avoid possible race for m_lastId
    return ClassifierFactory::ClassifierPtr(
             new VWTrainer(m_modelFilePrefix + "." + Moses::SPrint(m_lastId++) + (m_gzip ? ".gz" : "")));
  } else {
    return ClassifierFactory::ClassifierPtr(
             new VWPredictor(m_VWInstance, VW_DEFAULT_PARSER_OPTIONS + m_vwOptions));
  }
}

}

ext: cpp

max_issues_repo_name: Pangeamt/nectm

max_forks_repo_name: Pangeamt/nectm


text:
#include <iostream>
#include <memory>
#include <string>
#include <utility>
#include <vector>

#include <boost/thread.hpp>

#include <clipper/constants.hpp>
#include <clipper/datatypes.hpp>
#include <clipper/logging.hpp>
#include <clipper/query_processor.hpp>

using namespace clipper;

int main() {
  QueryProcessor qp;
  std::shared_ptr<Input> input =
      std::make_shared<DoubleVector>(std::vector<double>{1.1, 2.2, 3.3, 4.4});
  boost::future<Response> prediction =
      qp.predict({"test", 3, input, 20000, "EXP3", {std::make_pair("m", 1)}});
  log_info(LOGGING_TAG_CLIPPER, prediction.get().debug_string());
}

max_stars_repo_name: jegonzal/clipper

max_issues_repo_name: jegonzal/clipper

max_forks_repo_name: jegonzal/clipper


text:
#ifndef CPU_ONLY

#ifndef CAFFE_GPU_MANAGER_HPP_
#define CAFFE_GPU_MANAGER_HPP_

#include <boost/thread.hpp>
#include <glog/logging.h>

#include <cublas_v2.h>

#include "caffe/util/device_alternate.hpp"

#ifdef USE_CUDNN
#include "caffe/util/cudnn.hpp"
#endif    /** USE_CUDNN **/

// #define ObjectMember(val) (GpuStreamPool::Get().val)

#define DISABLE_COPY_AND_ASSIGN(classname)                      \
private:                                                        \
  classname(const classname&);                                  \
  classname& operator=(const classname&)                        \

#define GetCUDAConnNum(major, minor, num) {   \
  if (major == 3 && (minor == 2 || minor == 5)) {             \
    num = 8;                                  \
  } else if ((major == 3 && minor == 0) ||    \
      (major == 5 && minor == 3)) {           \
    num = 8;                                  \
  } else {                                    \
    num = 16;                                 \
  }                                           \
}

#define GetMaxBlocksNum(major, minor)    {          \
  switch (major) {                                  \
  case 2:                                           \
    return 8;                                       \
  case 3:                                           \
    return 16;                                      \
  case 5:                                           \
    return 32;                                      \
  default:                                          \
    return 32;                                      \
  }                                                 \
}

namespace caffe{
  class GpuStreamPool
  {
    public:
      /**
       * @brief   GpuStreamPool Destructor
       */
      ~GpuStreamPool();

      /**
       * @brief   Object access method.
       *
       * Thread local context for GpuStreamPool. Each device has a unique GpuStreamPool
       * object to manage the connection between host and deivce.
       *
       * @return  The corresponding GpuStreamPool object.
       */
      static GpuStreamPool& Get();

      void Reset();

      /**
       * @brief   Pool size setting function.
       * @param[in] pool_size   The required pool size.
       */
      void SetPoolSize(size_t pool_size = 0);

      /**
       * @brief   Set the corresponding GPU device.
       * @param[in] device_id   The target GPU device.
       */
      void SetDevice(const int device_id);

      /**
       * @brief   Get the maximum number of GPU streams.
       */
      inline unsigned int GetMaxNumOfStreams() {
        return GetCUDASettings(this->device_id_);
      }

      /**
       * @brief   Get the kernel running CUDA stream.
       * @param[in] stream_id   ID of the selected stream_id that the kernel will be launched.
       */
      inline cudaStream_t cuda_stream(int stream_id = -1) {
        if (stream_id == -1 or this->handle_num_ == 0) {
          return 0;
        }

        // return ObjectMember(streams_)[stream_id % ObjectMember(handle_num_)];
        return this->streams_[stream_id % handle_num_];
      }

      /**
       * @brief   Get the cuBLAS kernel handler.
       * @param[in] cublas_id   ID of the selected cuBLAS handler.
       */
      inline cublasHandle_t cublas_handle(int cublas_id = -1) {
        if (cublas_id == -1 or this->handle_num_ == 0) {
          // return ObjectMember(default_cublas_handle_);
          return this->default_cublas_handle_;
        }

        // return ObjectMember(cublas_handles_)[cublas_id % ObjectMember(handle_num_)];
        return this->cublas_handles_[cublas_id % handle_num_];
      }

      /**
       * @brief   Get the number of cuBLAS handler.
       */
      inline unsigned int cublas_handle_num () {
        // return ObjectMember(handle_num_);
        return this->handle_num_;
      }

#ifdef USE_CUDNN
      /**
       * @brief   Get the cuDNN kernel handler.
       * @param[in] cudnn_id    ID of the selected cuDNN handler.
       */
      inline cudnnHandle_t cudnn_handle(int cudnn_id = -1) {
        if (cudnn_id == -1 or this->handle_num_ == 0) {
          // return ObjectMember(default_cudnn_handle_);
          return this->default_cudnn_handle_;
        }

        // return ObjectMember(cudnn_handles_)[cudnn_id % ObjectMember(handle_num_)];
        return this->cudnn_handles_[cudnn_id % handle_num_];
      }

      /**
       * @brief   Get the number of cuDNN handler.
       */
      inline unsigned int cudnn_handle_num () {
        // return ObjectMember(handle_num_);
        return this->handle_num_;
      }
#endif

    protected:
      int device_id_;   /*< The GPU device ID. */

      cudaDeviceProp device_prop_;    /*< The property of the GPU device. */

      // TODO: Convolution layer-specific parameters.
      int group_;   /*< The convolution operations group_. */
      // Manager of streams on all GPU devices.
      cudaStream_t* streams_;   /*< cudaStream_t array, used to manage CUDA stream. */

      unsigned int handle_num_;   /*< The number of CUDA streams. */

      // Manager of cuBLAS handler on all GPU devices in all available streams.
      cublasHandle_t* cublas_handles_;    /*< cublasHandle_t array. */
      cublasHandle_t default_cublas_handle_;    /*< The default cuBLAS handler binding to the default stream. */

#ifdef USE_CUDNN
      // Manager of cuDNN handler on all GPU devices in all available streams.
      cudnnHandle_t* cudnn_handles_;    /*< cudnnHandle_t array. */
      cudnnHandle_t default_cudnn_handle_;    /*< The default cuDNN handler binding to the default stream. */
#endif  /** USE_CUDNN **/

    private:
      size_t GetCUDASettings(const int device_id);
      GpuStreamPool();

    DISABLE_COPY_AND_ASSIGN(GpuStreamPool);
  };
}

#endif    /** CAFFE_GPU_MANAGER_HPP_ **/
#endif    /** CPU_ONLY **/

ext: hpp


text:
// =-=-=-=-=-=-=-
// irods includes
#include "apiHandler.hpp"
#include "irods_load_plugin.hpp"
#include "irods_plugin_name_generator.hpp"
#include "irods_pack_table.hpp"
#include "irods_client_api_table.hpp"

#include <boost/filesystem.hpp>

#include <algorithm>

namespace irods
{
    // =-=-=-=-=-=-=-
    // public - ctor
    api_entry::api_entry( apidef_t& _def )
        : plugin_base( "api_instance", "api_context" )
        , apiNumber( _def.apiNumber )
        , apiVersion( _def.apiVersion )
        , clientUserAuth( _def.clientUserAuth )
        , proxyUserAuth( _def.proxyUserAuth )
        , inPackInstruct( _def.inPackInstruct )
        , inBsFlag( _def.inBsFlag )
        , outPackInstruct( _def.outPackInstruct )
        , outBsFlag( _def.outBsFlag )
        , call_wrapper(_def.call_wrapper)
        , operation_name(_def.operation_name)
        , clearInStruct( _def.clearInStruct )
    {
        operations_[ _def.operation_name ] = _def.svrHandler;
    } // ctor

    // =-=-=-=-=-=-=-
    // public - copy ctor
    api_entry::api_entry( const api_entry& _rhs )
        : plugin_base( _rhs )
        , apiNumber( _rhs.apiNumber )
        , apiVersion( _rhs.apiVersion )
        , clientUserAuth( _rhs.clientUserAuth )
        , proxyUserAuth( _rhs.proxyUserAuth )
        , inPackInstruct( _rhs.inPackInstruct )
        , inBsFlag( _rhs.inBsFlag )
        , outPackInstruct( _rhs.outPackInstruct )
        , outBsFlag( _rhs.outBsFlag )
        , call_wrapper(_rhs.call_wrapper)
        , in_pack_key(_rhs.in_pack_key)
        , out_pack_key(_rhs.out_pack_key)
        , in_pack_value(_rhs.in_pack_value)
        , out_pack_value(_rhs.out_pack_value)
        , operation_name(_rhs.operation_name)
        , extra_pack_struct(_rhs.extra_pack_struct)
        , clearInStruct( _rhs.clearInStruct )
    {
    } // cctor

    // =-=-=-=-=-=-=-
    // public - assignment operator
    api_entry& api_entry::operator=( const api_entry& _rhs )
    {
        if ( this == &_rhs ) {
            return *this;
        }

        plugin_base::operator=(_rhs);
        apiNumber       = _rhs.apiNumber;
        apiVersion      = _rhs.apiVersion;
        clientUserAuth  = _rhs.clientUserAuth;
        proxyUserAuth   = _rhs.proxyUserAuth;
        inPackInstruct  = _rhs.inPackInstruct;
        inBsFlag        = _rhs.inBsFlag;
        outPackInstruct = _rhs.outPackInstruct;
        outBsFlag       = _rhs.outBsFlag;

        return *this;
    } // assignment op

    // =-=-=-=-=-=-=-
    // public - ctor for api entry table
    api_entry_table::api_entry_table(apidef_t defs[], size_t num)
        : loaded_plugins_{}
    {
        for (size_t i = 0; i < num; ++i) {
            table_[defs[i].apiNumber] = api_entry_ptr(new api_entry(defs[i]));
        }
    } // ctor

    auto api_entry_table::is_plugin_loaded(std::string_view plugin_name) -> bool
    {
        const auto end = std::cend(loaded_plugins_);
        return std::find(std::cbegin(loaded_plugins_), end, plugin_name) != end;
    }

    auto api_entry_table::mark_plugin_as_loaded(std::string_view plugin_name) -> void
    {
        if (!is_plugin_loaded(plugin_name)) {
            loaded_plugins_.push_back(plugin_name.data());
        }
    }

    // =-=-=-=-=-=-=-
    // public - load api plugins
    error init_api_table(api_entry_table&  _api_tbl,
                         pack_entry_table& _pack_tbl,
                         bool              _cli_flg)
    {
        // =-=-=-=-=-=-=-
        // resolve plugin directory
        std::string plugin_home;
        error ret = resolve_plugin_path( irods::PLUGIN_TYPE_API, plugin_home );
        if ( !ret.ok() ) {
            return PASS( ret );
        }

        namespace fs = boost::filesystem;

        // =-=-=-=-=-=-=-
        // iterate over the API_HOME directory entries
        if (const fs::path so_dir(plugin_home); fs::exists(so_dir)) {
            for (fs::directory_iterator it(so_dir); it != fs::directory_iterator(); ++it) {
                const auto path = it->path();

                // Skip the API plugin if it was loaded before.
                if (_api_tbl.is_plugin_loaded(path.c_str())) {
                    const auto* msg = "init_api_table :: API plugin [%s] has already been loaded. Skipping ...";
                    rodsLog(LOG_DEBUG, msg, path.stem().c_str());
                    continue;
                }

                // =-=-=-=-=-=-=-
                // given a shared object, load the plugin from it
                std::string name = path.stem().string();

                // =-=-=-=-=-=-=-
                // if client side, skip server plugins, etc.
                if (std::string::npos != name.find(_cli_flg ? "_server" : "_client")) {
                    continue;
                }

                // =-=-=-=-=-=-=-
                // clip off the lib to remain compliant with
                // load_plugin's expected behavior
                size_t pos = name.find( "lib" );
                if ( std::string::npos == pos ) {
                    continue;
                }
                name = name.substr( 3 );

                api_entry* entry = nullptr;
                error ret = load_plugin<api_entry>(
                                entry,
                                name,
                                PLUGIN_TYPE_API,
                                "api_instance",
                                "api_context");
                if (ret.ok() && entry) {
                    rodsLog(
                        LOG_DEBUG,
                        "init_api_table :: adding %d - [%s] - [%s]",
                        entry->apiNumber,
                        entry->operation_name.c_str(),
                        name.c_str() );

                    // =-=-=-=-=-=-=-
                    // ask the plugin to fill in the api and pack
                    // tables with its appropriate values
                    _api_tbl[ entry->apiNumber ] = api_entry_ptr( entry );

                    // =-=-=-=-=-=-=-
                    // add the in struct
                    if ( !entry->in_pack_key.empty() ) {
                        _pack_tbl[ entry->in_pack_key ].packInstruct = entry->in_pack_value;
                        entry->inPackInstruct = entry->in_pack_key.c_str();
                    }

                    // =-=-=-=-=-=-=-
                    // add the out struct
                    if ( !entry->out_pack_key.empty() ) {
                        _pack_tbl[ entry->out_pack_key ].packInstruct = entry->out_pack_value;
                        entry->outPackInstruct = entry->out_pack_key.c_str();
                    }

                    // =-=-=-=-=-=-=-
                    // some plugins may define additional packinstructions
                    // which are composites of the in or out structs
                    if (!entry->extra_pack_struct.empty()) {
                        for (auto&& [key, value] : entry->extra_pack_struct) {
                            _pack_tbl[key].packInstruct = value;
                        }
                    }

                    // Remember that the current plugin has been loaded.
                    // This keeps the server from wasting resources loading the same
                    // plugin multiple times.
                    _api_tbl.mark_plugin_as_loaded(path.c_str());
                }
                else {
                    irods::log( PASS( ret ) );
                }
            } // for itr
        } // if exists

        return SUCCESS();
    } // init_api_table
} // namespace irods

max_stars_repo_name: JustinKyleJames/irods

max_issues_repo_name: JustinKyleJames/irods

max_forks_repo_name: JustinKyleJames/irods


text:
#include <boost/filesystem.hpp>
#include <iostream>
#include <iomanip>

#include "../lib/SchemaParser.h"
#include "../lib/TypeSystem.h"
#include "../lib/CodeGen.h"
#include "../lib/Tables.h"
#include "../lib/Filesystem.h"
#include "../lib/NotImplementedException.h"

namespace fs = boost::filesystem;

namespace tigl {
    const auto runtimeFiles = {
        "TixiHelper.h",
        "UniquePtr.h",
    };

    void processDirectory(const std::string& inputDirectory, const std::string& runtimeDirectory, const std::string& outputDirectory, const std::string& typeSystemGraphVisFile, Filesystem& fs, const std::string& ns = "") {
        // load tables from this directory
        const Tables tables(inputDirectory);

        // iterate all *.xsd files in the input directory
        for (const auto& e : fs::directory_iterator(inputDirectory)) {
            if (fs::is_regular_file(e) && e.path().has_extension() && e.path().extension() == ".xsd") {
                // read types and elements
                std::cout << "Parsing " << e.path() << std::endl;
                auto types = xsd::parseSchema(e.path().string());

                // generate type system from schema
                std::cout << "Creating type system" << std::endl;
                const auto& typeSystem = buildTypeSystem(types, tables);

                // write graph vis file for the generated type system
                if (!typeSystemGraphVisFile.empty()) {
                    auto p = fs::path{ typeSystemGraphVisFile };
                    if (p.has_parent_path())
                        fs::create_directories(p.parent_path());
                    writeGraphVisFile(typeSystem, typeSystemGraphVisFile);
                }

                // create output directory
                const auto nsOutputDirectory = ns.empty() ? outputDirectory : outputDirectory + "/" + ns;
                fs::create_directories(nsOutputDirectory);

                // generate code
                std::cout << "Generating classes" << std::endl;
                genCode(nsOutputDirectory, typeSystem, ns, tables, fs);
            }
        }

        // recurse on sub directories
        for (const auto& e : fs::directory_iterator(inputDirectory)) {
            if (fs::is_directory(e)) {
                if (!ns.empty())
                    throw NotImplementedException("Nested input directories are not implemented. Only 1 level of subdirectories (namespaces) is allowed.");

                const auto leafDir = e.path().leaf().string();
                processDirectory(e.path().string(), runtimeDirectory, outputDirectory, typeSystemGraphVisFile, fs, leafDir);
            }
        }
    }

    void run(const std::string& inputDirectory, const std::string& runtimeDirectory, const std::string& outputDirectory, const std::string& typeSystemGraphVisFile) {
        // create runtime output directory
        fs::create_directories(outputDirectory);

        std::cout << "Copying runtime" << std::endl;
        Filesystem fs;
        for (const auto& file : runtimeFiles)
            fs.newFile(outputDirectory + "/" + file).stream() << readFile(runtimeDirectory + "/" + file);

        // process schema files
        processDirectory(inputDirectory, runtimeDirectory, outputDirectory, typeSystemGraphVisFile, fs);

        fs.flushToDisk();

        std::cout << "\tWrote   " << std::setw(5) << fs.newlywritten << " new files" << std::endl;
        std::cout << "\tUpdated " << std::setw(5) << fs.overwritten << " existing files" << std::endl;
        std::cout << "\tSkipped " << std::setw(5) << fs.skipped << " files, no changes" << std::endl;
        std::cout << "\tDeleted " << std::setw(5) << fs.deleted << " files, pruned" << std::endl;
    }
}

int main(int argc, char* argv[]) {
    // parse command line arguments
    if (argc != 4 && argc != 5) {
        std::cerr
            << "Usage: CPACSGen configDir cpascGenSourceDir outputDir [typeSystemGraphVisFile] \n\n"
            << "Options:\n\n"
            << "  configDir              The directory containing the CPACS schema and\n"
            << "                         the table files.\n"
            << "  runtimeDir             The directory of where the runtime source files are.\n"
            << "  outputDir              The directory to which the CPACSGen output\n"
            << "                         file are written\n"
            << "  typeSystemGraphVisFile GraphVis file visualizing the built type system."
            << std::endl;
        return -1;
    }

    const std::string inputDirectory         = argv[1];
    const std::string runtimeDirectory       = argv[2];
    const std::string outputDirectory        = argv[3];
    const std::string typeSystemGraphVisFile = argc > 4 ? argv[4] : "";

    try {
        tigl::run(inputDirectory, runtimeDirectory, outputDirectory, typeSystemGraphVisFile);
        return 0;
    } catch (const std::exception& e) {
        std::cerr << "Exception: " << e.what() << std::endl;
        return -1;
    } catch (...) {
        std::cerr << "Unknown exception" << std::endl;
        return -1;
    }
}


text:
/*
			 Boost Software License - Version 1.0 - August 17th, 2003

			Permission is hereby granted, free of charge, to any person or organization
			obtaining a copy of the software and accompanying documentation covered by
			this license (the "Software") to use, reproduce, display, distribute,
			execute, and transmit the Software, and to prepare derivative works of the
			Software, and to permit third-parties to whom the Software is furnished to
			do so, all subject to the following:

			The copyright notices in the Software and this entire statement, including
			the above license grant, this restriction and the following disclaimer,
			must be included in all copies of the Software, in whole or in part, and
			all derivative works of the Software, unless such copies or derivative
			works are solely in the form of machine-executable object code generated by
			a source language processor.

			THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
			IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
			FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
			SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
			FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
			ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
			DEALINGS IN THE SOFTWARE.

					License Agreement
					For Open Source Computer Vision Library
					(3-clause BSD License)
 Redistribution and use in source and binary forms, with or without modification,
 are permitted provided that the following conditions are met:

   * Redistribution's of source code must retain the above copyright notice,
     this list of conditions and the following disclaimer.

   * Redistribution's in binary form must reproduce the above copyright notice,
     this list of conditions and the following disclaimer in the documentation
     and/or other materials provided with the distribution.

   * The name of the copyright holders may not be used to endorse or promote products
     derived from this software without specific prior written permission.

 This software is provided by the copyright holders and contributors "as is" and
 any express or implied warranties, including, but not limited to, the implied
 warranties of merchantability and fitness for a particular purpose are disclaimed.
 In no event shall the Intel Corporation or contributors be liable for any direct,
 indirect, incidental, special, exemplary, or consequential damages
 (including, but not limited to, procurement of substitute goods or services;
 loss of use, data, or profits; or business interruption) however caused
 and on any theory of liability, whether in contract, strict liability,
 or tort (including negligence or otherwise) arising in any way out of
 the use of this software, even if advised of the possibility of such damage.

 *  Created on: Nov 22, 2017
 *      Author: Trent Weiss
 */

#include "voronoi_art/image_processing/image_processing.h"
#include <iostream>
#include <string>
#include <boost/bind.hpp>
#include <ctime>
namespace voronoi_art {

image_processing::image_processing() : gen(std::time(0)), dist(0.0, 1.0){
	// TODO Auto-generated constructor stub
}

image_processing::~image_processing() {
	// TODO Auto-generated destructor stub
}
string image_processing::type2str(int type) {
	string r;

	uchar depth = type & CV_MAT_DEPTH_MASK;
	uchar chans = 1 + (type >> CV_CN_SHIFT);

	switch (depth) {
	case CV_8U:
		r = "8U";
		break;
	case CV_8S:
		r = "8S";
		break;
	case CV_16U:
		r = "16U";
		break;
	case CV_16S:
		r = "16S";
		break;
	case CV_32S:
		r = "32S";
		break;
	case CV_32F:
		r = "32F";
		break;
	case CV_64F:
		r = "64F";
		break;
	default:
		r = "User";
		break;
	}

	r += "C";
	r += (chans + '0');

	return r;
}
Mat image_processing::image_laplacian(const Mat& image) {
	int kernel_size = 3;
	Mat output, src_gray;
	int ddepth = IMAGE_TYPE;
	cvtColor(image, src_gray, CV_RGB2GRAY, 1);
	Laplacian(src_gray, output, ddepth, kernel_size);
	return output;
}
Mat image_processing::image_gradient(const Mat& image) {
	Mat src_gray;
	Mat grad;
	int scale = 1;
	int delta = 0;
	int ddepth = IMAGE_TYPE;
	cvtColor(image, src_gray, CV_RGB2GRAY, 1);
	/// Generate grad_x and grad_y
	Mat grad_x, grad_y;
	Mat abs_grad_x, abs_grad_y;

	/// Gradient X
	//Scharr( src_gray, grad_x, ddepth, 1, 0, scale, delta, BORDER_DEFAULT );
	Sobel(src_gray, grad_x, ddepth, 1, 0, 3, scale, delta, BORDER_DEFAULT);
	convertScaleAbs(grad_x, abs_grad_x);

	/// Gradient Y
	//Scharr( src_gray, grad_y, ddepth, 0, 1, scale, delta, BORDER_DEFAULT );
	Sobel(src_gray, grad_y, ddepth, 0, 1, 3, scale, delta, BORDER_DEFAULT);
	convertScaleAbs(grad_y, abs_grad_y);

	/// Total Gradient (approximate)
	addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad);

	return grad;
}

Mat image_processing::sharpen(const Mat& image) {
	Mat rtn;
	GaussianBlur(image, rtn, cv::Size(3, 3), 0.0);
	addWeighted(image, 1.5, rtn, -0.5, 0, rtn);

	return rtn;
}

vector<Point> image_processing::filter_intersection(const Mat& input,
		const vector<PixelFunctor>& filters) {
	vector<Point> rtn;
	for (unsigned int r = 0; r < input.rows; ++r) {
		for (unsigned int c = 0; c < input.cols; ++c) {
			bool include_pixel = true;
			for (const PixelFunctor filter : filters) {
				if(!filter(input, r, c)){
					include_pixel=false;
					break;
				}
			}
			if(include_pixel){
				rtn.push_back(cv_float_point(c, r));
			}
		}
	}
	return rtn;
}
vector<Point> image_processing::filter_union(const Mat& input,
		const vector<PixelFunctor>& filters) {
	vector<Point> rtn;
	for (unsigned int r = 0; r < input.rows; ++r) {
		for (unsigned int c = 0; c < input.cols; ++c) {
			for (const PixelFunctor filter : filters) {
				if (filter(input, r, c)) {
					rtn.push_back(cv_float_point(c, r));
					break;
				}
			}
		}
	}
	return rtn;
}
bool simple_threshold(const Mat& input, const unsigned int& r,
		const unsigned int& c, const unsigned int& threshold) {
	Scalar intensity = input.at<uchar>(r, c);
	return intensity[0] >= (double)threshold;

}
bool image_processing::random_threshold(const Mat& mat, const unsigned int& r, const unsigned int& c, const float& threshold){
	return dist(gen)>=threshold;
}

vector<Pixel> image_processing::image_to_point_vector(const Mat& image) {
	vector<Pixel> rtn(image.rows * image.cols);
	unsigned int index = 0;
	for(unsigned int r = 0; r < image.rows; ++r){
		for(unsigned int c = 0; c < image.cols; ++c){
			rtn[index].float_point_=cv_float_point((float)c, (float)r);
			rtn[index].int_point_=cv_int_point(c, r);
			rtn[index++].color_=image.at<Vec3b>(r,c);
		}
	}
	return rtn;
}

PixelFunctor image_processing::random_dropout(const float& dropout_prob){
	return boost::bind(&image_processing::random_threshold, this, _1,_2,_3,dropout_prob);
}
PixelFunctor image_processing::gradient_threshold(const Mat& input,
		const unsigned int& threshold) {
	const Mat grad = image_gradient(input);
	return boost::bind(simple_threshold,grad,_2,_3,threshold);
}

PixelFunctor image_processing::laplacian_threshold(const Mat& input,
		const unsigned int& threshold) {
	const Mat laplacian = image_laplacian(input);
	return boost::bind(simple_threshold,laplacian,_2,_3,threshold);
}
bool gt_threshold(const Mat& input, const unsigned int& r,
		const unsigned int& c, const unsigned int& threshold) {
	Scalar intensity = input.at<Vec3b>(r, c);
	return intensity.ddot(Scalar(1.0,1.0,1.0,0.0))/3.0 >= (double)threshold;

}
bool lt_threshold(const Mat& input, const unsigned int& r,
		const unsigned int& c, const unsigned int& threshold) {
	Scalar intensity = input.at<Vec3b>(r, c);
	return intensity.ddot(Scalar(1.0,1.0,1.0,0.0))/3.0 < (double)threshold;

}
PixelFunctor image_processing::pixel_lt(const Mat& input,
		const unsigned int& threshold) {
	return boost::bind(lt_threshold,input,_2,_3,threshold);
}
PixelFunctor image_processing::pixel_gt(const Mat& input,
		const unsigned int& threshold) {
	return boost::bind(gt_threshold,input,_2,_3,threshold);
}
} /* namespace voronoi_art */

max_stars_repo_name: Electric-Turtle/voronoi-art

max_issues_repo_name: Electric-Turtle/voronoi-art

max_forks_repo_name: Electric-Turtle/voronoi-art


text:
// Copyright (c) 2016
// Author: Chrono Law
#include <std.hpp>
using namespace std;

#include <boost/range.hpp>
#include <boost/iostreams/device/file.hpp>
#include <boost/iostreams/copy.hpp>

namespace io = boost::iostreams;
using namespace io;
using namespace boost;

///////////////////////////////////////

void case1()
{
    string str("file device");
    file_sink fsink("test.txt");

    io::copy(
        make_iterator_range(str),
        fsink);

    file_source fsrc("test.txt");
    io::copy(fsrc, cout);

}

///////////////////////////////////////

int main()
{
    std::cout << "hello iostreams file" << std::endl;

    case1();
}


text:
/*
 * timestamp_correction.hpp
 * Copyright (C) 2011, 2012 Danny Havenith
 *
 * Distributed under the Boost Software License, Version 1.0. (See
 * accompanying file LICENSE_1_0.txt or copy at
 * http://www.boost.org/LICENSE_1_0.txt)
 *
 *  Created on: Feb 17, 2012
 *      Author: danny
 */

#ifndef TIMESTAMP_CORRECTION_HPP_
#define TIMESTAMP_CORRECTION_HPP_

#include "messages.hpp"
#include "bytes_to_numbers.hpp"
#include "logscanner.hpp"

// boost meta state machine includes
#include <boost/msm/back/state_machine.hpp>
#include <boost/msm/front/state_machine_def.hpp>
#include <boost/mpl/vector.hpp>
#include <boost/ref.hpp>
#include <boost/cstdint.hpp>

#include <vector>
#include <iterator>
#include <limits>
#include <iostream>
#include <iterator>
#include <algorithm>

namespace timestamp_correction
{

/**
 * Class that adapts time stamp messages by changing their 'slope' and pivot point.
 *
 * This class writes all incoming packages to a standard output stream. Time stamps are
 * interpreted and their time stamp value may be adjusted by setting a pivot, a 'corrected pivot'
 * and a skew value. Strictly speaking only one pivot is necessary, but specifying two pivots
 * makes the definition more intuitive.
 *
 * The idea is that time stamp values describe a straight line that passes through a pivot point.
 * This class will multiply the values with a skew rate to change the slope and 'shift' the
 * pivot point up or down to a new 'corrected' pivot.
 *
 *

 *                   ++  corrected slope
 *                 ++
 *               ++
 *             ++
 *   cpivot->*+            xxxx  original slope
 *         ++^         xxxx
 *       ++  |     xxxx
 *     ++    | xxxx
 *         xx*x
 *           ^
 *           pivot
 *

 * This class can only perform one linear correction on a range of time stamp values. It can only be used
 * with the same settings in regions where relation between the old and corrected time stamps is linear.
 * The time_stamp_correcter class cuts the data streams into segments where this relation is indeed linear and
 * programs an object of this slope correction class accordingly.
 */
template< typename output_handler>
class slope_correction  : public rtlogs::messages_definition
{
public:
    slope_correction( output_handler &output)
    :output( output), pivot(0), corrected_pivot(0), skew(1.0),
     lower_treshold(0), upper_treshold( std::numeric_limits<timestamp_type>::max()){}

    typedef unsigned long timestamp_type;

    /// set the time stamp correction parameters.
    void set_skew( timestamp_type new_pivot, timestamp_type new_corrected_pivot, double new_skew)
    {
        pivot           = new_pivot;
        corrected_pivot = new_corrected_pivot;
        skew            = new_skew;
    }

    /// configure a time stamp filter that allows only time stamps that are within the given range.
    /// The allowed range includes both the lower and the upper limit.
    /// When no upper range is provided, only the lower range will be used to filter
    void set_allowed_range( timestamp_type lower, timestamp_type upper = std::numeric_limits<timestamp_type>::max())
    {
        lower_treshold = lower;
        upper_treshold = upper;
    }

    ///
    /// most messages just get send to output
    template<typename message, typename iterator>
    void handle( message, iterator begin, iterator end)
    {
        output.handle( message(), begin, end);
        //std::copy( begin, end, std::ostreambuf_iterator<char>( output));
    }

    /// time stamps get adapted before being sent to output
    template< typename iterator>
    void handle( timestamp, iterator begin, iterator end)
    {
        timestamp_type value = bytes_to_numbers::get_big_endian_u3( ++begin);

        // only allow time stamp values that are within reasonable limits. ignore all other time stamps.
        // note: I'm counting on unsigned integer underflow here. The reason for specifying the condition this way
        // is that now I can now give a range like [max - 10, 10] and this will accept values from max-10 up to max and values
        // from 0 to 10, with max being the maximum (24-bit) time stamp value.
        if (value - lower_treshold <= upper_treshold)
        {

            // this line could be faster by pre-calculating offset = corrected_pivot - pivot * skew
            // and saying here: value = value * skew + offset
            // ... but it would be harder to see what's going on...
            value = (value - pivot) * skew + corrected_pivot;

            // create a timestamp message.
            char message[5] = { timestamp::header, static_cast<char>(value >> 16), static_cast<char>(value >> 8), static_cast<char>(value) };
            message[4] = message[0] + message[1] + message[2] + message[3];
            output.handle( timestamp(), message, message + 5);
            //std::copy( message, message + 5, std::ostreambuf_iterator<char>( output));
        }
    }

private:
    output_handler  &output;
    timestamp_type  pivot;
    timestamp_type  corrected_pivot;
    double          skew;
    timestamp_type  lower_treshold;
    timestamp_type  upper_treshold;
};

/**
 * Boost.msm frontend class for the wedge_finder state machine.
 *
 * This class receives GPS (G) and TS (T) events (GPS time channel or Time Stamp). It tries to find
 * a succession T G T, where the time stamp value of the two T events are very close. This means that
 * the value of the G event can be correlated with the value of the first T event.
 *
 * The state machine implemented by this class looks like this:
 *
 *

 *     +-gps-+             +-time-+
 *     v     |             v      |
 *  (searching) --time--> (ts_found) --------gps--> (gps_found) --time[timestamps_close]--> (exit)
 *      ^                   ^                          |   |
 *      |                   +-time[~timestamps_close]--+   |
 *      +-----------------------------------gps----------- +
 *

 */
struct wedge_finder_ : public boost::msm::front::state_machine_def<wedge_finder_>
{

    //**********
    // events
    //**********

    /// gps event
    struct gps_ev
    {
        explicit gps_ev( unsigned long value) : value( value) {}
        unsigned long value;
    };

    /// timestamp event
    struct time_ev
    {
        explicit time_ev( unsigned long value) : value( value){}
        unsigned long value;
    };

    //**********
    // states
    //**********
    struct searching :  boost::msm::front::state<>
    {
    };
    struct ts_found  :  boost::msm::front::state<>
    {

        template< typename Event, typename FSM>
        void on_entry( const Event&, const FSM &) {}

        template<typename FSM>
        void on_entry( const time_ev &time, FSM &fsm)
        {
            fsm.last_timestamp = time.value;
        }
    };
    // this is an exit pseudo state that emits a 'time' event while exiting.
    struct exit : boost::msm::front::exit_pseudo_state<time_ev>
    {
    };
    struct gps_found :  boost::msm::front::state<>
    {

        template< typename Event, typename FSM>
        void on_entry( const Event&, const FSM &) {}

        template<typename FSM>
        void on_entry( const gps_ev &gps_time, FSM &fsm )
        {
            fsm.last_gps_time = gps_time.value;
        }
    };

    using initial_state = searching;

    /// is the given time events value close to that of the previously offered time event?
    bool timestamps_close( const time_ev &time_event)
    {
        return time_event.value > last_timestamp && time_event.value - last_timestamp < close_time_treshold;
    }

    //**********
    // state transition table
    //**********
    using wf = wedge_finder_;
    struct transition_table : boost::mpl::vector<
    //       Start      Event           Next       Action               Guard
    //   +-----------+-----------------+-----------+---------------------+----------------------+
     _row< searching , time_ev         , ts_found                                               >,
     _row< searching , gps_ev          , searching                                              >,
     _row< ts_found  , time_ev         , ts_found                                               >,
     _row< ts_found  , gps_ev          , gps_found                                              >,
     _row< gps_found , gps_ev          , searching                                              >,
     _row< gps_found , time_ev         , ts_found                                               >,
    g_row< gps_found , time_ev         , exit      ,                       &wf::timestamps_close>
    >{};

    // time stamps are close if their values are less than this threshold apart
    const static unsigned long  close_time_treshold = 10;
    unsigned long           last_timestamp;
    unsigned long           last_gps_time;
};

/**
 * The time correction state machine.
 *
 * This class describes a state machine that consists of two states, each sub-state machines of type wedge_finder.
 * The first state is the initial state, which is active as long as no TGT-wedge has been found (@see wedge_finder_).
 *
 * After the first wedge has been found, this machine remains in the searching state, returning to the searching
 * state with each wedge found. Whenever a wedge is found, the embedded slope correction object is configured and all message are flushed to
 * its output stream.
 *
 */
template< typename output_handler>
class time_correction_ :  public boost::msm::front::state_machine_def<time_correction_<output_handler> >
{
public:
    using front_end = boost::msm::front::state_machine_def<time_correction_<output_handler> >;
    time_correction_( output_handler &output)
    : correction( output), first_gps_time(0), previous_gps_time(0), previous_timestamp(0), skew(1.0)
    {

    }

    /**
     * Flush all buffered data after reprogramming the time stamp correction based on the given
     * time stamp and gps time values.
     */
    void flush(unsigned long  timestamp, unsigned long  gps_time)
    {

        if (timestamp > previous_timestamp && gps_time > previous_gps_time)
        {
            // calculate the value that we think the time stamp should have, given the progression of gps time stamps.
            // note that this value could be greater than 2^24, the maximum time stamp, but that is OK, we can just let the time stamps roll
            // over to zero modulo  2^24
            unsigned long corrected_timestamp = ((previous_gps_time - first_gps_time) / gps_timestamp_ratio) + first_timestamp;
            skew = (static_cast<double>( gps_time - previous_gps_time)/ gps_timestamp_ratio) / (timestamp - previous_timestamp);

            // set up the slope corrector and have it filter the contents of our buffer.
            correction.set_skew(previous_timestamp, corrected_timestamp, skew);
        }

        // due to the way the range is treated, this will also work when timestamp is smaller than previous_timestamp by overflow.
        // it will then simply allow the range [previous_timestamp, max_timestamp] and [0, timestamp].
        correction.set_allowed_range( previous_timestamp, timestamp);
        flush();

        previous_timestamp = timestamp;
        previous_gps_time  = gps_time;
    }

    /**
     * Flush all bytes in the buffer, using the last known good settings for time correction
     */
    void final_flush()
    {
        correction.set_allowed_range( 0);
        flush();
    }

    /**
     * Send all bytes in the buffer to the time stamp correction class.
     */
    void flush()
    {
        rtlogs::scan_log(correction, buffer.begin(), buffer.end());
        buffer.clear();
    }

    /**
     * This function is called when the first time stamp/gps time pair is found.
     */
    void store_first_timestamp(unsigned long  timestamp, unsigned long  gps_time)
    {
        previous_timestamp = timestamp;
        first_gps_time = previous_gps_time = gps_time;
        skew = 1.0;
    }

    /// add a range of bytes to the buffer.
    template<typename iterator>
    void add_to_buffer( iterator begin, iterator end)
    {
        std::copy( begin, end, std::back_inserter( buffer));
    }

    using gps_ev = wedge_finder_::gps_ev;
    using time_ev = wedge_finder_::time_ev;
    struct initial_ : public wedge_finder_
    {

        template< typename FSM>
        void on_exit(const time_ev & t, FSM &fsm)
        {
            fsm.store_first_timestamp(t.value, last_gps_time);
        }
    };

    struct searching_ : public wedge_finder_
    {
        template< typename FSM>
        void on_exit(const time_ev & t, FSM &fsm)
        {
            fsm.flush( t.value, last_gps_time);
        }

    };
    using initial = boost::msm::back::state_machine<initial_>;
    using searching = boost::msm::back::state_machine<searching_>;
    //typedef typename front_end::template _row<  initial:: template exit_pt<typename initial::exit>      , time_ev         , searching> row1type;

    using initial_state = initial;

    // I need quite a lot of 'template' and 'typename' boilerplate here because the compiler can't make any assumptions anymore since frontend is a
    // Dependent name. Need to figure out a way to make this simpler, this beats the purpose (readability) of a state transition table.
    struct transition_table :
        boost::mpl::vector<
        //                                  Start                                                                Event             Next
        //                                  +------------------------------------------------------------------+-----------------+-----------+
        typename front_end::template _row<  typename   initial:: template exit_pt<typename initial::exit>      , time_ev         , searching> ,
        typename front_end::template _row<  typename searching:: template exit_pt<typename searching::exit>    , time_ev         , searching>
        >
    {
    };

private:
    using buffer_type = std::vector<unsigned char>;
    buffer_type         buffer;
    slope_correction<output_handler> correction;

    /// this is the value that we give the time stamp that is associated with the first gps time packet.
    /// it can't be zero, because there might be time stamps before the first gps time packet and those
    /// should get a lower value.
    static const unsigned long first_timestamp = 15000;

    unsigned long first_gps_time;
    unsigned long previous_timestamp;
    unsigned long previous_gps_time;
    double        skew;

    // ten times as many gps time ticks as timer ticks ( gps ticks are 1000/s while timer ticks are 100/s).
    const static unsigned long gps_timestamp_ratio = 10;
};

/**
 * This class is the front end to the time correction state machine.
 *
 *
 */
template< typename output_handler>
class time_correction : public boost::msm::back::state_machine<time_correction_<output_handler> >, public rtlogs::messages_definition
{
public:
    using state_machine = boost::msm::back::state_machine<time_correction_<output_handler> >;

    /// forward the reference parameter to the backend.
    /// we need create this constructor because the backend normally forwards all its constructor arguments
    /// by value and we don't want to burden clients with the task of remembering to wrap the stream in a ref(...) wrapper.
    time_correction( output_handler &output)
    :boost::msm::back::state_machine<time_correction_<output_handler> >( boost::ref( output))
    {
        state_machine::start();
    }

    ~time_correction()
    {
        state_machine::final_flush();
    }

    /// most messages just get copied into the buffer
    template< typename message, typename iterator>
    void handle( message, iterator begin, iterator end)
    {
        this->add_to_buffer( begin, end);
    }

    /// messages that cannot be parsed are ignored.
    template<typename iterator>
    void handle( rtlogs::parse_error, iterator, iterator)
    {
        // ignore all unparseable bytes.
    }

    /**
     * timestamp messages generate events.
     */
    template< typename iterator>
    void handle( timestamp, iterator begin, iterator end)
    {
       this->add_to_buffer( begin, end);
       state_machine::process_event(
       typename state_machine::time_ev( bytes_to_numbers::get_big_endian_u3(++begin))
                );
    }

    /**
     * gps time events generate events.
     */
    template< typename iterator>
    void handle( gps_time_storage, iterator begin, iterator end)
    {
        this->add_to_buffer( begin, end);
        this->process_event(
                typename state_machine::gps_ev( bytes_to_numbers::get_big_endian<boost::uint32_t>(++begin))
        );
    }

};

}

#endif /* TIMESTAMP_CORRECTION_HPP_ */

max_stars_repo_name: DannyHavenith/RunFileParser

max_issues_repo_name: DannyHavenith/RunFileParser

max_forks_repo_name: DannyHavenith/RunFileParser


text:
#ifndef SINGLETONS_HPP
#define SINGLETONS_HPP

// Description: static environment for alsim
//              the Model contains a description of the robot model
//              the HALInterface is used to communicate with the sensors and
//                                                                  actuators

// C++ library
#include <iostream>
#include <string>
#include <sstream>
#include <cstdio>
#include <cstdlib>
// boost::algorithm::to_upper(std::string), boost::erase_all(std::string, "c");
#include <boost/algorithm/string.hpp>

// Webots library
#include <webots/utils/system.h>

// naoqi sdk library
#include <alnaosim/alnaosim.h>
#include <alrobotmodel/alrobotmodel.h>
#include <alsimutils/sim_launcher.h>
#include <qi/os.hpp>
#include <qi/path.hpp>
#include <qi/qi.hpp>

namespace Sim {
    class SimLauncher;
}

class Process;

class Singletons
{
    private:
        static Sim::Model *m_model;
        static Sim::HALInterface *m_hal;
        static Sim::SimLauncher *m_launcher;

    public:
        // Initialize and shutdown simulation environment
        static bool initialize(const std::string &model, int naoqi_port,
                                void (*hal_initialized_callback)());
        static void shutdown();

        // Robot model
        static Sim::Model *model();

        // Hardware abstraction layer
        static Sim::HALInterface *hal();
};

#endif


text:
//
// Copyright © 2017 Arm Ltd. All rights reserved.
// See LICENSE file in the project root for full license information.
//

#include <boost/test/unit_test.hpp>
#include "armnnCaffeParser/ICaffeParser.hpp"
#include "ParserPrototxtFixture.hpp"

BOOST_AUTO_TEST_SUITE(CaffeParser)

struct DropoutFixture : public ParserPrototxtFixture<armnnCaffeParser::ICaffeParser>
{
    DropoutFixture()
    {
        m_Prototext = "name: \"DropoutFixture\"\n"
            "layer {\n"
            "    name: \"data\"\n"
            "    type: \"Input\"\n"
            "    top: \"data\"\n"
            "    input_param { shape: { dim: 1 dim: 1 dim: 2 dim: 2 } }\n"
            "}\n"
            "layer {\n"
            "    bottom: \"data\"\n"
            "    top: \"drop1\"\n"
            "    name: \"drop1\"\n"
            "    type: \"Dropout\"\n"
            "}\n"
            "layer {\n"
            "    bottom: \"drop1\"\n"
            "    bottom: \"drop1\"\n"
            "    top: \"add\"\n"
            "    name: \"add\"\n"
            "    type: \"Eltwise\"\n"
            "}\n";
        SetupSingleInputSingleOutput("data", "add");
    }
};

BOOST_FIXTURE_TEST_CASE(ParseDropout, DropoutFixture)
{
    RunTest<4>(
        {
            1, 2,
            3, 4,
        },
        {
            2, 4,
            6, 8
        });
}

BOOST_AUTO_TEST_SUITE_END()