From 96a9c350af65eb80d003bda7fba292b3b67f41e7 Mon Sep 17 00:00:00 2001 From: psi Date: Wed, 2 May 2018 19:26:54 +0900 Subject: [PATCH] =?UTF-8?q?=E8=BF=94=E3=81=99=E3=81=A8=E3=81=8D=E3=81=AE?= =?UTF-8?q?=E3=82=A2=E3=83=AB=E3=82=B4=E3=83=AA=E3=82=BA=E3=83=A0=E3=82=92?= =?UTF-8?q?=E3=81=A7=E3=81=8D=E3=82=8B=E3=81=A0=E3=81=91=E5=81=8F=E3=82=89?= =?UTF-8?q?=E3=81=AA=E3=81=84=E3=82=88=E3=81=86=E3=81=AB=E3=81=97=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- web/moment.go | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/web/moment.go b/web/moment.go index 738da4b..1682f8c 100644 --- a/web/moment.go +++ b/web/moment.go @@ -84,13 +84,32 @@ func (srv *Web) serveMomentSearch(w http.ResponseWriter, r *http.Request, _ http size = 100 return } + pi2 := math.Pi * 2.0 + lst := make([][]*momentSummary, size) orig := srv.moments.AsSlice() + if size > len(orig) { + size = len(orig) + } + for _, v := range rand.Perm(len(orig)) { + s := srv.makeSummary(orig[v]) + i := int(math.Floor(s.Angle * float64(size) / pi2)) + lst[i] = append(lst[i], s) + } out := make([]*momentSummary, size) - for i, v := range rand.Perm(len(orig)) { - if i >= size { - break + cnt := 0 + step := 0 +end: + for cnt < size { + for _, v := range lst { + if step < len(v) { + out[cnt] = v[step] + cnt++ + if cnt >= size { + break end + } + } } - out[i] = srv.makeSummary(orig[v]) + step++ } body, err := json.MarshalIndent(out, "", " ") if err != nil {