text:
namespace RS::Unicorn {

    size_t str_common(const Ustring& s1, const Ustring& s2, size_t start) noexcept {
        if (start >= s1.size() || start >= s2.size())
            return 0;
        auto p = s1.data() + start;
        for (auto q = s2.data() + start, endp = p + std::min(s1.size(), s2.size()); p != endp && *p == *q; ++p, ++q) {}
        return p - s1.data() - start;
    }

    size_t str_common_utf(const Ustring& s1, const Ustring& s2, size_t start) noexcept {
        size_t n = str_common(s1, s2, start);
        if (n == 0)
            return 0;
        size_t pos = start + n;
        if (! is_nonstart_unit(s1[pos]) && ! is_nonstart_unit(s2[pos]))
            return n;
        --pos;
        while (pos > start && is_nonstart_unit(s1[pos]))
            --pos;
        return pos - start;
    }

    bool str_expect(Utf8Iterator& i, const Utf8Iterator& end, const Ustring& prefix) {
        size_t psize = prefix.size();
        if (psize == 0 || end.offset() - i.offset() < psize
                || memcmp(i.source().data() + i.offset(), prefix.data(), psize) != 0)
            return false;
        i = utf_iterator(i.source(), i.offset() + psize);
        return true;
    }

    bool str_expect(Utf8Iterator& i, const Ustring& prefix) {
        return str_expect(i, utf_end(i.source()), prefix);
    }

    Utf8Iterator str_find_char(const Utf8Iterator& b, const Utf8Iterator& e, char32_t c) {
        return std::find(b, e, c);
    }

    Utf8Iterator str_find_char(const Irange<Utf8Iterator>& range, char32_t c) {
        return std::find(range.begin(), range.end(), c);
    }

    Utf8Iterator str_find_char(const Ustring& str, char32_t c) {
        return utf_iterator(str, str.find(str_char(c)));
    }

    Utf8Iterator str_find_last_char(const Utf8Iterator& b, const Utf8Iterator& e, char32_t c) {
        auto i = e;
        while (i != b) {
            --i;
            if (*i == c)
                return i;
        }
        return e;
    }

    Utf8Iterator str_find_last_char(const Irange<Utf8Iterator>& range, char32_t c) {
        return str_find_last_char(range.begin(), range.end(), c);
    }

    Utf8Iterator str_find_last_char(const Ustring& str, char32_t c) {
        return utf_iterator(str, str.rfind(str_char(c)));
    }

    Utf8Iterator str_find_first_of(const Utf8Iterator& b, const Utf8Iterator& e, const Ustring& target) {
        auto u_target = to_utf32(target);
        return std::find_if(b, e,
            [&] (char32_t c) { return u_target.find(c) != npos; });
    }

    Utf8Iterator str_find_first_of(const Irange<Utf8Iterator>& range, const Ustring& target) {
        return str_find_first_of(range.begin(), range.end(), target);
    }

    Utf8Iterator str_find_first_of(const Ustring& str, const Ustring& target) {
        return str_find_first_of(utf_begin(str), utf_end(str), target);
    }

    Utf8Iterator str_find_first_not_of(const Utf8Iterator& b, const Utf8Iterator& e, const Ustring& target) {
        auto u_target = to_utf32(target);
        return std::find_if(b, e,
            [&] (char32_t c) { return u_target.find(c) == npos; });
    }

    Utf8Iterator str_find_first_not_of(const Irange<Utf8Iterator>& range, const Ustring& target) {
        return str_find_first_not_of(range.begin(), range.end(), target);
    }

    Utf8Iterator str_find_first_not_of(const Ustring& str, const Ustring& target) {
        return str_find_first_not_of(utf_begin(str), utf_end(str), target);
    }

    Utf8Iterator str_find_last_of(const Utf8Iterator& b, const Utf8Iterator& e, const Ustring& target) {
        auto u_target = to_utf32(target);
        auto i = e;
        while (i != b) {
            --i;
            if (u_target.find(*i) != npos)
                return i;
        }
        return e;
    }

    Utf8Iterator str_find_last_of(const Irange<Utf8Iterator>& range, const Ustring& target) {
        return str_find_last_of(range.begin(), range.end(), target);
    }

    Utf8Iterator str_find_last_of(const Ustring& str, const Ustring& target) {
        return str_find_last_of(utf_begin(str), utf_end(str), target);
    }

    Utf8Iterator str_find_last_not_of(const Utf8Iterator& b, const Utf8Iterator& e, const Ustring& target) {
        auto u_target = to_utf32(target);
        auto i = e;
        while (i != b) {
            --i;
            if (u_target.find(*i) == npos)
                return i;
        }
        return e;
    }

    Utf8Iterator str_find_last_not_of(const Irange<Utf8Iterator>& range, const Ustring& target) {
        return str_find_last_not_of(range.begin(), range.end(), target);
    }

    Utf8Iterator str_find_last_not_of(const Ustring& str, const Ustring& target) {
        return str_find_last_not_of(utf_begin(str), utf_end(str), target);
    }

    std::pair<size_t, size_t> str_line_column(const Ustring& str, size_t offset, uint32_t flags) {
        offset = std::min(offset, str.size());
        size_t line = 1;
        bool prev_lf = false;
        auto i = utf_begin(str), lstart = i;
        while (i.offset() < offset) {
            if (prev_lf) {
                ++line;
                lstart = i;
                prev_lf = false;
            }
            if (*i == U'\r') {
                ++i;
                if (*i != U'\n') {
                    prev_lf = true;
                } else if (i.offset() < offset) {
                    prev_lf = true;
                    ++i;
                }
            } else {
                prev_lf = char_is_line_break(*i);
                ++i;
            }
        }
        if (i.offset() > offset) {
            --i;
        } else if (prev_lf) {
            ++line;
            lstart = i;
        }
        size_t column = str_length(lstart, i, flags) + 1;
        return {line, column};
    }

    Irange<Utf8Iterator> str_search(const Utf8Iterator& b, const Utf8Iterator& e, const Ustring& target) {
        auto u_target = to_utf32(target);
        auto ub = u_target.begin(), ue = u_target.end();
        auto i = std::search(b, e, ub, ue);
        if (i == e)
            return {i, i};
        auto j = i.offset_by(target.size());
        return {i, j};
    }

    Irange<Utf8Iterator> str_search(const Irange<Utf8Iterator>& range, const Ustring& target) {
        return str_search(range.begin(), range.end(), target);
    }

    Irange<Utf8Iterator> str_search(const Ustring& str, const Ustring& target) {
        return str_search(utf_begin(str), utf_end(str), target);
    }

    size_t str_skipws(Utf8Iterator& i, const Utf8Iterator& end) {
        size_t n = 0;
        for (; i != end && char_is_white_space(*i); ++i, ++n) {}
        return n;
    }

    size_t str_skipws(Utf8Iterator& i) {
        return str_skipws(i, utf_end(i.source()));
    }

}


text:
char log_message[BUFFER_SIZE];
int write_log(int logfd,char ch, int direction){
	log_message[0]=0;
	switch(direction){
		case DIRECTION_SENDING:{
			sprintf(log_message,"SENT 1 BYTE: %c(%d)\n",ch,ch);
			break;
		}
		case DIRECTION_RECEIVING:{
			sprintf(log_message,"RECEIVED 1 BYTE: %c(%d)\n",ch,ch);
			break;
		}
	}
	write(logfd, log_message, strlen(log_message));
	return SUCCESS;
}

MCRYPT td;

int initialize_encryption(){
	char *key;
	char password[PASSWORD_SIZE];
	char *IV;
	int i;

	key = calloc(sizeof(char), ENCRYPTION_KEY_SIZE);
	int key_file_fd = open(ENCRYPTION_KEY_FILE, O_RDONLY);
	if(key_file_fd < 0){
		perror("Failed to open key file.");
		exit(EXIT_FAILED_TO_OPEN_KEY_FILE);
	}
	memset(password,0,PASSWORD_SIZE);
	int read_size = read(key_file_fd, password, PASSWORD_SIZE-1);
	memmove(key, password, strlen(password));

	td = mcrypt_module_open("twofish", NULL, "cfb", NULL);
	if(td == MCRYPT_FAILED){
		perror("Failed to open mcrypt module.");
		exit(EXIT_FAILED_TO_OPEN_MCRYPT_MODULE);
	}

	IV = malloc(mcrypt_enc_get_iv_size(td));
	for(i = 0; i<mcrypt_enc_get_iv_size(td); i=i+1){
		IV[i] = 0;
	}

	i = mcrypt_generic_init(td, key, ENCRYPTION_KEY_SIZE, IV);
	if(i<0){
		mcrypt_perror(i);
		exit(EXIT_FAILED_TO_INITIALIZE_MCRYPT);
	}

	return SUCCESS;
}

int close_encryption(){
	mcrypt_generic_end(td);
	return SUCCESS;
}

int put_char_and_encrypt(int sockfd,char ch,int logfd,int encrypt_flag){
	//TODO encrypt...
	if(encrypt_flag){
		mcrypt_generic(td, &ch, 1);
	}
	if(logfd>=0){
		write_log(logfd,ch,DIRECTION_SENDING);
	}
	g_debug("Writting char %c(%d) to sock %d",ch,ch,sockfd);
	ssize_t write_size = write(sockfd, &ch, 1);
	if(write_size < 0){
		perror("Failed to write to socket");
	}
	return SUCCESS;
}

char get_decrypted_char(int sockfd,int logfd,int encrypt_flag){
	//TODO depend on accepted_sockfd
	char ch;
	ssize_t read_size=read(sockfd, &ch, 1);
	g_debug("Received size %d with char %c(%d) from sock %d",read_size,ch,ch,sockfd);
	if(read_size<0){
		perror("Failed to read.");
		exit(EXIT_FAILED_TO_READ);
	}
	if(read_size==0){
		g_debug("Received EOF from sockfd %d", sockfd);
		return EOF;
	}
	if(logfd>=0){
		write_log(logfd,ch,DIRECTION_RECEIVING);
	}
	if(encrypt_flag){
		mdecrypt_generic(td, &ch, 1);
	}
	return ch;
}

redpajama_set_name: RedPajamaGithub


text:
module class_Circle
  implicit none
  private
  real :: pi = 3.1415926535897931d0 ! Class-wide private constant

  type, public :: Circle
     real :: radius
   contains
     procedure :: area => circle_area
     procedure :: print => circle_print
  end type Circle
contains
  function circle_area(this) result(area)
    class(Circle), intent(in) :: this
    real :: area
    area = pi * this%radius**2
  end function circle_area

  subroutine circle_print(this)
    class(Circle), intent(in) :: this
    real :: area
    area = this%area()  ! Call the type-bound function
    print *, 'Circle: r = ', this%radius, ' area = ', area
  end subroutine circle_print
end module class_Circle

program circle_test
  use class_Circle
  implicit none

  type(Circle) :: c     ! Declare a variable of type Circle.
  c = Circle(1.5)       ! Use the implicit constructor, radius = 1.5.
  call c%print          ! Call the type-bound subroutine
end program circle_test


text:
subroutine SHMultiTaperMaskSE(mtse, sd, cilm, lmax, tapers, lmaxt, K, &
                              taper_wt, norm, csphase, exitstatus)
!------------------------------------------------------------------------------
!
!   This subroutine will calculate the multitaper spectrum estimate utilizing
!   the first K localization windows of an arbitrarily shaped window. The
!   matrix TAPERS contains the spherical-harmonic coefficients of the windows
!   in packed form according to the conventions used by SHCilmToVector. The
!   standard error is calculated using an unbiased estimate of the sample
!   variance.
!
!   Calling Parameters
!
!       IN
!           cilm        Input spherical harmonic file.
!           lmax        Maximum degree of cilm.
!           tapers      The eigenvector matrix returned from a program
!                       such as SHReturnTapersMap, where each column
!                       corresponds to the spherical-harmonic coefficients of
!                       a window in the packed form used by SHCilmToVector.
!           lmaxt       Maximum degree of the eigentapers.
!           K           Number of tapers to use in the multitaper spectral
!                       estimation.
!
!       OUT
!           mtse        Multitaper spectrum estimate, valid up to and including
!                       a maximum degree lmax-lmaxt.
!           sd          Standard error of the multitaper spectrum estimate.
!
!       OPTIONAL (IN)
!           taper_wt    Weight to be applied to each direct spectral estimate.
!                       This should sum to unity.
!           csphase:    1: Do not include the phase factor of (-1)^m (default).
!                       -1: Apply the phase factor of (-1)^m.
!           norm:       Normalization to be used when calculating Legendre
!                       functions
!                           (1) "geodesy" (default)
!                           (2) Schmidt
!                           (3) unnormalized
!                           (4) orthonormalized
!
!       OPTIONAL (OUT)
!           exitstatus  If present, instead of executing a STOP when an error
!                       is encountered, the variable exitstatus will be
!                       returned describing the error.
!                       0 = No errors;
!                       1 = Improper dimensions of input array;
!                       2 = Improper bounds for input variable;
!                       3 = Error allocating memory;
!                       4 = File IO error.
!
!   Copyright (c) 2005-2019, SHTOOLS
!   All rights reserved.
!
!------------------------------------------------------------------------------
    use SHTOOLS, only:  SHPowerSpectrum, SHVectorToCilm, SHGLQ, SHExpandGLQ, &
                        MakeGridGLQ
    use ftypes

    implicit none

    real(dp), intent(out) :: mtse(:), sd(:)
    real(dp), intent(in) :: cilm(:,:,:), tapers(:,:)
    integer, intent(in) :: lmax, lmaxt, K
    real(dp), intent(in), optional :: taper_wt(:)
    integer(int32), intent(in), optional :: csphase, norm
    integer(int32), intent(out), optional :: exitstatus
    integer(int32) :: i, l, phase, mnorm, astat(7), lmaxmul, nlat, nlong
    real(dp) :: se(lmax-lmaxt+1, K), pi, factor
    real(dp), allocatable, save :: zero(:), w(:)
    integer(int32), save :: first = 1, lmaxmul_last = -1
    real(dp), allocatable :: shwin(:,:,:), shloc(:,:,:), grid1glq(:,:), &
                             gridwinglq(:,:), temp(:,:)

!$OMP   threadprivate(zero, w, first, lmaxmul_last)

    if (present(exitstatus)) exitstatus = 0

    pi = acos(-1.0_dp)

    if (size(cilm(:,1,1)) < 2 .or. size(cilm(1,:,1)) < lmax+1 .or. &
        size(cilm(1,1,:)) < lmax+1) then
        print*, "Error --- SHMultiTaperMaskSE"
        print*, "CILM must be dimensioned (2,LMAX+1, LMAX+1) where LMAX is ", &
                lmax
        print*, "Input array is dimensioned ", size(cilm(:,1,1)), &
                size(cilm(1,:,1)), size(cilm(1,1,:))
        if (present(exitstatus)) then
            exitstatus = 1
            return
        else
            stop
        end if

    else if (size(tapers(:,1)) < (lmaxt+1)**2 .or. size(tapers(1,:)) < K) then
        print*, "Error --- SHMultiTaperMaskSE"
        print*, "TAPERS must be dimensioned ((LMAXT+1)**2, K) where " // &
                "LMAXT and K are, ", lmaxt, K
        print*, "Input array is dimensioned ", size(tapers(:,1)), &
                size(tapers(1,:))
        if (present(exitstatus)) then
            exitstatus = 1
            return
        else
            stop
        end if

    else if (size(mtse) < lmax-lmaxt+1) then
        print*, "Error --- SHMultiTaperMaskSE"
        print*, "MTSE must be dimensioned as (LMAX-LMAXT+1) where " // &
                "LMAX and LMAXT are ", lmax, lmaxt
        print*, "Input dimension of array is ", size(mtse)
        if (present(exitstatus)) then
            exitstatus = 1
            return
        else
            stop
        end if

    else if (size(sd) < lmax-lmaxt+1) then
        print*, "Error --- SHMultiTaperMaskSE"
        print*, "SD must be dimensioned as (LMAX-LMAXT+1) " // &
                "where LMAX and LMAXT are ", lmax, lmaxt
        print*, "Input dimension of array is ", size(sd)
        if (present(exitstatus)) then
            exitstatus = 1
            return
        else
            stop
        end if

    else if (lmax < lmaxt) then
        print*, "Error --- SHMultiTaperMaskSE"
        print*, "LMAX must be larger than LMAXT."
        print*, "Input valuse of LMAX and LMAXT are ", lmax, lmaxt
        if (present(exitstatus)) then
            exitstatus = 2
            return
        else
            stop
        end if

    end if

    if (present(taper_wt)) then
        if (size(taper_wt) < K) then
            print*, "Error --- SHMultiTaperMaskSE"
            print*, "TAPER_WT must be dimensioned as (K) where K is ", K
            print*, "Input dimension of array is ", size(taper_wt)
            if (present(exitstatus)) then
                exitstatus = 1
                return
            else
                stop
            end if
        end if

    end if

    if (present(norm)) then
        if (norm > 4 .or. norm < 1) then
            print*, "Error --- SHMultiTaperMaskSE"
            print*, "Parameter NORM must be 1 (geodesy), 2 (Schmidt), " // &
                    "3 (unnormalized), or 4 (orthonormalized)."
            print*, "Input value is ", norm
            if (present(exitstatus)) then
                exitstatus = 2
                return
            else
                stop
            end if

        end if
        mnorm = norm

    else
        mnorm = 1

    end if

    if (present(csphase)) then
        if (csphase /= -1 .and. csphase /= 1) then
            print*, "Error --- SHMultiTaperMaskSE"
            print*, "CSPHASE must be 1 (exclude) or -1 (include)."
            print*, "Input value if ", csphase
            if (present(exitstatus)) then
                exitstatus = 2
                return
            else
                stop
            end if

        else
            phase = csphase

        end if

    else
        phase = 1

    end if

    lmaxmul = lmax + lmaxt
    nlat = lmax + lmaxt + 1
    nlong = 2 * (lmax + lmaxt) + 1

    allocate (shwin(2,lmaxt+1,lmaxt+1), stat = astat(1))
    allocate (shloc(2, lmax+lmaxt+1, lmax+lmaxt+1), stat = astat(2))
    allocate (grid1glq(nlat,nlong), stat = astat(3))
    allocate (gridwinglq(nlat,nlong), stat = astat(4))
    allocate (temp(nlat,nlong), stat = astat(5))

    if (sum(astat(1:5)) /= 0) then
        print*, "Error --- SHMultiTaperSE"
        print*, "Problem allocating arrays SHWIN, SHLOC, " // &
                 "GRID1GLQ, GRIDWINGLQ and TEMP", &
                    astat(1), astat(2), astat(3), astat(4), astat(5)
        if (present(exitstatus)) then
            exitstatus = 3
            return
        else
            stop
        end if

    end if

    if (first == 1) then
        first = 0
        lmaxmul_last = lmaxmul

        allocate (zero(lmaxmul+1), stat = astat(1))
        allocate (w(lmaxmul+1), stat = astat(2))

        if (sum(astat(1:2)) /= 0) then
            print*, "Error --- SHMultiTaperSE"
            print*, "Problem allocating arrays ZERO and W", astat(1), astat(2)
            if (present(exitstatus)) then
                exitstatus = 3
                return
            else
                stop
            end if

        end if

        if (present(exitstatus)) then
            call SHGLQ(lmaxmul, zero, w, csphase = phase, norm = mnorm, &
                       exitstatus = exitstatus)
            if (exitstatus /= 0) return
        else
            call SHGLQ(lmaxmul, zero, w, csphase = phase, norm = mnorm)
        end if

    end if

    if (lmaxmul /= lmaxmul_last) then
        lmaxmul_last = lmaxmul

        deallocate (zero)
        deallocate (w)
        allocate (zero(lmaxmul+1), stat = astat(1))
        allocate (w(lmaxmul+1), stat = astat(2))

        if (sum(astat(1:2)) /= 0) then
            print*, "Error --- SHMultiTaperSE"
            print*, "Problem allocating arrays ZERO and W", astat(1), astat(2)
            if (present(exitstatus)) then
                exitstatus = 3
                return
            else
                stop
            end if

        end if

        if (present(exitstatus)) then
            call SHGLQ(lmaxmul, zero, w, csphase = phase, norm = mnorm, &
                       exitstatus = exitstatus)
            if (exitstatus /= 0) return
        else
            call SHGLQ(lmaxmul, zero, w, csphase = phase, norm = mnorm)
        end if

    end if

    mtse = 0.0_dp
    sd = 0.0_dp

    !--------------------------------------------------------------------------
    !
    !   Calculate localized power spectra
    !
    !--------------------------------------------------------------------------

    call MakeGridGLQ(grid1glq, cilm(1:2,1:lmax+1, 1:lmax+1), &
                     lmaxmul, zero = zero, csphase = phase, norm = mnorm)

    do i = 1, K
        shwin = 0.0_dp

        if (present(exitstatus)) then
            call SHVectorToCilm(tapers(:,i), shwin, lmaxt, &
                                exitstatus = exitstatus)
            if (exitstatus /= 0) return
            call MakeGridGLQ(gridwinglq, shwin(1:2,1:lmaxt+1, 1:lmaxt+1), &
                             lmaxmul, zero = zero, csphase = phase, &
                             norm = mnorm, exitstatus = exitstatus)
            if (exitstatus /= 0) return
            temp(1:nlat,1:nlong) = grid1glq(1:nlat,1:nlong) &
                                   * gridwinglq(1:nlat,1:nlong)
            call SHExpandGLQ(shloc, lmaxmul, temp, w, zero = zero, &
                             csphase = phase, norm = mnorm, &
                             exitstatus = exitstatus)
            if (exitstatus /= 0) return
            call SHPowerSpectrum(shloc, lmax-lmaxt, se(:,i), &
                                 exitstatus = exitstatus)
            if (exitstatus /= 0) return

        else
            call SHVectorToCilm(tapers(:,i), shwin, lmaxt)
            call MakeGridGLQ(gridwinglq, shwin(1:2,1:lmaxt+1, 1:lmaxt+1), &
                             lmaxmul, zero = zero, csphase = phase, &
                             norm = mnorm)
            temp(1:nlat,1:nlong) = grid1glq(1:nlat,1:nlong) &
                                   * gridwinglq(1:nlat,1:nlong)
            call SHExpandGLQ(shloc, lmaxmul, temp, w, zero = zero, &
                             csphase = phase, norm = mnorm)
            call SHPowerSpectrum(shloc, lmax-lmaxt, se(:,i))

        end if

    end do

    if (present(taper_wt)) then

        factor = sum(taper_wt(1:K))**2 - sum(taper_wt(1:K)**2)
        factor = factor * sum(taper_wt(1:K))
        factor = sum(taper_wt(1:K)**2) / factor

        do l = 0, lmax-lmaxt, 1
            mtse(l+1) = dot_product(se(l+1,1:K), taper_wt(1:K)) / &
                        sum(taper_wt(1:K))

            if (K > 1) then
                sd(l+1) = dot_product( (se(l+1,1:K) - mtse(l+1) )**2, &
                                        taper_wt(1:K) ) * factor
            end if

        end do

    else

        do l = 0, lmax-lmaxt, 1
            mtse(l+1) = sum(se(l+1,1:K)) / dble(K)

            if (K > 1) then
                sd(l+1) = sum( ( se(l+1,1:K) - mtse(l+1) )**2 ) / dble(K-1) &
                          / dble(K)   ! standard error!
            end if

        end do

    end if

    if (K > 1) sd(1:lmax-lmaxt+1) = sqrt(sd(1:lmax-lmaxt+1) )

    deallocate (shwin)
    deallocate (shloc)
    deallocate (grid1glq)
    deallocate (gridwinglq)
    deallocate (temp)

end subroutine SHMultiTaperMaskSE


text:
namespace WindowsFormsApp1
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.button1 = new System.Windows.Forms.Button();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.label4 = new System.Windows.Forms.Label();
            this.textBox4 = new System.Windows.Forms.TextBox();
            this.label3 = new System.Windows.Forms.Label();
            this.textBox3 = new System.Windows.Forms.TextBox();
            this.label2 = new System.Windows.Forms.Label();
            this.textBox2 = new System.Windows.Forms.TextBox();
            this.label5 = new System.Windows.Forms.Label();
            this.textBox5 = new System.Windows.Forms.TextBox();
            this.button2 = new System.Windows.Forms.Button();
            this.groupBox2 = new System.Windows.Forms.GroupBox();
            this.label9 = new System.Windows.Forms.Label();
            this.textBox6 = new System.Windows.Forms.TextBox();
            this.groupBox3 = new System.Windows.Forms.GroupBox();
            this.button3 = new System.Windows.Forms.Button();
            this.button5 = new System.Windows.Forms.Button();
            this.button4 = new System.Windows.Forms.Button();
            this.listBox1 = new System.Windows.Forms.ListBox();
            this.groupBox1.SuspendLayout();
            this.groupBox2.SuspendLayout();
            this.groupBox3.SuspendLayout();
            this.SuspendLayout();
            //
            // button1
            //
            this.button1.Location = new System.Drawing.Point(276, 106);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 0;
            this.button1.Text = "Connect";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            //
            // textBox1
            //
            this.textBox1.Location = new System.Drawing.Point(75, 30);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(182, 20);
            this.textBox1.TabIndex = 1;
            this.textBox1.Text = "https://vsd.local";
            //
            // label1
            //
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(3, 33);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(29, 13);
            this.label1.TabIndex = 2;
            this.label1.Text = "VSD";
            //
            // groupBox1
            //
            this.groupBox1.Controls.Add(this.label4);
            this.groupBox1.Controls.Add(this.textBox4);
            this.groupBox1.Controls.Add(this.label3);
            this.groupBox1.Controls.Add(this.textBox3);
            this.groupBox1.Controls.Add(this.label2);
            this.groupBox1.Controls.Add(this.textBox2);
            this.groupBox1.Controls.Add(this.button1);
            this.groupBox1.Controls.Add(this.label1);
            this.groupBox1.Controls.Add(this.textBox1);
            this.groupBox1.Location = new System.Drawing.Point(12, 12);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(367, 145);
            this.groupBox1.TabIndex = 3;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "Connect";
            //
            // label4
            //
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(3, 111);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(66, 13);
            this.label4.TabIndex = 8;
            this.label4.Text = "Organization";
            //
            // textBox4
            //
            this.textBox4.Location = new System.Drawing.Point(75, 108);
            this.textBox4.Name = "textBox4";
            this.textBox4.Size = new System.Drawing.Size(182, 20);
            this.textBox4.TabIndex = 7;
            this.textBox4.Text = "csp";
            //
            // label3
            //
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(3, 85);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(53, 13);
            this.label3.TabIndex = 6;
            this.label3.Text = "Password";
            //
            // textBox3
            //
            this.textBox3.Location = new System.Drawing.Point(75, 82);
            this.textBox3.Name = "textBox3";
            this.textBox3.Size = new System.Drawing.Size(182, 20);
            this.textBox3.TabIndex = 5;
            this.textBox3.Text = "csproot";
            //
            // label2
            //
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(3, 59);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(29, 13);
            this.label2.TabIndex = 4;
            this.label2.Text = "User";
            //
            // textBox2
            //
            this.textBox2.Location = new System.Drawing.Point(75, 56);
            this.textBox2.Name = "textBox2";
            this.textBox2.Size = new System.Drawing.Size(182, 20);
            this.textBox2.TabIndex = 3;
            this.textBox2.Text = "csproot";
            //
            // label5
            //
            this.label5.AutoSize = true;
            this.label5.Location = new System.Drawing.Point(3, 22);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(66, 13);
            this.label5.TabIndex = 11;
            this.label5.Text = "Organization";
            //
            // textBox5
            //
            this.textBox5.Location = new System.Drawing.Point(75, 19);
            this.textBox5.Name = "textBox5";
            this.textBox5.Size = new System.Drawing.Size(182, 20);
            this.textBox5.TabIndex = 10;
            this.textBox5.Text = "Enterprise1";
            //
            // button2
            //
            this.button2.Location = new System.Drawing.Point(276, 17);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(75, 23);
            this.button2.TabIndex = 9;
            this.button2.Text = "Create";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            //
            // groupBox2
            //
            this.groupBox2.Controls.Add(this.label9);
            this.groupBox2.Controls.Add(this.textBox6);
            this.groupBox2.Controls.Add(this.button2);
            this.groupBox2.Controls.Add(this.label5);
            this.groupBox2.Controls.Add(this.textBox5);
            this.groupBox2.Enabled = false;
            this.groupBox2.Location = new System.Drawing.Point(12, 163);
            this.groupBox2.Name = "groupBox2";
            this.groupBox2.Size = new System.Drawing.Size(361, 87);
            this.groupBox2.TabIndex = 12;
            this.groupBox2.TabStop = false;
            this.groupBox2.Text = "Create Org";
            //
            // label9
            //
            this.label9.AutoSize = true;
            this.label9.Location = new System.Drawing.Point(6, 51);
            this.label9.Name = "label9";
            this.label9.Size = new System.Drawing.Size(18, 13);
            this.label9.TabIndex = 21;
            this.label9.Text = "ID";
            //
            // textBox6
            //
            this.textBox6.Location = new System.Drawing.Point(75, 48);
            this.textBox6.Name = "textBox6";
            this.textBox6.ReadOnly = true;
            this.textBox6.Size = new System.Drawing.Size(182, 20);
            this.textBox6.TabIndex = 20;
            //
            // groupBox3
            //
            this.groupBox3.Controls.Add(this.button3);
            this.groupBox3.Controls.Add(this.button5);
            this.groupBox3.Controls.Add(this.button4);
            this.groupBox3.Controls.Add(this.listBox1);
            this.groupBox3.Enabled = false;
            this.groupBox3.Location = new System.Drawing.Point(385, 14);
            this.groupBox3.Name = "groupBox3";
            this.groupBox3.Size = new System.Drawing.Size(270, 236);
            this.groupBox3.TabIndex = 13;
            this.groupBox3.TabStop = false;
            this.groupBox3.Text = "Enterprises";
            //
            // button3
            //
            this.button3.Location = new System.Drawing.Point(6, 207);
            this.button3.Name = "button3";
            this.button3.Size = new System.Drawing.Size(178, 23);
            this.button3.TabIndex = 22;
            this.button3.Text = "Refresh";
            this.button3.UseVisualStyleBackColor = true;
            this.button3.Click += new System.EventHandler(this.button3_Click);
            //
            // button5
            //
            this.button5.Location = new System.Drawing.Point(190, 48);
            this.button5.Name = "button5";
            this.button5.Size = new System.Drawing.Size(75, 23);
            this.button5.TabIndex = 10;
            this.button5.Text = "Delete";
            this.button5.UseVisualStyleBackColor = true;
            this.button5.Click += new System.EventHandler(this.button5_Click);
            //
            // button4
            //
            this.button4.Location = new System.Drawing.Point(190, 19);
            this.button4.Name = "button4";
            this.button4.Size = new System.Drawing.Size(75, 23);
            this.button4.TabIndex = 9;
            this.button4.Text = "Edit";
            this.button4.UseVisualStyleBackColor = true;
            this.button4.Click += new System.EventHandler(this.button4_Click);
            //
            // listBox1
            //
            this.listBox1.FormattingEnabled = true;
            this.listBox1.Location = new System.Drawing.Point(6, 19);
            this.listBox1.Name = "listBox1";
            this.listBox1.Size = new System.Drawing.Size(178, 173);
            this.listBox1.TabIndex = 0;
            //
            // Form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(665, 394);
            this.Controls.Add(this.groupBox3);
            this.Controls.Add(this.groupBox2);
            this.Controls.Add(this.groupBox1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            this.groupBox2.ResumeLayout(false);
            this.groupBox2.PerformLayout();
            this.groupBox3.ResumeLayout(false);
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.GroupBox groupBox1;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.TextBox textBox4;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.TextBox textBox3;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.TextBox textBox2;
        private System.Windows.Forms.Label label5;
        private System.Windows.Forms.TextBox textBox5;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.GroupBox groupBox2;
        private System.Windows.Forms.Label label9;
        private System.Windows.Forms.TextBox textBox6;
        private System.Windows.Forms.GroupBox groupBox3;
        private System.Windows.Forms.Button button3;
        private System.Windows.Forms.ListBox listBox1;
        private System.Windows.Forms.Button button5;
        private System.Windows.Forms.Button button4;
    }
}


text:
(function(w) {
	var CDN_PATH = "https://code.createjs.com/easeljs-{version}.min.js";
	var CDN_VERSIONS = [
		"0.7.1",
		"0.7.0",
		"0.6.1",
		"0.6.0",
		"0.5.0",
		"0.4.2",
		"0.4.1"
	];
	var NEXT_PATH = "../../lib/easeljs-NEXT.js";
	var AUTO_ITERATIONS = 3;
	var DATA_KEY = "workingperfdata";
	var SHARE_KEY = "perfdata";

	var qs, version, versionStr, times={}, startTimes={}, hasResults=false;;
	var data, iterations, result;

	(function setup() {
		injectUI();
		qs = w.queryString = readQueryString();

		var dataStr = localStorage[DATA_KEY];

		if (qs.auto) {
			iterations = parseInt(qs.auto);
			if (isNaN(iterations)) { iterations = AUTO_ITERATIONS; }

			var index = qs.index = Number(qs.index)|0;
			qs.iteration = Number(qs.iteration)|0;
			if (qs.index == 0 && qs.iteration == 0) {
				data = {rows:[], cols:[], raw:{}};
			} else {
				data = JSON.parse(dataStr);
			}
			qs.version = index==0 ? "NEXT" : CDN_VERSIONS[index-1];
		} else if (dataStr) {
			localStorage[DATA_KEY] = "";
			data = JSON.parse(dataStr);
			report(data);
			return;
		}

		version = getVersion(qs.version);
		versionStr = version ? qs.version.toUpperCase() : "";
		if (version) {
			setStatus("preparing...");
			setupTest();
			updateSelectedVersion();
		} else {
			setStatus("select a library version to begin");
		}
	})();

	function setupTest() {
		var script = document.createElement("script");
		document.body.appendChild(script);
		script.onload = onLibLoad;
		result = "";
		script.src = (versionStr == "NEXT") ? NEXT_PATH : CDN_PATH.replace("{version}", qs.version);
	}

	function onLibLoad() {
		setTimeout(runTest, 300);
	}

	function runTest() {
		setStatus("running...");
		if (!window.runTest) { throw("window.runTest is not defined."); }
		else { window.runTest(version, qs); }
	}

	function getVersion(v) {
		if (!v) { return; }
		if (v.toUpperCase() == "NEXT") { return ["NEXT"]; }
		var arr = v.split(".");
		for (var i=0; i<arr.length; i++) {
			var int = parseInt(arr[i]);
			arr[i] = isNaN(int) ? 0 : int;
		}
		return arr;
	}

	function updateSelectedVersion() {
		document.getElementById("version").value = versionStr;
	}

	function readQueryString() {
		var match, search = /([^&=]+)=?([^&]*)/g, o={};
		var query  = window.location.search.substring(1);
		var decode = function (s) { return decodeURIComponent(s.replace(/\+/g, " ")); };
		while (match = search.exec(query)) { o[decode(match[1])] = decode(match[2]); }
		return o;
	}

	function injectUI() {
		var versions = CDN_VERSIONS.slice(0);
		versions.unshift("", "NEXT");
		var doc = document;

		var div = doc.createElement("div");
		div.setAttribute("style","background:#EEE; padding: 0.75em;");
		div.textContent = "Version: ";

		var select = doc.createElement("select");
		select.id = "version";
		select.addEventListener("change", function() { console.log("eh"); reload(document.getElementById("version").value); });
		for (var i=0; i<versions.length; i++) {
			var option = doc.createElement("option");
			option.value = option.textContent = versions[i];
			select.appendChild(option);
		}
		div.appendChild(select);

		var span = doc.createElement("span");
		span.innerHTML += " <input type='button' value='auto' onclick='auto();'/>" +
						 " ";
		div.appendChild(span);

		doc.body.appendChild(div);
	}

	function setStatus(str) {
		var status = document.getElementById("status");
		status.innerHTML = str;
	}

	function reload(version) {
		if (version != null) { qs.version = version; }
		var url = window.location.protocol + "//" + window.location.hostname + (window.location.port ? ":" + window.location.port: "");
		url += window.location.pathname+"?";
		for (var n in qs) {
			url += n + "=" + encodeURIComponent(qs[n]) + "&";
		}
		url = url.substring(0, url.length-1);
		url += window.location.hash;
		window.location = url;
	}

	window.checkVersion = function(v) {
		if (v.toUpperCase() == "NEXT") { return versionStr == "NEXT"; }
		if (versionStr == "NEXT") { return true; }

		v = getVersion(v);
		var l = Math.max(v.length, version.length);
		for (var i=0; i<l; i++) {
			if ((v[i]||0) < (version[i]||0)) { return true; }
			if ((v[i]||0) > (version[i]||0)) { return false; }
		}
		return true;
	};

	var now = window.performance && (performance.now || performance.mozNow || performance.msNow || performance.oNow || performance.webkitNow);
	function getTime() {
		return (now&&now.call(performance))||(new Date().getTime());
	}

	window.time = function(str) {
		startTimes[str] = getTime();
	};

	window.endTime =function(str, log) {
		if (log == null) { log = true; }
		var t = startTimes[str];
		if (!t) { t = -1; }
		else { t = getTime()-t; }
		if (log && qs.auto) {
			times[str] = t;
			hasResults = true;
		} else {
			console&&console.log(str, t.toFixed(2)+"ms");
			result += str+": "+(t+0.5|0)+"ms ";
		}
		if (data && data.cols.indexOf(str) == -1) { data.cols.push(str); }
		return t;
	};

	window.endTest = function() {
		if (!qs.auto) { setStatus(result); return; }

		if (!qs.iteration) { data.rows.push(versionStr); }
		var raw = data.raw;
		if (!raw[versionStr]) { raw[versionStr] = []; }

		raw[versionStr][qs.iteration] = times;
		localStorage[DATA_KEY] = JSON.stringify(data);

		if (++qs.iteration >= iterations || !hasResults) {
			qs.index++;
			qs.iteration = 0;
		}
		if (qs.index > CDN_VERSIONS.length) {
			delete(qs.version);
			delete(qs.index);
			delete(qs.iteration);
			delete(qs.auto);
		}

		delete(qs.version);
		reload();
	};

	window.auto = function() {
		delete(qs.version);
		delete(qs.index);
		delete(qs.iteration);
		qs.auto = AUTO_ITERATIONS;
		reload();
	};

	function report(data) {
		normalizeData(data);
		prepData(data);

		if (qs.report) {
			launchReport(qs.report, data);
			return;
		}

		var i, j, str = "version", stats = data.stats, cols=data.cols, rows=data.rows, colCount = cols.length;
		for (i= 0; i<colCount; i++) { str += "\t"+cols[i]; }

		for (i=0; i<rows.length; i++) {
			str += "\n"+rows[i];
			for (j=0; j<colCount; j++) {
				var vals = stats[rows[i]][cols[j]];
				str += "\t"+(vals&&vals.mean ? vals.mean.toFixed(1) : "----")+"ms";
			}
		}
		console.log(str);
		setStatus("see console for results");
	}

	function launchReport(report, data) {
		localStorage[SHARE_KEY] = JSON.stringify(data);
		if (report.indexOf("/") == -1) { report = "./reports/"+report+".html"; }
		window.location = report + (report.indexOf("?") == -1 ? "?" : "&") + "key="+SHARE_KEY;
	}

	function normalizeData(data) {
		var results = data.results = {};
		var raw = data.raw;
		for (var row in raw) {
			var arr = raw[row];
			var o = results[row] = {};
			for (var i= 0,l=arr.length; i<l; i++) {
				var tests = arr[i];
				for (var test in tests) {
					if (!o[test]) { o[test] = []; }
					o[test].push(tests[test]);
				}
			}
		}
	}

	function prepData(data) {
		var stats = data.stats = {};
		var results = data.results;
		for (var row in results) {
			var tests = results[row];
			stats[row] = {};
			for (var test in tests) {
				stats[row][test] = getStats(tests[test]);
			}
		}
	}

	function getStats(arr) {
		var i, l=arr.length, mean, sum= 0, dev=0;
		for (i=0; i<l; i++) { sum += arr[i]; }
		mean = sum/l;
		for (i=0; i<l; i++) { dev += (arr[i]-mean)*(arr[i]-mean); }
		return {sum:sum, mean:mean, sd:Math.sqrt(dev/l), samples:l};
	}
})(window);


text:
package org.elasticsearch.action.admin.cluster.node.info;

import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Streamable;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentBuilderString;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class PluginsInfo implements Streamable, Serializable, ToXContent {
    static final class Fields {
        static final XContentBuilderString PLUGINS = new XContentBuilderString("plugins");
    }

    private List<PluginInfo> infos;

    public PluginsInfo() {
        infos = new ArrayList<PluginInfo>();
    }

    public PluginsInfo(int size) {
        infos = new ArrayList<PluginInfo>(size);
    }

    public List<PluginInfo> getInfos() {
        return infos;
    }

    public void add(PluginInfo info) {
        infos.add(info);
    }

    public static PluginsInfo readPluginsInfo(StreamInput in) throws IOException {
        PluginsInfo infos = new PluginsInfo();
        infos.readFrom(in);
        return infos;
    }

    @Override
    public void readFrom(StreamInput in) throws IOException {
        int plugins_size = in.readInt();
        for (int i = 0; i < plugins_size; i++) {
            infos.add(PluginInfo.readPluginInfo(in));
        }
    }

    @Override
    public void writeTo(StreamOutput out) throws IOException {
        out.writeInt(infos.size());
        for (PluginInfo plugin : infos) {
            plugin.writeTo(out);
        }
    }

    @Override
    public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
        builder.startArray(Fields.PLUGINS);
        for (PluginInfo pluginInfo : infos) {
            pluginInfo.toXContent(builder, params);
        }
        builder.endArray();

        return builder;
    }
}


text: Submitted October 19, 2017.


text:
<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

class Tasks extends MY_Model
{
    public function rules()
{
    $config = array(
            ['field' => 'task', 'label' => 'TODO task', 'rules' => 'alpha_numeric_spaces|max_length[64]'],
            ['field' => 'priority', 'label' => 'Priority', 'rules' => 'integer|less_than[4]'],
            ['field' => 'size', 'label' => 'Task size', 'rules' => 'integer|less_than[4]'],
            ['field' => 'group', 'label' => 'Task group', 'rules' => 'integer|less_than[5]'],
            ['field' => 'status', 'label' => 'Task status', 'rules' => 'integer|less_than[5]'],
    );
    return $config;
}
    public function __construct()
    {

        parent::__construct('tasks', 'id');
    }
    function getCategorizedTasks()
    {

        foreach ($this->all() as $task)
        {
            if ($task->status != 2)
                $undone[] = $task;
        }

        foreach ($undone as $task)
            $task->group = $this->groups->get($task->group)->name;

        usort($undone, "orderByCategory");

        foreach ($undone as $task)
            $converted[] = (array) $task;
        return $converted;
    }
}
function orderByPriority($a, $b)
{
    if ($a->priority > $b->priority)
        return -1;
    elseif ($a->priority < $b->priority)
        return 1;
    else
        return 0;
}


text:
import sys, string
import time, datetime, calendar
import email.utils
import numbers, random, string
import re, io, hashlib, base64

from functools import *

class OutputCatcher:
	'''Catch all print output.'''

	def __enter__(self):
		self.old_stdout = sys.stdout
		self.new_stdout = io.StringIO()
		self.output_result = ''
		sys.stdout = self.new_stdout
		return self

	def __exit__(self, exc_type, exc_value, traceback):
		self.output_result = self.new_stdout.getvalue()#
		self.new_stdout.close()
		sys.stdout = self.old_stdout

	def output(self):
		return self.output_result

def ignore_error(func):
	'''Decorator. ignore the errors.'''

	@wraps(func)
	def hook(*args, **kwds):
		try:
			return func(*args, **kwds)
		except Exception:
			IN.logger.debug()
		return

	return hook

def format_timestamp(ts):
	"""Formats a timestamp in the format used by HTTP.

	The argument may be a numeric timestamp as returned by `time.time`,
	a time tuple as returned by `time.gmtime`, or a `datetime.datetime`
	object.

	>>> format_timestamp(1359312200)
	'Sun, 27 Jan 2013 18:43:20 GMT'
	"""
	if isinstance(ts, numbers.Real):
		''''''
	elif isinstance(ts, (tuple, time.struct_time)):
		ts = calendar.timegm(ts)
	elif isinstance(ts, datetime.datetime):
		ts = calendar.timegm(ts.utctimetuple())
	else:
		raise TypeError("unknown timestamp type: %r" % ts)
	return email.utils.formatdate(ts, usegmt=True)

def raw_eval(source, g=globals(), l=globals(), err='IN.logger'):
	ret = ''
	#ret = eval(source, g, l)
	try:
		ret = eval(source, g, l)
	except Exception as e:
		IN.logger.debug()

	return ret

def raw_exec(source, g=globals(), l=globals(), err='IN.logger'):
	try:
		exec(source, g, l)

	except Exception as e:
		IN.logger.debug()

__to_seconds__ = {
	'year' : lambda value: 3.15569e7 * value,
	'month' : lambda value: 2.62974e6 * value,
	'week' : lambda value: 604800.0 * value,
	'day' : lambda value: 86400.0 * value,
	'hour' : lambda value: 3600.0 * value,
	'minute' : lambda value: 60.0 * value,
	'second' : lambda value: float(value),
}

def to_seconds(type, value):
	'''Convert value to seconds.'''
	try:
		return __to_seconds__[type](value)
	except:
		return 0

def format_datetime_friendly(date_time_arg):

	now = datetime.datetime.now()

	diff = now - date_time_arg
	second_diff = diff.seconds

	day_diff = diff.days

	if now.day == date_time_arg.day:
		return date_time_arg.strftime("%I:%M %p")

	if day_diff < 28:
		return date_time_arg.strftime("%a %d") # Tue 12

	if day_diff < 365:
		return date_time_arg.strftime("%b, %a %d") #Aug Tue 12

	return date_time_arg.strftime("%b, %a %d, %Y")

	#Tue, Tuesday, August 04 , 2015 10:18 AM
	#%a,  %A,		%B 	   %d , %Y 	 %I:%M %p

# Calculate a Drupal 7 compatible password hash.

class DrupalHash:
	'''Allows to login using drupal generated password.'''

	bytes_decode_type = 'replace'

	def __init__(self):
		self.itoa64 = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
		self.min_log = 7
		self.max_log = 30
		self.default_log = 15
		self.hash_length = 55

	def password_get_count_log2(self, setting):
		return self.itoa64.index(setting[3])

	def password_crypt(self, algo, password, setting):
		setting = setting[0:12]
		if setting[0] != '$' or setting[2] != '$':
			return False

		count_log2 = self.password_get_count_log2(setting)
		salt = setting[4:12]
		if len(salt) < 8:
			return False
		count = 1 << count_log2

		if algo == 'md5':
			hash_func = hashlib.md5
		elif algo == 'sha512':
			hash_func = hashlib.sha512
		else:
			return False

		pwd = salt + password
		pwd = pwd.encode('utf-8')
		passencode = password.encode('utf-8')

		pwd = hash_func(pwd).digest()

		for c in range(count):
			pwd = pwd + passencode
			pwd = hash_func(pwd).digest()

		output = setting + self.custom64(pwd.decode('ISO-8859-1', self.bytes_decode_type)) #

		return output[0:self.hash_length]

	def custom64(self, string, count = 0):
		if count == 0:
			count = len(string)

		output = []

		i = 0
		itoa64 = self.itoa64
		while 1:
			value = ord(string[i])

			i += 1
			output.append(itoa64[value & 0x3f])
			if i < count:
				value |= ord(string[i]) << 8

			output.append(itoa64[(value >> 6) & 0x3f])
			if i >= count:
				break
			i += 1
			if i < count:
				value |= ord(string[i]) << 16
			output.append(itoa64[(value >> 12) & 0x3f])
			if i >= count:
				break
			i += 1
			output.append(itoa64[(value >> 18) & 0x3f])
			if i >= count:
			   break
		return ''.join(output)

	def rehash(self, stored_hash, password):
		# Drupal 6 compatibility
		if len(stored_hash) == 32 and stored_hash.find('$') == -1:
			return hashlib.md5(password).hexdigest()
		# Drupal 7
		if stored_hash[0:2] == 'U$':
			stored_hash = stored_hash[1:]
			password = hashlib.md5(password).hexdigest()
		hash_type = stored_hash[0:3]
		if hash_type == '$S$':
			hash_str = self.password_crypt('sha512', password, stored_hash)
		elif hash_type == '$H$' or hash_type == '$P$':
			hash_str = self.password_crypt('md5', password, stored_hash)
		else:
			hash_str = False
		return hash_str

	def password_generate_salt(self, count_log2):
		output = '$S$'
		count_log2 = self._password_enforce_log2_boundaries(count_log2)
		output += self.itoa64[count_log2]
		output += self.custom64(self._random_string(6), 6)
		return output

	def hash(self, password, count_log2 = 0):
		if count_log2 == 0:
			count_log2 = self.default_log
		return self.password_crypt('sha512', password, self.password_generate_salt(count_log2))

	def _password_enforce_log2_boundaries(self, count_log2):
		if count_log2 < self.min_log:
			return self.min_log
		if count_log2 > self.max_log:
			return self.max_log
		return count_log2

	def _random_string(self, length):
		r = random.SystemRandom()
		chars = string.ascii_letters + string.digits
		return ''.join(r.choice(chars) for _ in range(length))

class RDict(dict):
	'''Recursive Dict to deep merge'''

	def update(self, source, **extra):
		self.__update__(source)
		if extra:
			self.__update__(extra)

	def __update__(self, source):
		for key, value in source.items():
			if key in self and isinstance(value, dict):
				self[key] = RDict(self[key])
				self[key].update(value)
			else:
				self[key] = value

builtins.RDict = RDict


text:
#pragma once
//#include <thread>
namespace nvbio {

// return true if any item in the range [0,n) evaluates to true
//
template <typename PredicateIterator>
bool any(
    const host_tag          tag,
    const uint32            n,
    const PredicateIterator pred)
{
    return thrust::reduce(
        pred,
        pred + n,
        false,
        thrust::logical_or<bool>() );
}

// return true if all items in the range [0,n) evaluate to true
//
template <typename PredicateIterator>
bool all(
    const host_tag          tag,
    const uint32            n,
    const PredicateIterator pred)
{
    return thrust::reduce(
        pred,
        pred + n,
        true,
        thrust::logical_and<bool>() );
}

#if defined(__CUDACC__)

// return true if any item in the range [0,n) evaluates to true
//
template <typename PredicateIterator>
bool any(
    const device_tag        tag,
    const uint32            n,
    const PredicateIterator pred)
{
    return cuda::any( n, pred );
}

// return true if any item in the range [0,n) evaluates to true
//
template <typename PredicateIterator>
bool all(
    const device_tag        tag,
    const uint32            n,
    const PredicateIterator pred)
{
    return cuda::all( n, pred );
}

#endif

// return true if any item in the range [0,n) evaluates to true
//
template <typename system_tag, typename PredicateIterator>
bool any(
    const uint32            n,
    const PredicateIterator pred)
{
    return any( system_tag(), n, pred );
}

// return true if all items in the range [0,n) evaluate to true
//
template <typename system_tag, typename PredicateIterator>
bool all(
    const uint32            n,
    const PredicateIterator pred)
{
    return all( system_tag(), n, pred );
}

// a pseudo-iterator to evaluate the predicate (it1[i] <= it2[i]) for arbitrary iterator pairs
//
template <typename Iterator1, typename Iterator2>
struct is_sorted_iterator
{
    typedef bool                                                        value_type;
    typedef value_type&                                                 reference;
    typedef value_type                                                  const_reference;
    typedef value_type*                                                 pointer;
    typedef typename std::iterator_traits<Iterator1>::difference_type   difference_type;
    typedef typename std::iterator_traits<Iterator1>::iterator_category iterator_category;

    // constructor
    NVBIO_FORCEINLINE NVBIO_HOST_DEVICE
    is_sorted_iterator(const Iterator1 _it1, const Iterator2 _it2) : it1( _it1 ), it2( _it2 ) {}

    // dereference operator
    NVBIO_FORCEINLINE NVBIO_HOST_DEVICE
    bool operator[] (const uint64 i) const { return it1[i] <= it2[i]; }

    // dereference operator
    NVBIO_FORCEINLINE NVBIO_HOST_DEVICE
    bool operator* () const { return it1[0] <= it2[0]; }

    // dereference operator
    NVBIO_FORCEINLINE NVBIO_HOST_DEVICE
    is_sorted_iterator& operator++ () { ++it1; ++it2; return *this; }

    Iterator1 it1;
    Iterator2 it2;
};

// operator+
template <typename T1, typename T2>
NVBIO_FORCEINLINE NVBIO_HOST_DEVICE
is_sorted_iterator<T1,T2> operator+ (const is_sorted_iterator<T1,T2> it, const int64 i)
{
    return is_sorted_iterator<T1,T2>( it.it1 + i, it.it2 + i );
}
// operator-
template <typename T1, typename T2>
NVBIO_FORCEINLINE NVBIO_HOST_DEVICE
int64 operator- (const is_sorted_iterator<T1,T2> it1, const is_sorted_iterator<T1,T2> it2)
{
    return it1.it1 - it2.it1;
}
// operator!=
template <typename T1, typename T2>
NVBIO_FORCEINLINE NVBIO_HOST_DEVICE
bool operator!= (const is_sorted_iterator<T1,T2> it1, const is_sorted_iterator<T1,T2> it2)
{
    return it1.it1 != it2.it1;
}
// operator==
template <typename T1, typename T2>
NVBIO_FORCEINLINE NVBIO_HOST_DEVICE
bool operator== (const is_sorted_iterator<T1,T2> it1, const is_sorted_iterator<T1,T2> it2)
{
    return it1.it1 == it2.it1;
}

// a pseudo-iterator to evaluate the predicate (hd[i] || (it1[i] <= it2[i])) for arbitrary iterator pairs
//
template <typename Iterator1, typename Iterator2, typename Headflags>
struct is_segment_sorted_iterator
{
    typedef bool                                                        value_type;
    typedef value_type&                                                 reference;
    typedef value_type                                                  const_reference;
    typedef value_type*                                                 pointer;
    typedef typename std::iterator_traits<Iterator1>::difference_type   difference_type;
    typedef typename std::iterator_traits<Iterator1>::iterator_category iterator_category;

    // constructor
    NVBIO_FORCEINLINE NVBIO_HOST_DEVICE
    is_segment_sorted_iterator(const Iterator1 _it1, const Iterator2 _it2, const Headflags _hd) : it1( _it1 ), it2( _it2 ), hd(_hd) {}

    // dereference operator
    NVBIO_FORCEINLINE NVBIO_HOST_DEVICE
    bool operator[] (const uint64 i) const { return hd[i] || (it1[i] <= it2[i]); }

    // dereference operator
    NVBIO_FORCEINLINE NVBIO_HOST_DEVICE
    bool operator* () const { return hd[0] || (it1[0] <= it2[0]); }

    // dereference operator
    NVBIO_FORCEINLINE NVBIO_HOST_DEVICE
    is_segment_sorted_iterator& operator++ () { ++it1; ++it2; ++hd; return *this; }

    Iterator1 it1;
    Iterator2 it2;
    Headflags hd;
};

// operator+
template <typename T1, typename T2, typename H>
NVBIO_FORCEINLINE NVBIO_HOST_DEVICE
is_segment_sorted_iterator<T1,T2,H> operator+ (const is_segment_sorted_iterator<T1,T2,H> it, const int64 i)
{
    return is_segment_sorted_iterator<T1,T2,H>( it.it1 + i, it.it2 + i, it.hd + i );
}
// operator-
template <typename T1, typename T2, typename H>
NVBIO_FORCEINLINE NVBIO_HOST_DEVICE
int64 operator- (const is_segment_sorted_iterator<T1,T2,H> it1, const is_segment_sorted_iterator<T1,T2,H> it2)
{
    return it1.it1 - it2.it1;
}
// operator!=
template <typename T1, typename T2, typename H>
NVBIO_FORCEINLINE NVBIO_HOST_DEVICE
bool operator!= (const is_segment_sorted_iterator<T1,T2,H> it1, const is_segment_sorted_iterator<T1,T2,H> it2)
{
    return it1.it1 != it2.it1;
}
// operator==
template <typename T1, typename T2, typename H>
NVBIO_FORCEINLINE NVBIO_HOST_DEVICE
bool operator== (const is_segment_sorted_iterator<T1,T2,H> it1, const is_segment_sorted_iterator<T1,T2,H> it2)
{
    return it1.it1 == it2.it1;
}

// return true if the items in the range [0,n) are sorted
//
template <typename system_tag, typename Iterator>
bool is_sorted(
    const uint32    n,
    const Iterator  values)
{
    return all<system_tag>( n-1, is_sorted_iterator<Iterator,Iterator>( values, values+1 ) );
}

// return true if the items in the range [0,n) are sorted by segment, where
// the beginning of each segment is identified by a set head flag
//
template <typename system_tag, typename Iterator, typename Headflags>
bool is_segment_sorted(
    const uint32            n,
    const Iterator          values,
    const Headflags         flags)
{
    return all<system_tag>( n-1, is_segment_sorted_iterator<Iterator,Iterator,Headflags>( values, values+1, flags+1 ) );
}

// invoke a functor for each element of the given sequence
//
template <typename Iterator, typename Functor>
void for_each(
    const host_tag          tag,
    const uint64            n,
    const Iterator          in,
          Functor           functor)
{
    #if defined(_OPENMP)
    #pragma omp parallel for if (n >= 256)
    #endif
    for (int64 i = 0; i < int64(n); ++i)
        functor( in[i] );
}

// invoke a functor for each element of the given sequence
//
template <typename Iterator, typename Functor>
void for_each(
    const device_tag        tag,
    const uint64            n,
    const Iterator          in,
          Functor           functor)
{
    thrust::for_each( in, in + n, functor );
}

// invoke a functor for each element of the given sequence
//
template <typename system_tag, typename Iterator, typename Functor>
void for_each(
    const uint64            n,
    const Iterator          in,
          Functor           functor)
{
    return for_each( system_tag(), n, in, functor );
}

// apply a functor to each element of the given sequence
//
template <typename Iterator, typename Output, typename Functor>
void transform(
    const device_tag        tag,
    const uint64            n,
    const Iterator          in,
    const Output            out,
    const Functor           functor)
{
    thrust::transform( in, in + n, out, functor );
}

// apply a functor to each element of the given sequence
//
template <typename Iterator, typename Output, typename Functor>
void transform(
    const host_tag          tag,
    const uint32            n,
    const Iterator          in,
    const Output            out,
    const Functor           functor)
{
    #if defined(_OPENMP)
    #pragma omp parallel for if (n >= 256)
    #endif
    for (int64 i = 0; i < int64(n); ++i)
        out[i] = functor( in[i] );
}

// apply a binary functor to each pair of elements of the given sequences
//
template <typename Iterator1, typename Iterator2, typename Output, typename Functor>
void transform(
    const device_tag        tag,
    const uint32            n,
    const Iterator1         in1,
    const Iterator2         in2,
    const Output            out,
    const Functor           functor)
{
    thrust::transform( in1, in1 + n, in2, out, functor );
}

// apply a binary functor to each pair of elements of the given sequences
//
template <typename Iterator1, typename Iterator2, typename Output, typename Functor>
void transform(
    const host_tag          tag,
    const uint32            n,
    const Iterator1         in1,
    const Iterator2         in2,
    const Output            out,
    const Functor           functor)
{
    #if defined(_OPENMP)
    #pragma omp parallel for if (n >= 256)
    #endif
    for (int64 i = 0; i < int64(n); ++i)
        out[i] = functor( in1[i], in2[i] );
}

// apply a functor to each element of the given sequence
//
template <typename system_tag, typename Iterator, typename Output, typename Functor>
void transform(
    const uint32            n,
    const Iterator          in,
    const Output            out,
    const Functor           functor)
{
    transform( system_tag(), n, in, out, functor );
}

// apply a binary functor to each pair of elements of the given sequences
//
template <typename system_tag, typename Iterator1, typename Iterator2, typename Output, typename Functor>
void transform(
    const uint32            n,
    const Iterator1         in1,
    const Iterator2         in2,
    const Output            out,
    const Functor           functor)
{
    transform( system_tag(), n, in1, in2, out, functor );
}

// host-wide reduce
//
// \param n                    number of items to reduce
// \param in                   a system iterator
// \param op                   the binary reduction operator
// \param temp_storage         some temporary storage
//
template <typename InputIterator, typename BinaryOp>
typename std::iterator_traits<InputIterator>::value_type reduce(
    host_tag                            tag,
    const uint32                        n,
    InputIterator                       in,
    BinaryOp                            op,
    nvbio::vector<host_tag,uint8>&      temp_storage)
{
    return thrust::reduce( in, in + n, 0u, op );
}

// host-wide inclusive scan
//
// \param n                    number of items to reduce
// \param in                 a device input iterator
// \param out                a device output iterator
// \param op                   the binary reduction operator
// \param temp_storage       some temporary storage
//
template <typename InputIterator, typename OutputIterator, typename BinaryOp>
void inclusive_scan(
    host_tag                            tag,
    const uint32                        n,
    InputIterator                       in,
    OutputIterator                      out,
    BinaryOp                            op,
    nvbio::vector<host_tag,uint8>&      temp_storage)
{
    thrust::inclusive_scan(
        in,
        in + n,
        out,
        op );
}

// host-wide exclusive scan
//
// \param n                    number of items to reduce
// \param in                 a device input iterator
// \param out                a device output iterator
// \param op                   the binary reduction operator
// \param identity             the identity element
// \param temp_storage       some temporary storage
//
template <typename InputIterator, typename OutputIterator, typename BinaryOp, typename Identity>
void exclusive_scan(
    host_tag                            tag,
    const uint32                        n,
    InputIterator                       in,
    OutputIterator                      out,
    BinaryOp                            op,
    Identity                            identity,
    nvbio::vector<host_tag,uint8>&      temp_storage)
{
    thrust::exclusive_scan(
        in,
        in + n,
        out,
        identity,
        op );
}

#if defined(__CUDACC__)

// system-wide reduce
//
// \param n                    number of items to reduce
// \param in                   a system iterator
// \param op                   the binary reduction operator
// \param temp_storage         some temporary storage
//
template <typename InputIterator, typename BinaryOp>
typename std::iterator_traits<InputIterator>::value_type reduce(
    device_tag                          tag,
    const uint32                        n,
    InputIterator                       in,
    BinaryOp                            op,
    nvbio::vector<device_tag,uint8>&    temp_storage)
{
    return cuda::reduce( n, in, op, temp_storage );
}

// device-wide inclusive scan
//
// \param n                    number of items to reduce
// \param in                 a device input iterator
// \param out                a device output iterator
// \param op                   the binary reduction operator
// \param temp_storage       some temporary storage
//
template <typename InputIterator, typename OutputIterator, typename BinaryOp>
void inclusive_scan(
    device_tag                          tag,
    const uint32                        n,
    InputIterator                       in,
    OutputIterator                      out,
    BinaryOp                            op,
    nvbio::vector<device_tag,uint8>&    temp_storage)
{
    cuda::inclusive_scan( n, in, out, op, temp_storage );
}

// device-wide exclusive scan
//
// \param n                    number of items to reduce
// \param in                 a device input iterator
// \param out                a device output iterator
// \param op                   the binary reduction operator
// \param identity             the identity element
// \param temp_storage       some temporary storage
//
template <typename InputIterator, typename OutputIterator, typename BinaryOp, typename Identity>
void exclusive_scan(
    device_tag                          tag,
    const uint32                        n,
    InputIterator                       in,
    OutputIterator                      out,
    BinaryOp                            op,
    Identity                            identity,
    nvbio::vector<device_tag,uint8>&    temp_storage)
{
    cuda::exclusive_scan( n, in, out, op, identity, temp_storage );
}

#endif

// system-wide reduce
//
// \param n                    number of items to reduce
// \param in                   a system iterator
// \param op                   the binary reduction operator
// \param temp_storage         some temporary storage
//
template <typename system_tag, typename InputIterator, typename BinaryOp>
typename std::iterator_traits<InputIterator>::value_type reduce(
    const uint32                        n,
    InputIterator                       in,
    BinaryOp                            op,
    nvbio::vector<system_tag,uint8>&    temp_storage)
{
    return reduce(
        system_tag(),
        n,
        in,
        op,
        temp_storage );
}

// device-wide inclusive scan
//
// \param n                    number of items to reduce
// \param in                 a device input iterator
// \param out                a device output iterator
// \param op                   the binary reduction operator
// \param temp_storage       some temporary storage
//
template <typename system_tag, typename InputIterator, typename OutputIterator, typename BinaryOp>
void inclusive_scan(
    const uint32                        n,
    InputIterator                       in,
    OutputIterator                      out,
    BinaryOp                            op,
    nvbio::vector<system_tag,uint8>&    temp_storage)
{
    inclusive_scan(
        system_tag(),
        n,
        in,
        out,
        op,
        temp_storage );
}

// device-wide exclusive scan
//
// \param n                    number of items to reduce
// \param in                 a device input iterator
// \param out                a device output iterator
// \param op                   the binary reduction operator
// \param identity             the identity element
// \param temp_storage       some temporary storage
//
template <typename system_tag, typename InputIterator, typename OutputIterator, typename BinaryOp, typename Identity>
void exclusive_scan(
    const uint32                        n,
    InputIterator                       in,
    OutputIterator                      out,
    BinaryOp                            op,
    Identity                            identity,
    nvbio::vector<system_tag,uint8>&    temp_storage)
{
    exclusive_scan(
        system_tag(),
        n,
        in,
        out,
        op,
        identity,
        temp_storage );
}

// host-wide copy of flagged items
//
// \param n                    number of input items
// \param in                    a input iterator
// \param flags                 a flags iterator
// \param out                   a output iterator
// \param temp_storage          some temporary storage
//
// \return                     the number of copied items
//
template <typename InputIterator, typename FlagsIterator, typename OutputIterator>
uint32 copy_flagged(
    const host_tag                  tag,
    const uint32                    n,
    InputIterator                   in,
    FlagsIterator                   flags,
    OutputIterator                  out,
    nvbio::vector<host_tag,uint8>&  temp_storage)
{
    return uint32( thrust::copy_if(
        in,
        in + n,
        flags,
        out,
        nvbio::is_true_functor<bool>() ) - out );
}

// host-wide copy of predicated items
//
// \param n                    number of input items
// \param in                   a input iterator
// \param flags                a flags iterator
// \param out                  a output iterator
// \param temp_storage         some temporary storage
//
// \return                     the number of copied items
//
template <typename InputIterator, typename OutputIterator, typename Predicate>
uint32 copy_if(
    const host_tag                      tag,
    const uint32                        n,
    InputIterator                       in,
    OutputIterator                      out,
    const Predicate                     pred,
    nvbio::vector<host_tag,uint8>&      temp_storage)
{
    return uint32( thrust::copy_if(
        in,
        in + n,
        out,
        pred ) - out );
}

// system-wide run-length encode
//
// \param n                     number of input items
// \param in                    a system input iterator
// \param out                   a system output iterator
// \param counts                a system output count iterator
// \param temp_storage          some temporary storage
//
// \return                     the number of copied items
//
template <typename InputIterator, typename OutputIterator, typename CountIterator>
uint32 runlength_encode(
    const host_tag                      tag,
    const uint32                        n,
    InputIterator                       in,
    OutputIterator                      out,
    CountIterator                       counts,
    nvbio::vector<host_tag,uint8>&      temp_storage)
{
    return uint32( thrust::reduce_by_key(
        in,
        in + n,
        thrust::make_constant_iterator<uint32>( 1u ),
        out,
        counts ).first - out );
};

// system-wide run-length encode
//
// \param n                     number of input items
// \param keys_in               a system input iterator
// \param values_in             a system input iterator
// \param keys_out              a system output iterator
// \param values_out            a system output iterator
// \param reduction_op          a reduction operator
// \param temp_storage          some temporary storage
//
// \return                      the number of copied items
//
template <typename KeyIterator, typename ValueIterator, typename OutputKeyIterator, typename OutputValueIterator, typename ReductionOp>
uint32 reduce_by_key(
    const host_tag                      tag,
    const uint32                        n,
    KeyIterator                         keys_in,
    ValueIterator                       values_in,
    OutputKeyIterator                   keys_out,
    OutputValueIterator                 values_out,
    ReductionOp                         reduction_op,
    nvbio::vector<host_tag,uint8>&      temp_storage)
{
    typedef typename std::iterator_traits<KeyIterator>::value_type key_type;

    return uint32( thrust::reduce_by_key(
        keys_in,
        keys_in + n,
        values_in,
        keys_out,
        values_out,
        nvbio::equal_functor<key_type>(),
        reduction_op ).first - keys_out );
}

#if defined(__CUDACC__)

// device-wide copy of flagged items
//
// \param n                    number of input items
// \param in                   a input iterator
// \param flags                a flags iterator
// \param out                  a output iterator
// \param temp_storage         some temporary storage
//
// \return                     the number of copied items
//
template <typename InputIterator, typename FlagsIterator, typename OutputIterator>
uint32 copy_flagged(
    const device_tag                    tag,
    const uint32                        n,
    InputIterator                       in,
    FlagsIterator                       flags,
    OutputIterator                      out,
    nvbio::vector<device_tag,uint8>&    temp_storage)
{
    return cuda::copy_flagged( n, in, flags, out, temp_storage );
}

// device-wide copy of predicated items
//
// \param n                    number of input items
// \param in                   a input iterator
// \param flags                a flags iterator
// \param out                  a output iterator
// \param temp_storage         some temporary storage
//
// \return                     the number of copied items
//
template <typename InputIterator, typename OutputIterator, typename Predicate>
uint32 copy_if(
    const device_tag                    tag,
    const uint32                        n,
    InputIterator                       in,
    OutputIterator                      out,
    const Predicate                     pred,
    nvbio::vector<device_tag,uint8>&    temp_storage)
{
    return cuda::copy_if( n, in, out, pred, temp_storage );
}

// system-wide run-length encode
//
// \param n                     number of input items
// \param in                    a device input iterator
// \param out                   a device output iterator
// \param counts                a device output count iterator
// \param temp_storage          some temporary storage
//
// \return                     the number of copied items
//
template <typename InputIterator, typename OutputIterator, typename CountIterator>
uint32 runlength_encode(
    const device_tag                    tag,
    const uint32                        n,
    InputIterator                       in,
    OutputIterator                      out,
    CountIterator                       counts,
    nvbio::vector<device_tag,uint8>&    temp_storage)
{
    return cuda::runlength_encode( n, in, out, counts, temp_storage );
};

// device-wide run-length encode
//
// \param n                     number of input items
// \param keys_in               a device input iterator
// \param values_in             a device input iterator
// \param keys_out              a device output iterator
// \param values_out            a device output iterator
// \param reduction_op          a reduction operator
// \param temp_storage          some temporary storage
//
// \return                      the number of copied items
//
template <typename KeyIterator, typename ValueIterator, typename OutputKeyIterator, typename OutputValueIterator, typename ReductionOp>
uint32 reduce_by_key(
    const device_tag                    tag,
    const uint32                        n,
    KeyIterator                         keys_in,
    ValueIterator                       values_in,
    OutputKeyIterator                   keys_out,
    OutputValueIterator                 values_out,
    ReductionOp                         reduction_op,
    nvbio::vector<device_tag,uint8>&    temp_storage)
{
    return cuda::reduce_by_key(
        n,
        keys_in,
        values_in,
        keys_out,
        values_out,
        reduction_op,
        temp_storage );
}

#endif

// system-wide copy of flagged items
//
// \param n                    number of input items
// \param in                 a device input iterator
// \param flags              a device flags iterator
// \param out                a device output iterator
// \param temp_storage       some temporary storage
//
// \return                     the number of copied items
//
template <typename system_tag, typename InputIterator, typename FlagsIterator, typename OutputIterator>
uint32 copy_flagged(
    const uint32                        n,
    InputIterator                       in,
    FlagsIterator                       flags,
    OutputIterator                      out,
    nvbio::vector<system_tag,uint8>&    temp_storage)
{
    return copy_flagged( system_tag(), n, in, flags, out, temp_storage );
};

// system-wide copy of predicated items
//
// \param n                    number of input items
// \param in                 a device input iterator
// \param out                a device output iterator
// \param pred                 a unary predicate functor
// \param temp_storage       some temporary storage
//
// \return                     the number of copied items
//
template <typename system_tag, typename InputIterator, typename OutputIterator, typename Predicate>
uint32 copy_if(
    const uint32                        n,
    InputIterator                       in,
    OutputIterator                      out,
    const Predicate                     pred,
    nvbio::vector<system_tag,uint8>&    temp_storage)
{
    return copy_if( system_tag(), n, in, out, pred, temp_storage );
};

// system-wide run-length encode
//
// \param n                     number of input items
// \param in                    a system input iterator
// \param out                   a system output iterator
// \param counts                a system output count iterator
// \param temp_storage          some temporary storage
//
// \return                     the number of copied items
//
template <typename system_tag, typename InputIterator, typename OutputIterator, typename CountIterator>
uint32 runlength_encode(
    const uint32                        n,
    InputIterator                       in,
    OutputIterator                      out,
    CountIterator                       counts,
    nvbio::vector<system_tag,uint8>&    temp_storage)
{
    return runlength_encode( system_tag(), n, in, out, counts, temp_storage );
};

// system-wide run-length encode
//
// \param n                     number of input items
// \param keys_in               a system input iterator
// \param values_in             a system input iterator
// \param keys_out              a system output iterator
// \param values_out            a system output iterator
// \param reduction_op          a reduction operator
// \param temp_storage          some temporary storage
//
// \return                      the number of copied items
//
template <typename system_tag, typename KeyIterator, typename ValueIterator, typename OutputKeyIterator, typename OutputValueIterator, typename ReductionOp>
uint32 reduce_by_key(
    const uint32                        n,
    KeyIterator                         keys_in,
    ValueIterator                       values_in,
    OutputKeyIterator                   keys_out,
    OutputValueIterator                 values_out,
    ReductionOp                         reduction_op,
    nvbio::vector<system_tag,uint8>&    temp_storage)
{
    return reduce_by_key(
        system_tag(),
        n,
        keys_in,
        values_in,
        keys_out,
        values_out,
        reduction_op,
        temp_storage );
}

// device-wide lower_bound
//
// \param n                    number of input items
// \param values               a system input iterator of values to be searched
// \param keys                 a system input iterator of sorted keys
// \param indices              a system output iterator
//
template <typename KeyIterator, typename ValueIterator, typename OutputIterator>
void lower_bound(
    const device_tag                    tag,
    const uint32                        n,
    ValueIterator                       values,
    const uint32                        n_keys,
    KeyIterator                         keys,
    OutputIterator                      indices)
{
    thrust::lower_bound(
        keys, keys + n_keys,
        values, values + n,
        indices );
}

// host-wide lower_bound
//
// \param n                    number of input items
// \param values               a system input iterator of values to be searched
// \param keys                 a system input iterator of sorted keys
// \param indices              a system output iterator
//
template <typename KeyIterator, typename ValueIterator, typename OutputIterator>
void lower_bound(
    const host_tag                      tag,
    const uint32                        n,
    ValueIterator                       values,
    const uint32                        n_keys,
    KeyIterator                         keys,
    OutputIterator                      indices)
{
    #pragma omp parallel for
    for (long i = 0; i < long(n); ++i)
        indices[i] = uint32( lower_bound( values[i], keys, n_keys ) - keys );
}

// system-wide lower_bound
//
// \param n                    number of input items
// \param values               a system input iterator of values to be searched
// \param keys                 a system input iterator of sorted keys
// \param indices              a system output iterator
//
template <typename system_tag, typename KeyIterator, typename ValueIterator, typename OutputIterator>
void lower_bound(
    const uint32                        n,
    ValueIterator                       values,
    const uint32                        n_keys,
    KeyIterator                         keys,
    OutputIterator                      indices)
{
    lower_bound(
        system_tag(),
        n,
        values,
        n_keys,
        keys,
        indices );
}

// device-wide upper_bound
//
// \param n                    number of input items
// \param values               a system input iterator of values to be searched
// \param keys                 a system input iterator of sorted keys
// \param indices              a system output iterator
//
template <typename KeyIterator, typename ValueIterator, typename OutputIterator>
void upper_bound(
    const device_tag                    tag,
    const uint32                        n,
    ValueIterator                       values,
    const uint32                        n_keys,
    KeyIterator                         keys,
    OutputIterator                      indices)
{
    thrust::upper_bound(
        keys, keys + n_keys,
        values, values + n,
        indices );
}

// host-wide upper_bound
//
// \param n                    number of input items
// \param values               a system input iterator of values to be searched
// \param keys                 a system input iterator of sorted keys
// \param indices              a system output iterator
//
template <typename KeyIterator, typename ValueIterator, typename OutputIterator>
void upper_bound(
    const host_tag                      tag,
    const uint32                        n,
    ValueIterator                       values,
    const uint32                        n_keys,
    KeyIterator                         keys,
    OutputIterator                      indices)
{
    #pragma omp parallel for
    for (long i = 0; i < long(n); ++i)
        indices[i] = uint32( upper_bound( values[i], keys, n_keys ) - keys );
}

// system-wide upper_bound
//
// \param n                    number of input items
// \param values               a system input iterator of values to be searched
// \param keys                 a system input iterator of sorted keys
// \param indices              a system output iterator
//
template <typename system_tag, typename KeyIterator, typename ValueIterator, typename OutputIterator>
void upper_bound(
    const uint32                        n,
    ValueIterator                       values,
    const uint32                        n_keys,
    KeyIterator                         keys,
    OutputIterator                      indices)
{
    upper_bound(
        system_tag(),
        n,
        values,
        n_keys,
        keys,
        indices );
}

#if defined(__CUDACC__)

// device-wide sort
//
// \param n                    number of input items
// \param keys                 a system input iterator of keys to be sorted
//
template <typename KeyIterator>
void radix_sort(
    const device_tag                    tag,
    const uint32                        n,
    KeyIterator                         keys,
    nvbio::vector<device_tag,uint8>&    temp_storage)
{
    typedef typename std::iterator_traits<KeyIterator>::value_type key_type;

    cuda::alloc_temp_storage( temp_storage, 2 * n * sizeof(key_type) );

    key_type* keys_ptr = reinterpret_cast<key_type*>( raw_pointer( temp_storage ) );

    thrust::device_ptr<key_type> keys_buf( keys_ptr );

    thrust::copy( keys, keys + n, keys_buf );

    cuda::SortBuffers<key_type*> sort_buffers;
    sort_buffers.keys[0] = keys_ptr;
    sort_buffers.keys[1] = keys_ptr + n;

    cuda::SortEnactor sort_enactor;
    sort_enactor.sort( n, sort_buffers );

    thrust::copy(
        keys_buf + sort_buffers.selector * n,
        keys_buf + sort_buffers.selector * n + n,
        keys );
}

// device-wide sort by key
//
// \param n                    number of input items
// \param keys                 a system input iterator of keys to be sorted
// \param values               a system input iterator of values to be sorted
//
template <typename KeyIterator, typename ValueIterator>
void radix_sort(
    const device_tag                    tag,
    const uint32                        n,
    KeyIterator                         keys,
    ValueIterator                       values,
    nvbio::vector<device_tag,uint8>&    temp_storage)
{
    typedef typename std::iterator_traits<KeyIterator>::value_type   key_type;
    typedef typename std::iterator_traits<ValueIterator>::value_type value_type;

    const uint32 aligned_key_bytes = align<16>( 2 * n * sizeof(key_type) );
    const uint32 aligned_val_bytes =            2 * n * sizeof(value_type);
    cuda::alloc_temp_storage( temp_storage, aligned_key_bytes + aligned_val_bytes );

    key_type*     keys_ptr = reinterpret_cast<key_type*>( raw_pointer( temp_storage ) );
    value_type* values_ptr = reinterpret_cast<value_type*>( raw_pointer( temp_storage ) + aligned_key_bytes );

    thrust::device_ptr<key_type> keys_buf( keys_ptr );
    thrust::device_ptr<key_type> values_buf( values_ptr );

    thrust::copy( keys,     keys + n,   keys_buf );
    thrust::copy( values,   values + n, values_buf );

    cuda::SortBuffers<key_type*, value_type*> sort_buffers;
    sort_buffers.keys[0]   = keys_ptr;
    sort_buffers.keys[1]   = keys_ptr + n;
    sort_buffers.values[0] = values_ptr;
    sort_buffers.values[1] = values_ptr + n;

    cuda::SortEnactor sort_enactor;
    sort_enactor.sort( n, sort_buffers );

    thrust::copy(
        keys_buf + sort_buffers.selector * n,
        keys_buf + sort_buffers.selector * n + n,
        keys );

    thrust::copy(
        values_buf + sort_buffers.selector * n,
        values_buf + sort_buffers.selector * n + n,
        values );
}

#endif

// host-wide sort
//
// \param n                    number of input items
// \param keys                 a system input iterator of keys to be sorted
//
template <typename KeyIterator>
void radix_sort(
    const host_tag                      tag,
    const uint32                        n,
    KeyIterator                         keys,
    nvbio::vector<host_tag,uint8>&      temp_storage)
{
    thrust::sort( keys, keys + n );
}

// system-wide sort
//
// \param n                    number of input items
// \param keys                 a system input iterator of keys to be sorted
//
template <typename system_tag, typename KeyIterator>
void radix_sort(
    const uint32                        n,
    KeyIterator                         keys,
    nvbio::vector<system_tag,uint8>&    temp_storage)
{
    radix_sort( system_tag(), n, keys, temp_storage );
}

// host-wide sort by key
//
// \param n                    number of input items
// \param keys                 a system input iterator of keys to be sorted
// \param values               a system input iterator of values to be sorted
//
template <typename KeyIterator, typename ValueIterator>
void radix_sort(
    const host_tag                      tag,
    const uint32                        n,
    KeyIterator                         keys,
    ValueIterator                       values,
    nvbio::vector<host_tag,uint8>&      temp_storage)
{
    thrust::sort_by_key( keys, keys + n, values, temp_storage );
}

// system-wide sort by key
//
// \param n                    number of input items
// \param keys                 a system input iterator of keys to be sorted
// \param values               a system input iterator of values to be sorted
//
template <typename system_tag, typename KeyIterator, typename ValueIterator>
void radix_sort(
    const uint32                        n,
    KeyIterator                         keys,
    ValueIterator                       values,
    nvbio::vector<system_tag,uint8>&    temp_storage)
{
    radix_sort( system_tag(), n, keys, values, temp_storage );
}

template <
    typename key_iterator1,
    typename key_iterator2>
uint2 corank(
    const int32         i,
    const key_iterator1 A,
    const int32         m,
    const key_iterator2 B,
    const int32         n)
{
    int32 j = min( i, m );
    int32 k = i - j;

    int32 j_lo = i >= n ? i - n : 0;
    int32 k_lo = 0;

    while (1)
    {
        if ((j > 0 || k < n) && A[j-1] > B[k])
        {
            // decrease j
            const int32 delta = util::divide_ri( j - j_lo, 2 );
            k_lo = k;
            j -= delta;
            k += delta;
            assert( j + k == i );
        }
        else if ((k > 0 || j < m) && B[k-1] >= A[j])
        {
            // decrease k
            const int32 delta = util::divide_ri( k - k_lo, 2 );
            j_lo = j;
            j += delta;
            k -= delta;
            assert( j + k == i );
        }
        else
            break;
    }
    return make_uint2( uint32(j), uint32(k) );
}

template <
    typename key_iterator1,
    typename key_iterator2,
    typename value_iterator1,
    typename value_iterator2,
    typename key_output,
    typename value_output>
void merge_by_key(
    const host_tag          tag,
    const uint32            A_len,
    const uint32            B_len,
    const key_iterator1     A_keys,
    const key_iterator2     B_keys,
    const value_iterator1   A_values,
    const value_iterator2   B_values,
          key_output        C_keys,
          value_output      C_values)
{
    if (A_len == 0)
    {
        #pragma omp parallel for
        for (int32 i = 0; i < int32( B_len ); ++i)
        {
            C_keys[i]   = A_keys[i];
            C_values[i] = A_values[i];
        }
    }
    else if (B_len == 0)
    {
        #pragma omp parallel for
        for (int32 i = 0; i < int32( A_len ); ++i)
        {
            C_keys[i]   = A_keys[i];
            C_values[i] = A_values[i];
        }
    }

    const uint32 n_threads = (uint32)omp_get_num_procs();
    //const uint32 n_threads = std::thread::hardware_concurrency();

    nvbio::vector<host_tag,uint32> A_diag( n_threads+1 );
    nvbio::vector<host_tag,uint32> B_diag( n_threads+1 );

    const uint32 C_len = A_len + B_len;

    A_diag[ n_threads ] = 0;
    B_diag[ n_threads ] = 0;
    A_diag[ n_threads ] = A_len;
    B_diag[ n_threads ] = B_len;

    const uint32 n_partition = util::divide_ri( C_len, n_threads );

    #pragma omp parallel for num_threads(n_threads)
    for (int32 i = 1; i < int32( n_threads ); ++i)
    {
        const int32 index = i * n_partition;

        const uint2 jk = corank( index, A_keys, A_len, B_keys, B_len );

        A_diag[i] = jk.x;
        B_diag[i] = jk.y;
    }

    #pragma omp parallel for num_threads(n_threads)
    for (int32 i = 0; i < int32( n_threads ); ++i)
    {
        nvbio::merge_by_key(
            A_keys   + A_diag[i],
            A_keys   + A_diag[i+1],
            B_keys   + B_diag[i],
            B_keys   + B_diag[i+1],
            A_values + A_diag[i],
            B_values + B_diag[i],
            C_keys   + i * n_partition,
            C_values + i * n_partition );
    }
/*  for (uint32 i = 1; i < C_len; ++i)
    {
        if (C_keys[i-1] > C_keys[i])
        {
            fprintf(stderr, "merging error at %u: %llu, %llu\n", i, C_keys[i-1], C_keys[i] );
            exit(1);
        }
    }*/
}

template <
    typename key_iterator1,
    typename key_iterator2,
    typename value_iterator1,
    typename value_iterator2,
    typename key_output,
    typename value_output>
void merge_by_key(
    const device_tag        tag,
    const uint32            A_len,
    const uint32            B_len,
    const key_iterator1     A_keys,
    const key_iterator2     B_keys,
    const value_iterator1   A_values,
    const value_iterator2   B_values,
          key_output        C_keys,
          value_output      C_values)
{
    thrust::merge_by_key(
        A_keys,
        A_keys + A_len,
        B_keys,
        B_keys + A_len,
        A_values,
        B_values,
        C_keys,
        C_values );
}

template <
    typename system_tag,
    typename key_iterator1,
    typename key_iterator2,
    typename value_iterator1,
    typename value_iterator2,
    typename key_output,
    typename value_output>
void merge_by_key(
    const uint32                        A_len,
    const uint32                        B_len,
    const key_iterator1                 A_keys,
    const key_iterator2                 B_keys,
    const value_iterator1               A_values,
    const value_iterator2               B_values,
          key_output                    C_keys,
          value_output                  C_values,
    nvbio::vector<system_tag,uint8>&    temp_storage)
{
    merge_by_key(
        system_tag(),
        A_len,
        B_len,
        A_keys,
        B_keys,
        A_values,
        B_values,
        C_keys,
        C_values );
}

#if defined(__CUDACC__)

/// A very simple for_each CUDA kernel
///
template <typename iterator_type, typename functor_type>
__global__
void for_each_kernel(const uint64 n, const iterator_type in, const functor_type f)
{
    const uint32 grid_size = blockDim.x * gridDim.x;

    for (uint64 i = threadIdx.x + blockIdx.x * blockDim.x; i < n; i += grid_size)
        f( in[i] );
};

#endif

// ask the optimizer how many blocks we should try using next
//
template <typename KernelFunction>
uint32 for_each_enactor<device_tag>::suggested_blocks(KernelFunction kernel, const uint32 cta_size) const
{
#if defined(__CUDACC__)
    if (m_blocks_hi == 0)
        return cuda::multiprocessor_count() * cuda::max_active_blocks_per_multiprocessor( kernel, cta_size, 0u );
    else if (m_blocks_lo == 0)
        return cuda::multiprocessor_count();
    else
        return cuda::multiprocessor_count() * (m_blocks_lo + m_blocks_hi) / 2;
#else
    return 0u;
#endif
}

// update the optimizer's internal state with the latest speed data-point
//
inline
void for_each_enactor<device_tag>::update(const uint32 n_blocks, const float speed)
{
#if defined(__CUDACC__)
    // carry out a little binary search over the best number of blocks/SM
    if (m_blocks_hi == 0)
    {
        m_blocks_hi = n_blocks / cuda::multiprocessor_count();
        m_speed_hi  = speed;
    }
    else if (m_blocks_lo == 0)
    {
        m_blocks_lo = n_blocks / cuda::multiprocessor_count();
        m_speed_lo  = speed;
    }
    else if (m_speed_lo > m_speed_hi)
    {
        m_blocks_hi = n_blocks / cuda::multiprocessor_count();
        m_speed_hi  = speed;
    }
    else
    {
        m_blocks_lo = n_blocks / cuda::multiprocessor_count();
        m_speed_lo  = speed;
    }
    // TODO: once the optimizer settles to a given value, it will never change:
    // we should explore using occasional "mutations" to adapt to possibly
    // changing conditions...
#endif
}

// enact the for_each
//
template <typename Iterator, typename Functor>
void for_each_enactor<device_tag>::operator () (
    const uint64            n,
    const Iterator          in,
          Functor           functor)
{
#if defined(__CUDACC__)
    const uint32 blockdim = 128;
    const uint32 n_blocks = suggested_blocks( for_each_kernel<Iterator,Functor>, blockdim );

    cuda::Timer timer;
    timer.start();

    for_each_kernel<<<n_blocks,blockdim>>>( n, in, functor );

    timer.stop();

    update( n_blocks, float(n) / timer.seconds() );
#endif
}

} // namespace nvbio