1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2025-02-09 07:30:11 +00:00

Categories: Keep them sorted

* The input from the disk is sorted in-memory, because an admin might want to fiddle with categories.json directly and break the sorted order.
* Insertion/deletion preserve order, thanks to a new dependency.
* The dependency is an official golang package, so I think it's safe to have it.
This commit is contained in:
Timur Ismagilov 2022-04-17 16:07:33 +03:00
parent 8752278f29
commit 6cf59f5e2d
3 changed files with 23 additions and 25 deletions

View File

@ -4,8 +4,10 @@ import (
"encoding/json"
"github.com/bouncepaw/mycorrhiza/files"
"github.com/bouncepaw/mycorrhiza/util"
"golang.org/x/exp/slices"
"log"
"os"
"sort"
"sync"
)
@ -29,6 +31,7 @@ func Init() {
for i, hyphaName := range cat.Hyphae {
cat.Hyphae[i] = util.CanonicalName(hyphaName)
}
sort.Strings(cat.Hyphae)
categoryToHyphae[cat.Name] = &categoryNode{hyphaList: cat.Hyphae}
}
@ -46,49 +49,44 @@ func Init() {
}
type categoryNode struct {
// TODO: ensure this is sorted
hyphaList []string
}
func (cn *categoryNode) storeHypha(hypname string) {
for _, hyphaName := range cn.hyphaList {
if hyphaName == hypname {
return
}
i, found := slices.BinarySearch(cn.hyphaList, hypname)
if found {
return
}
cn.hyphaList = append(cn.hyphaList, hypname)
cn.hyphaList = slices.Insert(cn.hyphaList, i, hypname)
}
func (cn *categoryNode) removeHypha(hypname string) {
for i, hyphaName := range cn.hyphaList {
if hyphaName == hypname {
cn.hyphaList[i] = cn.hyphaList[len(cn.hyphaList)-1]
cn.hyphaList = cn.hyphaList[:len(cn.hyphaList)-1]
}
i, found := slices.BinarySearch(cn.hyphaList, hypname)
if !found {
return
}
cn.hyphaList = slices.Delete(cn.hyphaList, i, i+1)
}
type hyphaNode struct {
// TODO: ensure this is sorted
categoryList []string
}
// inserts sorted
func (hn *hyphaNode) storeCategory(cat string) {
for _, category := range hn.categoryList {
if category == cat {
return
}
i, found := slices.BinarySearch(hn.categoryList, cat)
if found {
return
}
hn.categoryList = append(hn.categoryList, cat)
hn.categoryList = slices.Insert(hn.categoryList, i, cat)
}
func (hn *hyphaNode) removeCategory(cat string) {
for i, category := range hn.categoryList {
if category == cat {
hn.categoryList[i] = hn.categoryList[len(hn.categoryList)-1]
hn.categoryList = hn.categoryList[:len(hn.categoryList)-1]
}
i, found := slices.BinarySearch(hn.categoryList, cat)
if !found {
return
}
hn.categoryList = slices.Delete(hn.categoryList, i, i+1)
}
type catFileRecord struct {

2
go.mod
View File

@ -9,6 +9,7 @@ require (
github.com/gorilla/mux v1.8.0
github.com/valyala/quicktemplate v1.7.0
golang.org/x/crypto v0.0.0-20211108221036-ceb1ce70b4fa
golang.org/x/exp v0.0.0-20220414153411-bcd21879b8fd
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211
golang.org/x/text v0.3.7
)
@ -24,7 +25,6 @@ require (
// but do not commit the change to the path:
// replace github.com/bouncepaw/mycomarkup/v4 v4.0.0 => "/Users/bouncepaw/GolandProjects/mycomarkup"
// Use this utility every time Mycomarkup gets a major update:
// https://github.com/marwan-at-work/mod
// Or maybe just R every time, the utility is kinda weird.

4
go.sum
View File

@ -1,7 +1,5 @@
github.com/andybalholm/brotli v1.0.2/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y=
github.com/andybalholm/brotli v1.0.3/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
github.com/bouncepaw/mycomarkup/v4 v3.6.2 h1:5zqb12aOw19xg8/0QIvgoA8oEW2doSdWqCbXltPEaPQ=
github.com/bouncepaw/mycomarkup/v4 v3.6.2/go.mod h1:BpiGUVsYCgRZCDxF0iIdc08LJokm/Ab36S/Hif0J6D0=
github.com/bouncepaw/mycomarkup/v4 v4.0.0 h1:qokseZ+otcFuQ5vARdvxKqjlEZFMvsjFJ7YpJ4sUr8c=
github.com/bouncepaw/mycomarkup/v4 v4.0.0/go.mod h1:y0b8U6Xfnh3KfNUpG3QuAXRJwqFPPpmS2kYvLzaf688=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
@ -34,6 +32,8 @@ github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7Fw
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8=
golang.org/x/crypto v0.0.0-20211108221036-ceb1ce70b4fa h1:idItI2DDfCokpg0N51B2VtiLdJ4vAuXC9fnCb2gACo4=
golang.org/x/crypto v0.0.0-20211108221036-ceb1ce70b4fa/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/exp v0.0.0-20220414153411-bcd21879b8fd h1:zVFyTKZN/Q7mNRWSs1GOYnHM9NiFSJ54YVRsD0rNWT4=
golang.org/x/exp v0.0.0-20220414153411-bcd21879b8fd/go.mod h1:lgLbSvA5ygNOMpwM/9anMpWVlVJ7Z+cHWq/eFuinpGE=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210510120150-4163338589ed/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=