Exemple #1
0
std::pair<size_t, bool>
ZipIntKeyIndex::IntVecLowerBound(fstring binkey) const {
	assert(binkey.size() == sizeof(Int));
	Int rawkey = unaligned_load<Int>(binkey.data());
	if (rawkey < Int(m_minKey)) {
		return std::make_pair(0, false);
	}
	auto indexData = m_index.data();
	auto indexBits = m_index.uintbits();
	auto indexMask = m_index.uintmask();
	auto keysData = m_keys.data();
	auto keysBits = m_keys.uintbits();
	auto keysMask = m_keys.uintmask();
	size_t key = size_t(rawkey - Int(m_minKey));
	size_t hitPos = 0;
	size_t hitKey = 0;
	size_t i = 0, j = m_index.size();
	while (i < j) {
		size_t mid = (i + j) / 2;
		hitPos = UintVecMin0::fast_get(indexData, indexBits, indexMask, mid);
		hitKey = UintVecMin0::fast_get(keysData, keysBits, keysMask, hitPos);
		if (hitKey < key)
			i = mid + 1;
		else
			j = mid;
	}
	if (i < m_index.size()) {
		hitPos = UintVecMin0::fast_get(indexData, indexBits, indexMask, i);
		hitKey = UintVecMin0::fast_get(keysData, keysBits, keysMask, hitPos);
		return std::make_pair(i, key == hitKey);
	}
	return std::make_pair(i, false);
}
Exemple #2
0
std::pair<size_t, size_t>
ZipIntKeyIndex::IntVecEqualRange(fstring binkey) const {
	assert(binkey.size() == sizeof(Int));
	Int rawkey = unaligned_load<Int>(binkey.data());
	if (rawkey < Int(m_minKey)) {
		return std::make_pair(0, false);
	}
	auto indexData = m_index.data();
	auto indexBits = m_index.uintbits();
	auto indexMask = m_index.uintmask();
	auto keysData = m_keys.data();
	auto keysBits = m_keys.uintbits();
	auto keysMask = m_keys.uintmask();
	size_t key = size_t(rawkey - Int(m_minKey));
	size_t i = 0, j = m_index.size();
	size_t mid = 0;
	while (i < j) {
		mid = (i + j) / 2;
		size_t hitPos = UintVecMin0::fast_get(indexData, indexBits, indexMask, mid);
		size_t hitKey = UintVecMin0::fast_get(keysData, keysBits, keysMask, hitPos);
		if (hitKey < key)
			i = mid + 1;
		else if (hitKey > key)
			j = mid;
		else
			goto Found;
	}
	return std::make_pair(i, i);
Found:
	size_t lo = i, hi = mid;
	while (lo < hi) {
		size_t mid2 = (lo + hi) / 2;
		size_t hitPos = UintVecMin0::fast_get(indexData, indexBits, indexMask, mid2);
		size_t hitKey = UintVecMin0::fast_get(keysData, keysBits, keysMask, hitPos);
		if (hitKey < key) // for lower_bound
			lo = mid2 + 1;
		else
			hi = mid2;
	}
	i = lo;
	lo = mid + 1, hi = j;
	while (lo < hi) {
		size_t mid2 = (lo + hi) / 2;
		size_t hitPos = UintVecMin0::fast_get(indexData, indexBits, indexMask, mid2);
		size_t hitKey = UintVecMin0::fast_get(keysData, keysBits, keysMask, hitPos);
		if (hitKey <= key) // for upper_bound
			lo = mid2 + 1;
		else
			hi = mid2;
	}
	return std::make_pair(i, hi);
}
	void operator()(int len, int idx, fstring value) {
		if (strnlen(value.p, value.n) < value.size()) {
			printf("%-20.*s idx=%08d bin=", len, text, idx);
			for (int i = 0; i < value.n; ++i)
				printf("%02X", (byte_t)value.p[i]);
			printf("\n");
		}
		else {
			printf("%-20.*s idx=%08d val=%.*s\n"
				, len, text, idx, value.ilen(), value.data());
		}
		klen = len;
	}
	size_t split_by_any(fstring delims, Vec* F, size_t max_fields = ~size_t(0)) {
		size_t dlen = delims.size();
		if (0 == dlen) // empty delims redirect to blank delim
			return split(' ', F);
		if (1 == dlen)
			return split(delims[0], F);
		F->resize(0);
		char *col = p, *end = p + n;
		while (col <= end && F->size()+1 < max_fields) {
			char* next = col;
			while (next < end && memchr(delims.data(), *next, dlen) == NULL) ++next;
			F->push_back(typename Vec::value_type(col, next));
			*next = 0;
			col = next + 1;
		}
		if (col <= end)
			F->push_back(typename Vec::value_type(col, end));
		return F->size();
	}
	size_t split(fstring delims, Vec* F, size_t max_fields = ~size_t(0)) {
		size_t dlen = delims.size();
		if (0 == dlen) // empty delims redirect to blank delim
			return split(' ', F);
		if (1 == dlen)
			return split(delims[0], F);
		F->resize(0);
		char *col = p, *end = p + n;
		while (col <= end && F->size()+1 < max_fields) {
			char* next = (char*)memmem(col, end-col, delims.data(), dlen);
			if (NULL == next) next = end;
			F->push_back(typename Vec::value_type(col, next));
			*next = 0;
			col = next + dlen;
		}
		if (col <= end)
			F->push_back(typename Vec::value_type(col, end));
		return F->size();
	}