func (m *interfaceMap) store(key interface{}) bool {
	for i := range m.data {
		if reflect.DeepEqual(key, m.data[i]) {
			m.counter[i]++
			return m.counter[i] > m.threshold
		}
	}
	m.data = append(m.data, key)
	m.counter = append(m.counter, 1)
	return 1 > m.threshold
}
store increments the counter by 1 if we already have the same interface,
otherwise it store the new key with a counter starting from 1.
todo : bool doesn't mean whether the store operation succeed which can be confusing.
package dispatcher
import (
	"context"
	"reflect"
	"github.com/renproject/darknode/jsonrpc"
	"github.com/sirupsen/logrus"
)
type Iterator interface {
	Collect(id interface{}, cancel context.CancelFunc, responses <-chan jsonrpc.Response) jsonrpc.Response
}
type firstResponseIterator struct {
	responses *interfaceMap
}
func NewFirstResponseIterator() Iterator {
	return firstResponseIterator{}
}
func (iter firstResponseIterator) Collect(id interface{}, cancel context.CancelFunc, responses <-chan jsonrpc.Response) jsonrpc.Response {
	iter.responses = newInterfaceMap(cap(responses))
	defer cancel()
	for response := range responses {
		if response.Error == nil {
			return response
		} else {
			iter.responses.store(response)
		}
	}
	most := iter.responses.most()
	if most == nil {
		jsonErr := jsonrpc.NewError(jsonrpc.ErrorCodeInternal, "unable to query the network", nil)
		return jsonrpc.NewResponse(id, nil, &jsonErr)
	}
	return most.(jsonrpc.Response)
}
type majorityResponseIterator struct {
	logger    logrus.FieldLogger
	responses *interfaceMap
}
func NewMajorityResponseIterator(logger logrus.FieldLogger) Iterator {
	return majorityResponseIterator{
		logger: logger,
	}
}
func (iter majorityResponseIterator) Collect(id interface{}, cancel context.CancelFunc, responses <-chan jsonrpc.Response) jsonrpc.Response {
	iter.responses = newInterfaceMap(cap(responses))
	defer cancel()
	for response := range responses {
		if ok := iter.responses.store(response); ok {
			return response
		}
	}
	most := iter.responses.most()
	if most == nil {
		jsonErr := jsonrpc.NewError(jsonrpc.ErrorCodeInternal, "unable to query the network", nil)
		return jsonrpc.NewResponse(id, nil, &jsonErr)
	}
	return most.(jsonrpc.Response)
}
type interfaceMap struct {
	threshold int
	counter   []int
	data      []interface{}
}
func newInterfaceMap(total int) *interfaceMap {
	return &interfaceMap{
		threshold: (total - 1) / 3 * 2,
		counter:   make([]int, 0, total),
		data:      make([]interface{}, 0, total),
	}
}