func()

in qf.go [319:405]


func (qf *Filter) insertByHash(dq, dr, value uint64) bool {
	sd := qf.read(dq)

	// case 1, the slot is empty
	if sd.empty() {
		qf.entries++
		sd.setOccupied(true)
		sd.setR(dr)
		qf.write(uint64(dq), sd)
		if qf.storage != nil {
			qf.storage.Set(dq, value)
		}
		return false
	}

	// if the occupied bit is set for this dq, then we are
	// extending an existing run
	extendingRun := sd.occupied()

	// mark occupied if we are not extending a run
	if !extendingRun {
		sd.setOccupied(true)
		qf.write(dq, sd)
	}

	// ok, let's find the start
	runStart := dq
	if sd.shifted() {
		runStart = findStart(dq, qf.size, qf.filter.Get)
	}
	// now let's find the spot within the run
	slot := runStart
	if extendingRun {
		sd = qf.read(slot)
		for {
			if sd.empty() || sd.r() >= dr {
				break
			}
			right(&slot, qf.size)
			sd = qf.read(slot)
			if !sd.continuation() {
				break
			}
		}
	}

	// case 2, the value is already in the filter
	if dr == sd.r() {
		// update value
		if qf.storage != nil {
			qf.storage.Set(slot, value)
		}
		return true
	}
	qf.entries++

	// case 3: we have to insert into an existing run
	// we are writing remainder <dr> into <slot>
	shifted := (slot != uint64(dq))
	continuation := slot != runStart

	for {
		// dr -> the remainder to write here
		if qf.storage != nil {
			value = qf.storage.Swap(slot, value)
		}
		var new slotData
		new.setShifted(shifted)
		new.setContinuation(continuation)
		old := qf.read(slot)
		new.setOccupied(old.occupied())
		new.setR(dr)
		qf.write(slot, new)
		if old.empty() {
			break
		}
		if ((slot == runStart) && extendingRun) || old.continuation() {
			continuation = true
		} else {
			continuation = false
		}
		dr = old.r()
		right(&slot, qf.size)
		shifted = true
	}
	return false
}