Source

go-extra / fuse / ilmap.got

Full commit
package fuse(type t)

import (
	   "rand"
	   "sync"
	   )

type ilmap struct {
	sync.Mutex
	arr []t
}

func NewIlmap(size int) (res *ilmap) {
	res     = new(ilmap)
	res.arr = make([]t, size)
	return
}

func (i *ilmap)Put(v t) (key int) {
	i.Lock()
	defer i.Unlock()
	for {
		key  = int(rand.Int31()) % len(i.arr)
		old := i.arr[key]
		if old==nil {
			i.arr[key] = v
			break
		}
	}
	return
}

func (i *ilmap)Del(key int) {
	i.Lock()
	defer i.Unlock()
	i.arr[key%len(i.arr)] = nil
}

func (i *ilmap)Get(key int) t {
	return i.arr[key]
}

func (i *ilmap)Set(key int, val t) {
	i.Lock()
	defer i.Unlock()
	i.arr[key] = val
}