Пример #1
0
func (c *Collection) Put(key *engine.ObjectId, value *engine.ObjectIndex) bool {

	h := hash(key.Hash())
	i := indexFor(h, c.capacity)
	for e := c.table[i]; e != nil; e = e.next {
		if e.hashCode == h && e.key.Equals(key) {
			e.value = value
			return true
		}
	}
	ne := &entry{
		hashCode: h,
		key:      key,
		value:    value,
		next:     c.table[i],
	}
	c.table[i] = ne

	c.size++
	if c.size >= c.threshold {
		c.resize(2 * c.capacity)
	}
	return true

}
Пример #2
0
// Returns the find result & value to which the specified key is mapped,
// or nil if this map contains no mapping for the key.
//
// More formally, if this map contains a mapping from a key
// to a value such that (key==null ? k==null :
// key.equals(k)), then this method returns v; otherwise
// it returns nil.  (There can be at most one such mapping.)
func (c Collection) Get(key *engine.ObjectId) (*engine.ObjectIndex, bool) {
	//if key == nil {
	//	return c.getForNullKey()
	//}
	h := hash(key.Hash())

	for e := c.table[indexFor(h, c.capacity)]; e != nil; e = e.next {
		if e.hashCode == h && e.key.Equals(key) {
			return e.value, true
		}
	}
	return nil, false
}
Пример #3
0
//GetById
func (c Collection) GetById(id *engine.ObjectId) (*node.Node, bool) {

	hash := hash(id.Hash())
	es := c.table[indexFor(hash, c.capacity)]
	if es == nil {
		return nil, false
	}
	for e := es.head; e != nil; e = e.next {
		if e.value.Id.Equals(id) {
			return e.value, true
		}
	}
	return nil, false
}
Пример #4
0
// Returns the entry associated with the specified key in the
// Collection.  Returns nil if the Collection contains no mapping
// for the key.
func (c Collection) getEntry(key *engine.ObjectId) *entry {
	//hashCode := 0
	//if key != nil {
	//	hashCode = hash(key.HashCode())
	//}
	h := hash(key.Hash())

	for e := c.table[indexFor(h, c.capacity)]; e != nil; e = e.next {
		if e.hashCode == h && e.key.Equals(key) {
			return e
		}

	}
	return nil
}
Пример #5
0
// Removes and returns the entry associated with the specified key
// in the Collection.
//
// Returns if there was a mapping for key and the associated entry
func (c Collection) removeEntryForKey(key *engine.ObjectId) (*entry, bool) {
	//hashCode := 0
	//if key != nil {
	//hashCode = hash(key.HashCode())
	//}
	h := hash(key.Hash())
	i := indexFor(h, c.capacity)
	pre := c.table[i]
	for e := c.table[i]; e != nil; e = e.next {
		if e.hashCode == h && e.key.Equals(key) {
			pre.next = e.next
			c.size--
		} //if
		pre = e
	}
	return nil, false
}
Пример #6
0
func (c *Collection) Add(id *engine.ObjectId) (*node.Node, bool) {
	print(id.Value, "\t")
	println(c.size, 1111111111)
	h := hash(id.Hash())
	key := engine.NewObjectIndex(id.Hash())
	i := indexFor(h, c.capacity)
	es := c.table[i]
	if es == nil {

		es = &entrys{
			size: 0,
		}
		var counter uint32 = (es.size + 1) << 16
		r := rand.New(rand.NewSource(time.Now().UnixNano()))
		counter += uint32(r.Int31n(1<<16 - 1))
		key.SetRand(counter)

		n := node.New(id)
		n.Index = key

		ne := &entry{
			hashCode: h,
			key:      key.LocalIndex(),
			value:    n,
			next:     nil,
		}
		es.head = ne
		c.table[i] = es

		c.size++
		if c.size >= c.threshold {
			c.resize(2 * c.capacity)
		}
		return n, true
	} else {
		for e := es.head; e != nil; e = e.next {
			if e.value.Id.Equals(id) {
				return e.value, false
			}
		}
		var counter uint32 = (es.size + 1) << 16
		r := rand.New(rand.NewSource(time.Now().UnixNano()))
		counter += uint32(r.Int31n(1<<16 - 1))
		key.SetRand(counter)

		n := node.New(id)
		n.Index = key

		ne := &entry{
			hashCode: h,
			key:      key.LocalIndex(),
			value:    n,
			next:     es.head,
		}
		es.head = ne
		es.size++

		c.size++
		if c.size >= c.threshold {
			c.resize(2 * c.capacity)
		}
		return n, true
	}

}