func fprepend(bytes []byte, casid uint64) Memop {
	return fjoin(bytes, true, casid)
}
fprepend returns a Memop that prepends the given bytes to the value of an
existing key. if casid is non-zero, a cas check will be performed.
package cuckoo
import (
	"errors"
	"fmt"
	"math"
	"strconv"
	"time"
)
type Memval struct {
	Bytes   []byte
	Flags   uint32
	Casid   uint64
	Expires time.Time
}
type MemopResType int
type MemopRes struct {
	T MemopResType
	M *Memval
	E error
}
const (
	STORED       MemopResType = iota
	NOT_STORED                = iota
	EXISTS                    = iota
	NOT_FOUND                 = iota
	CLIENT_ERROR              = iota
	SERVER_ERROR              = -1
)
func (t MemopResType) String() string {
	switch t {
	case STORED:
		return "STORED"
	case NOT_STORED:
		return "NOT_STORED"
	case EXISTS:
		return "EXISTS"
	case NOT_FOUND:
		return "NOT_FOUND"
	case CLIENT_ERROR:
		return "CLIENT_ERROR"
	case SERVER_ERROR:
		return "SERVER_ERROR"
	default:
		panic(fmt.Sprintf("unknown type %d\n", t))
	}
}
type Memop func(Memval, bool) (Memval, MemopRes)
func fset(bytes []byte, flags uint32, expires time.Time) Memop {
	return func(old Memval, _ bool) (m Memval, r MemopRes) {
		m = Memval{bytes, flags, old.Casid + 1, expires}
		r.T = STORED
		r.M = &m
		return
	}
}
func fadd(bytes []byte, flags uint32, expires time.Time) Memop {
	return func(old Memval, exists bool) (m Memval, r MemopRes) {
		r.T = EXISTS
		if !exists {
			m = Memval{bytes, flags, old.Casid + 1, expires}
			r.T = STORED
			r.M = &m
		}
		return
	}
}
func freplace(bytes []byte, flags uint32, expires time.Time) Memop {
	return func(old Memval, exists bool) (m Memval, r MemopRes) {
		r.T = NOT_FOUND
		if exists {
			m = Memval{bytes, flags, old.Casid + 1, expires}
			r.T = STORED
			r.M = &m
		}
		return
	}
}
func fjoin(bytes []byte, prepend bool, casid uint64) Memop {
	return func(old Memval, exists bool) (m Memval, r MemopRes) {
		r.T = NOT_FOUND
		if exists {
			r.T = EXISTS
			if casid == 0 || old.Casid == casid {
				nb := make([]byte, 0, len(old.Bytes)+len(bytes))
				if prepend {
					nb = append(nb, bytes...)
					nb = append(nb, old.Bytes...)
				} else {
					nb = append(nb, old.Bytes...)
					nb = append(nb, bytes...)
				}
				m = Memval{nb, old.Flags, old.Casid + 1, old.Expires}
				r.T = STORED
				r.M = &m
			}
		}
		return
	}
}
func fappend(bytes []byte, casid uint64) Memop {
	return fjoin(bytes, false, casid)
}