英文:
Append slice to slice of slices
问题
我有一个数据结构:
type PosList []int
type InvertedIndex struct {
Capacity int
Len int
IndexList []PosList
}
我在Add方法上遇到了问题:
func (ii *InvertedIndex) Add(posList PosList, docId int) {
if ii.Len == ii.Capacity {
newIndexList := make([]PosList, ii.Len, (ii.Capacity+1)*2)
for i := 0; i < ii.Len; i++ {
newIndexList[i] = make([]int, len(ii.IndexList[i]))
copy(newIndexList[i], ii.IndexList[i])
}
ii.IndexList = newIndexList
}
ii.IndexList = ii.IndexList[0 : ii.Len+2]
ii.IndexList[docId] = posList
return
}
或者,我尝试了这样的方法:
func (ii *InvertedIndex) Add(posList PosList, docId int) {
if ii.Len == ii.Capacity {
newIndexList := make([]PosList, ii.Len, (ii.Capacity+1)*2)
copy(newIndexList, ii.IndexList)
ii.IndexList = newIndexList
}
ii.IndexList = ii.IndexList[0 : ii.Len+2]
ii.IndexList[docId] = posList
return
}
这两种方法都不起作用,也许有人可以解释一下如何将一个切片追加到这样的结构中。
英文:
I have data structure:
type PosList []int
type InvertedIndex struct {
Capacity int
Len int
IndexList []PosList
}
I have problem with Add method:
func (ii *InvertedIndex) Add(posList PosList, docId int) {
if ii.Len == ii.Capacity {
newIndexList := make([]PosList, ii.Len, (ii.Capacity+1)*2)
for i := 0; i < ii.Len; i++ {
newIndexList[i] = make([]int, len(ii.IndexList[i]))
copy(newIndexList[i], ii.IndexList[i])
}
ii.IndexList = newIndexList
}
ii.IndexList = ii.IndexList[0 : ii.Len+2]
ii.IndexList[docId] = posList
return
}
Or, i try something like this:
func (ii *InvertedIndex) Add(posList PosList, docId int) {
if ii.Len == ii.Capacity {
newIndexList := make([]PosList, ii.Len, (ii.Capacity+1)*2)
copy(newIndexList, ii.IndexList)
ii.IndexList = newIndexList
}
ii.IndexList = ii.IndexList[0 : ii.Len+2]
ii.IndexList[docId] = posList
return
}
Both of them don't work, may be someone can explain how can i append a slice to structure like this.
答案1
得分: 0
我不确定我完全理解你在做什么,但是像这样的代码应该可以正常工作,将 slice
替换为 map
:
type PosList []int
type InvertedIndex struct {
Len int
IndexList map[int]PosList
}
func (ii *InvertedIndex) Add(posList PosList, docId int) {
if ii.IndexList == nil {
ii.IndexList = make(map[int]PosList)
}
if _, ok := ii.IndexList[docId]; ok {
ii.IndexList[docId] = append(ii.IndexList[docId], posList...)
} else {
ii.IndexList[docId] = posList
}
ii.Len = len(ii.IndexList)
}
这段代码将 PosList
类型定义为一个整数切片,InvertedIndex
结构体包含一个整数 Len
和一个映射 IndexList
,映射的键是整数,值是 PosList
类型。Add
方法用于向 IndexList
中添加位置列表 posList
,并关联到文档 ID docId
。如果 IndexList
为空,则先创建一个空的映射。如果 docId
已存在于 IndexList
中,则将 posList
追加到对应的位置列表中,否则直接将 posList
赋值给对应的位置列表。最后,更新 Len
的值为 IndexList
的长度。
英文:
I'm not sure I fully understand what you're doing, however something like this should just work fine, replacing the slice
with a map
:
type PosList []int
type InvertedIndex struct {
Len int
IndexList map[int]PosList
}
func (ii *InvertedIndex) Add(posList PosList, docId int) {
if ii.IndexList == nil {
ii.IndexList = make(map[int]PosList)
}
if _, ok := ii.IndexList[docId]; ok {
ii.IndexList[docId] = append(ii.IndexList[docId], posList...)
} else {
ii.IndexList[docId] = posList
}
ii.Len = len(ii.IndexList)
}
答案2
得分: 0
你的问题有些混乱。我猜你想创建一个典型的倒排索引。在这种情况下,你可能想要像这样做:
package main
import "fmt"
type DocId int
type Positions []int
type docIndex struct {
docId DocId
positions Positions
}
type InvertedIndex struct {
docIndexes []docIndex
}
func New() *InvertedIndex {
return &InvertedIndex{}
}
func (ii *InvertedIndex) Add(docId DocId, positions Positions) {
for i, di := range (*ii).docIndexes {
if di.docId == docId {
di.positions = append(di.positions, positions...)
(*ii).docIndexes[i] = di
return
}
}
di := docIndex{
docId: docId,
positions: positions,
}
(*ii).docIndexes = append((*ii).docIndexes, di)
}
func main() {
ii := New()
docId := DocId(11)
positions := Positions{42, 7}
ii.Add(docId, positions)
positions = Positions{21, 4}
ii.Add(docId, positions)
docId = DocId(22)
positions = Positions{84, 14}
ii.Add(docId, positions)
fmt.Printf("%+v\n", *ii)
}
输出结果为:
{docIndexes:[{docId:11 positions:[42 7 21 4]} {docId:22 positions:[84 14]}]}
语句:
di.positions = append(di.positions, positions...)
将一个切片追加到另一个切片中。
参考资料:
Appending to and copying slices
Arrays, slices (and strings): The mechanics of 'append'
英文:
You question is confusing. I assume that you are trying to create a typical inverted index. In which case, you probably want to do something like this:
package main
import "fmt"
type DocId int
type Positions []int
type docIndex struct {
docId DocId
positions Positions
}
type InvertedIndex struct {
docIndexes []docIndex
}
func New() *InvertedIndex {
return &InvertedIndex{}
}
func (ii *InvertedIndex) Add(docId DocId, positions Positions) {
for i, di := range (*ii).docIndexes {
if di.docId == docId {
di.positions = append(di.positions, positions...)
(*ii).docIndexes[i] = di
return
}
}
di := docIndex{
docId: docId,
positions: positions,
}
(*ii).docIndexes = append((*ii).docIndexes, di)
}
func main() {
ii := New()
docId := DocId(11)
positions := Positions{42, 7}
ii.Add(docId, positions)
positions = Positions{21, 4}
ii.Add(docId, positions)
docId = DocId(22)
positions = Positions{84, 14}
ii.Add(docId, positions)
fmt.Printf("%+v\n", *ii)
}
Output:
{docIndexes:[{docId:11 positions:[42 7 21 4]} {docId:22 positions:[84 14]}]}
The statement:
di.positions = append(di.positions, positions...)
appends a slice to a slice.
References:
Appending to and copying slices
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论