将切片追加到切片的切片中。

huangapple go评论127阅读模式
英文:

Append slice to slice of slices

问题

我有一个数据结构:

  1. type PosList []int
  2. type InvertedIndex struct {
  3. Capacity int
  4. Len int
  5. IndexList []PosList
  6. }

我在Add方法上遇到了问题:

  1. func (ii *InvertedIndex) Add(posList PosList, docId int) {
  2. if ii.Len == ii.Capacity {
  3. newIndexList := make([]PosList, ii.Len, (ii.Capacity+1)*2)
  4. for i := 0; i < ii.Len; i++ {
  5. newIndexList[i] = make([]int, len(ii.IndexList[i]))
  6. copy(newIndexList[i], ii.IndexList[i])
  7. }
  8. ii.IndexList = newIndexList
  9. }
  10. ii.IndexList = ii.IndexList[0 : ii.Len+2]
  11. ii.IndexList[docId] = posList
  12. return
  13. }

或者,我尝试了这样的方法:

  1. func (ii *InvertedIndex) Add(posList PosList, docId int) {
  2. if ii.Len == ii.Capacity {
  3. newIndexList := make([]PosList, ii.Len, (ii.Capacity+1)*2)
  4. copy(newIndexList, ii.IndexList)
  5. ii.IndexList = newIndexList
  6. }
  7. ii.IndexList = ii.IndexList[0 : ii.Len+2]
  8. ii.IndexList[docId] = posList
  9. return
  10. }

这两种方法都不起作用,也许有人可以解释一下如何将一个切片追加到这样的结构中。

英文:

I have data structure:

  1. type PosList []int
  2. type InvertedIndex struct {
  3. Capacity int
  4. Len int
  5. IndexList []PosList
  6. }

I have problem with Add method:

  1. func (ii *InvertedIndex) Add(posList PosList, docId int) {
  2. if ii.Len == ii.Capacity {
  3. newIndexList := make([]PosList, ii.Len, (ii.Capacity+1)*2)
  4. for i := 0; i &lt; ii.Len; i++ {
  5. newIndexList[i] = make([]int, len(ii.IndexList[i]))
  6. copy(newIndexList[i], ii.IndexList[i])
  7. }
  8. ii.IndexList = newIndexList
  9. }
  10. ii.IndexList = ii.IndexList[0 : ii.Len+2]
  11. ii.IndexList[docId] = posList
  12. return
  13. }

Or, i try something like this:

  1. func (ii *InvertedIndex) Add(posList PosList, docId int) {
  2. if ii.Len == ii.Capacity {
  3. newIndexList := make([]PosList, ii.Len, (ii.Capacity+1)*2)
  4. copy(newIndexList, ii.IndexList)
  5. ii.IndexList = newIndexList
  6. }
  7. ii.IndexList = ii.IndexList[0 : ii.Len+2]
  8. ii.IndexList[docId] = posList
  9. return
  10. }

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

  1. type PosList []int
  2. type InvertedIndex struct {
  3. Len int
  4. IndexList map[int]PosList
  5. }
  6. func (ii *InvertedIndex) Add(posList PosList, docId int) {
  7. if ii.IndexList == nil {
  8. ii.IndexList = make(map[int]PosList)
  9. }
  10. if _, ok := ii.IndexList[docId]; ok {
  11. ii.IndexList[docId] = append(ii.IndexList[docId], posList...)
  12. } else {
  13. ii.IndexList[docId] = posList
  14. }
  15. ii.Len = len(ii.IndexList)
  16. }

这段代码将 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 :

  1. type PosList []int
  2. type InvertedIndex struct {
  3. Len int
  4. IndexList map[int]PosList
  5. }
  6. func (ii *InvertedIndex) Add(posList PosList, docId int) {
  7. if ii.IndexList == nil {
  8. ii.IndexList = make(map[int]PosList)
  9. }
  10. if _, ok := ii.IndexList[docId]; ok {
  11. ii.IndexList[docId] = append(ii.IndexList[docId], posList...)
  12. } else {
  13. ii.IndexList[docId] = posList
  14. }
  15. ii.Len = len(ii.IndexList)
  16. }

答案2

得分: 0

你的问题有些混乱。我猜你想创建一个典型的倒排索引。在这种情况下,你可能想要像这样做:

  1. package main
  2. import "fmt"
  3. type DocId int
  4. type Positions []int
  5. type docIndex struct {
  6. docId DocId
  7. positions Positions
  8. }
  9. type InvertedIndex struct {
  10. docIndexes []docIndex
  11. }
  12. func New() *InvertedIndex {
  13. return &InvertedIndex{}
  14. }
  15. func (ii *InvertedIndex) Add(docId DocId, positions Positions) {
  16. for i, di := range (*ii).docIndexes {
  17. if di.docId == docId {
  18. di.positions = append(di.positions, positions...)
  19. (*ii).docIndexes[i] = di
  20. return
  21. }
  22. }
  23. di := docIndex{
  24. docId: docId,
  25. positions: positions,
  26. }
  27. (*ii).docIndexes = append((*ii).docIndexes, di)
  28. }
  29. func main() {
  30. ii := New()
  31. docId := DocId(11)
  32. positions := Positions{42, 7}
  33. ii.Add(docId, positions)
  34. positions = Positions{21, 4}
  35. ii.Add(docId, positions)
  36. docId = DocId(22)
  37. positions = Positions{84, 14}
  38. ii.Add(docId, positions)
  39. fmt.Printf("%+v\n", *ii)
  40. }

输出结果为:

  1. {docIndexes:[{docId:11 positions:[42 7 21 4]} {docId:22 positions:[84 14]}]}

语句:

  1. 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:

  1. package main
  2. import &quot;fmt&quot;
  3. type DocId int
  4. type Positions []int
  5. type docIndex struct {
  6. docId DocId
  7. positions Positions
  8. }
  9. type InvertedIndex struct {
  10. docIndexes []docIndex
  11. }
  12. func New() *InvertedIndex {
  13. return &amp;InvertedIndex{}
  14. }
  15. func (ii *InvertedIndex) Add(docId DocId, positions Positions) {
  16. for i, di := range (*ii).docIndexes {
  17. if di.docId == docId {
  18. di.positions = append(di.positions, positions...)
  19. (*ii).docIndexes[i] = di
  20. return
  21. }
  22. }
  23. di := docIndex{
  24. docId: docId,
  25. positions: positions,
  26. }
  27. (*ii).docIndexes = append((*ii).docIndexes, di)
  28. }
  29. func main() {
  30. ii := New()
  31. docId := DocId(11)
  32. positions := Positions{42, 7}
  33. ii.Add(docId, positions)
  34. positions = Positions{21, 4}
  35. ii.Add(docId, positions)
  36. docId = DocId(22)
  37. positions = Positions{84, 14}
  38. ii.Add(docId, positions)
  39. fmt.Printf(&quot;%+v\n&quot;, *ii)
  40. }

Output:

  1. {docIndexes:[{docId:11 positions:[42 7 21 4]} {docId:22 positions:[84 14]}]}

The statement:

  1. di.positions = append(di.positions, positions...)

appends a slice to a slice.


References:

Appending to and copying slices

Arrays, slices (and strings): The mechanics of 'append'

inverted index

huangapple
  • 本文由 发表于 2014年3月29日 19:54:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/22730754.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定