Skip to content

Commit

Permalink
feat: get group member with binary search
Browse files Browse the repository at this point in the history
  • Loading branch information
wdvxdr1123 committed Apr 7, 2021
1 parent 5ec6c65 commit cde0aab
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 11 deletions.
1 change: 1 addition & 0 deletions client/c2c_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ func troopAddMemberBroadcastDecoder(c *QQClient, pMsg *msg.Message, _ *incomingP
}
group.Update(func(info *GroupInfo) {
info.Members = append(info.Members, mem)
info.sort()
})
c.dispatchNewMemberEvent(&MemberJoinGroupEvent{
Group: group,
Expand Down
14 changes: 9 additions & 5 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,9 @@ func (c *QQClient) GetGroupMembers(group *GroupInfo) ([]*GroupMemberInfo, error)
}
list = append(list, rsp.list...)
if nextUin == 0 {
sort.Slice(list, func(i, j int) bool {
return list[i].Uin < list[j].Uin
})
return list, nil
}
}
Expand Down Expand Up @@ -813,12 +816,13 @@ func (c *QQClient) kickGroupMember(groupCode, memberUin int64, msg string, block

func (g *GroupInfo) removeMember(uin int64) {
g.Update(func(info *GroupInfo) {
for i, m := range info.Members {
if m.Uin == uin {
info.Members = append(info.Members[:i], info.Members[i+1:]...)
break
}
i := sort.Search(len(info.Members), func(i int) bool {
return info.Members[i].Uin >= uin
})
if i >= len(info.Members) || info.Members[i].Uin != uin { // not found
return
}
info.Members = append(info.Members[:i], info.Members[i+1:]...)
})
}

Expand Down
20 changes: 14 additions & 6 deletions client/group_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"math/rand"
"net/url"
"sort"
"strings"
"sync"

Expand Down Expand Up @@ -329,13 +330,20 @@ func (g *GroupInfo) FindMember(uin int64) *GroupMemberInfo {
}

func (g *GroupInfo) FindMemberWithoutLock(uin int64) *GroupMemberInfo {
for _, m := range g.Members {
f := m
if f.Uin == uin {
return f
}
i := sort.Search(len(g.Members), func(i int) bool {
return g.Members[i].Uin >= uin
})
if i >= len(g.Members) || g.Members[i].Uin != uin {
return nil
}
return nil
return g.Members[i]
}

// sort call this method must hold the lock
func (g *GroupInfo) sort() {
sort.Slice(g.Members, func(i, j int) bool {
return g.Members[i].Uin < g.Members[j].Uin
})
}

func (g *GroupInfo) Update(f func(*GroupInfo)) {
Expand Down
1 change: 1 addition & 0 deletions client/group_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,7 @@ func (c *QQClient) parseGroupMessage(m *msg.Message) *message.GroupMessage {
}
mem = info
group.Members = append(group.Members, mem)
group.sort()
go c.dispatchNewMemberEvent(&MemberJoinGroupEvent{
Group: group,
Member: info,
Expand Down

0 comments on commit cde0aab

Please sign in to comment.