void FullContainerKey::setUpperCaseBit(const FullContainerKeyComponents &components,
									   BitArray &upperCaseBit) const {
	upperCaseBit.clear();

	const char8_t *strList[3] = { components.baseName_, components.affinityString_, components.systemPart_ };
	uint32_t lengthList[3] = { components.baseNameSize_, components.affinityStringSize_, components.systemPartSize_ };

	for (size_t i = 0; i < 3; i++) {
		for (size_t j = 0; j < lengthList[i]; j++) {
			upperCaseBit.append(isupper(strList[i][j]) != 0);
		}
	}
}
void Tree::writefile(char *pathIn){
    ifstream file;
    ofstream write;
    string aux;
    string aux2;
    string temporaria;
    int sobra = 0;
    int sizefile = 0;
    BitArray *buffer = new BitArray();

    aux.clear();
    temporaria.clear();
    codificar(root, aux);
    aux.clear();

    file.open(pathIn , ios::in | ios::binary | ios::ate);

    if(file.is_open()){

        int size = file.tellg();
        file.seekg(0, ios::beg);
        int i;
        int iw =1;

        for(i=0; i< size; i++){
            unsigned char c = file.get();
            int j = c;
            aux += codeNodes[j];
            if(aux.length() >= 1024*8){
                sobra = aux.length() - 1024*8;
                temporaria = aux.substr(1024*8, sobra);
                for(int k = 0;k < (aux.length()/8); k++){
                    int n = k*8;
                    aux2 = aux.substr(n, 8);
                    codenode_to_bitarray(aux2, buffer);

                }
                iw++;
                write.open("temp.huff", ios::out | ios::binary | ios::app);
                if(write.is_open()){
                    for(int z = 0;z < aux.length()/8;z++){
                        write << buffer->getArray()[z];
                        sizefile++;
                    }
                }

                write.close();
                aux.clear();
                aux = temporaria;
                buffer->clear();
            }
        }
        int y = (aux.length()/8);
        sobra = aux.length() - (y*8);
        aux2 = aux.substr(y*8,sobra);
        string teste = aux2;


        for(int k = 0;k < (aux.length()/8); k++){
            int n = k*8;
            aux2 = aux.substr(n, 8);
            codenode_to_bitarray(aux2, buffer);

        }

        int u = 8 - sobra;
        sizeTrash = u;
        aux2 = teste;

        for(int i = 0;i < u;i++){
            aux2 += '0';
        }

        codenode_to_bitarray(aux2, buffer);

        sizeLast = (buffer->getCurrent() / 8);
        sizeLast--;

        write.open("temp.huff", ios::out | ios::binary | ios::app);

        if(write.is_open()){
            for(int z=0; z <= sizeLast;z++){
                write << buffer->getArray()[z];

                sizefile++;
            }
        }
        write.close();

        sizeTrash = buffer->trashLenght();


        buffer->clear();

    }
    else cout << "TRASH!in WriteFile" << endl;
    file.close();

}
void FullContainerKey::deserialize(util::StackAllocator &alloc,
								   FullContainerKeyComponents &components,
								   BitArray &upperCaseBit,
								   bool unNormalized) const {
	try {
		if (isEmpty()) {
			GS_THROW_USER_ERROR(GS_ERROR_DS_DS_CONTAINER_NAME_INVALID,
				"container/table name is empty");
		}

		components.clear();
		upperCaseBit.clear();

		ContainerKeyInStream in(util::ArrayInStream(body_, size_));

		const char8_t *normalizedStr[3] = { NULL, NULL, NULL };

		uint8_t flag;
		in >> flag;

		if (flag & DBID_EXISTS) {
			in >> components.dbId_;
		}
		else {
			components.dbId_ = GS_PUBLIC_DB_ID;
		}

		components.baseNameSize_ = decodeVarInt(in);
		normalizedStr[0] = reinterpret_cast<const char8_t*>(body_ + in.base().position());
		in.base().position(in.base().position() + components.baseNameSize_);

		if (flag & LARGE_CONTAINERID_EXISTS) {
			components.largeContainerId_ = decodeVarLong(in);
		}

		if (flag & NODE_AFFINITY_NUM) {
			components.affinityNumber_ = decodeVarLong(in);
		}
		else if (flag & NODE_AFFINITY_STR) {
			components.affinityStringSize_ = decodeVarInt(in);
			normalizedStr[1] = reinterpret_cast<const char8_t*>(body_ + in.base().position());
			in.base().position(in.base().position() + components.affinityStringSize_);
		}

		if (flag & SYSTEM_PART_ID_NUM) {
			components.systemPartId_ = decodeVarLong(in);
		}
		else if (flag & SYSTEM_PART_ID_STR) {
			components.systemPartSize_ = decodeVarInt(in);
			normalizedStr[2] = reinterpret_cast<const char8_t*>(body_ + in.base().position());
			in.base().position(in.base().position() + components.systemPartSize_);
		}

		const uint64_t strLength = components.baseNameSize_
			+ components.affinityStringSize_
			+ components.systemPartSize_;

		if (in.base().remaining() != strLengthToBitLength(strLength)) {
			GS_THROW_USER_ERROR(GS_ERROR_DS_DS_CONTAINER_NAME_INVALID,
				"size of container/table name is invalid");
		}
		else {
			upperCaseBit.reserve(strLength);
			upperCaseBit.putAll(body_ + in.base().position(), strLength);
			in.base().position(in.base().position() + in.base().remaining());
		}

		if (in.base().position() != size_) {
			GS_THROW_USER_ERROR(GS_ERROR_DS_DS_CONTAINER_NAME_INVALID,
				"size of container/table name is invalid");
		}


		if (unNormalized) {
			util::XArray<char8_t> buf(alloc);
			buf.resize(static_cast<size_t>(strLength), '\0');
			uint64_t startPos = 0;

			createOriginalString(normalizedStr[0], components.baseNameSize_,
				buf.data()+startPos, upperCaseBit, startPos);
			components.baseName_ = buf.data()+startPos;
			startPos += components.baseNameSize_;

			if (components.affinityStringSize_ > 0) {
				createOriginalString(normalizedStr[1], components.affinityStringSize_,
					buf.data()+startPos, upperCaseBit, startPos);
				components.affinityString_ = buf.data()+startPos;
				startPos += components.affinityStringSize_;
			}

			if (components.systemPartSize_ > 0) {
				createOriginalString(normalizedStr[2], components.systemPartSize_,
					buf.data()+startPos, upperCaseBit, startPos);
				components.systemPart_ = buf.data()+startPos;
				startPos += components.systemPartSize_;
			}
		}
		else {
			components.baseName_ = normalizedStr[0];
			if (components.affinityStringSize_ > 0) {
				components.affinityString_ = normalizedStr[1];
			}
			if (components.systemPartSize_ > 0) {
				components.systemPart_ = normalizedStr[2];
			}
		}

	}