From c1b7500bcb270269c0b10db682e3987cc49c441c Mon Sep 17 00:00:00 2001 From: donutloop Date: Mon, 25 Jul 2022 14:50:31 +0200 Subject: [PATCH] List: add cap (#51) Cap return cap of the inner data --- datastructure/list/list.go | 5 +++++ datastructure/list/list_test.go | 12 ++++++++++++ docs/datastructure/list.md | 31 +++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+) diff --git a/datastructure/list/list.go b/datastructure/list/list.go index 0335adb..0f99123 100644 --- a/datastructure/list/list.go +++ b/datastructure/list/list.go @@ -248,6 +248,11 @@ func (l *List[T]) Size() int { return len(l.data) } +// Cap return cap of the inner data +func (l *List[T]) Cap() int { + return cap(l.data) +} + // Swap the value of index i and j in list func (l *List[T]) Swap(i, j int) { size := len(l.data) diff --git a/datastructure/list/list_test.go b/datastructure/list/list_test.go index b234e48..33a5c27 100644 --- a/datastructure/list/list_test.go +++ b/datastructure/list/list_test.go @@ -261,6 +261,18 @@ func TestSize(t *testing.T) { assert.Equal(0, empty.Size()) } +func TestCap(t *testing.T) { + assert := internal.NewAssert(t, "TestCap") + + data := make([]int, 0, 100) + list := NewList(data) + assert.Equal(100, list.Cap()) + + data = make([]int, 0) + list = NewList(data) + assert.Equal(0, list.Cap()) +} + func TestSwap(t *testing.T) { assert := internal.NewAssert(t, "TestSwap") diff --git a/docs/datastructure/list.md b/docs/datastructure/list.md index 483111e..98d6760 100644 --- a/docs/datastructure/list.md +++ b/docs/datastructure/list.md @@ -41,6 +41,7 @@ import ( - [Clone](#Clone) - [Merge](#Merge) - [Size](#Size) +- [Cap](#Cap) - [Swap](#Swap) - [Reverse](#Reverse) - [Unique](#Unique) @@ -651,6 +652,36 @@ func main() { +### Cap +

Cap return cap of the inner data

+ +Signature: + +```go +func (l *List[T]) Cap() int +``` +Example: + +```go +package main + +import ( + "fmt" + list "github.com/duke-git/lancet/v2/datastructure/list" +) + +func main() { + data := make([]int, 0, 100) + + li := list.NewList(data) + + fmt.Println(li.Cap()) // 100 +} +``` + + + + ### Swap

Swap the value at two index in list