{{ define "main" }}
{{- /*gotype: github.com/go-faster/jx/tools/mkint.Config*/ -}}
// Code generated by mkint, DO NOT EDIT.

package {{ $.PackageName }}

var digits []uint32

func init() {
	digits = make([]uint32, 1000)
    for i := uint32(0); i < 1000; i++ {
    	digits[i] = (((i / 100) + '0') << 16) + ((((i / 10) % 10) + '0') << 8) + i%10 + '0'
    	if i < 10 {
			digits[i] += 2 << 24
		} else if i < 100 {
			digits[i] += 1 << 24
		}
	}
}

func writeFirstBuf(w *Writer, v uint32) bool {
	r := make([]byte, 0, 4)
	start := v >> 24
	if start == 0 {
		r = append(r, byte(v>>16), byte(v>>8))
	} else if start == 1 {
		r = append(r, byte(v>>8))
	}
	r = append(r, byte(v))
	return writeStreamBytes(w, r...)
}

func writeBuf(w *Writer, v uint32) bool {
	return writeStreamBytes(w, byte(v>>16), byte(v>>8), byte(v))
}

{{ range $typ := $.Types }}
    {{ template "encode_uint" $typ }}
    {{ template "encode_int" $typ }}
{{- end }}

{{ end }}

{{ define "encode_uint" }}
{{- /*gotype: github.com/go-faster/jx/tools/mkint.IntType */ -}}
// U{{ title $.Name }} encodes u{{ $.Name }}.
func (w *Writer) U{{ title $.Name }}(v u{{ $.Name }}) (fail bool) {
	q0 := v
	{{- range $i, $_ := times $.EncoderIterations }}
	// Iteration {{ $i }}.
	{{- if not (eq $i 0) }}
	r{{$i}} := q{{add $i -1}} - q{{$i}}*1000
	{{- end }}

	{{- if eq $i (sub $.EncoderIterations 1) }}
	fail = fail || writeFirstBuf(w, digits[q{{$i}}])
	{{- range $r, $_ := times $i }}
	fail = fail || writeBuf(w, digits[r{{sub $i $r}}])
	{{- end }}
	{{- else }}
	q{{add $i 1}} := q{{$i}} / 1000
	if q{{add $i 1}} == 0 {
		fail = fail || writeFirstBuf(w, digits[q{{$i}}])
		{{- range $r, $_ := times $i }}
		fail = fail || writeBuf(w, digits[r{{sub $i $r}}])
		{{- end }}
		return fail
	}
	{{- end }}
	{{- end }}
	return fail
}

// U{{ title $.Name }} encodes u{{ $.Name }}.
func (e *Encoder) U{{ title $.Name }}(v u{{ $.Name }}) bool {
   	return e.comma() || e.w.U{{ title $.Name }}(v)
}
{{ end }}

{{ define "encode_int" }}
{{- /*gotype: github.com/go-faster/jx/tools/mkint.IntType */ -}}
// {{ title $.Name }} encodes {{ $.Name }}.
func (w *Writer) {{ title $.Name }}(v {{ $.Name }}) (fail bool) {
	var val u{{ $.Name }}
	if v < 0 {
		val = u{{ $.Name }}(-v)
		fail = w.byte('-')
	} else {
		val = u{{ $.Name }}(v)
	}
	return fail || w.U{{ title $.Name }}(val)
}

// {{ title $.Name }} encodes {{ $.Name }}.
func (e *Encoder) {{ title $.Name }}(v {{ $.Name }}) bool {
	return e.comma() || e.w.{{ title $.Name }}(v)
}
{{ end }}
