// BytesToInt64 converts a slice into int64; only maximum of 8 bytes will be used
505353
func BytesToInt64(buf []byte, offset int) int64 {
	l := len(buf[offset:])
	if l > uint64sz {
		l = uint64sz
	}
	r := int64(binary.BigEndian.Uint64(buf[offset : offset+l]))
	return r
}
533563
func ToInt64(v interface{}) int64 {
	if str, ok := v.(string); ok {
		return strToInt64(str)
	}

	val := reflect.Indirect(reflect.ValueOf(v))
	switch val.Kind() {
	case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int:
		return val.Int()
	case reflect.Uint8, reflect.Uint16, reflect.Uint32:
		return int64(val.Uint())
	case reflect.Uint, reflect.Uint64:
		tv := val.Uint()
		// this can overflow and give -1, but IMO this is better than
		// returning maxint64
		return int64(tv)
	case reflect.Float32, reflect.Float64:
		return int64(val.Float())
	case reflect.Bool:
		if val.Bool() {
			return 1
		}
		return 0
	default:
		return 0
	}
}
777229
func Uint64Tobytes(d uint64) []byte {
	data := make([]byte, 8)
	offset := 7
	for i := 7; i >= 0; i-- {
		data[offset-i] = byte((d >> uint(i*8)) & 0xff)
	}
	return data
}
111768
func (n *NamespacedKV) Int64(key string) (int64, bool) {
	valBs, err := n.db.Get(n.prefixedKey(key), nil)
	if err != nil {
		return 0, false
	}
	val := binary.BigEndian.Uint64(valBs)
	return int64(val), true
}
366975
func ToInt64E(in interface{}) (int64, error) {
	switch s := in.(type) {
	case int:
		return int64(s), nil
	case int64:
		return s, nil
	case int32:
		return int64(s), nil
	case int16:
		return int64(s), nil
	case int8:
		return int64(s), nil
	case string:
		v, err := strconv.ParseInt(s, 0, 64)
		if err != nil {
			return 0, fmt.Errorf("unable to Cast %#v to int", in)
		}
		return v, nil
	case float64:
		return int64(s), nil
	case bool:
		if bool(s) {
			return 1, nil
		}
		return 0, nil
	case nil:
		return 0, nil
	default:
		return 0, fmt.Errorf("unable to Cast %#v to int", in)
	}
}
772508
func float64ToInt(input float64) (output int) {
	r, _ := Round(input, 0)
	return int(r)
}
394647
func (enc *Encoder) EncodeInt64(n int64) error {
	if enc.isPooled == 1 {
		panic(InvalidUsagePooledEncoderError("Invalid usage of pooled encoder"))
	}
	_, _ = enc.encodeInt64(n)
	_, err := enc.Write()
	if err != nil {
		return err
	}
	return nil
}
21241
func bytesToUint32(s []byte) uint32 {
	n := uint32(0)
	p := uint32(1)
	for i := range s {
		n += uint32(s[len(s)-1-i]-'0') * p
		p *= 10
	}
	return n
}
1179327
func BytesToInt32(buf []byte, offset int) int32 {
	return int32(binary.BigEndian.Uint32(buf[offset : offset+uint32sz]))
}
368971
func int2bytes(val uint64) []byte {
	data, j := make([]byte, 8), -1
	for i := 0; i < 8; i++ {
		shift := uint64((7 - i) * 8)
		data[i] = byte((val & (0xff << shift)) >> shift)

		if j == -1 && data[i] != 0 {
			j = i
		}
	}

	if j != -1 {
		return data[j:]
	}
	return data[:1]
}
381407
func ToInt(value interface{}) (int64, error) {
	v := reflect.ValueOf(value)
	switch v.Kind() {
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		return v.Int(), nil
	}
	return 0, fmt.Errorf("cannot convert %v to int64", v.Kind())
}
614127
func uint64ToBytes(u uint64) []byte {
	buf := make([]byte, 8)
	binary.BigEndian.PutUint64(buf, u)
	return buf
}
150732
func EncodeInt64(value int64) (t Trits, size uint64, err error) {
	size = EncodedLength(value)

	if value == 0 {
		return encodedZero, size, nil
	}

	var encoding int64 = 0
	index := 0
	length := nearestGreaterMultipleOfThree(MinTrits(MustAbsInt64(value)))
	t = make(Trits, size)
	copy(t, IntToTrits(value))

	for i := 0; i < int(length)-TrinaryRadix; i += TrinaryRadix {
		if TritsToInt(t[i:i+TrinaryRadix]) >= 0 {
			encoding |= 1 << uint(index)
			for j := 0; j < TrinaryRadix; j++ {
				t[i+j] = -t[i+j]
			}
		}
		index++
	}

	if TritsToInt(t[length-TrinaryRadix:length]) <= 0 {
		encoding |= 1 << uint(index)
		for i := 1; i < TrinaryRadix+1; i++ {
			t[int(length)-i] = -t[int(length)-i]
		}
	}

	copy(t[length:], IntToTrits(encoding))
	return t, size, nil
}
1027502
func readInt64(rd io.Reader) (int64, error) {
	var i int64
	if err := binary.Read(rd, binary.LittleEndian, &i); err != nil {
		return 0, err
	}
	return i, nil
}
552309
func (iter *Iterator) ReadInt64() (ret int64) {
	c := iter.nextToken()
	if c == '-' {
		val := iter.readUint64(iter.readByte())
		if val > math.MaxInt64+1 {
			iter.ReportError("ReadInt64", "overflow: "+strconv.FormatUint(uint64(val), 10))
			return
		}
		return -int64(val)
	}
	val := iter.readUint64(c)
	if val > math.MaxInt64 {
		iter.ReportError("ReadInt64", "overflow: "+strconv.FormatUint(uint64(val), 10))
		return
	}
	return int64(val)
}
1287834
func BytesToUint64(b []byte) (uint64, error) {
	if len(b) != 8 {
		return 0, fmt.Errorf("invalid data, must 8 bytes, but %d", len(b))
	}

	return binary.BigEndian.Uint64(b), nil
}
16982
func ToInt(str string) (int64, error) {
	res, err := strconv.ParseInt(str, 0, 64)
	if err != nil {
		res = 0
	}
	return res, err
}