package server import ( "strconv" "sort" "fmt" "io" ) type autokey struct { flag bool n int v string } type automap map[string]interface{} type autokeys []autokey func (t autokey) String() string { return t.v } func (t autokeys) Len() int { return len(t) } func (t autokeys) Less(i, j int) bool { a := &t[i] b := &t[j] if a.flag && b.flag { return a.n < b.n } if !a.flag && !b.flag { return a.v < b.v } return b.flag } func (t autokeys) Swap(i, j int) { t[i], t[j] = t[j], t[i] } func (m automap) add(v interface{}) { for n := 1;; n++ { k := fmt.Sprint(n) if _, ok := m[k]; !ok { m[k] = v break } } } func (m automap) del(k string) bool { if _, ok := m[k]; !ok { return false } delete(m, k) return true } func (m automap) rename(old string, new string) bool { if _, ok := m[old]; !ok { return false } if _, ok := m[new]; ok { return false } m[new] = m[old] delete(m, old) return true } func (m automap) show(w io.Writer) { var keys autokeys for k, _ := range m { t := autokey{v: k} n, err := strconv.Atoi(k) if err == nil { t.flag = true t.n = n } keys = append(keys, t) } sort.Sort(keys) for _, k := range keys { fmt.Fprintln(w, k, m[k.v]) } }