text:
import pandas as pd
import numpy as np
import tensorflow as tf
import matplotlib.pylab as plt
from scipy.interpolate import interp1d
from scipy import signal
from scipy import optimize

'''
the LRTable is the different windows of the spectrum from the library.
'''
ComplexLRTable =[
        [[22, 72, 138, 219, 298, 432, 472],
        [62, 136, 219, 298, 401, 460, 489],],
        [[32, 147, 226, 315, 454],
        [147, 226, 315, 450, 505],],
        [[54, 90, 143, 218, 312, 463],
        [86, 131, 218, 312, 402, 494],],
        # 432--442 seems not important
        # [[54, 90, 143, 218, 312, 432, 463],
        # [86, 131, 218, 312, 402, 442, 494],],
        [[16, 147, 224, 307, 426, 473],
        [138, 224, 307, 421, 449, 485],]
    ]

LRTable =[
        [[ 138, 219, 298, ],
        [ 219, 298, 401, ],],
        [[ 147, 226, 315],
        [ 226, 315, 450],],
        [[143, 218, 312, ],
        [ 218, 312, 402, ],],
        [[ 147, 224, 307, ],
        [ 224, 307, 421, ],]
    ]

# Finding the envelop of the spectrum
def qhull(sample):
    '''
    Finding the envelop of the spectrum
    qhull(
        sample    numpy.ndarry of [wavelength, spectrum]
        )
    '''
    link = lambda a,b: np.concatenate((a,b[1:]))
    edge = lambda a,b: np.concatenate(([a],[b]))

    def dome(sample,base):
        h, t = base
        dists = np.dot(sample-h, np.dot(((0,-1),(1,0)),(t-h)))
        outer = np.repeat(sample, dists>0, axis=0)

        if len(outer):
            pivot = sample[np.argmax(dists)]
            return link(dome(outer, edge(h, pivot)),
                        dome(outer, edge(pivot, t)))
        else:
            return base

    if len(sample) > 2:
        axis = sample[:,0]
        base = np.take(sample, [np.argmin(axis), np.argmax(axis)], axis=0)
        return link(dome(sample, base),
                    dome(sample, base[::-1]))
    else:
        return sample

def changehull(hull, name_list):
    '''
    change the hull to fit all the data within [0-1] by considering the endpoints.
    :param hull:
    :param name_list:
    :return:
    '''
    c = 0
    for index in range(len(hull)):
        if hull[index][0] == name_list[-1]:
            c = index
            break
    hull = hull[:c+1]
    hull = np.vstack((hull, hull[0]))

    return hull

def inputData4Lib(testnumber):
    '''
    Input the standard library of the 4 REE spectrum: VNIR: 396nm -- 1003nm
    :return:
    '''
    # Read the file and store in pandas
    df = pd.read_excel('nameslib4data.xlsx', sheetname='Sheet1')
    col_name = df.columns
    name_list = list(col_name)
    # xbegin = name_list.index(396.329987)
    xbegin = name_list.index(600.820007)
    wavelengths = np.array(name_list[xbegin:])

    col_value = df.values
    value_list = list(col_value)
    spectrum = col_value[testnumber][xbegin:]
    return wavelengths, spectrum, name_list

def inputsampledata(testnumber):
    '''
    Input the samll sampledata of the Rock from: 600.820007
    :param testnumber: the input line of the rock spectrum
    :return:
    '''
    headers = list(['ID', 'X', 'Y'])
    with open('header.txt', 'r') as f:
        data = f.readlines()
        for line in data:
            spec = line.split(sep=',')
            headers.extend(map(float, spec))
            # headers.extend(spec)

    name_list = list(headers)
    xbegin = name_list.index(600.820007)

    df = pd.read_table('VNIR_sample1_25points.txt', header=None, skiprows=[0, 1, 2, 3, 4, 5, 6, 7], sep='\s+')
    # df = pd.read_table('VNIR_sample1_25points.txt', header=None, skiprows=[0, 1, 2, 3, 4, 5, 6, 7], sep='\s+')
    # print (df.head())
    wavelengths = np.array(name_list[xbegin:])

    col_value = df.values
    value_list = list(col_value)
    global col_length
    col_length = len(value_list)
    spectrum = col_value[testnumber][xbegin:]
    return wavelengths, spectrum, name_list

def hull_to_spectrum(hull, wavelengths):
    f = interp1d(hull['wavelength'], hull['intensity'])
    return f(wavelengths)

def callibpeaknum(testnumber):
    '''
    calculate the peak of the library infromation
    :param testnumber: from the library
    :return: peakcounts,  the peak number of the windows
             peaklist,  the peak index of the windows
             peakheights,  the peak heights of the windows
             extermeminpeaks, the exterme minimum of the windows
    '''
    wavelengths, spectrum, name_list = inputData4Lib(testnumber)
    sample = np.array([wavelengths, spectrum]).T
    hull = qhull(sample)
    hull = changehull(hull, name_list)
    hull = pd.DataFrame(hull, columns=['wavelength', 'intensity'])
    hull_spectrum = hull_to_spectrum(hull[:-1], wavelengths)
    spectrum2 = spectrum / hull_spectrum

    newspectrum = signal.savgol_filter(spectrum2, 17, 4, deriv=0)
    # get the peak values of the minimum
    peakind_tuple = signal.argrelmin(newspectrum)
    peakind = peakind_tuple[0][:]
    # print ('peakind:', peakind)

    newspectrum2 = signal.savgol_filter(spectrum2, 17, 4, deriv=1)
    # get the 1 derivative value of spectrum
    peakargmin = signal.argrelmin(newspectrum2)
    peakargmax = signal.argrelmax(newspectrum2)

    lvalue = LRTable[testnumber][0]
    rvalue = LRTable[testnumber][1]
    # print ('lvalue:', lvalue)
    # print ('rvalue:', rvalue)

    peakcounts = list()
    peaklist = list()
    peakheights = list()
    plotpeak = list()
    extermeminpeaks = list()
    for index in np.arange(len(lvalue)):
        peaks = [val for val in peakind[peakind > lvalue[index]] if val in peakind[peakind < rvalue[index]]]
        # print ('peaks:', peaks)

        extermeminpeak_temp = np.argmin(newspectrum[peaks])
        extermeminpeak = peaks[extermeminpeak_temp]
        extermeminpeaks.append(extermeminpeak)

        plotpeak.extend(peaks)
        peakcounts.append(len(peaks))
        peaklist.append(np.array(peaks))
        predict_val = (newspectrum[rvalue[index]] + newspectrum[lvalue[index]]) / 2
        peakheights.append(np.array(np.repeat(predict_val, len(peaks)) - newspectrum[peaks]))

    def printval():
        print ('Peakcounts:', peakcounts)
        print ('Peaklist:', peaklist)
        print ('peakheights:', peakheights)
        print ('extermeminpeaks:', extermeminpeaks)

    def showlipic():
        plt.figure()
        plt.title('The Library Spectrum:')
        plt.plot(wavelengths, spectrum2, c='k', label='Removed Spectrum')
        plt.scatter(wavelengths[plotpeak], spectrum2[plotpeak], c='r', label='Found peaks')
        plt.scatter(wavelengths[lvalue], spectrum2[lvalue], c='g', label='Left endpoints')
        plt.scatter(wavelengths[rvalue], spectrum2[rvalue], c='b', label='right endpoints')
        plt.legend()

        plt.figure()
        plt.plot(wavelengths, spectrum2, c='k', label='Removed Spectrum')
        plt.scatter(wavelengths[plotpeak], spectrum2[plotpeak], c='r', label='Found peaks')
        plt.scatter(wavelengths[peakargmax], spectrum2[peakargmax], c='g', label='peakargmax endpoints')
        plt.scatter(wavelengths[peakargmin], spectrum2[peakargmin], c='b', label='peakargmin endpoints')
        plt.legend()

        plt.show()

    # printval()
    # showlibpic()
    return peakcounts, peaklist, peakheights, extermeminpeaks

def calrockpeaknum(libnumber, rocknumber):
    '''
    calculate the peak of the library infromation
    :param testnumber: from the library
    :return: peakcounts,  the peak number of the windows
             peaklist,  the peak index of the windows
             peakheights,  the peak heights of the windows
             extermeminpeaks, the exterme minimum of the windows
    '''
    wavelengths, spectrum, name_list = inputsampledata(rocknumber)
    sample = np.array([wavelengths, spectrum]).T
    hull = qhull(sample)
    hull = changehull(hull, name_list)
    hull = pd.DataFrame(hull, columns=['wavelength', 'intensity'])
    hull_spectrum = hull_to_spectrum(hull[:-1], wavelengths)
    spectrum2 = spectrum / hull_spectrum

    newspectrum = signal.savgol_filter(spectrum2, 17, 4, deriv=0)
    # get the peak values of the minimum
    peakind_tuple = signal.argrelmin(newspectrum)
    peakind = peakind_tuple[0][:]
    print ('peakind:', peakind)

    newspectrum2 = signal.savgol_filter(spectrum2, 17, 4, deriv=1)
    # get the 1 derivative value of spectrum
    peakargmin = signal.argrelmin(newspectrum2)
    peakargmax = signal.argrelmax(newspectrum2)

    lvalue = LRTable[libnumber][0]
    rvalue = LRTable[libnumber][1]
    print ('lvalue:', lvalue)
    print ('rvalue:', rvalue)

    peakcounts = list()
    peaklist = list()
    peakheights = list()
    plotpeak = list()
    extermeminpeaks = list()
    for index in np.arange(len(lvalue)):
        peaks = [val for val in peakind[peakind > lvalue[index]] if val in peakind[peakind < rvalue[index]]]
        print ('peaks:', peaks)

        extermeminpeak_temp = np.argmin(newspectrum[peaks])
        extermeminpeak = peaks[extermeminpeak_temp]
        extermeminpeaks.append(extermeminpeak)

        plotpeak.extend(peaks)
        peakcounts.append(len(peaks))
        peaklist.append(np.array(peaks))
        predict_val = (newspectrum[rvalue[index]] + newspectrum[lvalue[index]]) / 2
        peakheights.append(np.array(np.repeat(predict_val, len(peaks)) - newspectrum[peaks]))

    def showval():
        print ('Peakcounts:', peakcounts)
        print ('Peaklist:', peaklist)
        print ('peakheights:', peakheights)
        print ('extermeminpeaks:', extermeminpeaks)

        plt.figure()
        plt.plot(wavelengths, newspectrum, c='k', label='Removed Spectrum')
        plt.scatter(wavelengths[plotpeak], newspectrum[plotpeak], c='r', label='Found peaks')
        plt.scatter(wavelengths[lvalue], newspectrum[lvalue], c='g', label='Left endpoints')
        plt.scatter(wavelengths[rvalue], newspectrum[rvalue], c='b', label='right endpoints')
        plt.legend()

        plt.figure()
        plt.plot(wavelengths, spectrum2, c='k', label='Removed Spectrum')
        plt.scatter(wavelengths[plotpeak], spectrum2[plotpeak], c='r', label='Found peaks')
        plt.scatter(wavelengths[peakargmax], spectrum2[peakargmax], c='g', label='peakargmax endpoints')
        plt.scatter(wavelengths[peakargmin], spectrum2[peakargmin], c='b', label='peakargmin endpoints')
        plt.legend()

        plt.show()
    # showval()

    return peakcounts, peaklist, peakheights, extermeminpeaks

def isPeakExist(libnumber, rockspectrum, libspectrum):
    '''
    Judge whether peak exists according to the rock spectrum and library spectrum
    :param testnumber:  the library spectrum number
    :param rockspectrum: the rock spectrums
    :param libspectrum: the library spectrums
    :return: judge the similarity and the scaling params and RMS
    '''
    print ('Calculate the information of the library peaks:')
    libpeakcounts, libpeaklist, libpeakheights, libextermeminpeaks = callibpeaknum(libnumber)
    lbands = LRTable[libnumber][0]
    rbands = LRTable[libnumber][1]

    # smoothrockspectrum = signal.savgol_filter(rockspectrum, 33, 4, deriv=0)
    smoothrockspectrum = rockspectrum
    rockpeakind_tuple = signal.argrelmin(smoothrockspectrum)
    rockpeakind = rockpeakind_tuple[0][:]

    judgethreshold = 12
    judgeflag = False
    distance = 0
    liballpeaks = list()
    libminpeakdepth = list()
    predrockmin = list()
    predrockdepth = list()
    rockmin = list()

    for index in np.arange(len(lbands)):
        # get each window of the library and rock spectrum.
        band = smoothrockspectrum[lbands[index] : rbands[index]]
        rockbandpeakind = [num for num in rockpeakind if (num > lbands[index] and num < rbands[index])]

        # print('rockbandpeakind:', rockbandpeakind)
        if len(rockbandpeakind) == 0:
            print('peaks not exists')
            return False

        # get the library peaks index
        libpeak = libpeaklist[index]
        liballpeaks.extend(libpeak)

        # Use the shift of the peak distance to calculate the similarity
        libmin = libextermeminpeaks[index]

        # print ('libmin:', libmin)
        depth_temp = (libspectrum[lbands[index]] + libspectrum[rbands[index]]) / 2 - libspectrum[libmin]
        libminpeakdepth.append(depth_temp)

        # get the really minimum peaks in rock and calculate distance use the really minimum values.
        minval = min([smoothrockspectrum[item] for item in rockbandpeakind])
        for item in rockbandpeakind:
            if smoothrockspectrum[item] == minval:
                distance += abs(libmin - item)
                rockmin.append(item)

        # get the minimum peaks from the rocks and calculate distance use the nearby one peak values.
        # distance += min([abs(libmin - item) for item in rockbandpeakind])

        print ('distance:', distance)

        # Calculate the value which close to the peaks seems to be the extreme min values of the band
        minval = min([abs(libmin - item) for item in rockbandpeakind])
        for item in rockbandpeakind:
            if abs(libmin - item) == minval:
                predrockmin.append(item)
                depth = (rockspectrum[lbands[index]] + rockspectrum[rbands[index]])/2 - rockspectrum[item]
                predrockdepth.append(depth)
        # print ('predrockdepth:', predrockdepth)

        # Calculate depth of the original rock peaks
        rockdepth = []
        for peakindex in np.arange(len(rockbandpeakind)):
            depth = (rockspectrum[lbands[index]] + rockspectrum[rbands[index]])/2 - rockspectrum[rockbandpeakind[peakindex]]
            rockdepth.append(depth)
        # print ('rockdepth:', rockdepth)

    # print('predrockmin: ', predrockmin)
    # print ('predrockdepth: ', predrockdepth)

    # Try1: Only use the minimum value to see whether it works 使用的是最靠近这个最小值的峰来进行判断的
    def calscalling_extrememindepth():
        print ('Try1: Calculate Scalling with only the exterme minimum depth:')
        scaling = 1.0
        # print ('libminpeakdepth:', libminpeakdepth)
        # print ('predrockdepth:', predrockdepth)
        def residuals(p, libminpeakdepth, predrockdepth):
            result = 0
            for item in np.arange(len(libminpeakdepth)):
                result += abs(libminpeakdepth[item] * p - predrockdepth[item])
            return result
        plsq = optimize.leastsq(residuals, scaling, args=(libminpeakdepth, predrockdepth))
        scaling = plsq[0]
        rms = residuals(scaling, libminpeakdepth, predrockdepth)
        print ('Scaling: %f, RMS: %f' %(scaling, rms))

    # calscalling_extrememindepth()

    # Try2: Use the same index to calculate depth 使用的是相同的index来计算的值
    def calscalling_costantmindepth():
        print('Try2: Calculate Scalling with only the constant depth:')
        scaling = 1.0
        # print ('libextermeminpeaks:', libextermeminpeaks)
        # print ('libdepth:', libminpeakdepth)

        def residuals(p, libextermeminpeaks):
            result = 0
            for item in np.arange(len(libextermeminpeaks)):
                rockdepth = (smoothrockspectrum[lbands[item]] + smoothrockspectrum[rbands[item]])/2 - smoothrockspectrum[libextermeminpeaks[item]]
                libdepth = libminpeakdepth[item]
                result += abs(rockdepth - p * libdepth)
            return result
        plsq = optimize.leastsq(residuals, scaling, args = (libextermeminpeaks))
        scaling = plsq[0]
        rms = residuals(scaling, libextermeminpeaks)
        print ('Scaling: %f, RMS: %f' %(scaling, rms))

    # calscalling_costantmindepth()

    weight = [0.45, 0.45, 0.1]
    # Try3: Use the multiple peaks of the library, the peakind is the costant number since it is not far away from those ones.
    def calscaling_multiconstant_mindepth():
        print('Try3: Calculate Scalling with the multiply constant minimum depth:')
        scaling = 1.0
        # print ('libpeaklist:', libpeaklist)
        def residuals(p, libpeaklist):
            result = 0
            for item in np.arange(len(libpeaklist)):
                for t in libpeaklist[item]:
                    rockdepth = (smoothrockspectrum[lbands[item]] + smoothrockspectrum[rbands[item]]) / 2 - smoothrockspectrum[t]
                    libdepth = (libspectrum[lbands[item]] + libspectrum[rbands[item]]) / 2 - libspectrum[t]
                    result += abs(rockdepth - p * libdepth) * weight[item]
            return result
        plsq = optimize.leastsq(residuals, scaling, args=(libpeaklist))
        scaling = plsq[0]
        rms = residuals(scaling, libpeaklist)
        # print ('Scaling: %f, RMS: %f' %(scaling, rms))
        return scaling, rms

    # calscaling_multiconstant_mindepth()

    # Try4: 使用库中最接近的多个峰进行判断和拟合,使用的非定值,但也不能完全的非定值,只能先找到最低点的,然后对于原来库中的进行相对的偏移来进行判断拟合
    def calscaling_multivariable_mindepth():
        print ('Try4: Calculate Scalling with the multiply variable minimum depth:')
        scaling = 1.0
        # print ('libpeaklist:', libpeaklist)
        # print ('libextermeminpeaks:', libextermeminpeaks)
        # print ('predrockmin:', predrockmin)
        def residuals(p, libextermeminpeaks, predrockmin, libpeaklist):
            result = 0
            for index in np.arange(len(libextermeminpeaks)):
                distance = libextermeminpeaks[index] - predrockmin[index]
                for t in libpeaklist[index]:
                    rockdepth = (smoothrockspectrum[lbands[index]] + smoothrockspectrum[rbands[index]]) / 2 - smoothrockspectrum[t - distance]
                    libdepth = (libspectrum[lbands[index]] + libspectrum[rbands[index]]) / 2 - libspectrum[t]
                    result += abs(rockdepth - p * libdepth)
            return result
        plsq = optimize.leastsq(residuals, scaling, args=(libextermeminpeaks, predrockmin, libpeaklist))
        scaling = plsq[0]
        rms = residuals(scaling, libextermeminpeaks, predrockmin, libpeaklist)
        print('Scaling: %f, RMS: %f' %(scaling, rms))

    # calscaling_multivariable_mindepth()

    def calscalling_samebandsweights():
        # use the same index to calculate depth and the no consideration about the weights of all bands
        print('liballpeaks:', liballpeaks)
        scaling = [1.0]

        def residuals(p, liballpeaks):
            result = 0
            for item in liballpeaks:
                # should use the depth to calculate the results.
                result += abs(smoothrockspectrum[item] - p * libspectrum[item])
            return result

        plsq = optimize.leastsq(residuals, scaling, args=(liballpeaks))
        scaling = plsq[0]
        rms = residuals(scaling, liballpeaks)
        print('Scaling: %f, RMS: %f' % (scaling, rms))

    averagedistance = distance / len(lbands)
    print ('averagedistance:', averagedistance)

    scaling, rms = calscaling_multiconstant_mindepth()
    if averagedistance < judgethreshold:
        # print ('Peak Exists.')

        return True, averagedistance, scaling, rms
    else:
        # print ('Peak not exist.')
        return False, averagedistance, scaling, rms

# 后面设计时需要考虑到: 对于不同的band段所设置的权值可能不一样; 对于同一band段但是根据峰的高度来设定不同的权重; 如何确定那个band更加的重要
def calscallingwithgaussian():
    pass

def calscallingwithsignaldepth():
    pass

def getspectrum2(wavelengths, spectrum, name_list):
    '''
    Get the removed spectrum from the original spectrum
    :param testnumber: from the library
    :return: return the removed spectrum
    '''
    sample = np.array([wavelengths, spectrum]).T
    hull = qhull(sample)

    hull = changehull(hull, name_list)
    # print (hull)
    hull = pd.DataFrame(hull, columns=['wavelength', 'intensity'])

    hull_spectrum = hull_to_spectrum(hull[:-1], wavelengths)
    spectrum2 = spectrum / hull_spectrum
    return  spectrum2

def addnoise_spectrum(wavelengths, spectrum):
    scaling = eval(input('Please input the testing scaling number between 0-1.5 :'))
    shift = np.random.randn(len(spectrum)) / 100
    newspectrum = scaling * spectrum + shift

    # fig, axs = plt.subplots(1, 1, figsize=(10, 5))  # multiple plots in one figure
    plt.plot(wavelengths, spectrum, c='b', label = 'Original spectrum')
    plt.plot(wavelengths, newspectrum, label = 'noisy spectrum')
    plt.legend()
    # plt.show()
    return newspectrum

def singletest():
    rocknumber = int(input('Please input the rock test number:'))
    libnumber = int(input("Please input the library test number:"))

    wavelengths, rockspectrum, rockname_list = inputsampledata(rocknumber)
    wavelengths, libspectrum, libname_list = inputData4Lib(libnumber)

    libspectrum2 = getspectrum2(wavelengths, libspectrum, libname_list)
    rockspectrum2 = getspectrum2(wavelengths, rockspectrum, rockname_list)

    # Calculate the lib peak information.
    print('Calculate the rock peak information:')
    calrockpeaknum(libnumber, rocknumber)
    isPeakExist(libnumber, rockspectrum2, libspectrum2)
    plt.show()

def tottest(rocknumber):
    # rocknumber = int(input('Please input the rock test number:'))
    libnumber = 0

    wavelengths, rockspectrum, rockname_list = inputsampledata(rocknumber)
    wavelengths, libspectrum, libname_list = inputData4Lib(libnumber)

    rocksmoothspectrum = signal.savgol_filter(rockspectrum, 31, 4, deriv=0)
    # plt.figure()
    # plt.plot(wavelengths, rocksmoothspectrum, label='original spectrum')
    #
    # plt.legend()
    # plt.show()

    libspectrum2 = getspectrum2(wavelengths, libspectrum, libname_list)
    rockspectrum2 = getspectrum2(wavelengths, rocksmoothspectrum, rockname_list)

    # Calculate the lib peak information.
    # print('Calculate the rock peak information:')
    # calrockpeaknum(libnumber, rocknumber)
    judgeflag, averagedistance, scaling, rms = isPeakExist(libnumber, rockspectrum2, libspectrum2)
    return judgeflag, averagedistance, scaling, rms
    # plt.show()

if __name__ == "__main__":
    # test with only the library & all rock data
    # singletest()

    # if the scaling value is less than 0 then it also can say the peak doesn't exist.
    wavelengths, rockspectrum, rockname_list = inputsampledata(0)

    with open('test3.txt', 'wt') as f:
        for number in np.arange(25):
            flag, averagedistance, scaling, rms = tottest(number)
            if (flag == True):
                print ('Number %d has peaks.' %number)
                print ('Number %d has peaks.' %number, file=f)
                print ('Averagedistance %.3f' %averagedistance, file=f)
                print ('Scaling: %.3f; RMS: %.3f' %(scaling, rms), file=f)

            else:
                print('Number %d doesnot has peaks.' % number)
            def showanswer(number, averagedistance):
                plt.figure()
                wavelengths, rockspectrum, rockname_list = inputsampledata(number)
                rocksmoothspectrum = signal.savgol_filter(rockspectrum, 31, 4, deriv=0)
                rockspectrum2 = getspectrum2(wavelengths, rocksmoothspectrum, rockname_list)

                wavelengths, libspectrum, libname_list = inputData4Lib(0)
                libsmoothspectrum = signal.savgol_filter(libspectrum, 17, 4, deriv=0)
                libspectrum2 = getspectrum2(wavelengths, libsmoothspectrum, libname_list)

                plt.plot(wavelengths, rockspectrum, label='original spectrum')
                plt.plot(wavelengths, rocksmoothspectrum, label='smoothed spectrum')
                plt.scatter(wavelengths[LRTable[0][0]], rocksmoothspectrum[LRTable[0][0]], c='r', s=150,
                            label='left points')
                plt.scatter(wavelengths[LRTable[0][1]], rocksmoothspectrum[LRTable[0][1]], c='g', s=150,
                            label='right points')
                # plt.plot(wavelengths, rockspectrum2, c='k', label='removed spectrum')
                plt.legend()
                plt.figure()
                plt.title('The smoothing window number: %d, averagedistance: %.3f' % (number, averagedistance))
                plt.plot(wavelengths, rockspectrum2, label='removed spectrum')
                plt.plot(wavelengths, 0.9 + libspectrum2 * 0.1, label='library spectrum')
                plt.scatter(wavelengths[LRTable[0][0]], rockspectrum2[LRTable[0][0]], c='r', s=150, label='left points')
                plt.scatter(wavelengths[LRTable[0][1]], rockspectrum2[LRTable[0][1]], c='g', s=150, label='right points')
                plt.legend()
                plt.show()
            showanswer(number, averagedistance)

redpajama_set_name: RedPajamaGithub


text:
const mm = require('plover-test-mate');

describe('plover-route/lib/plugin', () => {
  it('config routes', async() => {
    const app = create({
      applicationRoot: __dirname,
      routes: r => {
        r.get('/profile', 'users#show');
        r.use('/hello', ctx => {
          ctx.body = 'hello';
        });
      }
    });

    app.use(ctx => {
      if (ctx.route) {
        ctx.body = ctx.route;
      }
    });

    await app.get('/profile').expect({
      module: 'users',
      action: 'show',
      query: {}
    });

    await app.get('/hello').expect('hello');
  });

  it('routes with namespace', async() => {
    const app = create({
      applicationRoot: __dirname,
      disableDefaultRouter: true,
      routes: r => {
        r.namespace('/users', () => {
          r.use(async(ctx, next) => {
            await next();
            if (ctx.route) {
              ctx.body = ctx.route.url;
            } else {
              ctx.body = ctx.path;
            }
          });

          r.get('config', 'users#config');
        });
      }
    });

    await app.get('/users/config').expect('users:config');
    await app.get('/users/messages').expect('/users/messages');
    await app.get('/users/').expect('/users/');
    await app.get('/users').expect(404);
  });

  it('routes not config, for coverage', () => {
    create({
      applicationRoot: __dirname
    });

    (true).should.be.ok();
  });

  describe('http method', () => {
    const app = create({
      applicationRoot: __dirname,
      web: {
        csrf: {
          ignore: ['/*']
        }
      }
    });

    app.use(ctx => {
      ctx.body = ctx.method;
    });

    it('normal post', async() => {
      await app.agent.post('/save')
        .send({})
        .expect('POST');
    });

    it('put with _method', async() => {
      await app.agent.post('/update')
        .send({ _method: 'put' })
        .expect('PUT');
    });

    it('patch with header: x-http-method-override', async() => {
      await app.agent.post('/update')
        .set('X-HTTP-Method-Override', 'patch')
        .expect('PATCH');
    });
  });
});

function create(opts) {
  const app = mm(opts);
  app.install('plover-web');
  app.install(require('../lib/plugin'));
  return app;
}


text:
using System.Collections.Generic;
using System.Text.Json;
using Azure.Core;

namespace Azure.ResourceManager.CosmosDB.Models
{
    internal partial class SqlUserDefinedFunctionListResult
    {
        internal static SqlUserDefinedFunctionListResult DeserializeSqlUserDefinedFunctionListResult(JsonElement element)
        {
            Optional<IReadOnlyList<SqlUserDefinedFunctionGetResults>> value = default;
            foreach (var property in element.EnumerateObject())
            {
                if (property.NameEquals("value"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        property.ThrowNonNullablePropertyIsNull();
                        continue;
                    }
                    List<SqlUserDefinedFunctionGetResults> array = new List<SqlUserDefinedFunctionGetResults>();
                    foreach (var item in property.Value.EnumerateArray())
                    {
                        array.Add(SqlUserDefinedFunctionGetResults.DeserializeSqlUserDefinedFunctionGetResults(item));
                    }
                    value = array;
                    continue;
                }
            }
            return new SqlUserDefinedFunctionListResult(Optional.ToList(value));
        }
    }
}


text:
using System;
using System.Text;
using System.Security.Permissions;
using System.Runtime.Serialization;
using OpenADK.Library;
using OpenADK.Library.Global;

namespace OpenADK.Library.Infra{

/// <summary>This element holds the sub-message being sent.</summary>
/// <remarks>
///
/// <para>Author: Generated by adkgen</para>
/// <para>Version: 2.6</para>
/// <para>Since: 1.1</para>
/// </remarks>
[Serializable]
public class SIF_Wakeup : SifElement
{
	/// <summary>
	/// Creates an instance of a SIF_Wakeup
	/// </summary>
	public SIF_Wakeup() : base ( InfraDTD.SIF_WAKEUP ){}

	/// <summary>
	/// Constructor used by the .Net Serialization formatter
	/// </summary>
	[SecurityPermission( SecurityAction.Demand, SerializationFormatter=true )]
	protected SIF_Wakeup( SerializationInfo info, StreamingContext context ) : base( info, context ) {}
}}


text:
import logging
import os
import time
import urllib

import glanceclient.v1.client as glance_client
import keystoneclient.v2_0 as keystone_client
import novaclient.v1_1.client as nova_client

from charmhelpers.contrib.amulet.utils import (
    AmuletUtils
)

DEBUG = logging.DEBUG
ERROR = logging.ERROR

class OpenStackAmuletUtils(AmuletUtils):
    """OpenStack amulet utilities.

       This class inherits from AmuletUtils and has additional support
       that is specifically for use by OpenStack charms.
       """

    def __init__(self, log_level=ERROR):
        """Initialize the deployment environment."""
        super(OpenStackAmuletUtils, self).__init__(log_level)

    def validate_endpoint_data(self, endpoints, admin_port, internal_port,
                               public_port, expected):
        """Validate endpoint data.

           Validate actual endpoint data vs expected endpoint data. The ports
           are used to find the matching endpoint.
           """
        found = False
        for ep in endpoints:
            self.log.debug('endpoint: {}'.format(repr(ep)))
            if (admin_port in ep.adminurl and
                    internal_port in ep.internalurl and
                    public_port in ep.publicurl):
                found = True
                actual = {'id': ep.id,
                          'region': ep.region,
                          'adminurl': ep.adminurl,
                          'internalurl': ep.internalurl,
                          'publicurl': ep.publicurl,
                          'service_id': ep.service_id}
                ret = self._validate_dict_data(expected, actual)
                if ret:
                    return 'unexpected endpoint data - {}'.format(ret)

        if not found:
            return 'endpoint not found'

    def validate_svc_catalog_endpoint_data(self, expected, actual):
        """Validate service catalog endpoint data.

           Validate a list of actual service catalog endpoints vs a list of
           expected service catalog endpoints.
           """
        self.log.debug('actual: {}'.format(repr(actual)))
        for k, v in expected.iteritems():
            if k in actual:
                ret = self._validate_dict_data(expected[k][0], actual[k][0])
                if ret:
                    return self.endpoint_error(k, ret)
            else:
                return "endpoint {} does not exist".format(k)
        return ret

    def validate_tenant_data(self, expected, actual):
        """Validate tenant data.

           Validate a list of actual tenant data vs list of expected tenant
           data.
           """
        self.log.debug('actual: {}'.format(repr(actual)))
        for e in expected:
            found = False
            for act in actual:
                a = {'enabled': act.enabled, 'description': act.description,
                     'name': act.name, 'id': act.id}
                if e['name'] == a['name']:
                    found = True
                    ret = self._validate_dict_data(e, a)
                    if ret:
                        return "unexpected tenant data - {}".format(ret)
            if not found:
                return "tenant {} does not exist".format(e['name'])
        return ret

    def validate_role_data(self, expected, actual):
        """Validate role data.

           Validate a list of actual role data vs a list of expected role
           data.
           """
        self.log.debug('actual: {}'.format(repr(actual)))
        for e in expected:
            found = False
            for act in actual:
                a = {'name': act.name, 'id': act.id}
                if e['name'] == a['name']:
                    found = True
                    ret = self._validate_dict_data(e, a)
                    if ret:
                        return "unexpected role data - {}".format(ret)
            if not found:
                return "role {} does not exist".format(e['name'])
        return ret

    def validate_user_data(self, expected, actual):
        """Validate user data.

           Validate a list of actual user data vs a list of expected user
           data.
           """
        self.log.debug('actual: {}'.format(repr(actual)))
        for e in expected:
            found = False
            for act in actual:
                a = {'enabled': act.enabled, 'name': act.name,
                     'email': act.email, 'tenantId': act.tenantId,
                     'id': act.id}
                if e['name'] == a['name']:
                    found = True
                    ret = self._validate_dict_data(e, a)
                    if ret:
                        return "unexpected user data - {}".format(ret)
            if not found:
                return "user {} does not exist".format(e['name'])
        return ret

    def validate_flavor_data(self, expected, actual):
        """Validate flavor data.

           Validate a list of actual flavors vs a list of expected flavors.
           """
        self.log.debug('actual: {}'.format(repr(actual)))
        act = [a.name for a in actual]
        return self._validate_list_data(expected, act)

    def tenant_exists(self, keystone, tenant):
        """Return True if tenant exists."""
        return tenant in [t.name for t in keystone.tenants.list()]

    def authenticate_keystone_admin(self, keystone_sentry, user, password,
                                    tenant):
        """Authenticates admin user with the keystone admin endpoint."""
        unit = keystone_sentry
        service_ip = unit.relation('shared-db',
                                   'mysql:shared-db')['private-address']
        ep = "http://{}:35357/v2.0".format(service_ip.strip().decode('utf-8'))
        return keystone_client.Client(username=user, password=password,
                                      tenant_name=tenant, auth_url=ep)

    def authenticate_keystone_user(self, keystone, user, password, tenant):
        """Authenticates a regular user with the keystone public endpoint."""
        ep = keystone.service_catalog.url_for(service_type='identity',
                                              endpoint_type='publicURL')
        return keystone_client.Client(username=user, password=password,
                                      tenant_name=tenant, auth_url=ep)

    def authenticate_glance_admin(self, keystone):
        """Authenticates admin user with glance."""
        ep = keystone.service_catalog.url_for(service_type='image',
                                              endpoint_type='adminURL')
        return glance_client.Client(ep, token=keystone.auth_token)

    def authenticate_nova_user(self, keystone, user, password, tenant):
        """Authenticates a regular user with nova-api."""
        ep = keystone.service_catalog.url_for(service_type='identity',
                                              endpoint_type='publicURL')
        return nova_client.Client(username=user, api_key=password,
                                  project_id=tenant, auth_url=ep)

    def create_cirros_image(self, glance, image_name):
        """Download the latest cirros image and upload it to glance."""
        http_proxy = os.getenv('AMULET_HTTP_PROXY')
        self.log.debug('AMULET_HTTP_PROXY: {}'.format(http_proxy))
        if http_proxy:
            proxies = {'http': http_proxy}
            opener = urllib.FancyURLopener(proxies)
        else:
            opener = urllib.FancyURLopener()

        f = opener.open("http://download.cirros-cloud.net/version/released")
        version = f.read().strip()
        cirros_img = "tests/cirros-{}-x86_64-disk.img".format(version)

        if not os.path.exists(cirros_img):
            cirros_url = "http://{}/{}/{}".format("download.cirros-cloud.net",
                                                  version, cirros_img)
            opener.retrieve(cirros_url, cirros_img)
        f.close()

        with open(cirros_img) as f:
            image = glance.images.create(name=image_name, is_public=True,
                                         disk_format='qcow2',
                                         container_format='bare', data=f)
        count = 1
        status = image.status
        while status != 'active' and count < 10:
            time.sleep(3)
            image = glance.images.get(image.id)
            status = image.status
            self.log.debug('image status: {}'.format(status))
            count += 1

        if status != 'active':
            self.log.error('image creation timed out')
            return None

        return image

    def delete_image(self, glance, image):
        """Delete the specified image."""
        num_before = len(list(glance.images.list()))
        glance.images.delete(image)

        count = 1
        num_after = len(list(glance.images.list()))
        while num_after != (num_before - 1) and count < 10:
            time.sleep(3)
            num_after = len(list(glance.images.list()))
            self.log.debug('number of images: {}'.format(num_after))
            count += 1

        if num_after != (num_before - 1):
            self.log.error('image deletion timed out')
            return False

        return True

    def create_instance(self, nova, image_name, instance_name, flavor):
        """Create the specified instance."""
        image = nova.images.find(name=image_name)
        flavor = nova.flavors.find(name=flavor)
        instance = nova.servers.create(name=instance_name, image=image,
                                       flavor=flavor)

        count = 1
        status = instance.status
        while status != 'ACTIVE' and count < 60:
            time.sleep(3)
            instance = nova.servers.get(instance.id)
            status = instance.status
            self.log.debug('instance status: {}'.format(status))
            count += 1

        if status != 'ACTIVE':
            self.log.error('instance creation timed out')
            return None

        return instance

    def delete_instance(self, nova, instance):
        """Delete the specified instance."""
        num_before = len(list(nova.servers.list()))
        nova.servers.delete(instance)

        count = 1
        num_after = len(list(nova.servers.list()))
        while num_after != (num_before - 1) and count < 10:
            time.sleep(3)
            num_after = len(list(nova.servers.list()))
            self.log.debug('number of instances: {}'.format(num_after))
            count += 1

        if num_after != (num_before - 1):
            self.log.error('instance deletion timed out')
            return False

        return True


text:
package org.literacyapp.dao;

import java.util.List;
import java.util.ArrayList;
import android.database.Cursor;
import android.database.sqlite.SQLiteStatement;

import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.Property;
import org.greenrobot.greendao.internal.SqlUtils;
import org.greenrobot.greendao.internal.DaoConfig;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.database.DatabaseStatement;
import org.greenrobot.greendao.query.Query;
import org.greenrobot.greendao.query.QueryBuilder;

import java.util.Calendar;
import org.literacyapp.dao.converter.CalendarConverter;
import org.literacyapp.dao.converter.ContentStatusConverter;
import org.literacyapp.dao.converter.LocaleConverter;
import org.literacyapp.model.content.Word;
import org.literacyapp.model.content.multimedia.JoinAudiosWithNumbers;
import org.literacyapp.model.content.multimedia.JoinImagesWithNumbers;
import org.literacyapp.model.content.multimedia.JoinVideosWithNumbers;
import org.literacyapp.model.enums.Locale;
import org.literacyapp.model.enums.content.ContentStatus;

import org.literacyapp.model.content.Number;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/**
 * DAO for table "NUMBER".
*/
public class NumberDao extends AbstractDao<Number, Long> {

    public static final String TABLENAME = "NUMBER";

    /**
     * Properties of entity Number.<br/>
     * Can be used for QueryBuilder and for referencing column names.
     */
    public static class Properties {
        public final static Property Id = new Property(0, Long.class, "id", true, "_id");
        public final static Property Locale = new Property(1, String.class, "locale", false, "LOCALE");
        public final static Property TimeLastUpdate = new Property(2, Long.class, "timeLastUpdate", false, "TIME_LAST_UPDATE");
        public final static Property RevisionNumber = new Property(3, Integer.class, "revisionNumber", false, "REVISION_NUMBER");
        public final static Property ContentStatus = new Property(4, String.class, "contentStatus", false, "CONTENT_STATUS");
        public final static Property Value = new Property(5, Integer.class, "value", false, "VALUE");
        public final static Property Symbol = new Property(6, String.class, "symbol", false, "SYMBOL");
    }

    private DaoSession daoSession;

    private final LocaleConverter localeConverter = new LocaleConverter();
    private final CalendarConverter timeLastUpdateConverter = new CalendarConverter();
    private final ContentStatusConverter contentStatusConverter = new ContentStatusConverter();
    private Query<Number> audio_NumbersQuery;
    private Query<Number> image_NumbersQuery;
    private Query<Number> video_NumbersQuery;

    public NumberDao(DaoConfig config) {
        super(config);
    }

    public NumberDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
        this.daoSession = daoSession;
    }

    /** Creates the underlying database table. */
    public static void createTable(Database db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "\"NUMBER\" (" + //
                "\"_id\" INTEGER PRIMARY KEY ," + // 0: id
                "\"LOCALE\" TEXT NOT NULL ," + // 1: locale
                "\"TIME_LAST_UPDATE\" INTEGER," + // 2: timeLastUpdate
                "\"REVISION_NUMBER\" INTEGER NOT NULL ," + // 3: revisionNumber
                "\"CONTENT_STATUS\" TEXT NOT NULL ," + // 4: contentStatus
                "\"VALUE\" INTEGER NOT NULL ," + // 5: value
                "\"SYMBOL\" TEXT);"); // 6: symbol
    }

    /** Drops the underlying database table. */
    public static void dropTable(Database db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"NUMBER\"";
        db.execSQL(sql);
    }

    @Override
    protected final void bindValues(DatabaseStatement stmt, Number entity) {
        stmt.clearBindings();

        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
        stmt.bindString(2, localeConverter.convertToDatabaseValue(entity.getLocale()));

        Calendar timeLastUpdate = entity.getTimeLastUpdate();
        if (timeLastUpdate != null) {
            stmt.bindLong(3, timeLastUpdateConverter.convertToDatabaseValue(timeLastUpdate));
        }
        stmt.bindLong(4, entity.getRevisionNumber());
        stmt.bindString(5, contentStatusConverter.convertToDatabaseValue(entity.getContentStatus()));
        stmt.bindLong(6, entity.getValue());

        String symbol = entity.getSymbol();
        if (symbol != null) {
            stmt.bindString(7, symbol);
        }
    }

    @Override
    protected final void bindValues(SQLiteStatement stmt, Number entity) {
        stmt.clearBindings();

        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
        stmt.bindString(2, localeConverter.convertToDatabaseValue(entity.getLocale()));

        Calendar timeLastUpdate = entity.getTimeLastUpdate();
        if (timeLastUpdate != null) {
            stmt.bindLong(3, timeLastUpdateConverter.convertToDatabaseValue(timeLastUpdate));
        }
        stmt.bindLong(4, entity.getRevisionNumber());
        stmt.bindString(5, contentStatusConverter.convertToDatabaseValue(entity.getContentStatus()));
        stmt.bindLong(6, entity.getValue());

        String symbol = entity.getSymbol();
        if (symbol != null) {
            stmt.bindString(7, symbol);
        }
    }

    @Override
    protected final void attachEntity(Number entity) {
        super.attachEntity(entity);
        entity.__setDaoSession(daoSession);
    }

    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }

    @Override
    public Number readEntity(Cursor cursor, int offset) {
        Number entity = new Number( //
            cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
            localeConverter.convertToEntityProperty(cursor.getString(offset + 1)), // locale
            cursor.isNull(offset + 2) ? null : timeLastUpdateConverter.convertToEntityProperty(cursor.getLong(offset + 2)), // timeLastUpdate
            cursor.getInt(offset + 3), // revisionNumber
            contentStatusConverter.convertToEntityProperty(cursor.getString(offset + 4)), // contentStatus
            cursor.getInt(offset + 5), // value
            cursor.isNull(offset + 6) ? null : cursor.getString(offset + 6) // symbol
        );
        return entity;
    }

    @Override
    public void readEntity(Cursor cursor, Number entity, int offset) {
        entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setLocale(localeConverter.convertToEntityProperty(cursor.getString(offset + 1)));
        entity.setTimeLastUpdate(cursor.isNull(offset + 2) ? null : timeLastUpdateConverter.convertToEntityProperty(cursor.getLong(offset + 2)));
        entity.setRevisionNumber(cursor.getInt(offset + 3));
        entity.setContentStatus(contentStatusConverter.convertToEntityProperty(cursor.getString(offset + 4)));
        entity.setValue(cursor.getInt(offset + 5));
        entity.setSymbol(cursor.isNull(offset + 6) ? null : cursor.getString(offset + 6));
     }

    @Override
    protected final Long updateKeyAfterInsert(Number entity, long rowId) {
        entity.setId(rowId);
        return rowId;
    }

    @Override
    public Long getKey(Number entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    @Override
    public boolean hasKey(Number entity) {
        return entity.getId() != null;
    }

    @Override
    protected final boolean isEntityUpdateable() {
        return true;
    }

    /** Internal query to resolve the "numbers" to-many relationship of Audio. */
    public List<Number> _queryAudio_Numbers(long audioId) {
        synchronized (this) {
            if (audio_NumbersQuery == null) {
                QueryBuilder<Number> queryBuilder = queryBuilder();
                queryBuilder.join(JoinAudiosWithNumbers.class, JoinAudiosWithNumbersDao.Properties.NumberId)
                    .where(JoinAudiosWithNumbersDao.Properties.AudioId.eq(audioId));
                audio_NumbersQuery = queryBuilder.build();
            }
        }
        Query<Number> query = audio_NumbersQuery.forCurrentThread();
        query.setParameter(0, audioId);
        return query.list();
    }

    /** Internal query to resolve the "numbers" to-many relationship of Image. */
    public List<Number> _queryImage_Numbers(long imageId) {
        synchronized (this) {
            if (image_NumbersQuery == null) {
                QueryBuilder<Number> queryBuilder = queryBuilder();
                queryBuilder.join(JoinImagesWithNumbers.class, JoinImagesWithNumbersDao.Properties.NumberId)
                    .where(JoinImagesWithNumbersDao.Properties.ImageId.eq(imageId));
                image_NumbersQuery = queryBuilder.build();
            }
        }
        Query<Number> query = image_NumbersQuery.forCurrentThread();
        query.setParameter(0, imageId);
        return query.list();
    }

    /** Internal query to resolve the "numbers" to-many relationship of Video. */
    public List<Number> _queryVideo_Numbers(long videoId) {
        synchronized (this) {
            if (video_NumbersQuery == null) {
                QueryBuilder<Number> queryBuilder = queryBuilder();
                queryBuilder.join(JoinVideosWithNumbers.class, JoinVideosWithNumbersDao.Properties.NumberId)
                    .where(JoinVideosWithNumbersDao.Properties.VideoId.eq(videoId));
                video_NumbersQuery = queryBuilder.build();
            }
        }
        Query<Number> query = video_NumbersQuery.forCurrentThread();
        query.setParameter(0, videoId);
        return query.list();
    }

    private String selectDeep;

    protected String getSelectDeep() {
        if (selectDeep == null) {
            StringBuilder builder = new StringBuilder("SELECT ");
            SqlUtils.appendColumns(builder, "T", getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T0", daoSession.getWordDao().getAllColumns());
            builder.append(" FROM NUMBER T");
            builder.append(" LEFT JOIN WORD T0 ON T.\"_id\"=T0.\"_id\"");
            builder.append(' ');
            selectDeep = builder.toString();
        }
        return selectDeep;
    }

    protected Number loadCurrentDeep(Cursor cursor, boolean lock) {
        Number entity = loadCurrent(cursor, 0, lock);
        int offset = getAllColumns().length;

        Word word = loadCurrentOther(daoSession.getWordDao(), cursor, offset);
        entity.setWord(word);

        return entity;
    }

    public Number loadDeep(Long key) {
        assertSinglePk();
        if (key == null) {
            return null;
        }

        StringBuilder builder = new StringBuilder(getSelectDeep());
        builder.append("WHERE ");
        SqlUtils.appendColumnsEqValue(builder, "T", getPkColumns());
        String sql = builder.toString();

        String[] keyArray = new String[] { key.toString() };
        Cursor cursor = db.rawQuery(sql, keyArray);

        try {
            boolean available = cursor.moveToFirst();
            if (!available) {
                return null;
            } else if (!cursor.isLast()) {
                throw new IllegalStateException("Expected unique result, but count was " + cursor.getCount());
            }
            return loadCurrentDeep(cursor, true);
        } finally {
            cursor.close();
        }
    }

    /** Reads all available rows from the given cursor and returns a list of new ImageTO objects. */
    public List<Number> loadAllDeepFromCursor(Cursor cursor) {
        int count = cursor.getCount();
        List<Number> list = new ArrayList<Number>(count);

        if (cursor.moveToFirst()) {
            if (identityScope != null) {
                identityScope.lock();
                identityScope.reserveRoom(count);
            }
            try {
                do {
                    list.add(loadCurrentDeep(cursor, false));
                } while (cursor.moveToNext());
            } finally {
                if (identityScope != null) {
                    identityScope.unlock();
                }
            }
        }
        return list;
    }

    protected List<Number> loadDeepAllAndCloseCursor(Cursor cursor) {
        try {
            return loadAllDeepFromCursor(cursor);
        } finally {
            cursor.close();
        }
    }

    /** A raw-style query where you can pass any WHERE clause and arguments. */
    public List<Number> queryDeep(String where, String... selectionArg) {
        Cursor cursor = db.rawQuery(getSelectDeep() + where, selectionArg);
        return loadDeepAllAndCloseCursor(cursor);
    }

}


text:
@interface RBQFetchedResultsControllerTests : RBQTestCase

@end

@implementation RBQFetchedResultsControllerTests

- (void)testPerformFetch
{
    [self insertDifferentSectionNameTestObject];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"inTable = YES"];
    RBQFetchRequest *fetchRequest = [RBQFetchRequest fetchRequestWithEntityName:@"TestObject" inRealm:[RLMRealm defaultRealm] predicate:predicate];
    RLMSortDescriptor *sectionNameSortDescriptor = [RLMSortDescriptor sortDescriptorWithProperty:@"sectionName" ascending:YES];
    fetchRequest.sortDescriptors = @[sectionNameSortDescriptor];
    RBQFetchedResultsController *fetchedResultsController = [[RBQFetchedResultsController alloc] initWithFetchRequest:fetchRequest sectionNameKeyPath:@"sectionName" cacheName:@"cache"];
    [fetchedResultsController performFetch];

    XCTAssert([fetchedResultsController numberOfSections] == 2);
    XCTAssert([fetchedResultsController.sectionNameKeyPath isEqualToString:@"sectionName"]);
    XCTAssert([fetchedResultsController.cacheName isEqualToString:@"cache"]);
    XCTAssert(fetchedResultsController.fetchedObjects.count == 10);
}

- (void)testPeformFetchWithoutSortDescriptor
{
    [self insertDifferentSectionNameTestObject];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"inTable = YES"];
    RBQFetchRequest *fetchRequest = [RBQFetchRequest fetchRequestWithEntityName:@"TestObject" inRealm:[RLMRealm defaultRealm] predicate:predicate];
    RBQFetchedResultsController *fetchedResultsController = [[RBQFetchedResultsController alloc] initWithFetchRequest:fetchRequest sectionNameKeyPath:@"sectionName" cacheName:@"cache"];
    [fetchedResultsController performFetch];

    XCTAssert([fetchedResultsController numberOfSections] == 10);
    XCTAssert([fetchedResultsController.sectionNameKeyPath isEqualToString:@"sectionName"]);
    XCTAssert([fetchedResultsController.cacheName isEqualToString:@"cache"]);
    XCTAssert(fetchedResultsController.fetchedObjects.count == 10);
}

- (void)testDeleteWithCacheName
{
    [self insertDifferentSectionNameTestObject];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"inTable = YES"];
    RBQFetchRequest *fetchRequest = [RBQFetchRequest fetchRequestWithEntityName:@"TestObject" inRealm:[RLMRealm defaultRealm] predicate:predicate];
    RLMSortDescriptor *sectionNameSortDescriptor = [RLMSortDescriptor sortDescriptorWithProperty:@"sectionName" ascending:YES];
    fetchRequest.sortDescriptors = @[sectionNameSortDescriptor];
    RBQFetchedResultsController *fetchedResultsController = [[RBQFetchedResultsController alloc] initWithFetchRequest:fetchRequest sectionNameKeyPath:@"sectionName" cacheName:@"cache"];
    [fetchedResultsController performFetch];

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    TestObject *testObject = [fetchedResultsController objectAtIndexPath:indexPath];

    XCTAssert([fetchedResultsController.cacheName isEqualToString:@"cache"]);
    XCTAssert(fetchedResultsController.fetchedObjects.count == 10);
    XCTAssert([testObject.sectionName isEqualToString:@"section 1"]);
    XCTAssert(testObject.sortIndex == 0);

    [RBQFetchedResultsController deleteCacheWithName:@"cache"];

    XCTAssert([fetchedResultsController.cacheName isEqualToString:@"cache"]);
    XCTAssert(fetchedResultsController.fetchedObjects.count == 10);
    XCTAssertNil([fetchedResultsController objectAtIndexPath:indexPath]);
}

// TODO: - deleteWithCacheName pass nil

- (void)testUpdateFetchRequestSectionNameKeyPathAndPeformFetch
{
    [self insertDifferentSectionNameTestObject];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"inTable = YES"];
    RBQFetchRequest *fetchRequest = [RBQFetchRequest fetchRequestWithEntityName:@"TestObject" inRealm:[RLMRealm defaultRealm] predicate:predicate];
    RLMSortDescriptor *sectionNameSortDescriptor = [RLMSortDescriptor sortDescriptorWithProperty:@"sectionName" ascending:YES];
    fetchRequest.sortDescriptors = @[sectionNameSortDescriptor];
    RBQFetchedResultsController *fetchedResultsController = [[RBQFetchedResultsController alloc] initWithFetchRequest:fetchRequest sectionNameKeyPath:@"sectionName" cacheName:@"cache"];
    [fetchedResultsController performFetch];
    XCTAssert(fetchedResultsController.fetchedObjects.count == 10);
    NSPredicate *anotherPredicate = [NSPredicate predicateWithFormat:@"inTable = NO"];
    RBQFetchRequest *anotherFetchRequest = [RBQFetchRequest fetchRequestWithEntityName:@"TestObject" inRealm:[RLMRealm defaultRealm] predicate: anotherPredicate];
    [fetchedResultsController updateFetchRequest:anotherFetchRequest sectionNameKeyPath:@"sectionName" andPerformFetch:YES];
    XCTAssert(fetchedResultsController.fetchedObjects.count == 0);
}

@end


text:
using Microsoft.Win32.SafeHandles;
using System;
using System.Runtime.ConstrainedExecution;
using System.Runtime.InteropServices;

namespace Vanara.PInvoke
{
	public static partial class Kernel32
	{
		/// Optional actions.
		[Flags]
		public enum DUPLICATE_HANDLE_OPTIONS : uint
		{
			/// Closes the source handle. This occurs regardless of any error status returned.
			DUPLICATE_CLOSE_SOURCE = 0x00000001,

			/// Ignores the dwDesiredAccess parameter. The duplicate handle has the same access as the source handle.
			DUPLICATE_SAME_ACCESS = 0x00000002,
		}

		/// A set of bit flags that specify properties of the object handle.
		[Flags]
		public enum HANDLE_FLAG
		{
			/// None.
			NONE = 0,

			///
			/// If this flag is set, a child process created with the bInheritHandles parameter of CreateProcess set to TRUE will inherit the
			/// object handle.
			///
			HANDLE_FLAG_INHERIT = 1,

			/// If this flag is set, calling the CloseHandle function will not close the object handle.
			HANDLE_FLAG_PROTECT_FROM_CLOSE = 2
		}

		/// Closes an open object handle.
		/// <param name="hObject">A valid handle to an open object.</param>
		/// <returns>
		/// <para>If the function succeeds, the return value is nonzero.</para>
		/// <para>If the function fails, the return value is zero. To get extended error information, call <c>GetLastError</c>.</para>
		/// <para>
		/// If the application is running under a debugger, the function will throw an exception if it receives either a handle value that is
		/// not valid or a pseudo-handle value. This can happen if you close a handle twice, or if you call <c>CloseHandle</c> on a handle
		/// returned by the <c>FindFirstFile</c> function instead of calling the <c>FindClose</c> function.
		/// </para>
		/// </returns>
		// BOOL WINAPI CloseHandle( _In_ HANDLE hObject); https://msdn.microsoft.com/en-us/library/windows/desktop/ms724211(v=vs.85).aspx
		[DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)]
		[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
		[PInvokeData("Winbase.h", MSDNShortId = "ms724211")]
		[return: MarshalAs(UnmanagedType.Bool)]
		public static extern bool CloseHandle(IntPtr hObject);

		/// Closes an open object handle.
		/// <typeparam name="THandle">The type of the handle.</typeparam>
		/// <param name="handle">The handle.</param>
		/// <returns>
		/// <para>If the function succeeds, the return value is nonzero.</para>
		/// <para>If the function fails, the return value is zero. To get extended error information, call <c>GetLastError</c>.</para>
		/// <para>
		/// If the application is running under a debugger, the function will throw an exception if it receives either a handle value that
		/// is not valid or a pseudo-handle value. This can happen if you close a handle twice, or if you call <c>CloseHandle</c> on a
		/// handle returned by the <c>FindFirstFile</c> function instead of calling the <c>FindClose</c> function.
		/// </para>
		/// </returns>
		[PInvokeData("Winbase.h", MSDNShortId = "ms724211")]
		public static bool CloseHandle<THandle>(THandle handle) where THandle : struct, IKernelHandle => CloseHandle(handle.DangerousGetHandle());

		/// Compares two object handles to determine if they refer to the same underlying kernel object.
		/// <param name="hFirstObjectHandle">The first object handle to compare.</param>
		/// <param name="hSecondObjectHandle">The second object handle to compare.</param>
		/// <returns>
		/// A Boolean value that indicates if the two handles refer to the same underlying kernel object. TRUE if the same, otherwise FALSE.
		/// </returns>
		// BOOL WINAPI CompareObjectHandles( _In_ HANDLE hFirstObjectHandle, _In_ HANDLE hSecondObjectHandle); https://msdn.microsoft.com/en-us/library/windows/desktop/mt438733(v=vs.85).aspx
		[DllImport(Lib.KernelBase, SetLastError = false, ExactSpelling = true)]
		[PInvokeData("Handleapi.h", MSDNShortId = "mt438733")]
		[return: MarshalAs(UnmanagedType.Bool)]
		public static extern bool CompareObjectHandles(IntPtr hFirstObjectHandle, IntPtr hSecondObjectHandle);

		/// Determines if two object handles refer to the same underlying kernel object.
		/// <param name="h1">The first object handle to compare.</param>
		/// <param name="h2">The second object handle to compare.</param>
		/// <returns><see langword="true"/> if the two handles refer to the same underlying kernel object; <see langword="false"/> otherwise.</returns>
		public static bool Equals(this IKernelHandle h1, IKernelHandle h2) => CompareObjectHandles(h1.DangerousGetHandle(), h2?.DangerousGetHandle() ?? IntPtr.Zero);

		/// Duplicates an object handle.
		/// <param name="hSourceHandle">
		/// The handle to be duplicated. This is an open object handle that is valid in the context of the source process. For a list of
		/// objects whose handles can be duplicated, see the following Remarks section.
		/// </param>
		/// <param name="bInheritHandle">
		/// A variable that indicates whether the handle is inheritable. If <c>TRUE</c>, the duplicate handle can be inherited by new
		/// processes created by the target process. If <c>FALSE</c>, the new handle cannot be inherited.
		/// </param>
		/// <param name="dwOptions">
		/// <para>Optional actions. This parameter can be zero, or any combination of the following values.</para>
		/// <para>
		/// <list type="table">
		/// <listheader>
		/// <term>Value</term>
		/// <term>Meaning</term>
		/// </listheader>
		/// <item>
		/// <term>DUPLICATE_CLOSE_SOURCE0x00000001</term>
		/// <term>Closes the source handle. This occurs regardless of any error status returned.</term>
		/// </item>
		/// <item>
		/// <term>DUPLICATE_SAME_ACCESS0x00000002</term>
		/// <term>Ignores the dwDesiredAccess parameter. The duplicate handle has the same access as the source handle.</term>
		/// </item>
		/// </list>
		/// </para>
		/// </param>
		/// <param name="dwDesiredAccess">
		/// <para>
		/// The access requested for the new handle. For the flags that can be specified for each object type, see the following Remarks section.
		/// </para>
		/// <para>
		/// This parameter is ignored if the dwOptions parameter specifies the DUPLICATE_SAME_ACCESS flag. Otherwise, the flags that can be
		/// specified depend on the type of object whose handle is to be duplicated.
		/// </para>
		/// </param>
		/// <returns>
		/// <para>The duplicate handle. This handle value is valid in the context of the target process.</para>
		/// <para>
		/// If hSourceHandle is a pseudo handle returned by <c>GetCurrentProcess</c> or <c>GetCurrentThread</c>, <c>DuplicateHandle</c>
		/// converts it to a real handle to a process or thread, respectively.
		/// </para>
		/// </returns>
		public static IntPtr Duplicate(this IKernelHandle hSourceHandle, bool bInheritHandle = true, DUPLICATE_HANDLE_OPTIONS dwOptions = DUPLICATE_HANDLE_OPTIONS.DUPLICATE_SAME_ACCESS, uint dwDesiredAccess = default) =>
			DuplicateHandle(GetCurrentProcess(), hSourceHandle.DangerousGetHandle(), GetCurrentProcess(), out var h, dwDesiredAccess, bInheritHandle, dwOptions) ? h : IntPtr.Zero;

		/// Duplicates an object handle.
		/// <typeparam name="THandle">The type of the handle.</typeparam>
		/// <typeparam name="TAccess">The type of the access value (enum or uint).</typeparam>
		/// <param name="hSourceHandle">
		/// The handle to be duplicated. This is an open object handle that is valid in the context of the source process. For a list of
		/// objects whose handles can be duplicated, see the following Remarks section.
		/// </param>
		/// <param name="dwDesiredAccess">
		/// <para>
		/// The access requested for the new handle. For the flags that can be specified for each object type, see the following Remarks section.
		/// </para>
		/// <para>
		/// This parameter is ignored if the dwOptions parameter specifies the DUPLICATE_SAME_ACCESS flag. Otherwise, the flags that can be
		/// specified depend on the type of object whose handle is to be duplicated.
		/// </para>
		/// </param>
		/// <param name="bInheritHandle">
		/// A variable that indicates whether the handle is inheritable. If <c>TRUE</c>, the duplicate handle can be inherited by new
		/// processes created by the target process. If <c>FALSE</c>, the new handle cannot be inherited.
		/// </param>
		/// <param name="dwOptions">
		/// <para>Optional actions. This parameter can be zero, or any combination of the following values.</para>
		/// <para>
		/// <list type="table">
		/// <listheader>
		/// <term>Value</term>
		/// <term>Meaning</term>
		/// </listheader>
		/// <item>
		/// <term>DUPLICATE_CLOSE_SOURCE 0x00000001</term>
		/// <term>Closes the source handle. This occurs regardless of any error status returned.</term>
		/// </item>
		/// <item>
		/// <term>DUPLICATE_SAME_ACCESS 0x00000002</term>
		/// <term>Ignores the dwDesiredAccess parameter. The duplicate handle has the same access as the source handle.</term>
		/// </item>
		/// </list>
		/// </para>
		/// </param>
		/// <returns>
		/// <para>The duplicate handle. This handle value is valid in the context of the target process.</para>
		/// <para>
		/// If hSourceHandle is a pseudo handle returned by <c>GetCurrentProcess</c> or <c>GetCurrentThread</c>, <c>DuplicateHandle</c>
		/// converts it to a real handle to a process or thread, respectively.
		/// </para>
		/// </returns>
		public static THandle Duplicate<THandle, TAccess>(this THandle hSourceHandle, TAccess dwDesiredAccess, bool bInheritHandle = true, DUPLICATE_HANDLE_OPTIONS dwOptions = DUPLICATE_HANDLE_OPTIONS.DUPLICATE_SAME_ACCESS)
			where THandle : SafeKernelHandle where TAccess : struct, IConvertible =>
			Win32Error.ThrowLastErrorIfFalse(SafeKernelHandle.DuplicateHandle(hSourceHandle, out var ret, dwDesiredAccess, default, default, bInheritHandle, dwOptions)) ? ret : default;

		/// Duplicates an object handle.
		/// <param name="hSourceProcessHandle">
		/// <para>A handle to the process with the handle to be duplicated.</para>
		/// <para>The handle must have the PROCESS_DUP_HANDLE access right. For more information, see Process Security and Access Rights.</para>
		/// </param>
		/// <param name="hSourceHandle">
		/// The handle to be duplicated. This is an open object handle that is valid in the context of the source process. For a list of
		/// objects whose handles can be duplicated, see the following Remarks section.
		/// </param>
		/// <param name="hTargetProcessHandle">
		/// A handle to the process that is to receive the duplicated handle. The handle must have the PROCESS_DUP_HANDLE access right.
		/// </param>
		/// <param name="lpTargetHandle">
		/// <para>A pointer to a variable that receives the duplicate handle. This handle value is valid in the context of the target process.</para>
		/// <para>
		/// If hSourceHandle is a pseudo handle returned by <c>GetCurrentProcess</c> or <c>GetCurrentThread</c>, <c>DuplicateHandle</c>
		/// converts it to a real handle to a process or thread, respectively.
		/// </para>
		/// <para>
		/// If lpTargetHandle is <c>NULL</c>, the function duplicates the handle, but does not return the duplicate handle value to the
		/// caller. This behavior exists only for backward compatibility with previous versions of this function. You should not use this
		/// feature, as you will lose system resources until the target process terminates.
		/// </para>
		/// </param>
		/// <param name="dwDesiredAccess">
		/// <para>
		/// The access requested for the new handle. For the flags that can be specified for each object type, see the following Remarks section.
		/// </para>
		/// <para>
		/// This parameter is ignored if the dwOptions parameter specifies the DUPLICATE_SAME_ACCESS flag. Otherwise, the flags that can be
		/// specified depend on the type of object whose handle is to be duplicated.
		/// </para>
		/// </param>
		/// <param name="bInheritHandle">
		/// A variable that indicates whether the handle is inheritable. If <c>TRUE</c>, the duplicate handle can be inherited by new
		/// processes created by the target process. If <c>FALSE</c>, the new handle cannot be inherited.
		/// </param>
		/// <param name="dwOptions">
		/// <para>Optional actions. This parameter can be zero, or any combination of the following values.</para>
		/// <para>
		/// <list type="table">
		/// <listheader>
		/// <term>Value</term>
		/// <term>Meaning</term>
		/// </listheader>
		/// <item>
		/// <term>DUPLICATE_CLOSE_SOURCE0x00000001</term>
		/// <term>Closes the source handle. This occurs regardless of any error status returned.</term>
		/// </item>
		/// <item>
		/// <term>DUPLICATE_SAME_ACCESS0x00000002</term>
		/// <term>Ignores the dwDesiredAccess parameter. The duplicate handle has the same access as the source handle.</term>
		/// </item>
		/// </list>
		/// </para>
		/// </param>
		/// <returns>
		/// <para>If the function succeeds, the return value is nonzero.</para>
		/// <para>If the function fails, the return value is zero. To get extended error information, call <c>GetLastError</c>.</para>
		/// </returns>
		// BOOL WINAPI DuplicateHandle( _In_ HANDLE hSourceProcessHandle, _In_ HANDLE hSourceHandle, _In_ HANDLE hTargetProcessHandle, _Out_
		// LPHANDLE lpTargetHandle, _In_ DWORD dwDesiredAccess, _In_ BOOL bInheritHandle, _In_ DWORD dwOptions); https://msdn.microsoft.com/en-us/library/windows/desktop/ms724251(v=vs.85).aspx
		[DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)]
		[PInvokeData("Winbase.h", MSDNShortId = "ms724251")]
		[return: MarshalAs(UnmanagedType.Bool)]
		public static extern bool DuplicateHandle([In] HPROCESS hSourceProcessHandle, [In] IntPtr hSourceHandle, [In] HPROCESS hTargetProcessHandle,
			out IntPtr lpTargetHandle, uint dwDesiredAccess, [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle, DUPLICATE_HANDLE_OPTIONS dwOptions);

		/// Retrieves certain properties of an object handle.
		/// <param name="hObject">
		/// <para>A handle to an object whose information is to be retrieved.</para>
		/// <para>
		/// You can specify a handle to one of the following types of objects: access token, console input buffer, console screen buffer,
		/// event, file, file mapping, job, mailslot, mutex, pipe, printer, process, registry key, semaphore, serial communication device,
		/// socket, thread, or waitable timer.
		/// </para>
		/// </param>
		/// <param name="lpdwFlags">
		/// <para>
		/// A pointer to a variable that receives a set of bit flags that specify properties of the object handle or 0. The following values
		/// are defined.
		/// </para>
		/// <para>
		/// <list type="table">
		/// <listheader>
		/// <term>Value</term>
		/// <term>Meaning</term>
		/// </listheader>
		/// <item>
		/// <term>HANDLE_FLAG_INHERIT0x00000001</term>
		/// <term>
		/// If this flag is set, a child process created with the bInheritHandles parameter of CreateProcess set to TRUE will inherit the
		/// object handle.
		/// </term>
		/// </item>
		/// <item>
		/// <term>HANDLE_FLAG_PROTECT_FROM_CLOSE0x00000002</term>
		/// <term>If this flag is set, calling the CloseHandle function will not close the object handle.</term>
		/// </item>
		/// </list>
		/// </para>
		/// </param>
		/// <returns>
		/// <para>If the function succeeds, the return value is nonzero.</para>
		/// <para>If the function fails, the return value is zero. To get extended error information, call <c>GetLastError</c>.</para>
		/// </returns>
		// BOOL WINAPI GetHandleInformation( _In_ HANDLE hObject, _Out_ LPDWORD lpdwFlags); https://msdn.microsoft.com/en-us/library/windows/desktop/ms724329(v=vs.85).aspx
		[DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)]
		[PInvokeData("Winbase.h", MSDNShortId = "ms724329")]
		[return: MarshalAs(UnmanagedType.Bool)]
		public static extern bool GetHandleInformation([In] IntPtr hObject, out HANDLE_FLAG lpdwFlags);

		/// Retrieves certain properties of an object handle.
		/// <param name="hObj">A handle to an object whose information is to be retrieved.</param>
		/// <returns>A variable that receives a set of bit flags that specify properties of the object handle</returns>
		public static HANDLE_FLAG GetInformation(this IKernelHandle hObj) => GetHandleInformation(hObj.DangerousGetHandle(), out var flag) ? flag : 0;

		/// Sets certain properties of an object handle.
		/// <param name="hObject">
		/// <para>A handle to an object whose information is to be set.</para>
		/// <para>
		/// You can specify a handle to one of the following types of objects: access token, console input buffer, console screen buffer,
		/// event, file, file mapping, job, mailslot, mutex, pipe, printer, process, registry key, semaphore, serial communication device,
		/// socket, thread, or waitable timer.
		/// </para>
		/// </param>
		/// <param name="dwMask">
		/// A mask that specifies the bit flags to be changed. Use the same constants shown in the description of dwFlags.
		/// </param>
		/// <param name="dwFlags">
		/// <para>
		/// Set of bit flags that specifies properties of the object handle. This parameter can be 0 or one or more of the following values.
		/// </para>
		/// <para>
		/// <list type="table">
		/// <listheader>
		/// <term>Value</term>
		/// <term>Meaning</term>
		/// </listheader>
		/// <item>
		/// <term>HANDLE_FLAG_INHERIT0x00000001</term>
		/// <term>
		/// If this flag is set, a child process created with the bInheritHandles parameter of CreateProcess set to TRUE will inherit the
		/// object handle.
		/// </term>
		/// </item>
		/// <item>
		/// <term>HANDLE_FLAG_PROTECT_FROM_CLOSE0x00000002</term>
		/// <term>If this flag is set, calling the CloseHandle function will not close the object handle.</term>
		/// </item>
		/// </list>
		/// </para>
		/// </param>
		/// <returns>
		/// <para>If the function succeeds, the return value is nonzero.</para>
		/// <para>If the function fails, the return value is zero. To get extended error information, call <c>GetLastError</c>.</para>
		/// </returns>
		// BOOL WINAPI SetHandleInformation( _In_ HANDLE hObject, _In_ DWORD dwMask, _In_ DWORD dwFlags);// https://msdn.microsoft.com/en-us/library/windows/desktop/ms724935(v=vs.85).aspx
		[DllImport(Lib.Kernel32, SetLastError = true, ExactSpelling = true)]
		[PInvokeData("Winbase.h", MSDNShortId = "ms724935")]
		[return: MarshalAs(UnmanagedType.Bool)]
		public static extern bool SetHandleInformation([In] IntPtr hObject, HANDLE_FLAG dwMask, HANDLE_FLAG dwFlags);

		/// Provides a <see cref="SafeHandle"/> to a handle that releases a created HANDLE instance at disposal using CloseHandle.
		public abstract class SafeKernelHandle : SafeHANDLE, IKernelHandle
		{
			/// Initializes a new instance of the <see cref="SafeSyncHandle"/> class.
			protected SafeKernelHandle() : base() { }

			/// Initializes a new instance of the <see cref="SafeHANDLE"/> class and assigns an existing handle.
			/// <param name="preexistingHandle">An <see cref="IntPtr"/> object that represents the pre-existing handle to use.</param>
			/// <param name="ownsHandle">
			/// <see langword="true"/> to reliably release the handle during the finalization phase; otherwise, <see langword="false"/> (not recommended).
			/// </param>
			protected SafeKernelHandle(IntPtr preexistingHandle, bool ownsHandle = true) : base(preexistingHandle, ownsHandle) { }

			/// Duplicates an object handle.
			/// <typeparam name="THandle">The type of the handle.</typeparam>
			/// <typeparam name="TAccess">The type of the access value (enum or uint).</typeparam>
			/// <param name="hSourceHandle">
			/// The handle to be duplicated. This is an open object handle that is valid in the context of the source process. For a list of
			/// objects whose handles can be duplicated, see the following Remarks section.
			/// </param>
			/// <param name="lpTargetHandle">
			/// <para>
			/// A pointer to a variable that receives the duplicate handle. This handle value is valid in the context of the target process.
			/// </para>
			/// <para>
			/// If hSourceHandle is a pseudo handle returned by <c>GetCurrentProcess</c> or <c>GetCurrentThread</c>, <c>DuplicateHandle</c>
			/// converts it to a real handle to a process or thread, respectively.
			/// </para>
			/// <para>
			/// If lpTargetHandle is <c>NULL</c>, the function duplicates the handle, but does not return the duplicate handle value to the
			/// caller. This behavior exists only for backward compatibility with previous versions of this function. You should not use
			/// this feature, as you will lose system resources until the target process terminates.
			/// </para>
			/// </param>
			/// <param name="dwDesiredAccess">
			/// <para>
			/// The access requested for the new handle. For the flags that can be specified for each object type, see the following Remarks section.
			/// </para>
			/// <para>
			/// This parameter is ignored if the dwOptions parameter specifies the DUPLICATE_SAME_ACCESS flag. Otherwise, the flags that can
			/// be specified depend on the type of object whose handle is to be duplicated.
			/// </para>
			/// </param>
			/// <param name="hSourceProcessHandle">
			/// <para>A handle to the process with the handle to be duplicated.</para>
			/// <para>The handle must have the PROCESS_DUP_HANDLE access right. For more information, see Process Security and Access Rights.</para>
			/// </param>
			/// <param name="hTargetProcessHandle">
			/// A handle to the process that is to receive the duplicated handle. The handle must have the PROCESS_DUP_HANDLE access right.
			/// </param>
			/// <param name="bInheritHandle">
			/// A variable that indicates whether the handle is inheritable. If <c>TRUE</c>, the duplicate handle can be inherited by new
			/// processes created by the target process. If <c>FALSE</c>, the new handle cannot be inherited.
			/// </param>
			/// <param name="dwOptions">
			/// <para>Optional actions. This parameter can be zero, or any combination of the following values.</para>
			/// <para>
			/// <list type="table">
			/// <listheader>
			/// <term>Value</term>
			/// <term>Meaning</term>
			/// </listheader>
			/// <item>
			/// <term>DUPLICATE_CLOSE_SOURCE0x00000001</term>
			/// <term>Closes the source handle. This occurs regardless of any error status returned.</term>
			/// </item>
			/// <item>
			/// <term>DUPLICATE_SAME_ACCESS0x00000002</term>
			/// <term>Ignores the dwDesiredAccess parameter. The duplicate handle has the same access as the source handle.</term>
			/// </item>
			/// </list>
			/// </para>
			/// </param>
			/// <returns>
			/// <para>If the function succeeds, the return value is nonzero.</para>
			/// <para>If the function fails, the return value is zero. To get extended error information, call <c>GetLastError</c>.</para>
			/// </returns>
			public static bool DuplicateHandle<THandle, TAccess>(THandle hSourceHandle, out THandle lpTargetHandle, TAccess dwDesiredAccess,
				HPROCESS hSourceProcessHandle = default, HPROCESS hTargetProcessHandle = default, bool bInheritHandle = false, DUPLICATE_HANDLE_OPTIONS dwOptions = 0)
				where THandle : SafeKernelHandle where TAccess : struct, IConvertible
			{
				var ret = Kernel32.DuplicateHandle(hSourceProcessHandle == default ? GetCurrentProcess() : hSourceProcessHandle, hSourceHandle.DangerousGetHandle(),
					hTargetProcessHandle == default ? GetCurrentProcess() : hTargetProcessHandle, out IntPtr h, Convert.ToUInt32(dwDesiredAccess), bInheritHandle, dwOptions);
				lpTargetHandle = (THandle)Activator.CreateInstance(typeof(THandle), h, true);
				return ret;
			}

			/// <inheritdoc/>
			protected override bool InternalReleaseHandle() => CloseHandle(handle);
		}

		/// Provides a <see cref="SafeHandle"/> to a synchronization object that is automatically disposed using CloseHandle.
		/// <remarks></remarks>
		public abstract class SafeSyncHandle : SafeKernelHandle, ISyncHandle
		{
			/// Initializes a new instance of the <see cref="SafeSyncHandle"/> class.
			protected SafeSyncHandle() : base() { }

			/// Initializes a new instance of the <see cref="SafeSyncHandle"/> class and assigns an existing handle.
			/// <param name="preexistingHandle">An <see cref="IntPtr"/> object that represents the pre-existing handle to use.</param>
			/// <param name="ownsHandle">
			/// <see langword="true"/> to reliably release the handle during the finalization phase; otherwise, <see langword="false"/> (not recommended).
			/// </param>
			protected SafeSyncHandle(IntPtr preexistingHandle, bool ownsHandle = true) : base(preexistingHandle, ownsHandle) { }

			/// Performs an implicit conversion from <see cref="SafeSyncHandle"/> to <see cref="SafeWaitHandle"/>.
			/// <param name="h">The SafeSyncHandle instance.</param>
			/// <returns>The result of the conversion.</returns>
			public static implicit operator SafeWaitHandle(SafeSyncHandle h) => new SafeWaitHandle(h.handle, false);
		}
	}
}


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

namespace Infra.IoC
{
    [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = false)]
    public class IoCAttribute : Attribute
    {
    }
}


text:
require "graphql/analysis/ast/visitor"
require "graphql/analysis/ast/analyzer"
require "graphql/analysis/ast/field_usage"
require "graphql/analysis/ast/query_complexity"
require "graphql/analysis/ast/max_query_complexity"
require "graphql/analysis/ast/query_depth"
require "graphql/analysis/ast/max_query_depth"

module GraphQL
  module Analysis
    module AST
      module_function
      # Analyze a multiplex, and all queries within.
      # Multiplex analyzers are ran for all queries, keeping state.
      # Query analyzers are ran per query, without carrying state between queries.
      #
      # @param multiplex [GraphQL::Execution::Multiplex]
      # @param analyzers [Array<GraphQL::Analysis::AST::Analyzer>]
      # @return [Array<Any>] Results from multiplex analyzers
      def analyze_multiplex(multiplex, analyzers)
        multiplex_analyzers = analyzers.map { |analyzer| analyzer.new(multiplex) }

        multiplex.trace("analyze_multiplex", { multiplex: multiplex }) do
          query_results = multiplex.queries.map do |query|
            if query.valid?
              analyze_query(
                query,
                query.analyzers,
                multiplex_analyzers: multiplex_analyzers
              )
            else
              []
            end
          end

          multiplex_results = multiplex_analyzers.map(&:result)
          multiplex_errors = analysis_errors(multiplex_results)

          multiplex.queries.each_with_index do |query, idx|
            query.analysis_errors = multiplex_errors + analysis_errors(query_results[idx])
          end
          multiplex_results
        end
      end

      # @param query [GraphQL::Query]
      # @param analyzers [Array<GraphQL::Analysis::AST::Analyzer>]
      # @return [Array<Any>] Results from those analyzers
      def analyze_query(query, analyzers, multiplex_analyzers: [])
        query.trace("analyze_query", { query: query }) do
          query_analyzers = analyzers
            .map { |analyzer| analyzer.new(query) }
            .select { |analyzer| analyzer.analyze? }

          analyzers_to_run = query_analyzers + multiplex_analyzers
          if analyzers_to_run.any?
            visitor = GraphQL::Analysis::AST::Visitor.new(
              query: query,
              analyzers: analyzers_to_run
            )

            visitor.visit

            if visitor.rescued_errors.any?
              visitor.rescued_errors
            else
              query_analyzers.map(&:result)
            end
          else
            []
          end
        end
      end

      def analysis_errors(results)
        results.flatten.select { |r| r.is_a?(GraphQL::AnalysisError) }
      end
    end
  end
end


text:
package com.zimbra.cs.taglib.tag.folder;

import com.zimbra.common.service.ServiceException;
import com.zimbra.cs.taglib.tag.ZimbraSimpleTag;
import com.zimbra.cs.taglib.bean.ZFolderBean;
import com.zimbra.client.ZFolder;
import com.zimbra.soap.type.SearchSortBy;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.JspTagException;
import java.io.IOException;

public class CreateSearchFolderTag extends ZimbraSimpleTag {

    private String mParentId;
    private String mName;
    private String mVar;
    private ZFolder.Color mColor;
    private SearchSortBy mSortBy;
    private String mTypes;
    private String mQuery;

    public void setParentid(String parentid) { mParentId = parentid; }
    public void setName(String name) { mName = name; }
    public void setTypes(String types) { mTypes = types; }
    public void setQuery(String query) { mQuery = query; }
    public void setVar(String var) { mVar = var; }
    public void setColor(String color) throws ServiceException { mColor = ZFolder.Color.fromString(color); }
    public void setSort(String sort) throws ServiceException { mSortBy = SearchSortBy.fromString(sort); }

    @Override
    public void doTag() throws JspException, IOException {
        try {
            ZFolderBean result = new ZFolderBean(getMailbox().createSearchFolder(mParentId, mName, mQuery, mTypes, mSortBy, mColor));
            getJspContext().setAttribute(mVar, result, PageContext.PAGE_SCOPE);
        } catch (ServiceException e) {
            throw new JspTagException(e);
        }
    }
}


text:
/***
 * The goal of this file is mainly to intialize require.js AMD module loader configuration.
 */
requirejs.config({
    /***
     * The path where your JavaScripts files are located.
     */
    baseUrl: '../src/apps/',

    /***
     * Path config is also part of Require and allows to define paths for vendors
     * not found directly under baseUrl.
     * See http://requirejs.org/docs/api.html#config-paths for details.
     */
    paths: {
        /*** List of frameworks/libraries to be included in the codebase. ***/
        'jquery': '../../bower_components/jquery/jquery.min',
        'underscore': '../../bower_components/underscore/underscore-min',
        'backbone': '../../bower_components/backbone/backbone',
        'text': '../../bower_components/requirejs-text/text',
        'handlebars': '../../bower_components/handlebars/handlebars.min',
        'bootstrap': '../../bower_components/bootstrap/dist/js/bootstrap.min',
        'template': '../systems/utilities/hd-template-mapper',
        'templates': '../../templates'
    },

    /***
     * shim config is part of `Require 2.0`_ and allows to Configure the dependencies
     * and exports for older, traditional "browser globals" scripts that do not use
     * define() to declare the dependencies and set a module value.
     * See http://requirejs.org/docs/api.html#config-shim for more details.
     */
    shim: {
        underscore: {
            exports: '_'
        },
        backbone: {
            deps: ['jquery', 'underscore'],
            exports: 'Backbone'
        },
        text: {
            exports: 'text'
        },
        handlebars: {
            exports: 'Handlebars'
        },
        bootstrap: {
            deps: ['jquery'],
            exports: 'bootstrap'
        }
    }
});


text:
namespace ouzel
{
    namespace graphics
    {
        class OGLRenderDevice;

        class OGLShader final: public OGLRenderResource
        {
        public:
            OGLShader(OGLRenderDevice& renderDeviceOGL,
                      const std::vector<uint8_t>& newFragmentShader,
                      const std::vector<uint8_t>& newVertexShader,
                      const std::set<Vertex::Attribute::Usage>& newVertexAttributes,
                      const std::vector<Shader::ConstantInfo>& newFragmentShaderConstantInfo,
                      const std::vector<Shader::ConstantInfo>& newVertexShaderConstantInfo,
                      uint32_t newFragmentShaderDataAlignment,
                      uint32_t newVertexShaderDataAlignment,
                      const std::string& fragmentShaderFunction,
                      const std::string& vertexShaderFunction);
            ~OGLShader();

            void reload() override;

            struct Location final
            {
                GLint location;
                DataType dataType;
            };

            inline const std::set<Vertex::Attribute::Usage>& getVertexAttributes() const { return vertexAttributes; }

            inline const std::vector<Location>& getFragmentShaderConstantLocations() const { return fragmentShaderConstantLocations; }
            inline const std::vector<Location>& getVertexShaderConstantLocations() const { return vertexShaderConstantLocations; }

            inline GLuint getProgramId() const { return programId; }

        private:
            void compileShader();
            std::string getShaderMessage(GLuint shaderId);
            std::string getProgramMessage();

            std::vector<uint8_t> fragmentShaderData;
            std::vector<uint8_t> vertexShaderData;

            std::set<Vertex::Attribute::Usage> vertexAttributes;

            std::vector<Shader::ConstantInfo> fragmentShaderConstantInfo;
            std::vector<Shader::ConstantInfo> vertexShaderConstantInfo;

            GLuint fragmentShaderId = 0;
            GLuint vertexShaderId = 0;
            GLuint programId = 0;

            std::vector<Location> fragmentShaderConstantLocations;
            std::vector<Location> vertexShaderConstantLocations;
        };
    } // namespace graphics
} // namespace ouzel

#endif

#endif // OUZEL_GRAPHICS_OGLSHADER_HPP


text:
package edu.harvard.iq.dataverse.engine.command.impl;

import edu.harvard.iq.dataverse.DvObject;
import edu.harvard.iq.dataverse.authorization.Permission;
import edu.harvard.iq.dataverse.authorization.groups.impl.explicit.ExplicitGroup;
import edu.harvard.iq.dataverse.engine.command.AbstractCommand;
import edu.harvard.iq.dataverse.engine.command.CommandContext;
import edu.harvard.iq.dataverse.engine.command.DataverseRequest;
import edu.harvard.iq.dataverse.engine.command.RequiredPermissions;
import edu.harvard.iq.dataverse.engine.command.exception.CommandException;
import java.util.List;

@RequiredPermissions( Permission.ManageDataversePermissions )
public class ListExplicitGroupsCommand extends AbstractCommand<List<ExplicitGroup>> {

    public ListExplicitGroupsCommand(DataverseRequest aRequest, DvObject anAffectedDvObject) {
        super(aRequest, anAffectedDvObject);
    }

    @Override
    public List<ExplicitGroup> execute(CommandContext ctxt) throws CommandException {
        return ctxt.explicitGroups().findByOwner(getAffectedDvObjects().get("").getId());
    }

}


text:
tags:  TODO
title: Docker
---


text:
Elasticell is a distributed NoSQL database with strong consistency and reliability.

### __Compatible with Redis protocol__
Use Elasticell as Redis. You can replace Redis with Elasticell to power your application without changing a single line of code in most cases([unsupport-redis-commands](./unsupport-command.md)).

### __Horizontal scalability__
Grow Elasticell as your business grows. You can increase the capacity simply by adding more machines.

### __Strong consistent persistence storage__
Elasticell put your data on multiple machines as replication without worrying about consistency. Elasticell makes your application use redis as a database and not just only the cache.

### __High availability__
All of the three components, PD, Cell and Proxy, can tolerate the failure of some instances without impacting the availability of the entire cluster.


text:
<?php namespace Owl\Services;

use Owl\Repositories\CommentRepositoryInterface;

class CommentService extends Service
{
    protected $commentRepo;

    public function __construct(
        CommentRepositoryInterface $commentRepo
    ) {
        $this->commentRepo = $commentRepo;
    }

    /**
     * Create a new comment.
     *
     * @param $comment object item_id, user_id, body, username, email
     * @return Illuminate\Database\Eloquent\Model
     */
    public function createComment($cmt)
    {
        return $this->commentRepo->createComment($cmt);
    }

    /**
     * Update a comment's body.
     *
     * @param $id int
     * @param $body string
     * @return Illuminate\Database\Eloquent\Model
     */
    public function updateComment($id, $body)
    {
        return $this->commentRepo->updateComment($id, $body);
    }

    /**
     * Delete a comment.
     *
     * @param $id int
     * @return boolean
     */
    public function deleteComment($id)
    {
        return $this->commentRepo->deleteComment($id);
    }

    /**
     * Get a comment by comment id.
     *
     * @param $id int
     * @return Illuminate\Database\Eloquent\Model
     */
    public function getCommentById($id)
    {
        return $this->commentRepo->getCommentById($id);
    }
}


text:
set -u -e -o pipefail

# This script runs unit tests from angular/material2.

# Save the dir for the root of the Angular repo.
angular_dir=$(pwd)

# Switch into Material directory.
cd ${MATERIAL_REPO_TMP_DIR}

# Install this version of Angular into the freshly cloned repo.
rm -rf ./node_modules/@angular/*
cp -r ${angular_dir}/dist/packages-dist-ivy-aot/* ./node_modules/@angular/

# The angular/material2 CI sets TEST_PLATFORM to either "local", "saucelabs", or "browserstack".
# For angular/angular, we only want to run the "local" tests.
export TEST_PLATFORM=local

# Append the test blocklist into angular/material2's karma-test-shim.js.
# This filters out known-failing tests because the goal is to prevent regressions.
cat ${angular_dir}/tools/material-ci/angular_material_test_blocklist.js >> ./test/karma-test-shim.js

# Now actually run the tests.
yarn gulp test:single-run