bool ternary::encode(const int_vector &v, int_vector &z){
	z.setIntWidth( v.getIntWidth() );
	size_t z_bit_size = 0;
	for(typename int_vector::const_iterator it = v.begin(), end = v.end(); it != end; ++it){
		z_bit_size += encoding_length(*it);
	}
	z.bit_resize( z_bit_size ); // Initial size of z
	if( z_bit_size & 0x3F ){ // if z_bit_size % 64 != 0
		*(z.m_data + (z_bit_size>>6)) = 0; // initialize last word
	}
		/*! 
		 * Constructor for building the Index
		 * \param[in] str C-string of the text
		 */
		index_bidirectional_waveletindex(const unsigned char* str) : index() {

			size_t n = strlen((const char*)str);
			int_vector<> sa(n+1, 0, bit_magic::l1BP(n+1)+1);
			setText(str, n+1);

			unsigned char *bwt = new unsigned char[n+1];

			algorithm::calculate_sa(str, n+1, sa);   // calculate the suffix array sa of str

			{ /* Calculate Burrows-Wheeler-Transform */
				size_t i = 0;
				for(int_vector<>::const_iterator it = sa.begin(), end = sa.end(); it != end; ++it, ++i){
					bwt[i] = m_char2comp[str[(*it+n)%(n+1)]];
				}
			}

			backward_index = WaveletTree(bwt, n+1, m_sigma);

			/* Construct the SA-Samples */
			m_sa_sample.setIntWidth( bit_magic::l1BP(sa.size())+1 );
			m_sa_sample.resize( (sa.size()+SampleDens-1)/SampleDens );
			size_t idx=0;
			size_t i=(sa.size()-1-SampleDens*(m_sa_sample.size()-1));
			for(int_vector<>::const_iterator it = sa.begin()+(ptrdiff_t)i; i < sa.size(); it += (ptrdiff_t)SampleDens, i += SampleDens, ++idx){
				m_sa_sample[idx] = *it;
			} 

			unsigned char* reverse = new unsigned char[n+1];
			for (size_t i=0; i<n; i++) reverse[i] = str[n-1-i];
			reverse[n] = '\0';

			algorithm::calculate_sa(reverse, n+1, sa);   // calculate the suffix array sa of reverse string str

			{ /* Calculate Burrows-Wheeler-Transform */
				size_t i = 0;
				for(int_vector<>::const_iterator it = sa.begin(), end = sa.end(); it != end; ++it, ++i){
					bwt[i] = m_char2comp[reverse[(*it+n)%(n+1)]];
				}
			}

			forward_index = WaveletTree(bwt, n+1, m_sigma);

			delete [] bwt;
			delete [] reverse;

		}
    /*!
     * Constructor for building the Index
     * \param[in] str C-string of the text
     */
    index_csa_psi_text(const unsigned char *str) : index() {

        size_t n = strlen((const char*)str);
        int_vector<> sa(n+1, 0, bit_magic::l1BP(n+1)+1);
        algorithm::calculate_sa(str, n+1, sa);   // calculate the suffix array sa of str
        int_vector<> m_psi;
        sdsl::algorithm::sa2psi(sa, m_psi);
        psi = EncVector(m_psi);
        setText(str, n+1);

        text = int_vector<>(sa.size(), 0, bit_magic::l1BP(sigma)+1);
        for (size_t i=0; i<sa.size(); i++) text[i] = char2comp[str[i]];

        /* Construct the SA-Samples */
        m_sa_sample.setIntWidth( bit_magic::l1BP(sa.size())+1 );
        m_sa_sample.resize( (sa.size()+SampleDens-1)/SampleDens );
        size_t i=0, idx=0;
        for(int_vector<>::const_iterator it = sa.begin(); i < sa.size(); it += (ptrdiff_t)SampleDens, i += SampleDens, ++idx) {
            m_sa_sample[idx] = *it;
        }
    }