forked from p93542168/wheat-cache
feat(listx): add listx
This commit is contained in:
parent
74e42e78ac
commit
4f6fb64ecc
|
@ -50,4 +50,6 @@ type ListXInterface interface {
|
|||
Insert(int, bool, ...string) (UpdateLength, error)
|
||||
Length() int
|
||||
Slice(start, end int) (UpdateLength, error) // 切片, O(n)复杂度
|
||||
Range(start, end int) ([]string, error)
|
||||
Remove(value string, count int) (int, UpdateLength)
|
||||
}
|
||||
|
|
|
@ -101,6 +101,31 @@ func (l *Listx) rightIndex(index int) (int, error) {
|
|||
return 0, errorx.New("the index is not valid, index:%d", index)
|
||||
}
|
||||
|
||||
func (l *Listx) remove(node *ListxNode) {
|
||||
|
||||
if node == nil {
|
||||
return
|
||||
}
|
||||
|
||||
l.length -= 1
|
||||
|
||||
if node == l.head {
|
||||
l.head = node.next
|
||||
node.next.pre = nil
|
||||
return
|
||||
}
|
||||
|
||||
if node == l.tail {
|
||||
l.tail = node.pre
|
||||
node.pre.next = nil
|
||||
return
|
||||
}
|
||||
|
||||
node.pre.next = node.next
|
||||
node.next.pre = node.pre
|
||||
|
||||
}
|
||||
|
||||
func (l *Listx) SizeByte() int64 {
|
||||
bytes := 0
|
||||
r := l.head
|
||||
|
@ -334,6 +359,7 @@ func (l *Listx) Slice(start int, end int) (structure.UpdateLength, error) {
|
|||
return structure.UpdateLength(updateLength), nil
|
||||
}
|
||||
|
||||
// Range 遍历
|
||||
func (l *Listx) Range(start, end int) ([]string, error) {
|
||||
startOffset, err := l.rightIndex(start)
|
||||
if err != nil {
|
||||
|
@ -361,3 +387,42 @@ func (l *Listx) Range(start, end int) ([]string, error) {
|
|||
}
|
||||
return values, nil
|
||||
}
|
||||
|
||||
func (l *Listx) Remove(value string, count int) (int, structure.UpdateLength) {
|
||||
|
||||
if count == 0 {
|
||||
return 0, 0
|
||||
}
|
||||
|
||||
updateLength := 0
|
||||
remCount := count
|
||||
|
||||
// 头删除
|
||||
if count > 0 {
|
||||
node := l.head
|
||||
for node != nil && remCount > 0 {
|
||||
if node.val.ToString() == value {
|
||||
l.remove(node)
|
||||
remCount--
|
||||
updateLength += node.val.GetSize()
|
||||
}
|
||||
node = node.next
|
||||
}
|
||||
|
||||
return count - remCount, structure.UpdateLength(updateLength)
|
||||
}
|
||||
|
||||
// 尾删除
|
||||
node := l.tail
|
||||
for node != nil && remCount < 0 {
|
||||
if node.val.ToString() == value {
|
||||
l.remove(node)
|
||||
remCount++
|
||||
updateLength += node.val.GetSize()
|
||||
}
|
||||
node = node.pre
|
||||
}
|
||||
|
||||
return remCount - count, structure.UpdateLength(updateLength)
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue