1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-04 12:52:28 +08:00

Compare commits

...

262 Commits

Author SHA1 Message Date
dudaodong
bf332b9f1c release v2.1.8 2022-10-21 16:22:52 +08:00
dudaodong
6b6cd66f9f Merge branch 'main' into v2 2022-10-19 17:31:51 +08:00
燕归来
5399c2290e feat: support type alias for Number constraint (#60) 2022-10-19 17:21:53 +08:00
dudaodong
6248293c49 doc: add docment for Replace and ReplaceAll 2022-10-17 10:40:35 +08:00
dudaodong
eced25b76d test: add unit test for Replace and ReplaceAll 2022-10-17 10:26:54 +08:00
dudaodong
96a4327aa7 fix: fix some static check issues 2022-10-15 15:03:24 +08:00
dudaodong
fcfbdea597 feat: add Replace and ReplaceAll for slice 2022-10-15 14:49:49 +08:00
dudaodong
87896f917a doc: add document for pipeline 2022-10-15 12:36:11 +08:00
dudaodong
b8563ed646 feat: add Pipeline function 2022-10-15 12:29:47 +08:00
dudaodong
1ccf0af2b3 doc: update document for datetime package 2022-10-14 10:20:38 +08:00
dudaodong
6314889c6a release v2.1.7 2022-10-14 10:08:43 +08:00
dudaodong
294bd5a5ed doc: update document for hashmap 2022-10-11 15:41:09 +08:00
dudaodong
ca44815fd5 Merge branch 'main' into v2 2022-10-11 15:02:28 +08:00
dudaodong
bcd1cabf80 feat: add Keys and Values function for hashmap 2022-10-10 16:34:05 +08:00
CyJaySong
4edefcca67 expand BeginOfWeek、EndOfWeek (#59) 2022-10-10 15:44:22 +08:00
dudaodong
fab24c8d12 feat: add Iterate for hashmap datastructure 2022-10-10 15:25:30 +08:00
dudaodong
604acd9b07 doc:add lancet api doc website 2022-09-29 18:41:22 +08:00
dudaodong
531cb19fd1 release v2.1.6 2022-08-31 17:07:32 +08:00
dudaodong
4f0161ca53 doc: add source code link for http_client 2022-08-31 15:56:41 +08:00
dudaodong
73362b7f69 doc: update readme file 2022-08-31 11:40:24 +08:00
dudaodong
b289f2975b doc: add document for functions in netutil/http_client.go 2022-08-31 11:20:22 +08:00
dudaodong
90ce2705ca test: add unit test for http client 2022-08-30 17:33:58 +08:00
dudaodong
35e1d09ce3 feat: add http client for sending http request 2022-08-30 17:22:17 +08:00
dudaodong
d67d8fad3a doc: fix missing word in file condition.md 2022-08-29 14:02:30 +08:00
dudaodong
c5e6d01a31 update readme file: add new package and function 2022-08-29 11:50:37 +08:00
dudaodong
6d6c3f692f refactor: change TernaryOperator signature 2022-08-29 11:27:51 +08:00
dudaodong
c695837b16 doc: add document for package condition 2022-08-29 11:23:02 +08:00
dudaodong
ef28b52963 fix: fix regex isChineseMobileRegexMatcher not work 2022-08-28 20:58:34 +08:00
dudaodong
18b2b6ff7c feat: add functons: And, Or, Xor, Nor, Xnor, Nand 2022-08-28 20:57:28 +08:00
dudaodong
6a05a123f4 feat: add Bool function to check if a value is truthy or falsy 2022-08-28 20:38:19 +08:00
dudaodong
f7c33f258d Merge branch 'main' into v2 2022-08-27 19:25:24 +08:00
dudaodong
04058dd7da refactor: change common package to condition package 2022-08-27 19:19:34 +08:00
郑一诺她爸
72e1d92fa1 TernaryOperator Reduce if else operations (#56)
* feat:(slice add AppendIfAbsent function)

* feat:(slice add TernaryOperator function)

Co-authored-by: george.zheng <george.zheng@ambergroup.io>
2022-08-27 19:12:03 +08:00
dudaodong
063df0f0d1 doc: add docment for IsZeroValue function 2022-08-27 19:04:57 +08:00
dudaodong
fc10689b25 feat: add IsZeroValue function 2022-08-27 18:57:45 +08:00
dudaodong
9239bcfdc3 doc: fmt code example 2022-08-26 16:34:20 +08:00
dudaodong
c984815dea doc: fmt code example 2022-08-26 16:29:55 +08:00
dudaodong
301cb5db87 doc: add hashmap 2022-08-26 16:26:16 +08:00
dudaodong
cc1bacff74 doc: add document for EncodeByte and DecodeByte 2022-08-26 10:54:51 +08:00
dudaodong
f49f75b371 test: add unit test for EncodeByte and DecodeByte 2022-08-26 10:47:25 +08:00
dudaodong
b30f4a7bab feat: add DecodeByte 2022-08-26 10:35:54 +08:00
dudaodong
982cb8932b feat: add EncodeByte 2022-08-26 10:34:42 +08:00
dudaodong
9107eb4b32 fix: fix resize bug in HashMap 2022-08-25 14:04:26 +08:00
dudaodong
1acf977b24 release v2.1.5 2022-08-25 11:11:36 +08:00
dudaodong
5c878d0873 doc: change ReverseStr to Reverse in all docment 2022-08-24 10:37:56 +08:00
dudaodong
ab0716a472 refactor: change ReverseStr to Reverse in strutil package 2022-08-24 10:35:18 +08:00
dudaodong
3c2e0ca5b3 doc: add document for hashmap 2022-08-24 10:28:57 +08:00
dudaodong
551e66ba29 refactor: update HashMap Put method 2022-08-24 10:20:02 +08:00
dudaodong
4eeeabb227 feat: add Delete method for hashmap 2022-08-23 15:07:54 +08:00
dudaodong
b3437fdddf feat: add Delete method for hashmap 2022-08-23 15:07:13 +08:00
dudaodong
312dcab369 feat: add Delete method for hashmap 2022-08-23 15:05:32 +08:00
dudaodong
0cb89f4f46 test: add unit test for HashMap 2022-08-23 14:15:51 +08:00
dudaodong
a2dec87995 feat: add Put method for hashmap 2022-08-23 14:11:14 +08:00
dudaodong
9094cb29bf feat: add new HashMap data structure 2022-08-22 15:21:30 +08:00
dudaodong
ddb992ad2f refactor: BuildMaxHeap 2022-08-22 11:10:17 +08:00
dudaodong
24f18aaaec refactor: change function sign for QuickSort 2022-08-22 11:02:25 +08:00
dudaodong
36169874e5 refactor: change sort file name 2022-08-22 10:58:47 +08:00
dudaodong
23aeb6ac99 refactor: change sort file name 2022-08-22 10:57:04 +08:00
dudaodong
a1984f0a59 feat: add BuildMaxHeap 2022-08-22 10:51:37 +08:00
IceCafeCup
c95db23d2c feat: Set struct uses empty struct as value (#55) 2022-08-13 12:39:30 +08:00
Abirdcfly
fbf251d805 delete minor unreachable code caused by log.Fatal (#54)
Signed-off-by: Abirdcfly <fp544037857@gmail.com>
2022-08-10 14:19:21 +08:00
dudaodong
337f08a04b fix: issue #53 2022-08-06 17:41:45 +08:00
dudaodong
c6a7371049 change lastest v1 version 2022-08-03 10:02:46 +08:00
dudaodong
b697858038 release v2.1.4 2022-08-03 09:56:02 +08:00
dudaodong
5446f7e33c doc: add document for list methods 2022-07-28 12:56:11 +08:00
dudaodong
553f63e76b refactor: update param name in AppendIfAbsent 2022-07-27 15:45:34 +08:00
dudaodong
fc3e94df58 doc: add document for AppendIfAbsent function 2022-07-27 15:44:54 +08:00
dudaodong
5c66f38a5b test: fix test function name TestAppendIfAbsent 2022-07-27 15:38:51 +08:00
郑一诺她爸
70e213b3f7 feat:(slice add AppendIfAbsent function) (#52)
Co-authored-by: george.zheng <george.zheng@ambergroup.io>
2022-07-25 20:52:33 +08:00
donutloop
c1b7500bcb List: add cap (#51)
Cap return cap of the inner data
2022-07-25 20:50:31 +08:00
donutloop
3d7600a9e4 list: add DeleteIf method (#50)
DeleteIf delete all satisfying f(data[i]), returns count of removed elements
2022-07-23 18:52:29 +08:00
donutloop
0299c454ab list: add sublist method (#49)
SubList returns a sub list of the original list between the specified fromIndex, inclusive, and toIndex, exclusive.
2022-07-23 15:00:04 +08:00
dudaodong
741af66404 test: add todo fix for TestSchedule 2022-07-22 10:30:44 +08:00
dudaodong
ef5d0379a1 doc: fix missing word 2022-07-22 10:24:34 +08:00
dudaodong
105ab49763 refactor: change slice Contain logic, convert to map first, then check existence 2022-07-22 10:22:34 +08:00
dudaodong
b5ba9ba573 fix: fix TestMapToSlice function result independent of order of elements 2022-07-22 10:17:23 +08:00
donutloop
ac3baac5c6 list: Add LastIndexOfFunc and IndexOfFunc method (#48)
* LastIndexOfFunc returns the index of the last occurrence of the value in this list satisfying f(data[i])
* IndexOfFunc returns the first index satisfying f(v)
2022-07-22 10:04:31 +08:00
donutloop
a82b5dd206 list: add LastIndexOf (#46)
Add LastIndexOf method too List and fix doc

LastIndexOf returns the index of the last occurrence of the value in this list.
if not found return -1
2022-07-21 15:19:18 +08:00
CyJaySong
ac0fb5ef25 Update convertor_zh-CN.md (#45)
fix docs
2022-07-21 11:22:30 +08:00
dudaodong
9bd1c205fe release: update readme file and release v2.1.3 2022-07-21 11:16:19 +08:00
dudaodong
6ccf9fd3cf doc: add document for GetOsBits function 2022-07-21 11:05:17 +08:00
dudaodong
c02ef2c62d doc: format code in markdown file 2022-07-20 19:42:17 +08:00
dudaodong
6414031754 doc: fix mistaken link to map source file 2022-07-20 19:38:14 +08:00
dudaodong
5336130570 doc: add funcation IsDisjoint 2022-07-20 19:33:37 +08:00
dudaodong
bfa46d46a2 doc: format code in markdown file 2022-07-20 19:33:02 +08:00
dudaodong
aab28b914c Merge branch 'main' into v2 2022-07-20 17:48:26 +08:00
donutloop
336e454ce7 maputil: IsDisjoint (#43)
Verifies map a and map b have no keys in common.

if it returns true then the maps are disjoint otherwise a join in some form available
2022-07-20 17:26:54 +08:00
anyanfei
35a50bd792 System: Add the system bits judgment (#44)
Co-authored-by: anyanfei <anyanfei@yungengxin.com>
2022-07-20 17:25:14 +08:00
dudaodong
2fd23f02f6 doc: format code in markdown file 2022-07-20 15:44:33 +08:00
dudaodong
aad5b447c9 doc: format code in markdown file 2022-07-20 15:13:44 +08:00
dudaodong
cece13e929 fix: fix aes/des cbc crypto iv bug 2022-07-20 11:42:52 +08:00
donutloop
ecf325a06c maputil: preallocate len and cap for keys and values slice (#41)
Length of keys and values is know in advance, use this knowledge to preallocate memory.
Space usage increases by const factor
2022-07-19 15:04:19 +08:00
dudaodong
44370aef5e refactor: change variable name to 2022-07-18 15:52:51 +08:00
dudaodong
1782b11ec4 refactor: refact Intersection function 2022-07-18 15:33:45 +08:00
dudaodong
fe6495123c doc: add document for EncodeUrl function 2022-07-18 11:31:12 +08:00
dudaodong
d442f564ce Merge branch 'main' into v2 2022-07-17 13:35:12 +08:00
dudaodong
f35446cc13 docs: fix word mistaken 2022-07-17 13:34:19 +08:00
dudaodong
506a8b4776 feat: add EncodeUrl function 2022-07-15 16:29:41 +08:00
dudaodong
47ecfbfd5f docs: add doc for Flatten function 2022-07-14 17:25:47 +08:00
dudaodong
82f7401368 feat: add Flatten function 2022-07-14 11:53:16 +08:00
dudaodong
068c878d1e release v2.1.2 2022-07-13 10:40:03 +08:00
dudaodong
47b2402345 docs: update readme file add new functions 2022-07-13 10:32:03 +08:00
dudaodong
a245716f99 docs: add doc for new function: ToChannel ToMap MapToSlice 2022-07-13 10:26:49 +08:00
dudaodong
71c85bb8f0 docs: add doc for functions GetRequestPublicIp and IsInternalIP 2022-07-13 10:09:23 +08:00
dudaodong
5edafec393 Merge branch 'main' into v2 2022-07-13 09:55:04 +08:00
tangqiu0205
957878dd98 feat: add func GetRequestPublicIp and IsInternalIP (#40)
* add: add func GetRequestPublicIp and IsInternalIP

* fix: fix test fileutil fail

Co-authored-by: zhanghu <305835360@qq.com>
2022-07-13 09:52:35 +08:00
dudaodong
12e979cf3c feat: add ToMapToSlice function 2022-07-11 11:28:46 +08:00
dudaodong
ded42f8ff5 feat: add ToChannel function 2022-07-11 11:11:16 +08:00
dudaodong
d06cde3fcf feat: add ToMap function 2022-07-11 10:27:38 +08:00
dudaodong
9cd8bfb8e0 docs: compress log png 2022-07-08 10:52:53 +08:00
dudaodong
51c9877224 docs: update change param type to for some slice functions 2022-07-07 17:47:02 +08:00
dudaodong
f7f6427919 merge main branch 2022-07-07 16:35:46 +08:00
tangqiu0205
56b6844a2d update: update type constraint any -> Comparable. (#37)
* update: update type constraint any -> Comparable.

* update: update type constraint any -> Comparable.

Co-authored-by: zhanghu <305835360@qq.com>
2022-07-07 16:31:48 +08:00
dudaodong
cce56f0479 docs: update query string type in http request 2022-07-07 11:21:16 +08:00
dudaodong
3dbd7d8980 fix: sending post request with header multipart/form-data support query string type is map[string]string 2022-07-07 10:23:15 +08:00
dudaodong
3625921912 fix: fix missused function ToSlice -> ToSlicePointer 2022-07-06 11:31:11 +08:00
dudaodong
1faaf54986 release v2.1.1 2022-07-06 11:25:02 +08:00
dudaodong
efe1fba267 docs: add doc for heap datastructure 2022-07-06 11:22:00 +08:00
dudaodong
0c43cb3f68 docs: update convertor package doc 2022-07-06 11:17:45 +08:00
dudaodong
c25111e349 docs: add doc for ToSlice and ToSlicePointer function 2022-07-06 11:15:51 +08:00
dudaodong
a61ab5716e docs: add doc for ToPointer function 2022-07-06 11:04:30 +08:00
dudaodong
628404dc7f fix: fix lint error 2022-07-06 10:57:45 +08:00
tangqiu0205
bab0a27e40 Add ToPointer,ToSlicePointer,ToSlice func (#36)
* Add ToPointer func

* Add ToSlice and ToSlicePointer func

Co-authored-by: zhanghu <305835360@qq.com>
2022-07-05 19:15:54 +08:00
dudaodong
fc0e104591 Merge branch 'main' into v2 2022-06-30 14:12:54 +08:00
dudaodong
e83e9a1651 docs: add doc for MaxHeap 2022-06-29 16:43:56 +08:00
dudaodong
3d2e6295c4 docs: add doc for MaxHeap 2022-06-29 15:40:14 +08:00
dudaodong
221fe44e63 docs: add doc for MaxHeap 2022-06-29 15:37:42 +08:00
dudaodong
7930f517ae feat: add MaxHeap func PrintStructure 2022-06-29 11:08:07 +08:00
dudaodong
0f61321d5b test: add unit test for MaxHeap 2022-06-27 17:55:35 +08:00
dudaodong
7ddeeb51e5 feat: add MaxHeap 2022-06-27 17:01:17 +08:00
dudaodong
484d2845b3 feat: add MaxHeap 2022-06-27 16:27:34 +08:00
dudaodong
bbbc6b6941 docs: update go version badge 2022-06-24 09:39:38 +08:00
dudaodong
acf028cdcd release v2.1.0 2022-06-22 16:10:54 +08:00
dudaodong
31e43ec356 docs: add doc for UniqueBy function 2022-06-22 16:08:37 +08:00
dudaodong
9f45e68fef test: add fuzz test for Equal function 2022-06-21 14:17:33 +08:00
dudaodong
d2df99a6f0 feat: add UniqueBy function in slice.go 2022-06-17 15:18:04 +08:00
dudaodong
713c341831 docs: update comment for Size function of ArrayQueue 2022-06-16 18:03:50 +08:00
dudaodong
0b05a6dd6f release: v2.0.9 2022-06-15 15:27:01 +08:00
dudaodong
d21c101caf refactor: change function name EqualWithFunc to EqualWith 2022-06-15 15:25:44 +08:00
dudaodong
bf49db60d9 docs: change function name EqualWithFunc to EqualWith 2022-06-15 15:24:58 +08:00
dudaodong
d57fa3b603 docs: update readme file add new functions 2022-06-15 15:23:30 +08:00
dudaodong
b3a29ce82d docs: add doc for function Equal and EqualWithFunc 2022-06-13 15:14:37 +08:00
dudaodong
1d8a37d6e8 docs: add doc for function CreateDir 2022-06-13 15:00:12 +08:00
dudaodong
ef2d8e14b0 feat: add EqualWithFunc function for slice 2022-06-13 14:15:32 +08:00
dudaodong
0cbb3dd97e feat: add Equal function for slice 2022-06-13 14:06:20 +08:00
dudaodong
38a7f9423f fmt: gofmt package datastructure/queue 2022-06-08 14:40:37 +08:00
dudaodong
2c0ab9e922 feat: add CreateDir function in file.go 2022-06-08 14:38:35 +08:00
dudaodong
9aa3b742ff docs: add doc for package datastructure 2022-06-07 14:54:23 +08:00
dudaodong
6bfaba750d docs: add doc tree-zh-CN.md 2022-06-07 14:39:32 +08:00
dudaodong
69f8bee935 docs: add doc tree datastructure 2022-06-07 14:17:34 +08:00
dudaodong
07d04a9cd0 refactor: change function name 2022-06-07 13:48:32 +08:00
dudaodong
6f17ba9116 docs: add doc set_zh-CN.md 2022-06-06 11:20:55 +08:00
dudaodong
56d1e5e95c docs: fix code format 2022-06-06 11:12:53 +08:00
dudaodong
11d86d0270 docs: add doc for Set datastructer 2022-06-06 11:07:43 +08:00
dudaodong
d1c20a1da8 fix: fix function comment 2022-06-06 11:07:13 +08:00
dudaodong
f87ab89207 docs: fix misspell in doc queue.md 2022-06-06 10:34:18 +08:00
dudaodong
9ebb0c7920 docs: add doc queue_zh-CN.md 2022-06-06 10:30:26 +08:00
dudaodong
31a5ed11a3 docs: add doc for PriorityQueue 2022-06-05 17:32:37 +08:00
dudaodong
a706d488e6 feat: add Size func for PriorityQueue 2022-06-05 17:29:36 +08:00
dudaodong
61251fb0f1 refactor: change func Length to Size in CircularQueue 2022-06-05 16:57:03 +08:00
dudaodong
747dc30b02 refactor: Change param __TEXT __DATA __OBJC others dec hex to in NewCircularQueue function 2022-06-05 16:49:30 +08:00
dudaodong
d54f27d9a9 docs: add IsFull function for ArrayQueue 2022-06-05 16:46:01 +08:00
dudaodong
dbca2f6be4 feat: add IsFull function for ArrayQueue 2022-06-05 16:43:15 +08:00
dudaodong
2ef9b56d22 feat: add Contain function for LinkedQueue 2022-06-05 16:41:16 +08:00
dudaodong
52ecde7e24 docs: add doc for LinkedQueue 2022-06-05 16:36:56 +08:00
dudaodong
a7dce5e4d3 docs: add doc for LinkedQueue 2022-06-04 18:57:43 +08:00
dudaodong
4718da44f4 refactor: rename function 'EnQueue' -> 'Enqueue', 'DeQueue' -> 'Dequeue' 2022-06-04 18:53:46 +08:00
dudaodong
1aa1f95524 docs: add doc for queue 2022-06-04 18:49:24 +08:00
dudaodong
d922b2e778 docs: fix code format 2022-06-04 11:26:39 +08:00
dudaodong
5a7b3c4a37 docs: add stack_zh-CN.md doc 2022-06-03 19:20:14 +08:00
dudaodong
1c2b1a2f02 docs: update Peak function comment 2022-06-02 22:26:26 +08:00
dudaodong
c68440ecf8 docs: add docs for stack 2022-06-02 22:25:11 +08:00
dudaodong
6a48b3f99e docs: add docs for linklist 2022-06-01 20:48:30 +08:00
dudaodong
bab9ae32d4 docs: add docs for package datastructure DoublyLink 2022-06-01 20:30:30 +08:00
dudaodong
f0e6e94c5e fix: fix incorrect comment for DoublyLink Clear function 2022-06-01 20:16:34 +08:00
dudaodong
a6fe155781 docs: fix code format 2022-05-31 21:47:56 +08:00
dudaodong
d5be9009fc docs: update list doc 2022-05-31 21:43:26 +08:00
dudaodong
ca47be41f6 fix: fix incorrect comment for SinglyLink Clear function 2022-05-31 21:42:31 +08:00
dudaodong
ff5db30208 docs: add docs for package datastructure SinglyLink 2022-05-31 21:41:25 +08:00
dudaodong
a429d46072 docs: add list_zh-CN.md 2022-05-30 21:09:21 +08:00
dudaodong
7b84fbfd94 fix: fix misspellings -> 2022-05-30 20:54:34 +08:00
dudaodong
fc6f618885 docs: add docs for package datastructure list 2022-05-30 20:53:11 +08:00
dudaodong
7f03c3b0a2 docs: update algorithm package doc 2022-05-30 17:53:21 +08:00
dudaodong
4b4386bd47 Merge branch 'v2' of github.com:duke-git/lancet into v2 2022-05-28 10:54:55 +08:00
dudaodong
10a1706613 docs: update function comments 2022-05-28 10:51:15 +08:00
dudaodong
d097f356fd docs: change some function comments for slice doc 2022-05-21 17:00:28 +08:00
dudaodong
bc9bacaa55 refactor: change param to item 2022-05-21 16:50:57 +08:00
dudaodong
c949059fb1 release: v2.0.7 2022-05-18 15:29:55 +08:00
dudaodong
9c6d489db7 docs: add some new funcs documentation 2022-05-18 15:28:18 +08:00
dudaodong
45436798af fix: fix TestMinBy test function 2022-05-18 15:23:57 +08:00
dudaodong
bd0eb0682c docs: add doc for func MaxBy and MinBy 2022-05-18 15:22:44 +08:00
dudaodong
bdbc06b095 docs: add doc for funcs IndexOf and LastIndexOf 2022-05-18 15:00:09 +08:00
dudaodong
2f504ce851 test: add unit test for func LastIndexOf 2022-05-16 11:11:40 +08:00
dudaodong
02a24b461c test: add unit test for func LastIndexOf 2022-05-16 11:05:52 +08:00
dudaodong
3f7a81c005 test: add unit test for func IndexOf 2022-05-16 11:04:40 +08:00
dudaodong
cc6e10ee5a add func IndexOf and LastIndexOf 2022-05-16 10:40:33 +08:00
dudaodong
eff2f22440 test: add unit test for MinBy function 2022-05-15 18:28:40 +08:00
dudaodong
47b6747bb0 feat: add MinBy function 2022-05-15 18:25:46 +08:00
dudaodong
298219aee7 test: add unit test for MaxBy function 2022-05-15 18:21:01 +08:00
dudaodong
d5607b0a5a feat: add MaxBy function 2022-05-15 18:15:05 +08:00
IHIDCHAOS
f47873fbc7 Update system_zh-CN.md (#32)
fix typo
2022-05-15 17:28:37 +08:00
dudaodong
f24ad26692 release: v2.0.6 2022-05-12 10:13:13 +08:00
dudaodong
88de9bfac2 docs: add doc for SplitEx function 2022-05-12 10:12:19 +08:00
dudaodong
e4777a0986 docs: add doc for SplitEx function 2022-05-12 10:11:12 +08:00
franktz
84a69d1fa5 feat: strutil.SplitEx. Split a given string whether the result contains empty string (#31)
Co-authored-by: Frank.Town <tangzhi21@sfmail.sf-express.com>
2022-05-11 15:19:53 +08:00
dudaodong
5eb056277d update readme file 2022-04-27 11:51:02 +08:00
dudaodong
4e6d586251 update readme file 2022-04-27 11:42:06 +08:00
dudaodong
61c7012e17 test: add unit test TestBSTree_IsSubTree 2022-04-27 11:36:51 +08:00
dudaodong
0d48778886 feat: add HasSubTree func for BSTree 2022-04-27 10:43:01 +08:00
dudaodong
e98c46c903 feat: add IsSubTree func for BSTree 2022-04-27 10:36:11 +08:00
dudaodong
155f287ab0 refactor: add param comparator in NewBSTree func 2022-04-26 11:04:04 +08:00
dudaodong
652916b7d7 test: add unit test for PriorityQueue Dequeue function 2022-04-26 10:56:24 +08:00
dudaodong
955de2bdbf feat: add Dequeue function 2022-04-26 10:50:27 +08:00
dudaodong
a4c1d40faa test: add unit test for PriorityQueue Enqueue function 2022-04-26 10:45:19 +08:00
dudaodong
f155e0caa6 test: add unit test for PriorityQueue Enqueue function 2022-04-26 10:40:48 +08:00
dudaodong
fb0332449c feat: add IsEmpty and IsFull for PriorityQueue 2022-04-26 10:24:10 +08:00
dudaodong
68f0fd1d4c feat: add PriorityQueue 2022-04-26 10:19:31 +08:00
dudaodong
70995c5098 feat: add PriorityQueue 2022-04-26 10:18:43 +08:00
dudaodong
55e62ed8ca feat: add PriorityQueue 2022-04-26 10:18:06 +08:00
dudaodong
e614274f07 release v2.0.5 2022-04-24 10:34:25 +08:00
dudaodong
c524eb04a1 docs: add doc for concurrency in readme file 2022-04-24 10:32:55 +08:00
dudaodong
985b3cddd8 fix: fix TestTee func 2022-04-24 10:26:07 +08:00
dudaodong
abadeec007 docs: add doc for channel.go 2022-04-24 10:17:34 +08:00
dudaodong
3ab05154aa docs: add doc for channel.go 2022-04-22 17:12:41 +08:00
dudaodong
9f1c89bf0e refactor: refact FanIn func in channel.go 2022-04-21 14:31:43 +08:00
dudaodong
9444582e44 fix: fix OrDone func 2022-04-21 14:19:38 +08:00
dudaodong
c27ccad2b9 refactor: refact Or func in channel.go 2022-04-21 14:13:19 +08:00
dudaodong
d1c6c57700 feat: add func Bridge 2022-04-21 11:26:02 +08:00
dudaodong
922999037f feat: add func Tee 2022-04-21 11:18:45 +08:00
dudaodong
046e90486d feat: add func in channel.go 2022-04-19 16:21:23 +08:00
dudaodong
fc6dee9e77 feat: add func in channel.go 2022-04-19 16:03:12 +08:00
dudaodong
980ff2c363 feat: add FanIn for channel 2022-04-19 15:34:26 +08:00
dudaodong
763aa8e10d refactor: replace param done channel with ctx context 2022-04-19 14:42:55 +08:00
dudaodong
83c0d1d6e6 fix: fix misspellings 2022-04-15 17:02:34 +08:00
dudaodong
dfc6b868fb feat: add RepeateFn function 2022-04-15 16:32:55 +08:00
dudaodong
f66c0938e5 feat: add Take function to generate a chan of values taken from another chan 2022-04-15 16:22:19 +08:00
dudaodong
a4900fecb4 feat: add Repeate function to generate a chan of repeate values 2022-04-15 16:08:14 +08:00
dudaodong
dc25bdab2f feat: add Generate chan function in concurrency package 2022-04-15 15:56:06 +08:00
dudaodong
bb23c9eef8 release v2.0.4 2022-04-14 11:51:34 +08:00
dudaodong
b4cd0750e4 fix: fix http post to support multipart/form-data header 2022-04-14 11:48:39 +08:00
dudaodong
c960841491 docs: update sort function doc 2022-04-12 14:45:22 +08:00
dudaodong
5d6f9443fd refactor: rewrite sort function 2022-04-12 14:30:43 +08:00
dudaodong
5483380066 refactor: rewrite arrayqueue 2022-04-08 17:39:54 +08:00
dudaodong
20b9e5353e fix: fix goreport issue 2022-04-06 11:33:48 +08:00
dudaodong
1ec3a5af87 release v2.0.3 2022-04-06 10:54:54 +08:00
dudaodong
f31dde97b1 docs: add maputil docs in read me file 2022-04-05 14:24:33 +08:00
dudaodong
1718fd1cf1 docs: add doc for maputil package 2022-04-05 14:16:58 +08:00
dudaodong
513c0f829c docs: add doc for SymmetricDifference fun in slice doc 2022-04-05 13:47:13 +08:00
dudaodong
c51a806aff feat: add SymmetricDifference func for set 2022-04-01 18:06:34 +08:00
dudaodong
3c16d50c4b refactor: rename ReverseIntersect to SymmetricDifference 2022-04-01 18:05:02 +08:00
dudaodong
21dd6ab8aa feat: add SymmetricDifference func for set 2022-04-01 18:02:12 +08:00
dudaodong
f5bf5183cc feat: add Minus func for set 2022-04-01 16:48:37 +08:00
dudaodong
f28b5b2f92 feat: add Minus func for set 2022-04-01 16:44:25 +08:00
dudaodong
79867e8a63 feat: add ReverseIntersect function 2022-03-31 19:42:27 +08:00
dudaodong
19939c2b03 feat: add Minus func 2022-03-31 18:00:11 +08:00
dudaodong
bf7ffbfa8d feat: add Intersect func 2022-03-31 16:56:12 +08:00
dudaodong
cbb46f9cb4 feat: add Filter func 2022-03-30 19:52:43 +08:00
dudaodong
3ad142d5d7 feat: add ForEach func 2022-03-30 19:41:55 +08:00
dudaodong
1a436aeb41 feat: add Merge func 2022-03-30 18:06:22 +08:00
dudaodong
1af8fe8daf test: add unit test for Keys and Values func 2022-03-30 17:45:17 +08:00
dudaodong
ae54c8db6f feat: add Values func 2022-03-30 17:34:01 +08:00
dudaodong
be942ec33e feat: add package maputil and Keys func 2022-03-30 17:27:50 +08:00
115 changed files with 19886 additions and 1019 deletions

1
.gitignore vendored
View File

@@ -6,4 +6,5 @@ fileutil/*.txt
fileutil/*.zip
fileutil/*.link
fileutil/unzip/*
slice/testdata/*
cryptor/*.pem

175
README.md
View File

@@ -1,10 +1,10 @@
<div align=center>
<img src="./logo.png" width="200" height="200"/>
<a href="https://uvdream.github.io/lancet-docs/en/"><img src="./logo.png" width="200" height="200"/></a>
<br/>
![Go version](https://img.shields.io/badge/go-%3E%3D1.16<recommend>-9cf)
[![Release](https://img.shields.io/badge/release-2.0.2-green.svg)](https://github.com/duke-git/lancet/releases)
![Go version](https://img.shields.io/badge/go-%3E%3Dv1.18-9cf)
[![Release](https://img.shields.io/badge/release-2.1.8-green.svg)](https://github.com/duke-git/lancet/releases)
[![GoDoc](https://godoc.org/github.com/duke-git/lancet/v2?status.svg)](https://pkg.go.dev/github.com/duke-git/lancet/v2)
[![Go Report Card](https://goreportcard.com/badge/github.com/duke-git/lancet/v2)](https://goreportcard.com/report/github.com/duke-git/lancet/v2)
[![test](https://github.com/duke-git/lancet/actions/workflows/codecov.yml/badge.svg?branch=main&event=push)](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
@@ -23,7 +23,7 @@ English | [简体中文](./README_zh-CN.md)
## Feature
- 👏 Comprehensive, efficient and reusable.
- 💪 250+ go util functions, support string, slice, datetime, net, crypt...
- 💪 300+ go util functions, support string, slice, datetime, net, crypt...
- 💅 Only depend on the go standard library.
- 🌍 Unit test for every exported function.
@@ -35,9 +35,9 @@ English | [简体中文](./README_zh-CN.md)
go get github.com/duke-git/lancet/v2 // will install latest version of v2.x.x
```
2. <b>For users who use version below go1.18, you should install v1.x.x. now latest v1 is v1.2.7. </b>
2. <b>For users who use version below go1.18, you should install v1.x.x. now latest v1 is v1.3.2. </b>
```go
go get github.com/duke-git/lancet@v1.2.7 // below go1.18, install latest version of v1.x.x
go get github.com/duke-git/lancet@v1.3.2 // below go1.18, install latest version of v1.x.x
```
## Usage
@@ -50,7 +50,7 @@ import "github.com/duke-git/lancet/v2/strutil"
## Example
Here takes the string function ReverseStr (reverse order string) as an example, and the strutil package needs to be imported.
Here takes the string function Reverse (reverse order string) as an example, and the strutil package needs to be imported.
```go
package main
@@ -62,14 +62,14 @@ import (
func main() {
s := "hello"
rs := strutil.ReverseStr(s)
rs := strutil.Reverse(s)
fmt.Println(rs) //olleh
}
```
## API Documentation
### Algorithm package implements some basic algorithm. eg. sort, search.
## [lancet API doc](https://uvdream.github.io/lancet-docs/) Thanks [@UvDream](https://github.com/UvDream) for contributing.
### 1. Algorithm package implements some basic algorithm. eg. sort, search.
```go
import "github.com/duke-git/lancet/v2/algorithm"
@@ -89,7 +89,40 @@ import "github.com/duke-git/lancet/v2/algorithm"
- [LRUCache](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#LRUCache)
### Convertor package contains some functions for data convertion.
### 2. Concurrency package contain some functions to support concurrent programming. eg, goroutine, channel, async.
```go
import "github.com/duke-git/lancet/v2/concurrency"
```
#### Function list:
- [NewChannel](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#NewChannel)
- [Bridge](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#Bridge)
- [FanIn](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#FanIn)
- [Generate](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#Generate)
- [Or](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#Or)
- [OrDone](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#OrDone)
- [Repeat](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#Repeat)
- [RepeatFn](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#RepeatFn)
- [Take](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#Take)
- [Tee](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#Tee)
### 3. Condition package contains some functions for conditional judgment. eg. And, Or, TernaryOperator...
```go
import "github.com/duke-git/lancet/v2/condition"
```
#### Function list:
- [Bool](https://github.com/duke-git/lancet/blob/main/docs/condition.md#Bool)
- [And](https://github.com/duke-git/lancet/blob/main/docs/condition.md#And)
- [Or](https://github.com/duke-git/lancet/blob/main/docs/condition.md#Or)
- [Xor](https://github.com/duke-git/lancet/blob/main/docs/condition.md#Xor)
- [Nor](https://github.com/duke-git/lancet/blob/main/docs/condition.md#Nor)
- [Nand](https://github.com/duke-git/lancet/blob/main/docs/condition.md#Nand)
- [TernaryOperator](https://github.com/duke-git/lancet/blob/main/docs/condition.md#TernaryOperator)
### 4. Convertor package contains some functions for data convertion.
```go
import "github.com/duke-git/lancet/v2/convertor"
@@ -100,12 +133,19 @@ import "github.com/duke-git/lancet/v2/convertor"
- [ToBool](https://github.com/duke-git/lancet/blob/main/docs/convertor.md#ToBool)
- [ToBytes](https://github.com/duke-git/lancet/blob/main/docs/convertor.md#ToBytes)
- [ToChar](https://github.com/duke-git/lancet/blob/main/docs/convertor.md#ToChar)
- [ToChannel](https://github.com/duke-git/lancet/blob/main/docs/convertor.md#ToChannel)
- [ToFloat](https://github.com/duke-git/lancet/blob/main/docs/convertor.md#ToFloat)
- [ToInt](https://github.com/duke-git/lancet/blob/main/docs/convertor.md#ToInt)
- [ToJson](https://github.com/duke-git/lancet/blob/main/docs/convertor.md#ToJson)
- [ToMap](https://github.com/duke-git/lancet/blob/main/docs/convertor.md#ToMap)
- [ToPointer](https://github.com/duke-git/lancet/blob/main/docs/convertor.md#ToPointer)
- [ToString](https://github.com/duke-git/lancet/blob/main/docs/convertor.md#ToString)
- [StructToMap](https://github.com/duke-git/lancet/blob/main/docs/convertor.md#StructToMap)
- [MapToSlice](https://github.com/duke-git/lancet/blob/main/docs/convertor.md#MapToSlice)
- [EncodeByte](https://github.com/duke-git/lancet/blob/main/docs/convertor.md#EncodeByte)
- [DecodeByte](https://github.com/duke-git/lancet/blob/main/docs/convertor.md#DecodeByte)
### Cryptor package is for data encryption and decryption.
### 5. Cryptor package is for data encryption and decryption.
```go
import "github.com/duke-git/lancet/v2/cryptor"
@@ -145,7 +185,7 @@ import "github.com/duke-git/lancet/v2/cryptor"
- [RsaEncrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#RsaEncrypt)
- [RsaDecrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#RsaDecrypt)
### Datetime package supports date and time format and compare.
### 6. Datetime package supports date and time format and compare.
```go
@@ -183,7 +223,33 @@ import "github.com/duke-git/lancet/v2/datetime"
- [ToFormatForTpl](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#ToFormatForTpl)
- [ToIso8601](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#ToIso8601)
### Fileutil package implements some basic functions for file operations.
### 7. Datastructure package constains some common data structure. eg. list, linklist, stack, queue, set, tree, graph.
```go
import list "github.com/duke-git/lancet/v2/datastructure/list"
import link "github.com/duke-git/lancet/v2/datastructure/link"
import stack "github.com/duke-git/lancet/v2/datastructure/stack"
import queue "github.com/duke-git/lancet/v2/datastructure/queue"
import set "github.com/duke-git/lancet/v2/datastructure/set"
import tree "github.com/duke-git/lancet/v2/datastructure/tree"
import heap "github.com/duke-git/lancet/v2/datastructure/heap"
import hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
```
#### Function list:
- [List](https://github.com/duke-git/lancet/blob/main/docs/datastructure/list.md)
- [Linklist](https://github.com/duke-git/lancet/blob/main/docs/datastructure/linklist.md)
- [Stack](https://github.com/duke-git/lancet/blob/main/docs/datastructure/stack.md)
- [Queue](https://github.com/duke-git/lancet/blob/main/docs/datastructure/queue.md)
- [Set](https://github.com/duke-git/lancet/blob/main/docs/datastructure/set.md)
- [Tree](https://github.com/duke-git/lancet/blob/main/docs/datastructure/tree.md)
- [Heap](https://github.com/duke-git/lancet/blob/main/docs/datastructure/heap.md)
- [HashMap](https://github.com/duke-git/lancet/blob/main/docs/datastructure/hashmap.md)
### 8. Fileutil package implements some basic functions for file operations.
```go
import "github.com/duke-git/lancet/v2/fileutil"
@@ -193,6 +259,7 @@ import "github.com/duke-git/lancet/v2/fileutil"
- [ClearFile](https://github.com/duke-git/lancet/blob/main/docs/fileutil.md#ClearFile)
- [CreateFile](https://github.com/duke-git/lancet/blob/main/docs/fileutil.md#CreateFile)
- [CreateDir](https://github.com/duke-git/lancet/blob/main/docs/fileutil.md#CreateDir)
- [CopyFile](https://github.com/duke-git/lancet/blob/main/docs/fileutil.md#CopyFile)
- [FileMode](https://github.com/duke-git/lancet/blob/main/docs/fileutil.md#FileMode)
- [MiMeType](https://github.com/duke-git/lancet/blob/main/docs/fileutil.md#MiMeType)
@@ -206,7 +273,7 @@ import "github.com/duke-git/lancet/v2/fileutil"
- [Zip](https://github.com/duke-git/lancet/blob/main/docs/fileutil.md#Zip)
- [UnZip](https://github.com/duke-git/lancet/blob/main/docs/fileutil.md#UnZip)
### Formatter contains some functions for data formatting.
### 9. Formatter contains some functions for data formatting.
```go
import "github.com/duke-git/lancet/v2/formatter"
@@ -214,7 +281,7 @@ import "github.com/duke-git/lancet/v2/formatter"
#### Function list:
- [Comma](https://github.com/duke-git/lancet/blob/main/docs/formatter.md#Comma)
### Function package can control the flow of function execution and support part of functional programming
### 10. Function package can control the flow of function execution and support part of functional programming
```go
import "github.com/duke-git/lancet/v2/function"
@@ -227,10 +294,28 @@ import "github.com/duke-git/lancet/v2/function"
- [Compose](https://github.com/duke-git/lancet/blob/main/docs/function.md#Compose)
- [Debounced](https://github.com/duke-git/lancet/blob/main/docs/function.md#Debounced)
- [Delay](https://github.com/duke-git/lancet/blob/main/docs/function.md#Delay)
- [Pipeline](https://github.com/duke-git/lancet/blob/main/docs/function.md#Pipeline)
- [Watcher](https://github.com/duke-git/lancet/blob/main/docs/function.md#Watcher)
### Mathutil package implements some functions for math calculation.
### 11. Maputil package includes some functions to manipulate map.
```go
import "github.com/duke-git/lancet/v2/maputil"
```
#### Function list:
- [ForEach](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#ForEach)
- [Filter](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#Filter)
- [Intersect](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#Intersect)
- [Keys](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#Keys)
- [Merge](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#Merge)
- [Minus](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#Minus)
- [Values](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#Values)
- [IsDisjoint](https://github.com/duke-git/lancet/blob/main/docs/maputil.md#IsDisjoint)
### 12. Mathutil package implements some functions for math calculation.
```go
import "github.com/duke-git/lancet/v2/mathutil"
@@ -242,14 +327,16 @@ import "github.com/duke-git/lancet/v2/mathutil"
- [Fibonacci](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Fibonacci)
- [Factorial](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Factorial)
- [Max](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Max)
- [MaxBy](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#MaxBy)
- [Min](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Min)
- [MinBy](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#MinBy)
- [Percent](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Percent)
- [RoundToFloat](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#RoundToFloat)
- [RoundToString](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#RoundToString)
- [TruncRound](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#TruncRound)
### Netutil package contains functions to get net information and send http request.
### 13. Netutil package contains functions to get net information and send http request.
```go
import "github.com/duke-git/lancet/v2/netutil"
@@ -257,19 +344,28 @@ import "github.com/duke-git/lancet/v2/netutil"
#### Function list:
- [ConvertMapToQueryString](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#ConvertMapToQueryString)
- [EncodeUrl](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#EncodeUrl)
- [GetInternalIp](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#GetInternalIp)
- [GetIps](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#GetIps)
- [GetMacAddrs](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#GetMacAddrs)
- [GetPublicIpInfo](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#GetPublicIpInfo)
- [GetRequestPublicIp](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#GetRequestPublicIp)
- [IsPublicIP](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#IsPublicIP)
- [HttpGet](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#HttpGet)
- [HttpDelete](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#HttpDelete)
- [HttpPost](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#HttpPost)
- [HttpPut](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#HttpPut)
- [HttpPatch](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#HttpPatch)
- [IsInternalIP](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#IsInternalIP)
- [HttpRequest](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#HttpRequest)
- [HttpClient](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#HttpClient)
- [SendRequest](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#SendRequest)
- [DecodeResponse](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#DecodeResponse)
- [StructToUrlValues](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#StructToUrlValues)
- [HttpGet<sup>Deprecated</sup>](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#HttpGet)
- [HttpDelete<sup>Deprecated</sup>](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#HttpDelete)
- [HttpPost<sup>Deprecated</sup>](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#HttpPost)
- [HttpPut<sup>Deprecated</sup>](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#HttpPut)
- [HttpPatch<sup>Deprecated</sup>](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#HttpPatch)
- [ParseHttpResponse](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#ParseHttpResponse)
### Random package implements some basic functions to generate random int and string.
### 14. Random package implements some basic functions to generate random int and string.
```go
import "github.com/duke-git/lancet/v2/random"
@@ -281,7 +377,7 @@ import "github.com/duke-git/lancet/v2/random"
- [RandString](https://github.com/duke-git/lancet/blob/main/docs/random.md#RandString)
- [UUIdV4](https://github.com/duke-git/lancet/blob/main/docs/random.md#UUIdV4)
### Retry package is for executing a function repeatedly until it was successful or canceled by the context.
### 15. Retry package is for executing a function repeatedly until it was successful or canceled by the context.
```go
import "github.com/duke-git/lancet/v2/retry"
@@ -294,13 +390,14 @@ import "github.com/duke-git/lancet/v2/retry"
- [RetryDuration](https://github.com/duke-git/lancet/blob/main/docs/retry.md#RetryDuration)
- [RetryTimes](https://github.com/duke-git/lancet/blob/main/docs/retry.md#RetryTimes)
### Slice contains some functions to manipulate slice.
### 16. Slice contains some functions to manipulate slice.
```go
import "github.com/duke-git/lancet/v2/slice"
```
#### Function list:
- [AppendIfAbsent](https://github.com/duke-git/lancet/blob/main/docs/slice.md#AppendIfAbsent)
- [Contain](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Contain)
- [ContainSubSlice](https://github.com/duke-git/lancet/blob/main/docs/slice.md#ContainSubSlice)
- [Chunk](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Chunk)
@@ -312,11 +409,14 @@ import "github.com/duke-git/lancet/v2/slice"
- [DifferenceWith](https://github.com/duke-git/lancet/blob/main/docs/slice.md#DifferenceWith)
- [DeleteAt](https://github.com/duke-git/lancet/blob/main/docs/slice.md#DeleteAt)
- [Drop](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Drop)
- [Equal](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Equal)
- [EqualWith](https://github.com/duke-git/lancet/blob/main/docs/slice.md#EqualWith)
- [Every](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Every)
- [Filter](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Filter)
- [Find](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Find)
- [FindLast](https://github.com/duke-git/lancet/blob/main/docs/slice.md#FindLast)
- [FlattenDeep](#FlattenDeep)
- [Flatten](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Flatten)
- [FlattenDeep](https://github.com/duke-git/lancet/blob/main/docs/slice.md#FlattenDeep)
- [ForEach](https://github.com/duke-git/lancet/blob/main/docs/slice.md#ForEach)
- [GroupBy](https://github.com/duke-git/lancet/blob/main/docs/slice.md#GroupBy)
- [GroupWith](https://github.com/duke-git/lancet/blob/main/docs/slice.md#GroupWith)
@@ -324,19 +424,27 @@ import "github.com/duke-git/lancet/v2/slice"
- [InterfaceSlice](https://github.com/duke-git/lancet/blob/main/docs/slice.md#InterfaceSlice)
- [Intersection](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Intersection)
- [InsertAt](https://github.com/duke-git/lancet/blob/main/docs/slice.md#InsertAt)
- [IndexOf](https://github.com/duke-git/lancet/blob/main/docs/slice.md#IndexOf)
- [LastIndexOf](https://github.com/duke-git/lancet/blob/main/docs/slice.md#LastIndexOf)
- [Map](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Map)
- [Reverse](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Reverse)
- [Reduce](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Reduce)
- [Replace](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Replace)
- [ReplaceAll](https://github.com/duke-git/lancet/blob/main/docs/slice.md#ReplaceAll)
- [Shuffle](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Shuffle)
- [SortByField](https://github.com/duke-git/lancet/blob/main/docs/slice.md#SortByField)
- [Some](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Some)
- [StringSlice](https://github.com/duke-git/lancet/blob/main/docs/slice.md#StringSlice)
- [SymmetricDifference](https://github.com/duke-git/lancet/blob/main/docs/slice.md#SymmetricDifference)
- [ToSlice](https://github.com/duke-git/lancet/blob/main/docs/slice.md#ToSlice)
- [ToSlicePointer](https://github.com/duke-git/lancet/blob/main/docs/slice.md#ToSlicePointer)
- [Unique](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Unique)
- [UniqueBy](https://github.com/duke-git/lancet/blob/main/docs/slice.md#UniqueBy)
- [Union](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Union)
- [UpdateAt](https://github.com/duke-git/lancet/blob/main/docs/slice.md#UpdateAt)
- [Without](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Without)
### Strutil package contains some functions to manipulate string.
### 17. Strutil package contains some functions to manipulate string.
```go
import "github.com/duke-git/lancet/v2/strutil"
```
@@ -355,12 +463,13 @@ import "github.com/duke-git/lancet/v2/strutil"
- [UpperFirst](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#UpperFirst)
- [PadEnd](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#PadEnd)
- [PadStart](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#PadStart)
- [ReverseStr](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#ReverseStr)
- [Reverse](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Reverse)
- [SnakeCase](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#SnakeCase)
- [SplitEx](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#SplitEx)
- [Wrap](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Wrap)
- [Unwrap](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Unwrap)
### System package contain some functions about os, runtime, shell command.
### 19. System package contain some functions about os, runtime, shell command.
```go
import "github.com/duke-git/lancet/v2/system"
@@ -374,8 +483,9 @@ import "github.com/duke-git/lancet/v2/system"
- [RemoveOsEnv](https://github.com/duke-git/lancet/blob/main/docs/system.md#RemoveOsEnv)
- [CompareOsEnv](https://github.com/duke-git/lancet/blob/main/docs/system.md#CompareOsEnv)
- [ExecCommand](https://github.com/duke-git/lancet/blob/main/docs/system.md#ExecCommand)
- [GetOsBits](https://github.com/duke-git/lancet/blob/main/docs/system.md#GetOsBits)
### Validator package contains some functions for data validation.
### 19. Validator package contains some functions for data validation.
```go
import "github.com/duke-git/lancet/v2/validator"
@@ -409,7 +519,8 @@ import "github.com/duke-git/lancet/v2/validator"
- [IsStrongPassword](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsStrongPassword)
- [IsUrl](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsUrl)
- [IsWeakPassword](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsWeakPassword)
### xerror package implements helpers for errors.
- [IsZeroValue](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsZeroValue)
### 20. xerror package implements helpers for errors.
```go
import "github.com/duke-git/lancet/v2/xerror"

View File

@@ -1,10 +1,10 @@
<div align=center>
<img src="./logo.png" width="200" height="200"/>
<a href="https://uvdream.github.io/lancet-docs/"><img src="./logo.png" width="200" height="200"/><a/>
<br/>
![Go version](https://img.shields.io/badge/go-%3E%3D1.16<recommend>-9cf)
[![Release](https://img.shields.io/badge/release-2.0.2-green.svg)](https://github.com/duke-git/lancet/releases)
![Go version](https://img.shields.io/badge/go-%3E%3Dv1.18-9cf)
[![Release](https://img.shields.io/badge/release-2.1.8-green.svg)](https://github.com/duke-git/lancet/releases)
[![GoDoc](https://godoc.org/github.com/duke-git/lancet/v2?status.svg)](https://pkg.go.dev/github.com/duke-git/lancet/v2)
[![Go Report Card](https://goreportcard.com/badge/github.com/duke-git/lancet/v2)](https://goreportcard.com/report/github.com/duke-git/lancet/v2)
[![test](https://github.com/duke-git/lancet/actions/workflows/codecov.yml/badge.svg?branch=main&event=push)](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
@@ -23,7 +23,7 @@
## 特性
- 👏 全面、高效、可复用
- 💪 250+常用go工具函数支持string、slice、datetime、net、crypt...
- 💪 300+常用go工具函数支持string、slice、datetime、net、crypt...
- 💅 只依赖go标准库
- 🌍 所有导出函数单元测试覆盖率100%
@@ -35,9 +35,9 @@
go get github.com/duke-git/lancet/v2 //安装v2最新版本v2.x.x
```
2. <b>使用go1.18以下版本的用户必须安装v1.x.x。目前最新的v1版本是v1.2.7。</b>
2. <b>使用go1.18以下版本的用户必须安装v1.x.x。目前最新的v1版本是v1.3.2。</b>
```go
go get github.com/duke-git/lancet@v1.2.7 // 使用go1.18以下版本, 必须安装v1.x.x版本
go get github.com/duke-git/lancet@v1.3.2 // 使用go1.18以下版本, 必须安装v1.x.x版本
```
## 用法
@@ -50,7 +50,7 @@ import "github.com/duke-git/lancet/v2/strutil"
## 例子
此处以字符串工具函数ReverseStr逆序字符串为例需要导入strutil包:
此处以字符串工具函数Reverse逆序字符串为例需要导入strutil包:
```go
package main
@@ -62,14 +62,14 @@ import (
func main() {
s := "hello"
rs := strutil.ReverseStr(s)
rs := strutil.Reverse(s)
fmt.Println(rs) //olleh
}
```
## API文档
### algorithm算法包实现一些基本算法。eg. sort, search.
## [lancet API doc](https://uvdream.github.io/lancet-docs/) 感谢[@UvDream](https://github.com/UvDream)整理
### 1. algorithm算法包实现一些基本算法。eg. sort, search.
```go
import "github.com/duke-git/lancet/v2/algorithm"
@@ -88,7 +88,40 @@ import "github.com/duke-git/lancet/v2/algorithm"
- [LinearSearch](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#LinearSearch)
- [LRUCache](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#LRUCache)
### convertor转换器包支持一些常见的数据类型转换。
### 2. concurrency并发包包含一些支持并发编程的功能。例如goroutine, channel, async等。
```go
import "github.com/duke-git/lancet/v2/concurrency"
```
#### Function list:
- [NewChannel](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#NewChannel)
- [Bridge](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Bridge)
- [FanIn](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#FanIn)
- [Generate](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Generate)
- [Or](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Or)
- [OrDone](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#OrDone)
- [Repeat](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Repeat)
- [RepeatFn](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#RepeatFn)
- [Take](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Take)
- [Tee](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Tee)
### 3. condition条件包含一些用于条件判断的函数。eg. And, Or, TernaryOperator...
```go
import "github.com/duke-git/lancet/v2/condition"
```
#### Function list:
- [Bool](https://github.com/duke-git/lancet/blob/main/docs/condition_zh-CN.md#Bool)
- [And](https://github.com/duke-git/lancet/blob/main/docs/condition_zh-CN.md#And)
- [Or](https://github.com/duke-git/lancet/blob/main/docs/condition_zh-CN.md#Or)
- [Xor](https://github.com/duke-git/lancet/blob/main/docs/condition_zh-CN.md#Xor)
- [Nor](https://github.com/duke-git/lancet/blob/main/docs/condition_zh-CN.md#Nor)
- [Nand](https://github.com/duke-git/lancet/blob/main/docs/condition_zh-CN.md#Nand)
- [TernaryOperator](https://github.com/duke-git/lancet/blob/main/docs/condition_zh-CN.md#TernaryOperator)
### 4. convertor转换器包支持一些常见的数据类型转换。
```go
import "github.com/duke-git/lancet/v2/convertor"
@@ -99,12 +132,19 @@ import "github.com/duke-git/lancet/v2/convertor"
- [ToBool](https://github.com/duke-git/lancet/blob/main/docs/convertor_zh-CN.md#ToBool)
- [ToBytes](https://github.com/duke-git/lancet/blob/main/docs/convertor_zh-CN.md#ToBytes)
- [ToChar](https://github.com/duke-git/lancet/blob/main/docs/convertor_zh-CN.md#ToChar)
- [ToChannel](https://github.com/duke-git/lancet/blob/main/docs/convertor_zh-CN.md#ToChannel)
- [ToFloat](https://github.com/duke-git/lancet/blob/main/docs/convertor_zh-CN.md#ToFloat)
- [ToInt](https://github.com/duke-git/lancet/blob/main/docs/convertor_zh-CN.md#ToInt)
- [ToJson](https://github.com/duke-git/lancet/blob/main/docs/convertor_zh-CN.md#ToJson)
- [ToMap](https://github.com/duke-git/lancet/blob/main/docs/convertor_zh-CN.md#ToMap)
- [ToPointer](https://github.com/duke-git/lancet/blob/main/docs/convertor_zh-CN.md#ToPointer)
- [ToString](https://github.com/duke-git/lancet/blob/main/docs/convertor_zh-CN.md#ToString)
- [StructToMap](https://github.com/duke-git/lancet/blob/main/docs/convertor_zh-CN.md#StructToMap)
- [MapToSlice](https://github.com/duke-git/lancet/blob/main/docs/convertor_zh-CN.md#MapToSlice)
- [EncodeByte](https://github.com/duke-git/lancet/blob/main/docs/convertor_zh-CN.md#EncodeByte)
- [DecodeByte](https://github.com/duke-git/lancet/blob/main/docs/convertor_zh-CN.md#DecodeByte)
### cryptor加密包支持数据加密和解密获取md5hash值。支持base64, md5, hmac, aes, des, rsa。
### 5. cryptor加密包支持数据加密和解密获取md5hash值。支持base64, md5, hmac, aes, des, rsa。
```go
import "github.com/duke-git/lancet/v2/cryptor"
@@ -144,7 +184,7 @@ import "github.com/duke-git/lancet/v2/cryptor"
- [RsaEncrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#RsaEncrypt)
- [RsaDecrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#RsaDecrypt)
### datetime日期时间处理包格式化日期比较日期。
### 6. datetime日期时间处理包格式化日期比较日期。
```go
@@ -182,7 +222,32 @@ import "github.com/duke-git/lancet/v2/datetime"
- [ToFormatForTpl](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#ToFormatForTpl)
- [ToIso8601](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#ToIso8601)
### fileutil包支持文件基本操作。
### 7. datastructure包含一些普通的数据结构实现。例如list, linklist, stack, queue, set, tree, graph.
```go
import list "github.com/duke-git/lancet/v2/datastructure/list"
import link "github.com/duke-git/lancet/v2/datastructure/link"
import stack "github.com/duke-git/lancet/v2/datastructure/stack"
import queue "github.com/duke-git/lancet/v2/datastructure/queue"
import set "github.com/duke-git/lancet/v2/datastructure/set"
import tree "github.com/duke-git/lancet/v2/datastructure/tree"
import heap "github.com/duke-git/lancet/v2/datastructure/heap"
import hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
```
#### Function list:
- [List](https://github.com/duke-git/lancet/blob/main/docs/datastructure/list_zh-CN.md)
- [Linklist](https://github.com/duke-git/lancet/blob/main/docs/datastructure/linklist_zh-CN.md)
- [Stack](https://github.com/duke-git/lancet/blob/main/docs/datastructure/stack_zh-CN.md)
- [Queue](https://github.com/duke-git/lancet/blob/main/docs/datastructure/queue_zh-CN.md)
- [Set](https://github.com/duke-git/lancet/blob/main/docs/datastructure/set_zh-CN.md)
- [Tree](https://github.com/duke-git/lancet/blob/main/docs/datastructure/tree_zh-CN.md)
- [Heap](https://github.com/duke-git/lancet/blob/main/docs/datastructure/heap.md)
- [HashMap](https://github.com/duke-git/lancet/blob/main/docs/datastructure/hashmap.md)
### 8. fileutil包支持文件基本操作。
```go
import "github.com/duke-git/lancet/v2/fileutil"
@@ -192,6 +257,7 @@ import "github.com/duke-git/lancet/v2/fileutil"
- [ClearFile](https://github.com/duke-git/lancet/blob/main/docs/fileutil_zh-CN.md#ClearFile)
- [CreateFile](https://github.com/duke-git/lancet/blob/main/docs/fileutil_zh-CN.md#CreateFile)
- [CreateDir](https://github.com/duke-git/lancet/blob/main/docs/fileutil_zh-CN.md#CreateDir)
- [CopyFile](https://github.com/duke-git/lancet/blob/main/docs/fileutil_zh-CN.md#CopyFile)
- [FileMode](https://github.com/duke-git/lancet/blob/main/docs/fileutil_zh-CN.md#FileMode)
- [MiMeType](https://github.com/duke-git/lancet/blob/main/docs/fileutil_zh-CN.md#MiMeType)
@@ -205,7 +271,7 @@ import "github.com/duke-git/lancet/v2/fileutil"
- [Zip](https://github.com/duke-git/lancet/blob/main/docs/fileutil_zh-CN.md#Zip)
- [UnZip](https://github.com/duke-git/lancet/blob/main/docs/fileutil_zh-CN.md#UnZip)
### formatter格式化器包含一些数据格式化处理方法。
### 9. formatter格式化器包含一些数据格式化处理方法。
```go
import "github.com/duke-git/lancet/v2/formatter"
@@ -214,7 +280,7 @@ import "github.com/duke-git/lancet/v2/formatter"
- [Comma](https://github.com/duke-git/lancet/blob/main/docs/formatter_zh-CN.md#Comma)
### function函数包控制函数执行流程包含部分函数式编程。
### 10. function函数包控制函数执行流程包含部分函数式编程。
```go
import "github.com/duke-git/lancet/v2/function"
@@ -227,9 +293,27 @@ import "github.com/duke-git/lancet/v2/function"
- [Compose](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Compose)
- [Debounced](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Debounced)
- [Delay](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Delay)
- [Pipeline](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Pipeline)
- [Watcher](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Watcher)
### mathutil包实现了一些数学计算的函数。
### 11. maputil包包括一些操作map的函数.
```go
import "github.com/duke-git/lancet/v2/maputil"
```
#### 函数列表:
- [ForEach](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#ForEach)
- [Filter](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#Filter)
- [Intersect](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#Intersect)
- [Keys](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#Keys)
- [Merge](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#Merge)
- [Minus](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#Minus)
- [Values](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#Values)
- [IsDisjoint](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#IsDisjoint)
### 12. mathutil包实现了一些数学计算的函数。
```go
import "github.com/duke-git/lancet/v2/mathutil"
@@ -241,13 +325,15 @@ import "github.com/duke-git/lancet/v2/mathutil"
- [Fibonacci](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Fibonacci)
- [Factorial](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Factorial)
- [Max](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Max)
- [MaxBy](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#MaxBy)
- [Min](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Min)
- [MinBy](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#MinBy)
- [Percent](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Percent)
- [RoundToFloat](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#RoundToFloat)
- [RoundToString](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#RoundToString)
- [TruncRound](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#TruncRound)
### netutil网络包支持获取ip地址发送http请求。
### 13. netutil网络包支持获取ip地址发送http请求。
```go
import "github.com/duke-git/lancet/v2/netutil"
@@ -256,18 +342,27 @@ import "github.com/duke-git/lancet/v2/netutil"
#### 函数列表:
- [ConvertMapToQueryString](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#ConvertMapToQueryString)
- [GetInternalIp](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#GetInternalIp)
- [EncodeUrl](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#EncodeUrl)
- [GetIps](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#GetIps)
- [GetMacAddrs](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#GetMacAddrs)
- [GetPublicIpInfo](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#GetPublicIpInfo)
- [GetRequestPublicIp](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#GetRequestPublicIp)
- [IsPublicIP](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#IsPublicIP)
- [HttpGet](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#HttpGet)
- [HttpDelete](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#HttpDelete)
- [HttpPost](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#HttpPost)
- [HttpPut](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#HttpPut)
- [HttpPatch](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#HttpPatch)
- [IsInternalIP](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#IsInternalIP)
- [HttpRequest](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#HttpRequest)
- [HttpClient](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#HttpClient)
- [SendRequest](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#SendRequest)
- [DecodeResponse](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#DecodeResponse)
- [StructToUrlValues](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#StructToUrlValues)
- [HttpGet<sup>Deprecated</sup>](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#HttpGet)
- [HttpDelete<sup>Deprecated</sup>](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#HttpDelete)
- [HttpPost<sup>Deprecated</sup>](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#HttpPost)
- [HttpPut<sup>Deprecated</sup>](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#HttpPut)
- [HttpPatch<sup>Deprecated</sup>](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#HttpPatch)
- [ParseHttpResponse](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#ParseHttpResponse)
### random随机数生成器包可以生成随机[]bytes, int, string。
### 14. random随机数生成器包可以生成随机[]bytes, int, string。
```go
import "github.com/duke-git/lancet/v2/random"
@@ -278,7 +373,7 @@ import "github.com/duke-git/lancet/v2/random"
- [RandInt](https://github.com/duke-git/lancet/blob/main/docs/random_zh-CN.md#RandInt)
- [RandString](https://github.com/duke-git/lancet/blob/main/docs/random_zh-CN.md#RandString)
- [UUIdV4](https://github.com/duke-git/lancet/blob/main/docs/random.md#UUIdV4)
### retry重试执行函数直到函数运行成功或被context cancel。
### 15. retry重试执行函数直到函数运行成功或被context cancel。
```go
import "github.com/duke-git/lancet/v2/retry"
@@ -292,13 +387,14 @@ import "github.com/duke-git/lancet/v2/retry"
- [RetryTimes](https://github.com/duke-git/lancet/blob/main/docs/retry_zh-CN.md#RetryTimes)
### slice包包含操作切片的方法集合。
### 16. slice包包含操作切片的方法集合。
```go
import "github.com/duke-git/lancet/v2/slice"
```
#### 函数列表:
- [AppendIfAbsent](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#AppendIfAbsent)
- [Contain](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Contain)
- [ContainSubSlice](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#ContainSubSlice)
- [Chunk](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Chunk)
@@ -314,7 +410,8 @@ import "github.com/duke-git/lancet/v2/slice"
- [Filter](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Filter)
- [Find](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Find)
- [FindLast](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#FindLast)
- [FlattenDeep](#FlattenDeep)
- [Flatten](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Flatten)
- [FlattenDeep](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#FlattenDeep)
- [ForEach](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#ForEach)
- [GroupBy](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#GroupBy)
- [GroupWith](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#GroupWith)
@@ -322,20 +419,28 @@ import "github.com/duke-git/lancet/v2/slice"
- [InterfaceSlice](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#InterfaceSlice)
- [Intersection](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Intersection)
- [InsertAt](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#InsertAt)
- [IndexOf](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#IndexOf)
- [LastIndexOf](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#LastIndexOf)
- [Map](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Map)
- [Reverse](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Reverse)
- [Reduce](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Reduce)
- [Replace](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Replace)
- [ReplaceAll](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#ReplaceAll)
- [Shuffle](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Shuffle)
- [SortByField](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#SortByField)
- [Some](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Some)
- [StringSlice](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#StringSlice)
- [SymmetricDifference](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#SymmetricDifference)
- [ToSlice](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#ToSlice)
- [ToSlicePointer](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#ToSlicePointer)
- [Unique](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Unique)
- [UniqueBy](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#UniqueBy)
- [Union](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Union)
- [UpdateAt](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#UpdateAt)
- [Without](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Without)
### strutil包含处理字符串的相关函数。
### 17. strutil包含处理字符串的相关函数。
```go
import "github.com/duke-git/lancet/v2/strutil"
@@ -355,13 +460,14 @@ import "github.com/duke-git/lancet/v2/strutil"
- [UpperFirst](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#UpperFirst)
- [PadEnd](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#PadEnd)
- [PadStart](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#PadStart)
- [ReverseStr](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#ReverseStr)
- [Reverse](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#Reverse)
- [SnakeCase](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#SnakeCase)
- [SplitEx](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#SplitEx)
- [Wrap](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#Wrap)
- [Unwrap](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#Unwrap)
### system包含os, runtime, shell command相关函数。
### 18. system包含os, runtime, shell command相关函数。
```go
import "github.com/duke-git/lancet/v2/system"
@@ -376,8 +482,9 @@ import "github.com/duke-git/lancet/v2/system"
- [RemoveOsEnv](https://github.com/duke-git/lancet/blob/main/docs/system_zh-CN.md#RemoveOsEnv)
- [CompareOsEnv](https://github.com/duke-git/lancet/blob/main/docs/system_zh-CN.md#CompareOsEnv)
- [ExecCommand](https://github.com/duke-git/lancet/blob/main/docs/system_zh-CN.md#ExecCommand)
- [GetOsBits](https://github.com/duke-git/lancet/blob/main/docs/system_zh-CN.md#GetOsBits)
### validator验证器包包含常用字符串格式验证函数。
### 19. validator验证器包包含常用字符串格式验证函数。
```go
import "github.com/duke-git/lancet/v2/validator"
@@ -410,9 +517,9 @@ import "github.com/duke-git/lancet/v2/validator"
- [IsStrongPassword](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#IsStrongPassword)
- [IsUrl](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#IsUrl)
- [IsWeakPassword](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#IsWeakPassword)
- [IsZeroValue](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#IsZeroValue)
validator.md#IsWeakPassword)
### xerror包实现一些错误处理函数
### 20. xerror包实现一些错误处理函数
```go
import "github.com/duke-git/lancet/v2/xerror"

View File

@@ -7,7 +7,7 @@ package algorithm
import "github.com/duke-git/lancet/v2/lancetconstraints"
// BubbleSort use bubble to sort slice.
func BubbleSort[T any](slice []T, comparator lancetconstraints.Comparator) []T {
func BubbleSort[T any](slice []T, comparator lancetconstraints.Comparator) {
for i := 0; i < len(slice); i++ {
for j := 0; j < len(slice)-1-i; j++ {
isCurrGreatThanNext := comparator.Compare(slice[j], slice[j+1]) == 1
@@ -16,38 +16,26 @@ func BubbleSort[T any](slice []T, comparator lancetconstraints.Comparator) []T {
}
}
}
return slice
}
// InsertionSort use insertion to sort slice.
func InsertionSort[T any](slice []T, comparator lancetconstraints.Comparator) []T {
size := len(slice)
if size <= 1 {
return slice
}
for i := 1; i < size; i++ {
currentItem := slice[i]
preIndex := i - 1
preItem := slice[preIndex]
isPreLessThanCurrent := comparator.Compare(preItem, currentItem) == -1
for preIndex >= 0 && isPreLessThanCurrent {
slice[preIndex+1] = slice[preIndex]
preIndex--
func InsertionSort[T any](slice []T, comparator lancetconstraints.Comparator) {
for i := 0; i < len(slice); i++ {
for j := i; j > 0; j-- {
isPreLessThanCurrent := comparator.Compare(slice[j], slice[j-1]) == -1
if isPreLessThanCurrent {
swap(slice, j, j-1)
} else {
break
}
}
slice[preIndex+1] = currentItem
}
return slice
}
// SelectionSort use selection to sort slice.
func SelectionSort[T any](slice []T, comparator lancetconstraints.Comparator) []T {
func SelectionSort[T any](slice []T, comparator lancetconstraints.Comparator) {
for i := 0; i < len(slice); i++ {
min := i
for j := i + 1; j < len(slice); j++ {
if comparator.Compare(slice[j], slice[min]) == -1 {
min = j
@@ -55,15 +43,11 @@ func SelectionSort[T any](slice []T, comparator lancetconstraints.Comparator) []
}
swap(slice, i, min)
}
return slice
}
// ShellSort shell sort slice.
func ShellSort[T any](slice []T, comparator lancetconstraints.Comparator) []T {
func ShellSort[T any](slice []T, comparator lancetconstraints.Comparator) {
size := len(slice)
if size <= 1 {
return slice
}
gap := 1
for gap < size/3 {
@@ -76,21 +60,21 @@ func ShellSort[T any](slice []T, comparator lancetconstraints.Comparator) []T {
swap(slice, j, j-gap)
}
}
gap /= 3
gap = gap / 3
}
return slice
}
// QuickSort quick sorting for slice, lowIndex is 0 and highIndex is len(slice)-1
func QuickSort[T any](slice []T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) []T {
func QuickSort[T any](slice []T, comparator lancetconstraints.Comparator) {
quickSort(slice, 0, len(slice)-1, comparator)
}
func quickSort[T any](slice []T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) {
if lowIndex < highIndex {
p := partition(slice, lowIndex, highIndex, comparator)
QuickSort(slice, lowIndex, p-1, comparator)
QuickSort(slice, p+1, highIndex, comparator)
quickSort(slice, lowIndex, p-1, comparator)
quickSort(slice, p+1, highIndex, comparator)
}
return slice
}
// partition split slice into two parts
@@ -110,7 +94,7 @@ func partition[T any](slice []T, lowIndex, highIndex int, comparator lancetconst
}
// HeapSort use heap to sort slice
func HeapSort[T any](slice []T, comparator lancetconstraints.Comparator) []T {
func HeapSort[T any](slice []T, comparator lancetconstraints.Comparator) {
size := len(slice)
for i := size/2 - 1; i >= 0; i-- {
@@ -120,8 +104,6 @@ func HeapSort[T any](slice []T, comparator lancetconstraints.Comparator) []T {
swap(slice, 0, j)
sift(slice, 0, j-1, comparator)
}
return slice
}
func sift[T any](slice []T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) {
@@ -145,15 +127,17 @@ func sift[T any](slice []T, lowIndex, highIndex int, comparator lancetconstraint
}
// MergeSort merge sorting for slice
func MergeSort[T any](slice []T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) []T {
func MergeSort[T any](slice []T, comparator lancetconstraints.Comparator) {
mergeSort(slice, 0, len(slice)-1, comparator)
}
func mergeSort[T any](slice []T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) {
if lowIndex < highIndex {
mid := (lowIndex + highIndex) / 2
MergeSort(slice, lowIndex, mid, comparator)
MergeSort(slice, mid+1, highIndex, comparator)
mergeSort(slice, lowIndex, mid, comparator)
mergeSort(slice, mid+1, highIndex, comparator)
merge(slice, lowIndex, mid, highIndex, comparator)
}
return slice
}
func merge[T any](slice []T, lowIndex, midIndex, highIndex int, comparator lancetconstraints.Comparator) {

View File

@@ -28,21 +28,6 @@ func (pc *peopleAgeComparator) Compare(v1 any, v2 any) int {
return 1
}
return 0
//decending order
// if p1.Age > p2.Age {
// return -1
// } else if p1.Age < p2.Age {
// return 1
// }
}
var peoples = []people{
{Name: "a", Age: 20},
{Name: "b", Age: 10},
{Name: "c", Age: 17},
{Name: "d", Age: 8},
{Name: "e", Age: 28},
}
type intComparator struct{}
@@ -60,40 +45,49 @@ func (c *intComparator) Compare(v1 any, v2 any) int {
return 0
}
var intSlice = []int{2, 1, 5, 3, 6, 4}
func TestBubbleSortForStructSlice(t *testing.T) {
asssert := internal.NewAssert(t, "TestBubbleSortForStructSlice")
peoples := []people{
{Name: "a", Age: 20},
{Name: "b", Age: 10},
{Name: "c", Age: 17},
{Name: "d", Age: 8},
{Name: "e", Age: 28},
}
comparator := &peopleAgeComparator{}
sortedPeopleByAge := BubbleSort(peoples, comparator)
t.Log(sortedPeopleByAge)
BubbleSort(peoples, comparator)
expected := "[{d 8} {b 10} {c 17} {a 20} {e 28}]"
actual := fmt.Sprintf("%v", sortedPeopleByAge)
actual := fmt.Sprintf("%v", peoples)
asssert.Equal(expected, actual)
}
func TestBubbleSortForIntSlice(t *testing.T) {
asssert := internal.NewAssert(t, "TestBubbleSortForIntSlice")
numbers := []int{2, 1, 5, 3, 6, 4}
comparator := &intComparator{}
sortedInt := BubbleSort(intSlice, comparator)
expected := []int{1, 2, 3, 4, 5, 6}
BubbleSort(numbers, comparator)
asssert.Equal(expected, sortedInt)
asssert.Equal([]int{1, 2, 3, 4, 5, 6}, numbers)
}
func TestInsertionSort(t *testing.T) {
asssert := internal.NewAssert(t, "TestInsertionSort")
peoples := []people{
{Name: "a", Age: 20},
{Name: "b", Age: 10},
{Name: "c", Age: 17},
{Name: "d", Age: 8},
{Name: "e", Age: 28},
}
comparator := &peopleAgeComparator{}
sortedPeopleByAge := InsertionSort(peoples, comparator)
t.Log(sortedPeopleByAge)
InsertionSort(peoples, comparator)
expected := "[{e 28} {a 20} {c 17} {b 10} {d 8}]"
actual := fmt.Sprintf("%v", sortedPeopleByAge)
expected := "[{d 8} {b 10} {c 17} {a 20} {e 28}]"
actual := fmt.Sprintf("%v", peoples)
asssert.Equal(expected, actual)
}
@@ -101,12 +95,18 @@ func TestInsertionSort(t *testing.T) {
func TestSelectionSort(t *testing.T) {
asssert := internal.NewAssert(t, "TestSelectionSort")
peoples := []people{
{Name: "a", Age: 20},
{Name: "b", Age: 10},
{Name: "c", Age: 17},
{Name: "d", Age: 8},
{Name: "e", Age: 28},
}
comparator := &peopleAgeComparator{}
sortedPeopleByAge := SelectionSort(peoples, comparator)
t.Log(sortedPeopleByAge)
SelectionSort(peoples, comparator)
expected := "[{d 8} {b 10} {c 17} {a 20} {e 28}]"
actual := fmt.Sprintf("%v", sortedPeopleByAge)
actual := fmt.Sprintf("%v", peoples)
asssert.Equal(expected, actual)
}
@@ -114,12 +114,18 @@ func TestSelectionSort(t *testing.T) {
func TestShellSort(t *testing.T) {
asssert := internal.NewAssert(t, "TestShellSort")
peoples := []people{
{Name: "a", Age: 20},
{Name: "b", Age: 10},
{Name: "c", Age: 17},
{Name: "d", Age: 8},
{Name: "e", Age: 28},
}
comparator := &peopleAgeComparator{}
sortedPeopleByAge := ShellSort(peoples, comparator)
t.Log(sortedPeopleByAge)
ShellSort(peoples, comparator)
expected := "[{d 8} {b 10} {c 17} {a 20} {e 28}]"
actual := fmt.Sprintf("%v", sortedPeopleByAge)
actual := fmt.Sprintf("%v", peoples)
asssert.Equal(expected, actual)
}
@@ -127,12 +133,18 @@ func TestShellSort(t *testing.T) {
func TestQuickSort(t *testing.T) {
asssert := internal.NewAssert(t, "TestQuickSort")
peoples := []people{
{Name: "a", Age: 20},
{Name: "b", Age: 10},
{Name: "c", Age: 17},
{Name: "d", Age: 8},
{Name: "e", Age: 28},
}
comparator := &peopleAgeComparator{}
sortedPeopleByAge := QuickSort(peoples, 0, len(peoples)-1, comparator)
t.Log(sortedPeopleByAge)
QuickSort(peoples, comparator)
expected := "[{d 8} {b 10} {c 17} {a 20} {e 28}]"
actual := fmt.Sprintf("%v", sortedPeopleByAge)
actual := fmt.Sprintf("%v", peoples)
asssert.Equal(expected, actual)
}
@@ -140,12 +152,18 @@ func TestQuickSort(t *testing.T) {
func TestHeapSort(t *testing.T) {
asssert := internal.NewAssert(t, "TestHeapSort")
peoples := []people{
{Name: "a", Age: 20},
{Name: "b", Age: 10},
{Name: "c", Age: 17},
{Name: "d", Age: 8},
{Name: "e", Age: 28},
}
comparator := &peopleAgeComparator{}
sortedPeopleByAge := HeapSort(peoples, comparator)
t.Log(sortedPeopleByAge)
HeapSort(peoples, comparator)
expected := "[{d 8} {b 10} {c 17} {a 20} {e 28}]"
actual := fmt.Sprintf("%v", sortedPeopleByAge)
actual := fmt.Sprintf("%v", peoples)
asssert.Equal(expected, actual)
}
@@ -153,12 +171,18 @@ func TestHeapSort(t *testing.T) {
func TestMergeSort(t *testing.T) {
asssert := internal.NewAssert(t, "TestMergeSort")
peoples := []people{
{Name: "a", Age: 20},
{Name: "b", Age: 10},
{Name: "c", Age: 17},
{Name: "d", Age: 8},
{Name: "e", Age: 28},
}
comparator := &peopleAgeComparator{}
sortedPeopleByAge := MergeSort(peoples, 0, len(peoples)-1, comparator)
t.Log(sortedPeopleByAge)
MergeSort(peoples, comparator)
expected := "[{d 8} {b 10} {c 17} {a 20} {e 28}]"
actual := fmt.Sprintf("%v", sortedPeopleByAge)
actual := fmt.Sprintf("%v", peoples)
asssert.Equal(expected, actual)
}
@@ -166,6 +190,13 @@ func TestMergeSort(t *testing.T) {
func TestCountSort(t *testing.T) {
asssert := internal.NewAssert(t, "TestCountSort")
peoples := []people{
{Name: "a", Age: 20},
{Name: "b", Age: 10},
{Name: "c", Age: 17},
{Name: "d", Age: 8},
{Name: "e", Age: 28},
}
comparator := &peopleAgeComparator{}
sortedPeopleByAge := CountSort(peoples, comparator)
t.Log(sortedPeopleByAge)

243
concurrency/channel.go Normal file
View File

@@ -0,0 +1,243 @@
// Copyright 2021 dudaodong@gmail.com. All rights reserved.
// Use of this source code is governed by MIT license
// Package concurrency contain some functions to support concurrent programming. eg, goroutine, channel, async.
package concurrency
import (
"context"
"sync"
)
// Channel is a logic object which can generate or manipulate go channel
// all methods of Channel are in the book tilted《Concurrency in Go》
type Channel struct {
}
// NewChannel return a Channel instance
func NewChannel() *Channel {
return &Channel{}
}
// Generate a data of type any chan, put param `values` into the chan
func (c *Channel) Generate(ctx context.Context, values ...any) <-chan any {
dataStream := make(chan any)
go func() {
defer close(dataStream)
for _, v := range values {
select {
case <-ctx.Done():
return
case dataStream <- v:
}
}
}()
return dataStream
}
// Repeat return a data of type any chan, put param `values` into the chan repeatly until cancel the context.
func (c *Channel) Repeat(ctx context.Context, values ...any) <-chan any {
dataStream := make(chan any)
go func() {
defer close(dataStream)
for {
for _, v := range values {
select {
case <-ctx.Done():
return
case dataStream <- v:
}
}
}
}()
return dataStream
}
// RepeatFn return a chan, excutes fn repeatly, and put the result into retruned chan
// until close the `done` channel
func (c *Channel) RepeatFn(ctx context.Context, fn func() any) <-chan any {
dataStream := make(chan any)
go func() {
defer close(dataStream)
for {
select {
case <-ctx.Done():
return
case dataStream <- fn():
}
}
}()
return dataStream
}
// Take return a chan whose values are tahken from another chan
func (c *Channel) Take(ctx context.Context, valueStream <-chan any, number int) <-chan any {
takeStream := make(chan any)
go func() {
defer close(takeStream)
for i := 0; i < number; i++ {
select {
case <-ctx.Done():
return
case takeStream <- <-valueStream:
}
}
}()
return takeStream
}
// FanIn merge multiple channels into one channel
func (c *Channel) FanIn(ctx context.Context, channels ...<-chan any) <-chan any {
out := make(chan any)
go func() {
var wg sync.WaitGroup
wg.Add(len(channels))
for _, c := range channels {
go func(c <-chan any) {
defer wg.Done()
for v := range c {
select {
case <-ctx.Done():
return
case out <- v:
}
}
}(c)
}
wg.Wait()
close(out)
}()
return out
}
// Tee split one chanel into two channels
func (c *Channel) Tee(ctx context.Context, in <-chan any) (<-chan any, <-chan any) {
out1 := make(chan any)
out2 := make(chan any)
go func() {
defer close(out1)
defer close(out2)
for val := range c.OrDone(ctx, in) {
var out1, out2 = out1, out2
for i := 0; i < 2; i++ {
select {
case <-ctx.Done():
case out1 <- val:
out1 = nil
case out2 <- val:
out2 = nil
}
}
}
}()
return out1, out2
}
// Bridge link multiply channels into one channel
func (c *Channel) Bridge(ctx context.Context, chanStream <-chan <-chan any) <-chan any {
valStream := make(chan any)
go func() {
defer close(valStream)
for {
var stream <-chan any
select {
case maybeStream, ok := <-chanStream:
if ok == false {
return
}
stream = maybeStream
case <-ctx.Done():
return
}
for val := range c.OrDone(ctx, stream) {
select {
case valStream <- val:
case <-ctx.Done():
}
}
}
}()
return valStream
}
// Or read one or more channels into one channel, will close when any readin channel is closed
func (c *Channel) Or(channels ...<-chan any) <-chan any {
switch len(channels) {
case 0:
return nil
case 1:
return channels[0]
}
orDone := make(chan any)
go func() {
defer close(orDone)
switch len(channels) {
case 2:
select {
case <-channels[0]:
case <-channels[1]:
}
default:
m := len(channels) / 2
select {
case <-c.Or(channels[:m]...):
case <-c.Or(channels[m:]...):
}
// select {
// case <-channels[0]:
// case <-channels[1]:
// case <-channels[2]:
// case <-c.Or(append(channels[3:], orDone)...):
// }
}
}()
return orDone
}
// OrDone read a channel into another channel, will close until cancel context.
func (c *Channel) OrDone(ctx context.Context, channel <-chan any) <-chan any {
valStream := make(chan any)
go func() {
defer close(valStream)
for {
select {
case <-ctx.Done():
return
case v, ok := <-channel:
if !ok {
return
}
select {
case valStream <- v:
case <-ctx.Done():
}
}
}
}()
return valStream
}

206
concurrency/channel_test.go Normal file
View File

@@ -0,0 +1,206 @@
package concurrency
import (
"context"
"testing"
"time"
"github.com/duke-git/lancet/v2/internal"
)
func TestGenerate(t *testing.T) {
assert := internal.NewAssert(t, "TestGenerate")
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
c := NewChannel()
intStream := c.Generate(ctx, 1, 2, 3)
// for v := range intStream {
// t.Log(v) //1, 2, 3
// }
assert.Equal(1, <-intStream)
assert.Equal(2, <-intStream)
assert.Equal(3, <-intStream)
}
func TestRepeat(t *testing.T) {
assert := internal.NewAssert(t, "TestRepeat")
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
c := NewChannel()
intStream := c.Take(ctx, c.Repeat(ctx, 1, 2), 5)
// for v := range intStream {
// t.Log(v) //1, 2, 1, 2, 1
// }
assert.Equal(1, <-intStream)
assert.Equal(2, <-intStream)
assert.Equal(1, <-intStream)
assert.Equal(2, <-intStream)
assert.Equal(1, <-intStream)
}
func TestRepeatFn(t *testing.T) {
assert := internal.NewAssert(t, "TestRepeatFn")
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
fn := func() any {
s := "a"
return s
}
c := NewChannel()
dataStream := c.Take(ctx, c.RepeatFn(ctx, fn), 3)
// for v := range dataStream {
// t.Log(v) //a, a, a
// }
assert.Equal("a", <-dataStream)
assert.Equal("a", <-dataStream)
assert.Equal("a", <-dataStream)
}
func TestTake(t *testing.T) {
assert := internal.NewAssert(t, "TestTake")
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
numbers := make(chan any, 5)
numbers <- 1
numbers <- 2
numbers <- 3
numbers <- 4
numbers <- 5
defer close(numbers)
c := NewChannel()
intStream := c.Take(ctx, numbers, 3)
assert.Equal(1, <-intStream)
assert.Equal(2, <-intStream)
assert.Equal(3, <-intStream)
}
func TestFanIn(t *testing.T) {
assert := internal.NewAssert(t, "TestFanIn")
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
c := NewChannel()
channels := make([]<-chan any, 3)
for i := 0; i < 3; i++ {
channels[i] = c.Take(ctx, c.Repeat(ctx, i), 3)
}
mergedChannel := c.FanIn(ctx, channels...)
for val := range mergedChannel {
t.Logf("\t%d\n", val)
}
assert.Equal(1, 1)
}
func TestOr(t *testing.T) {
assert := internal.NewAssert(t, "TestOr")
sig := func(after time.Duration) <-chan any {
c := make(chan interface{})
go func() {
defer close(c)
time.Sleep(after)
}()
return c
}
start := time.Now()
c := NewChannel()
<-c.Or(
sig(1*time.Second),
sig(2*time.Second),
sig(3*time.Second),
sig(4*time.Second),
sig(5*time.Second),
)
t.Logf("done after %v", time.Since(start))
assert.Equal(1, 1)
}
func TestOrDone(t *testing.T) {
assert := internal.NewAssert(t, "TestOrDone")
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
c := NewChannel()
intStream := c.Take(ctx, c.Repeat(ctx, 1), 3)
var res any
for val := range c.OrDone(ctx, intStream) {
t.Logf("%v", val)
res = val
}
assert.Equal(1, res)
}
func TestTee(t *testing.T) {
assert := internal.NewAssert(t, "TestTee")
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
c := NewChannel()
inStream := c.Take(ctx, c.Repeat(ctx, 1), 4)
out1, out2 := c.Tee(ctx, inStream)
for val := range out1 {
val1 := val
val2 := <-out2
// t.Log("val1 is", val1)
// t.Log("val2 is", val2)
assert.Equal(1, val1)
assert.Equal(1, val2)
}
}
func TestBridge(t *testing.T) {
assert := internal.NewAssert(t, "TestBridge")
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
c := NewChannel()
genVals := func() <-chan <-chan any {
chanStream := make(chan (<-chan any))
go func() {
defer close(chanStream)
for i := 0; i < 10; i++ {
stream := make(chan any, 1)
stream <- i
close(stream)
chanStream <- stream
}
}()
return chanStream
}
index := 0
for val := range c.Bridge(ctx, genVals()) {
// t.Logf("%v ", val) //0 1 2 3 4 5 6 7 8 9
assert.Equal(index, val)
index++
}
}

77
condition/condition.go Normal file
View File

@@ -0,0 +1,77 @@
// Copyright 2021 dudaodong@gmail.com. All rights reserved.
// Use of this source code is governed by MIT license
// Package condition contains some functions for conditional judgment. eg. And, Or, TernaryOperator ...
// The implementation of this package refers to the implementation of carlmjohnson's truthy package, you may find more
// useful information in truthy(https://github.com/carlmjohnson/truthy), thanks carlmjohnson.
package condition
import "reflect"
// Bool returns the truthy value of anything.
// If the value's type has a Bool() bool method, the method is called and returned.
// If the type has an IsZero() bool method, the opposite value is returned.
// Slices and maps are truthy if they have a length greater than zero.
// All other types are truthy if they are not their zero value.
func Bool[T any](value T) bool {
switch m := any(value).(type) {
case interface{ Bool() bool }:
return m.Bool()
case interface{ IsZero() bool }:
return !m.IsZero()
}
return reflectValue(&value)
}
func reflectValue(vp any) bool {
switch rv := reflect.ValueOf(vp).Elem(); rv.Kind() {
case reflect.Map, reflect.Slice:
return rv.Len() != 0
default:
is := rv.IsZero()
return !is
}
}
// And returns true if both a and b are truthy.
func And[T, U any](a T, b U) bool {
return Bool(a) && Bool(b)
}
// Or returns false iff neither a nor b is truthy.
func Or[T, U any](a T, b U) bool {
return Bool(a) || Bool(b)
}
// Xor returns true iff a or b but not both is truthy.
func Xor[T, U any](a T, b U) bool {
valA := Bool(a)
valB := Bool(b)
return (valA || valB) && valA != valB
}
// Nor returns true iff neither a nor b is truthy.
func Nor[T, U any](a T, b U) bool {
return !(Bool(a) || Bool(b))
}
// Xnor returns true iff both a and b or neither a nor b are truthy.
func Xnor[T, U any](a T, b U) bool {
valA := Bool(a)
valB := Bool(b)
return (valA && valB) || (!valA && !valB)
}
// Nand returns false iff both a and b are truthy.
func Nand[T, U any](a T, b U) bool {
return !Bool(a) || !Bool(b)
}
// TernaryOperator checks the value of param `isTrue`, if true return ifValue else return elseValue
func TernaryOperator[T, U any](isTrue T, ifValue U, elseValue U) U {
if Bool(isTrue) {
return ifValue
} else {
return elseValue
}
}

119
condition/condition_test.go Normal file
View File

@@ -0,0 +1,119 @@
package condition
import (
"errors"
"testing"
"time"
"github.com/duke-git/lancet/v2/internal"
)
type TestStruct struct{}
func TestBool(t *testing.T) {
assert := internal.NewAssert(t, "TestBool")
// bool
assert.Equal(false, Bool(false))
assert.Equal(true, Bool(true))
// integer
assert.Equal(false, Bool(0))
assert.Equal(true, Bool(1))
// float
assert.Equal(false, Bool(0.0))
assert.Equal(true, Bool(0.1))
// string
assert.Equal(false, Bool(""))
assert.Equal(true, Bool(" "))
assert.Equal(true, Bool("0"))
// slice
var nums [2]int
assert.Equal(false, Bool(nums))
nums = [2]int{0, 1}
assert.Equal(true, Bool(nums))
// map
assert.Equal(false, Bool(map[string]string{}))
assert.Equal(true, Bool(map[string]string{"a": "a"}))
// channel
var ch chan int
assert.Equal(false, Bool(ch))
ch = make(chan int)
assert.Equal(true, Bool(ch))
// interface
var err error
assert.Equal(false, Bool(err))
err = errors.New("error message")
assert.Equal(true, Bool(err))
// struct
assert.Equal(false, Bool(struct{}{}))
assert.Equal(true, Bool(time.Now()))
// struct pointer
ts := TestStruct{}
assert.Equal(false, Bool(ts))
assert.Equal(true, Bool(&ts))
}
func TestAnd(t *testing.T) {
assert := internal.NewAssert(t, "TestAnd")
assert.Equal(false, And(0, 1))
assert.Equal(false, And(0, ""))
assert.Equal(false, And(0, "0"))
assert.Equal(true, And(1, "0"))
}
func TestOr(t *testing.T) {
assert := internal.NewAssert(t, "TestOr")
assert.Equal(false, Or(0, ""))
assert.Equal(true, Or(0, 1))
assert.Equal(true, Or(0, "0"))
assert.Equal(true, Or(1, "0"))
}
func TestXor(t *testing.T) {
assert := internal.NewAssert(t, "TestOr")
assert.Equal(false, Xor(0, 0))
assert.Equal(true, Xor(0, 1))
assert.Equal(true, Xor(1, 0))
assert.Equal(false, Xor(1, 1))
}
func TestNor(t *testing.T) {
assert := internal.NewAssert(t, "TestNor")
assert.Equal(true, Nor(0, 0))
assert.Equal(false, Nor(0, 1))
assert.Equal(false, Nor(1, 0))
assert.Equal(false, Nor(1, 1))
}
func TestXnor(t *testing.T) {
assert := internal.NewAssert(t, "TestXnor")
assert.Equal(true, Xnor(0, 0))
assert.Equal(false, Xnor(0, 1))
assert.Equal(false, Xnor(1, 0))
assert.Equal(true, Xnor(1, 1))
}
func TestNand(t *testing.T) {
assert := internal.NewAssert(t, "TestNand")
assert.Equal(true, Nand(0, 0))
assert.Equal(true, Nand(0, 1))
assert.Equal(true, Nand(1, 0))
assert.Equal(false, Nand(1, 1))
}
func TestTernaryOperator(t *testing.T) {
assert := internal.NewAssert(t, "TernaryOperator")
trueValue := "1"
falseValue := "0"
assert.Equal(trueValue, TernaryOperator(true, trueValue, falseValue))
}

View File

@@ -7,6 +7,7 @@ package convertor
import (
"bytes"
"encoding/binary"
"encoding/gob"
"encoding/json"
"fmt"
"math"
@@ -74,72 +75,86 @@ func ToChar(s string) []string {
return c
}
// ToChannel convert a array of elements to a read-only channels
func ToChannel[T any](array []T) <-chan T {
ch := make(chan T)
go func() {
for _, item := range array {
ch <- item
}
close(ch)
}()
return ch
}
// ToString convert value to string
func ToString(value any) string {
res := ""
result := ""
if value == nil {
return res
return result
}
v := reflect.ValueOf(value)
switch value.(type) {
case float32, float64:
res = strconv.FormatFloat(v.Float(), 'f', -1, 64)
return res
result = strconv.FormatFloat(v.Float(), 'f', -1, 64)
return result
case int, int8, int16, int32, int64:
res = strconv.FormatInt(v.Int(), 10)
return res
result = strconv.FormatInt(v.Int(), 10)
return result
case uint, uint8, uint16, uint32, uint64:
res = strconv.FormatUint(v.Uint(), 10)
return res
result = strconv.FormatUint(v.Uint(), 10)
return result
case string:
res = v.String()
return res
result = v.String()
return result
case []byte:
res = string(v.Bytes())
return res
result = string(v.Bytes())
return result
default:
newValue, _ := json.Marshal(value)
res = string(newValue)
return res
result = string(newValue)
return result
}
}
// ToJson convert value to a valid json string
func ToJson(value any) (string, error) {
res, err := json.Marshal(value)
result, err := json.Marshal(value)
if err != nil {
return "", err
}
return string(res), nil
return string(result), nil
}
// ToFloat convert value to a float64, if input is not a float return 0.0 and error
func ToFloat(value any) (float64, error) {
v := reflect.ValueOf(value)
res := 0.0
result := 0.0
err := fmt.Errorf("ToInt: unvalid interface type %T", value)
switch value.(type) {
case int, int8, int16, int32, int64:
res = float64(v.Int())
return res, nil
result = float64(v.Int())
return result, nil
case uint, uint8, uint16, uint32, uint64:
res = float64(v.Uint())
return res, nil
result = float64(v.Uint())
return result, nil
case float32, float64:
res = v.Float()
return res, nil
result = v.Float()
return result, nil
case string:
res, err = strconv.ParseFloat(v.String(), 64)
result, err = strconv.ParseFloat(v.String(), 64)
if err != nil {
res = 0.0
result = 0.0
}
return res, err
return result, err
default:
return res, err
return result, err
}
}
@@ -147,29 +162,45 @@ func ToFloat(value any) (float64, error) {
func ToInt(value any) (int64, error) {
v := reflect.ValueOf(value)
var res int64
var result int64
err := fmt.Errorf("ToInt: invalid interface type %T", value)
switch value.(type) {
case int, int8, int16, int32, int64:
res = v.Int()
return res, nil
result = v.Int()
return result, nil
case uint, uint8, uint16, uint32, uint64:
res = int64(v.Uint())
return res, nil
result = int64(v.Uint())
return result, nil
case float32, float64:
res = int64(v.Float())
return res, nil
result = int64(v.Float())
return result, nil
case string:
res, err = strconv.ParseInt(v.String(), 0, 64)
result, err = strconv.ParseInt(v.String(), 0, 64)
if err != nil {
res = 0
result = 0
}
return res, err
return result, err
default:
return res, err
return result, err
}
}
// ToPointer returns a pointer to this value
func ToPointer[T any](value T) *T {
return &value
}
// ToMap convert a slice or an array of structs to a map based on iteratee function
func ToMap[T any, K comparable, V any](array []T, iteratee func(T) (K, V)) map[K]V {
result := make(map[K]V, len(array))
for _, item := range array {
k, v := iteratee(item)
result[k] = v
}
return result
}
// StructToMap convert struct to map, only convert exported struct field
// map key is specified same as struct field tag `json` value
func StructToMap(value any) (map[string]any, error) {
@@ -183,7 +214,7 @@ func StructToMap(value any) (map[string]any, error) {
return nil, fmt.Errorf("data type %T not support, shuld be struct or pointer to struct", value)
}
res := make(map[string]any)
result := make(map[string]any)
fieldNum := t.NumField()
pattern := `^[A-Z]`
@@ -192,12 +223,23 @@ func StructToMap(value any) (map[string]any, error) {
name := t.Field(i).Name
tag := t.Field(i).Tag.Get("json")
if regex.MatchString(name) && tag != "" {
//res[name] = v.Field(i).Interface()
res[tag] = v.Field(i).Interface()
//result[name] = v.Field(i).Interface()
result[tag] = v.Field(i).Interface()
}
}
return res, nil
return result, nil
}
// MapToSlice convert a map to a slice based on iteratee function
func MapToSlice[T any, K comparable, V any](aMap map[K]V, iteratee func(K, V) T) []T {
result := make([]T, 0, len(aMap))
for k, v := range aMap {
result = append(result, iteratee(k, v))
}
return result
}
// ColorHexToRGB convert hex color to rgb color
@@ -229,3 +271,21 @@ func ColorRGBToHex(red, green, blue int) string {
return "#" + r + g + b
}
// EncodeByte encode data to byte
func EncodeByte(data any) ([]byte, error) {
buffer := bytes.NewBuffer(nil)
encoder := gob.NewEncoder(buffer)
err := encoder.Encode(data)
if err != nil {
return nil, err
}
return buffer.Bytes(), nil
}
// DecodeByte decode byte data to target object
func DecodeByte(data []byte, target any) error {
buffer := bytes.NewBuffer(data)
decoder := gob.NewDecoder(buffer)
return decoder.Decode(target)
}

View File

@@ -2,9 +2,11 @@ package convertor
import (
"fmt"
"strconv"
"testing"
"github.com/duke-git/lancet/v2/internal"
"github.com/duke-git/lancet/v2/slice"
)
func TestToChar(t *testing.T) {
@@ -21,6 +23,23 @@ func TestToChar(t *testing.T) {
}
}
func TestToChannel(t *testing.T) {
assert := internal.NewAssert(t, "TestToChannel")
ch := ToChannel([]int{1, 2, 3})
val1, _ := <-ch
assert.Equal(1, val1)
val2, _ := <-ch
assert.Equal(2, val2)
val3, _ := <-ch
assert.Equal(3, val3)
_, ok := <-ch
assert.Equal(false, ok)
}
func TestToBool(t *testing.T) {
assert := internal.NewAssert(t, "TestToBool")
@@ -142,6 +161,25 @@ func TestToJson(t *testing.T) {
assert.Equal("{\"Name\":\"TestStruct\"}", structJsonStr)
}
func TestToMap(t *testing.T) {
assert := internal.NewAssert(t, "TestToMap")
type Message struct {
name string
code int
}
messages := []Message{
{name: "Hello", code: 100},
{name: "Hi", code: 101},
}
result := ToMap(messages, func(msg Message) (int, string) {
return msg.code, msg.name
})
expected := map[int]string{100: "Hello", 101: "Hi"}
assert.Equal(expected, result)
}
func TestStructToMap(t *testing.T) {
assert := internal.NewAssert(t, "TestStructToMap")
@@ -154,10 +192,25 @@ func TestStructToMap(t *testing.T) {
100,
}
pm, _ := StructToMap(p)
var expected = map[string]any{"name": "test"}
expected := map[string]any{"name": "test"}
assert.Equal(expected, pm)
}
func TestMapToSlice(t *testing.T) {
assert := internal.NewAssert(t, "TestMapToSlice")
aMap := map[string]int{"a": 1, "b": 2, "c": 3}
result := MapToSlice(aMap, func(key string, value int) string {
return key + ":" + strconv.Itoa(value)
})
assert.Equal(3, len(result))
assert.Equal(true, slice.Contain(result, "a:1"))
assert.Equal(true, slice.Contain(result, "b:2"))
assert.Equal(true, slice.Contain(result, "c:3"))
}
func TestColorHexToRGB(t *testing.T) {
colorHex := "#003366"
r, g, b := ColorHexToRGB(colorHex)
@@ -178,3 +231,28 @@ func TestColorRGBToHex(t *testing.T) {
assert := internal.NewAssert(t, "TestColorRGBToHex")
assert.Equal(expected, colorHex)
}
func TestToPointer(t *testing.T) {
assert := internal.NewAssert(t, "TestToPointer")
result := ToPointer(123)
assert.Equal(*result, 123)
}
func TestEncodeByte(t *testing.T) {
assert := internal.NewAssert(t, "TestEncodeByte")
byteData, _ := EncodeByte("abc")
expected := []byte{6, 12, 0, 3, 97, 98, 99}
assert.Equal(expected, byteData)
}
func TestDecodeByte(t *testing.T) {
assert := internal.NewAssert(t, "TestDecodeByte")
var obj string
byteData := []byte{6, 12, 0, 3, 97, 98, 99}
DecodeByte(byteData, &obj)
assert.Equal("abc", obj)
}

View File

@@ -38,7 +38,7 @@ func AesEcbEncrypt(data, key []byte) []byte {
func AesEcbDecrypt(encrypted, key []byte) []byte {
cipher, _ := aes.NewCipher(generateAesKey(key))
decrypted := make([]byte, len(encrypted))
//
for bs, be := 0, cipher.BlockSize(); bs < len(encrypted); bs, be = bs+cipher.BlockSize(), be+cipher.BlockSize() {
cipher.Decrypt(decrypted[bs:be], encrypted[bs:be])
}
@@ -54,14 +54,18 @@ func AesEcbDecrypt(encrypted, key []byte) []byte {
// AesCbcEncrypt encrypt data with key use AES CBC algorithm
// len(key) should be 16, 24 or 32
func AesCbcEncrypt(data, key []byte) []byte {
// len(key) should be 16, 24 or 32
block, _ := aes.NewCipher(key)
blockSize := block.BlockSize()
data = pkcs7Padding(data, blockSize)
blockMode := cipher.NewCBCEncrypter(block, key[:blockSize])
data = pkcs7Padding(data, block.BlockSize())
encrypted := make([]byte, aes.BlockSize+len(data))
iv := encrypted[:aes.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
panic(err)
}
mode := cipher.NewCBCEncrypter(block, iv)
mode.CryptBlocks(encrypted[aes.BlockSize:], data)
encrypted := make([]byte, len(data))
blockMode.CryptBlocks(encrypted, data)
return encrypted
}
@@ -69,12 +73,14 @@ func AesCbcEncrypt(data, key []byte) []byte {
// len(key) should be 16, 24 or 32
func AesCbcDecrypt(encrypted, key []byte) []byte {
block, _ := aes.NewCipher(key)
blockSize := block.BlockSize()
blockMode := cipher.NewCBCDecrypter(block, key[:blockSize])
decrypted := make([]byte, len(encrypted))
blockMode.CryptBlocks(decrypted, encrypted)
decrypted = pkcs7UnPadding(decrypted)
iv := encrypted[:aes.BlockSize]
encrypted = encrypted[aes.BlockSize:]
mode := cipher.NewCBCDecrypter(block, iv)
mode.CryptBlocks(encrypted, encrypted)
decrypted := pkcs7UnPadding(encrypted)
return decrypted
}

View File

@@ -55,12 +55,16 @@ func DesEcbDecrypt(encrypted, key []byte) []byte {
// len(key) should be 8
func DesCbcEncrypt(data, key []byte) []byte {
block, _ := des.NewCipher(key)
blockSize := block.BlockSize()
data = pkcs7Padding(data, blockSize)
blockMode := cipher.NewCBCEncrypter(block, key[:blockSize])
data = pkcs7Padding(data, block.BlockSize())
encrypted := make([]byte, len(data))
blockMode.CryptBlocks(encrypted, data)
encrypted := make([]byte, des.BlockSize+len(data))
iv := encrypted[:des.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
panic(err)
}
mode := cipher.NewCBCEncrypter(block, iv)
mode.CryptBlocks(encrypted[des.BlockSize:], data)
return encrypted
}
@@ -69,13 +73,14 @@ func DesCbcEncrypt(data, key []byte) []byte {
// len(key) should be 8
func DesCbcDecrypt(encrypted, key []byte) []byte {
block, _ := des.NewCipher(key)
blockSize := block.BlockSize()
blockMode := cipher.NewCBCDecrypter(block, key[:blockSize])
decrypted := make([]byte, len(encrypted))
blockMode.CryptBlocks(decrypted, encrypted)
decrypted = pkcs7UnPadding(decrypted)
iv := encrypted[:des.BlockSize]
encrypted = encrypted[des.BlockSize:]
mode := cipher.NewCBCDecrypter(block, iv)
mode.CryptBlocks(encrypted, encrypted)
decrypted := pkcs7UnPadding(encrypted)
return decrypted
}

View File

@@ -0,0 +1,175 @@
// Copyright 2021 dudaodong@gmail.com. All rights reserved.
// Use of this source code is governed by MIT license
// Package datastructure implements some data structure. eg. list, linklist, stack, queue, tree, graph.
package datastructure
import (
"fmt"
"hash/fnv"
)
var defaultMapCapacity uint64 = 1 << 10
type mapNode struct {
key any
value any
next *mapNode
}
//HashMap implements a hash map
type HashMap struct {
capacity uint64
size uint64
table []*mapNode
}
// NewHashMap return a HashMap instance
func NewHashMap() *HashMap {
return &HashMap{
capacity: defaultMapCapacity,
table: make([]*mapNode, defaultMapCapacity),
}
}
// NewHashMapWithCapacity return a HashMap instance with given size and capacity
func NewHashMapWithCapacity(size, capacity uint64) *HashMap {
return &HashMap{
size: size,
capacity: capacity,
table: make([]*mapNode, capacity),
}
}
// Get return the value of given key in hashmap
func (hm *HashMap) Get(key any) any {
hashValue := hm.hash(key)
node := hm.table[hashValue]
if node != nil {
return node.value
}
return nil
}
// Put new key value in hashmap
func (hm *HashMap) Put(key any, value any) {
hm.putValue(hm.hash(key), key, value)
}
func (hm *HashMap) putValue(hash uint64, key, value any) {
if hm.capacity == 0 {
hm.capacity = defaultMapCapacity
hm.table = make([]*mapNode, defaultMapCapacity)
}
node := hm.table[hash]
if node == nil {
hm.table[hash] = newMapNode(key, value)
} else if node.key == key {
hm.table[hash] = newMapNodeWithNext(key, value, node)
} else {
hm.resize()
hm.putValue(hash, value, value)
}
hm.size++
}
// Delete item by given key in hashmap
func (hm *HashMap) Delete(key any) {
hash := hm.hash(key)
node := hm.table[hash]
if node == nil {
return
}
hm.table = append(hm.table[:hash], hm.table[hash+1:]...)
hm.size--
}
// Contains checks if given key is in hashmap or not
func (hm *HashMap) Contains(key any) bool {
node := hm.table[hm.hash(key)]
return node != nil
}
// Iterate executes iteratee funcation for every key and value pair of hashmap (random order)
func (hm *HashMap) Iterate(iteratee func(key, value any)) {
if hm.size > 0 {
for i := 0; i < len(hm.table); i++ {
item := hm.table[i]
if item != nil {
iteratee(item.key, item.value)
}
}
}
}
// Keys returns a slice of the hashmap's keys (random order)
func (hm *HashMap) Keys() []any {
keys := make([]any, int(hm.size))
index := 0
if hm.size > 0 {
hm.Iterate(func(key, value any) {
keys[index] = key
index++
})
}
return keys
}
// Values returns a slice of the hashmap's keys (random order)
func (hm *HashMap) Values() []any {
values := make([]any, int(hm.size))
index := 0
if hm.size > 0 {
hm.Iterate(func(key, value any) {
values[index] = value
index++
})
}
return values
}
func (hm *HashMap) resize() {
hm.capacity <<= 1
tempTable := hm.table
hm.table = make([]*mapNode, hm.capacity)
for i := 0; i < len(tempTable); i++ {
node := tempTable[i]
if node == nil {
continue
}
hm.table[hm.hash(node.key)] = node
}
}
func (hm *HashMap) hash(key any) uint64 {
h := fnv.New64a()
_, _ = h.Write([]byte(fmt.Sprintf("%v", key)))
hashValue := h.Sum64()
return (hm.capacity - 1) & (hashValue ^ (hashValue >> 16))
}
func newMapNode(key, value any) *mapNode {
return &mapNode{
key: key,
value: value,
}
}
func newMapNodeWithNext(key, value any, next *mapNode) *mapNode {
return &mapNode{
key: key,
value: value,
next: next,
}
}

View File

@@ -0,0 +1,71 @@
package datastructure
import (
"testing"
"github.com/duke-git/lancet/v2/internal"
)
func TestHashMap_PutAndGet(t *testing.T) {
assert := internal.NewAssert(t, "TestHashMap_PutAndGet")
hm := NewHashMap()
hm.Put("abc", 3)
assert.Equal(3, hm.Get("abc"))
assert.IsNil(hm.Get("abcd"))
hm.Put("abc", 4)
assert.Equal(4, hm.Get("abc"))
}
func TestHashMap_Resize(t *testing.T) {
assert := internal.NewAssert(t, "TestHashMap_Resize")
hm := NewHashMapWithCapacity(3, 3)
for i := 0; i < 20; i++ {
hm.Put(i, 10)
}
assert.Equal(10, hm.Get(5))
}
func TestHashMap_Delete(t *testing.T) {
assert := internal.NewAssert(t, "TestHashMap_Delete")
hm := NewHashMap()
hm.Put("abc", 3)
assert.Equal(3, hm.Get("abc"))
hm.Delete("abc")
assert.IsNil(hm.Get("abc"))
}
func TestHashMap_Contains(t *testing.T) {
assert := internal.NewAssert(t, "TestHashMap_Contains")
hm := NewHashMap()
assert.Equal(false, hm.Contains("abc"))
hm.Put("abc", 3)
assert.Equal(true, hm.Contains("abc"))
}
func TestHashMap_KeysValues(t *testing.T) {
assert := internal.NewAssert(t, "TestHashMap_KeysValues")
hm := NewHashMap()
hm.Put("a", 1)
hm.Put("b", 2)
hm.Put("c", 3)
keys := hm.Keys()
values := hm.Values()
t.Log(keys, values)
assert.Equal(3, len(values))
assert.Equal(3, len(keys))
}

View File

@@ -0,0 +1,203 @@
// Copyright 2021 dudaodong@gmail.com. All rights reserved.
// Use of this source code is governed by MIT license
// Package datastructure implements some data structure. eg. list, linklist, stack, queue, tree, graph.
package datastructure
import (
"fmt"
"github.com/duke-git/lancet/v2/lancetconstraints"
)
// MaxHeap implements a binary max heap
// type T should implements Compare function in lancetconstraints.Comparator interface.
type MaxHeap[T any] struct {
data []T
comparator lancetconstraints.Comparator
}
// NewMaxHeap returns a MaxHeap instance with the given comparator.
func NewMaxHeap[T any](comparator lancetconstraints.Comparator) *MaxHeap[T] {
return &MaxHeap[T]{
data: make([]T, 0),
comparator: comparator,
}
}
// BuildMaxHeap builds a MaxHeap instance with data and given comparator.
func BuildMaxHeap[T any](data []T, comparator lancetconstraints.Comparator) *MaxHeap[T] {
heap := &MaxHeap[T]{
data: make([]T, 0, len(data)),
comparator: comparator,
}
for _, v := range data {
heap.Push(v)
}
return heap
}
// Push value into the heap
func (h *MaxHeap[T]) Push(value T) {
h.data = append(h.data, value)
h.heapifyUp(len(h.data) - 1)
}
// heapifyUp heapify the data from bottom to top
func (h *MaxHeap[T]) heapifyUp(i int) {
for h.comparator.Compare(h.data[parentIndex(i)], h.data[i]) < 0 {
h.swap(parentIndex(i), i)
i = parentIndex(i)
}
}
// Pop return the largest value, and remove it from the heap
// if heap is empty, return zero value and fasle
func (h *MaxHeap[T]) Pop() (T, bool) {
var val T
if h.Size() == 0 {
return val, false
}
val = h.data[0]
l := len(h.data) - 1
h.data[0] = h.data[l]
h.data = h.data[:l]
h.heapifyDown(0)
return val, true
}
// heapifyDown heapify the data from top to bottom
func (h *MaxHeap[T]) heapifyDown(i int) {
lastIndex := len(h.data) - 1
l, r := leftChildIndex(i), rightChildIndex(i)
childToCompare := 0
for l <= lastIndex {
if l == lastIndex {
childToCompare = l
} else if h.comparator.Compare(h.data[l], h.data[r]) > 0 {
childToCompare = l
} else {
childToCompare = r
}
if h.comparator.Compare(h.data[i], h.data[childToCompare]) < 0 {
h.swap(i, childToCompare)
i = childToCompare
l, r = leftChildIndex(i), rightChildIndex(i)
} else {
break
}
}
}
// Peek returns the largest element from the heap without removing it.
// if heap is empty, it returns zero value and false.
func (h *MaxHeap[T]) Peek() (T, bool) {
if h.Size() == 0 {
var val T
return val, false
}
return h.data[0], true
}
// Size return the number of elements in the heap
func (h *MaxHeap[T]) Size() int {
return len(h.data)
}
// Data return data of the heap
func (h *MaxHeap[T]) Data() []T {
return h.data
}
// PrintStructure print the structure of the heap
func (h *MaxHeap[T]) PrintStructure() {
level := 1
data := h.data
length := len(h.data)
index := 0
list := [][]string{}
temp := []string{}
for index < length {
start := powerTwo(level-1) - 1
end := start + powerTwo(level-1) - 1
temp = append(temp, fmt.Sprintf("%v", data[index]))
index++
if index > end || index >= length {
list = append(list, temp)
temp = []string{}
if index < length {
level++
}
}
}
lastNum := powerTwo(level - 1)
lastLen := lastNum + (lastNum - 1)
heapTree := make([][]string, level, level)
for i := 0; i < level; i++ {
heapTree[i] = make([]string, lastLen, lastLen)
for j := 0; j < lastLen; j++ {
heapTree[i][j] = ""
}
}
for k := 0; k < len(list); k++ {
vals := list[k]
tempLevel := level - k
st := powerTwo(tempLevel-1) - 1
for _, v := range vals {
heapTree[k][st] = v
gap := powerTwo(tempLevel)
st = st + gap
}
}
for m := 0; m < level; m++ {
for n := 0; n < lastLen; n++ {
val := heapTree[m][n]
if val == "" {
fmt.Printf(" ")
} else {
fmt.Printf(val)
}
}
fmt.Println()
}
}
// parentIndex get parent index of the given index
func parentIndex(i int) int {
return (i - 1) / 2
}
// leftChildIndex get left child index of the given index
func leftChildIndex(i int) int {
return 2*i + 1
}
// rightChildIndex get right child index of the given index
func rightChildIndex(i int) int {
return 2*i + 2
}
// swap two elements in the heap
func (h *MaxHeap[T]) swap(i, j int) {
h.data[i], h.data[j] = h.data[j], h.data[i]
}
func powerTwo(n int) int {
return 1 << n
}

View File

@@ -0,0 +1,92 @@
package datastructure
import (
"testing"
"github.com/duke-git/lancet/v2/internal"
)
type intComparator struct{}
func (c *intComparator) Compare(v1, v2 any) int {
val1, _ := v1.(int)
val2, _ := v2.(int)
if val1 < val2 {
return -1
} else if val1 > val2 {
return 1
}
return 0
}
func TestMaxHeap_BuildMaxHeap(t *testing.T) {
assert := internal.NewAssert(t, "TestMaxHeap_BuildMaxHeap")
values := []int{6, 5, 2, 4, 7, 10, 12, 1, 3, 8, 9, 11}
heap := BuildMaxHeap(values, &intComparator{})
expected := []int{12, 9, 11, 4, 8, 10, 7, 1, 3, 5, 6, 2}
assert.Equal(expected, heap.data)
assert.Equal(12, heap.Size())
heap.PrintStructure()
}
func TestMaxHeap_Push(t *testing.T) {
assert := internal.NewAssert(t, "TestMaxHeap_Push")
heap := NewMaxHeap[int](&intComparator{})
values := []int{6, 5, 2, 4, 7, 10, 12, 1, 3, 8, 9, 11}
for _, v := range values {
heap.Push(v)
}
expected := []int{12, 9, 11, 4, 8, 10, 7, 1, 3, 5, 6, 2}
assert.Equal(expected, heap.data)
assert.Equal(12, heap.Size())
heap.PrintStructure()
}
func TestMaxHeap_Pop(t *testing.T) {
assert := internal.NewAssert(t, "TestMaxHeap_Pop")
heap := NewMaxHeap[int](&intComparator{})
_, ok := heap.Pop()
assert.Equal(false, ok)
values := []int{6, 5, 2, 4, 7, 10, 12, 1, 3, 8, 9, 11}
for _, v := range values {
heap.Push(v)
}
val, ok := heap.Pop()
assert.Equal(12, val)
assert.Equal(true, ok)
assert.Equal(11, heap.Size())
}
func TestMaxHeap_Peek(t *testing.T) {
assert := internal.NewAssert(t, "TestMaxHeap_Peek")
heap := NewMaxHeap[int](&intComparator{})
_, ok := heap.Peek()
assert.Equal(false, ok)
values := []int{6, 5, 2, 4, 7, 10, 12, 1, 3, 8, 9, 11}
for _, v := range values {
heap.Push(v)
}
val, ok := heap.Peek()
assert.Equal(12, val)
assert.Equal(true, ok)
assert.Equal(12, heap.Size())
}

View File

@@ -208,13 +208,13 @@ func (link *DoublyLink[T]) Size() int {
// Values return slice of all doubly linklist node value
func (link *DoublyLink[T]) Values() []T {
res := []T{}
result := []T{}
current := link.Head
for current != nil {
res = append(res, current.Value)
result = append(result, current.Value)
current = current.Next
}
return res
return result
}
// Print all nodes info of a linked list
@@ -234,7 +234,7 @@ func (link *DoublyLink[T]) IsEmpty() bool {
return link.length == 0
}
// Clear checks if link is empty or not
// Clear all nodes in doubly linklist
func (link *DoublyLink[T]) Clear() {
link.Head = nil
link.length = 0

View File

@@ -213,13 +213,13 @@ func (link *SinglyLink[T]) Size() int {
// Values return slice of all singly linklist node value
func (link *SinglyLink[T]) Values() []T {
res := []T{}
result := []T{}
current := link.Head
for current != nil {
res = append(res, current.Value)
result = append(result, current.Value)
current = current.Next
}
return res
return result
}
// IsEmpty checks if link is empty or not
@@ -227,7 +227,7 @@ func (link *SinglyLink[T]) IsEmpty() bool {
return link.length == 0
}
// Clear checks if link is empty or not
// Clear all the node in singly linklist
func (link *SinglyLink[T]) Clear() {
link.Head = nil
link.length = 0

View File

@@ -31,7 +31,7 @@ func (l *List[T]) ValueOf(index int) (*T, bool) {
return &l.data[index], true
}
// IndexOf reture the index of value. if not found return -1
// IndexOf returns the index of value. if not found return -1
func (l *List[T]) IndexOf(value T) int {
index := -1
data := l.data
@@ -44,6 +44,48 @@ func (l *List[T]) IndexOf(value T) int {
return index
}
// LastIndexOf returns the index of the last occurrence of the value in this list.
// if not found return -1
func (l *List[T]) LastIndexOf(value T) int {
index := -1
data := l.data
for i := len(data) - 1; i >= 0; i-- {
if reflect.DeepEqual(data[i], value) {
index = i
break
}
}
return index
}
// IndexOfFunc returns the first index satisfying f(v)
// if not found return -1
func (l *List[T]) IndexOfFunc(f func(T) bool) int {
index := -1
data := l.data
for i, v := range data {
if f(v) {
index = i
break
}
}
return index
}
// LastIndexOfFunc returns the index of the last occurrence of the value in this list satisfying f(data[i])
// if not found return -1
func (l *List[T]) LastIndexOfFunc(f func(T) bool) int {
index := -1
data := l.data
for i := len(data) - 1; i >= 0; i-- {
if f(data[i]) {
index = i
break
}
}
return index
}
// Contain checks if the value in the list or not
func (l *List[T]) Contain(value T) bool {
data := l.data
@@ -121,6 +163,31 @@ func (l *List[T]) DeleteAt(index int) {
l.data = data
}
// DeleteIf delete all satisfying f(data[i]), returns count of removed elements
func (l *List[T]) DeleteIf(f func(T) bool) int {
data := l.data
size := len(data)
var c int
for index := 0; index < len(data); index++ {
if !f(data[index]) {
continue
}
if index == size-1 {
data = append(data[:index])
} else {
data = append(data[:index], data[index+1:]...)
index--
}
c++
}
if c > 0 {
l.data = data
}
return c
}
// UpdateAt update value of list at index, index shoud between 0 and list size -1
func (l *List[T]) UpdateAt(index int, value T) {
data := l.data
@@ -132,8 +199,8 @@ func (l *List[T]) UpdateAt(index int, value T) {
l.data = append(data[:index], append([]T{value}, data[index+1:]...)...)
}
// Equtal compare list to other list, use reflect.DeepEqual
func (l *List[T]) Equtal(other *List[T]) bool {
// Equal compare list to other list, use reflect.DeepEqual
func (l *List[T]) Equal(other *List[T]) bool {
if len(l.data) != len(other.data) {
return false
}
@@ -181,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)
@@ -222,24 +294,32 @@ func (l *List[T]) Unique() {
// Union creates a new list contain all element in list l and other, remove duplicate element.
func (l *List[T]) Union(other *List[T]) *List[T] {
res := NewList([]T{})
result := NewList([]T{})
res.data = append(res.data, l.data...)
res.data = append(res.data, other.data...)
res.Unique()
result.data = append(result.data, l.data...)
result.data = append(result.data, other.data...)
result.Unique()
return res
return result
}
// Intersection creates a new list whose element both be contained in list l and other
func (l *List[T]) Intersection(other *List[T]) *List[T] {
res := NewList(make([]T, 0, 0))
result := NewList(make([]T, 0, 0))
for _, v := range l.data {
if other.Contain(v) {
res.data = append(res.data, v)
result.data = append(result.data, v)
}
}
return res
return result
}
// SubList returns a sub list of the original list between the specified fromIndex, inclusive, and toIndex, exclusive.
func (l *List[T]) SubList(fromIndex, toIndex int) *List[T] {
data := l.data[fromIndex:toIndex]
subList := make([]T, len(data))
copy(subList, data)
return NewList(subList)
}

View File

@@ -36,6 +36,51 @@ func TestIndexOf(t *testing.T) {
assert.Equal(-1, i)
}
func TestIndexOfFunc(t *testing.T) {
assert := internal.NewAssert(t, "TestIndexOf")
list := NewList([]int{1, 2, 3})
i := list.IndexOfFunc(func(a int) bool { return a == 1 })
assert.Equal(0, i)
i = list.IndexOfFunc(func(a int) bool { return a == 4 })
assert.Equal(-1, i)
}
func TestLastIndexOf(t *testing.T) {
assert := internal.NewAssert(t, "TestIndexOf")
list := NewList([]int{1, 2, 3, 3, 3, 3, 4, 5, 6, 9})
i := list.LastIndexOf(3)
assert.Equal(5, i)
i = list.LastIndexOf(10)
assert.Equal(-1, i)
i = list.LastIndexOf(4)
assert.Equal(6, i)
i = list.LastIndexOf(1)
assert.Equal(0, i)
}
func TestLastIndexOfFunc(t *testing.T) {
assert := internal.NewAssert(t, "TestIndexOf")
list := NewList([]int{1, 2, 3, 3, 3, 3, 4, 5, 6, 9})
i := list.LastIndexOfFunc(func(a int) bool { return a == 3 })
assert.Equal(5, i)
i = list.LastIndexOfFunc(func(a int) bool { return a == 10 })
assert.Equal(-1, i)
i = list.LastIndexOfFunc(func(a int) bool { return a == 4 })
assert.Equal(6, i)
i = list.LastIndexOfFunc(func(a int) bool { return a == 1 })
assert.Equal(0, i)
}
func TestContain(t *testing.T) {
assert := internal.NewAssert(t, "TestContain")
@@ -155,15 +200,15 @@ func TestUpdateAt(t *testing.T) {
assert.Equal([]int{5, 2, 3, 1}, list.Data())
}
func TestEqutal(t *testing.T) {
assert := internal.NewAssert(t, "TestEqutal")
func TestEqual(t *testing.T) {
assert := internal.NewAssert(t, "TestEqual")
list1 := NewList([]int{1, 2, 3, 4})
list2 := NewList([]int{1, 2, 3, 4})
list3 := NewList([]int{1, 2, 3})
assert.Equal(true, list1.Equtal(list2))
assert.Equal(false, list1.Equtal(list3))
assert.Equal(true, list1.Equal(list2))
assert.Equal(false, list1.Equal(list3))
}
func TestIsEmpty(t *testing.T) {
@@ -192,7 +237,7 @@ func TestClone(t *testing.T) {
list1 := NewList([]int{1, 2, 3, 4})
list2 := list1.Clone()
assert.Equal(true, list1.Equtal(list2))
assert.Equal(true, list1.Equal(list2))
}
func TestMerge(t *testing.T) {
@@ -203,7 +248,7 @@ func TestMerge(t *testing.T) {
expected := NewList([]int{1, 2, 3, 4, 4, 5, 6})
list3 := list1.Merge(list2)
assert.Equal(true, expected.Equtal(list3))
assert.Equal(true, expected.Equal(list3))
}
func TestSize(t *testing.T) {
@@ -216,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")
@@ -224,7 +281,7 @@ func TestSwap(t *testing.T) {
list.Swap(0, 3)
assert.Equal(true, expected.Equtal(list))
assert.Equal(true, expected.Equal(list))
}
func TestReverse(t *testing.T) {
@@ -235,7 +292,7 @@ func TestReverse(t *testing.T) {
list.Reverse()
assert.Equal(true, expected.Equtal(list))
assert.Equal(true, expected.Equal(list))
}
func TestUnique(t *testing.T) {
@@ -246,7 +303,7 @@ func TestUnique(t *testing.T) {
list.Unique()
assert.Equal(true, expected.Equtal(list))
assert.Equal(true, expected.Equal(list))
}
func TestUnion(t *testing.T) {
@@ -257,7 +314,7 @@ func TestUnion(t *testing.T) {
expected := NewList([]int{1, 2, 3, 4, 5, 6})
list3 := list1.Union(list2)
assert.Equal(true, expected.Equtal(list3))
assert.Equal(true, expected.Equal(list3))
}
func TestIntersection(t *testing.T) {
@@ -268,5 +325,35 @@ func TestIntersection(t *testing.T) {
expected := NewList([]int{4})
list3 := list1.Intersection(list2)
assert.Equal(true, expected.Equtal(list3))
assert.Equal(true, expected.Equal(list3))
}
func TestSubSlice(t *testing.T) {
assert := internal.NewAssert(t, "TestSubSlice")
list := NewList([]int{1, 2, 3, 4, 5, 8})
subList := list.SubList(2, 5)
assert.Equal([]int{3, 4, 5}, subList.Data())
}
func BenchmarkSubSlice(b *testing.B) {
list := NewList([]int{1, 2, 3, 4, 5, 8})
for n := 0; n < b.N; n++ {
list.SubList(2, 5)
}
}
func TestDeleteIf(t *testing.T) {
assert := internal.NewAssert(t, "TestDeleteIf")
list := NewList([]int{1, 1, 1, 1, 2, 3, 1, 1, 4, 1, 1, 1, 1, 1, 1})
count := list.DeleteIf(func(a int) bool { return a == 1 })
assert.Equal([]int{2, 3, 4}, list.Data())
assert.Equal(12, count)
count = list.DeleteIf(func(a int) bool { return a == 5 })
assert.Equal([]int{2, 3, 4}, list.Data())
assert.Equal(0, count)
}

View File

@@ -40,12 +40,12 @@ func NewQueueNode[T any](value T) *QueueNode[T] {
// TreeNode is node of tree
type TreeNode[T any] struct {
Data T
Value T
Left *TreeNode[T]
Right *TreeNode[T]
}
// NewTreeNode return a TreeNode pointer
func NewTreeNode[T any](data T) *TreeNode[T] {
return &TreeNode[T]{data, nil, nil}
func NewTreeNode[T any](val T) *TreeNode[T] {
return &TreeNode[T]{val, nil, nil}
}

View File

@@ -1,79 +1,106 @@
package datastructure
import (
"errors"
"fmt"
"reflect"
)
// ArrayQueue implements queue with slice
type ArrayQueue[T any] struct {
data []T
length int
items []T
head int
tail int
capacity int
size int
}
// NewArrayQueue return a empty ArrayQueue pointer
func NewArrayQueue[T any](values ...T) *ArrayQueue[T] {
data := make([]T, len(values))
for i, v := range values {
data[i] = v
func NewArrayQueue[T any](capacity int) *ArrayQueue[T] {
return &ArrayQueue[T]{
items: make([]T, 0, capacity),
head: 0,
tail: 0,
capacity: capacity,
size: 0,
}
return &ArrayQueue[T]{data: data, length: len(values)}
}
// Data return queue data
// Data return slice of queue data
func (q *ArrayQueue[T]) Data() []T {
return q.data
items := []T{}
for i := q.head; i < q.tail; i++ {
items = append(items, q.items[i])
}
return items
}
// Size return length of queue data
// Size return number of elements in queue
func (q *ArrayQueue[T]) Size() int {
return q.length
return q.size
}
// IsEmpty checks if queue is empty or not
func (q *ArrayQueue[T]) IsEmpty() bool {
return q.length == 0
return q.size == 0
}
// IsFull checks if queue is full or not
func (q *ArrayQueue[T]) IsFull() bool {
return q.size == q.capacity
}
// Front return front value of queue
func (q *ArrayQueue[T]) Front() T {
return q.data[0]
return q.items[0]
}
// Back return back value of queue
func (q *ArrayQueue[T]) Back() T {
return q.data[q.length-1]
return q.items[q.size-1]
}
// EnQueue put element into queue
func (q *ArrayQueue[T]) EnQueue(value T) {
q.data = append(q.data, value)
q.length++
func (q *ArrayQueue[T]) Enqueue(item T) bool {
if q.head == 0 && q.tail == q.capacity {
return false
} else if q.head != 0 && q.tail == q.capacity {
for i := q.head; i < q.tail; i++ {
q.items[i-q.head] = q.items[i]
}
q.tail = q.tail - q.head
q.head = 0
}
q.items = append(q.items, item)
q.tail++
q.size++
return true
}
// DeQueue remove head element of queue and return it, if queue is empty, return nil and error
func (q *ArrayQueue[T]) DeQueue() (*T, error) {
if q.IsEmpty() {
return nil, errors.New("queue is empty")
func (q *ArrayQueue[T]) Dequeue() (T, bool) {
var item T
if q.head == q.tail {
return item, false
}
headItem := q.data[0]
q.data = q.data[1:]
q.length--
return &headItem, nil
item = q.items[q.head]
q.head++
q.size--
return item, true
}
// Clear the queue data
func (q *ArrayQueue[T]) Clear() {
q.data = []T{}
q.length = 0
capacity := q.capacity
q.items = make([]T, 0, capacity)
q.head = 0
q.tail = 0
q.size = 0
q.capacity = capacity
}
// Contain checks if the value is in queue or not
func (q *ArrayQueue[T]) Contain(value T) bool {
for _, v := range q.data {
for _, v := range q.items {
if reflect.DeepEqual(v, value) {
return true
}
@@ -84,8 +111,8 @@ func (q *ArrayQueue[T]) Contain(value T) bool {
// Print queue data
func (q *ArrayQueue[T]) Print() {
info := "["
for _, v := range q.data {
info += fmt.Sprintf("%+v, ", v)
for i := q.head; i < q.tail; i++ {
info += fmt.Sprintf("%+v, ", q.items[i])
}
info += "]"
fmt.Println(info)

View File

@@ -6,13 +6,13 @@ import (
"github.com/duke-git/lancet/v2/internal"
)
func TestArrayQueue_EnQueue(t *testing.T) {
assert := internal.NewAssert(t, "TestArrayQueue_EnQueue")
func TestArrayQueue_Enqueue(t *testing.T) {
assert := internal.NewAssert(t, "TestArrayQueue_Enqueue")
queue := NewArrayQueue[int]()
queue.EnQueue(1)
queue.EnQueue(2)
queue.EnQueue(3)
queue := NewArrayQueue[int](5)
queue.Enqueue(1)
queue.Enqueue(2)
queue.Enqueue(3)
expected := []int{1, 2, 3}
data := queue.Data()
@@ -24,26 +24,30 @@ func TestArrayQueue_EnQueue(t *testing.T) {
assert.Equal(3, size)
}
func TestArrayQueue_DeQueue(t *testing.T) {
assert := internal.NewAssert(t, "TestArrayQueue_DeQueue")
func TestArrayQueue_Dequeue(t *testing.T) {
assert := internal.NewAssert(t, "TestArrayQueue_Dequeue")
queue := NewArrayQueue(1, 2, 3)
queue := NewArrayQueue[int](4)
queue.Enqueue(1)
queue.Enqueue(2)
queue.Enqueue(3)
val, err := queue.DeQueue()
if err != nil {
t.Fail()
}
val, ok := queue.Dequeue()
assert.Equal(true, ok)
queue.Print()
assert.Equal(1, *val)
assert.Equal(1, val)
assert.Equal([]int{2, 3}, queue.Data())
}
func TestArrayQueue_Front(t *testing.T) {
assert := internal.NewAssert(t, "TestArrayQueue_Front")
queue := NewArrayQueue(1, 2, 3)
queue := NewArrayQueue[int](4)
queue.Enqueue(1)
queue.Enqueue(2)
queue.Enqueue(3)
val := queue.Front()
queue.Print()
@@ -55,7 +59,11 @@ func TestArrayQueue_Front(t *testing.T) {
func TestArrayQueue_Back(t *testing.T) {
assert := internal.NewAssert(t, "TestArrayQueue_Back")
queue := NewArrayQueue(1, 2, 3)
queue := NewArrayQueue[int](4)
queue.Enqueue(1)
queue.Enqueue(2)
queue.Enqueue(3)
val := queue.Back()
queue.Print()
@@ -67,7 +75,10 @@ func TestArrayQueue_Back(t *testing.T) {
func TestArrayQueue_Contain(t *testing.T) {
assert := internal.NewAssert(t, "TestArrayQueue_Contain")
queue := NewArrayQueue(1, 2, 3)
queue := NewArrayQueue[int](4)
queue.Enqueue(1)
queue.Enqueue(2)
queue.Enqueue(3)
assert.Equal(true, queue.Contain(1))
assert.Equal(false, queue.Contain(4))
@@ -76,11 +87,12 @@ func TestArrayQueue_Contain(t *testing.T) {
func TestArrayQueue_Clear(t *testing.T) {
assert := internal.NewAssert(t, "TestArrayQueue_Clear")
queue := NewArrayQueue[int]()
queue := NewArrayQueue[int](4)
assert.Equal(true, queue.IsEmpty())
assert.Equal(0, queue.Size())
queue.EnQueue(1)
queue.Enqueue(1)
assert.Equal(false, queue.IsEmpty())
assert.Equal(1, queue.Size())
@@ -88,3 +100,14 @@ func TestArrayQueue_Clear(t *testing.T) {
assert.Equal(true, queue.IsEmpty())
assert.Equal(0, queue.Size())
}
func TestArrayQueue_IsFull(t *testing.T) {
assert := internal.NewAssert(t, "TestArrayQueue_IsFull")
queue := NewArrayQueue[int](3)
queue.Enqueue(1)
queue.Enqueue(2)
queue.Enqueue(3)
assert.Equal(true, queue.IsFull())
}

View File

@@ -7,21 +7,21 @@ import (
)
// CircularQueue implements circular queue with slice,
// last index of CircularQueue don't contain value, so acturl capacity is size - 1
// last index of CircularQueue don't contain value, so acturl capacity is capacity - 1
type CircularQueue[T any] struct {
data []T
front int
rear int
size int
data []T
front int
rear int
capacity int
}
// NewCircularQueue return a empty CircularQueue pointer
func NewCircularQueue[T any](size int) *CircularQueue[T] {
data := make([]T, size)
return &CircularQueue[T]{data: data, front: 0, rear: 0, size: size}
func NewCircularQueue[T any](capacity int) *CircularQueue[T] {
data := make([]T, capacity)
return &CircularQueue[T]{data: data, front: 0, rear: 0, capacity: capacity}
}
// Data return queue data
// Data return slice of queue data
func (q *CircularQueue[T]) Data() []T {
data := []T{}
@@ -37,12 +37,12 @@ func (q *CircularQueue[T]) Data() []T {
return data
}
// Length return current data length of queue
func (q *CircularQueue[T]) Length() int {
if q.size == 0 {
// Size return number of elements in circular queue
func (q *CircularQueue[T]) Size() int {
if q.capacity == 0 {
return 0
}
return (q.rear - q.front + q.size) % q.size
return (q.rear - q.front + q.capacity) % q.capacity
}
// IsEmpty checks if queue is empty or not
@@ -52,7 +52,7 @@ func (q *CircularQueue[T]) IsEmpty() bool {
// IsFull checks if queue is full or not
func (q *CircularQueue[T]) IsFull() bool {
return (q.rear+1)%q.size == q.front
return (q.rear+1)%q.capacity == q.front
}
// Front return front value of queue
@@ -65,23 +65,23 @@ func (q *CircularQueue[T]) Back() T {
if q.rear-1 >= 0 {
return q.data[q.rear-1]
}
return q.data[q.size-1]
return q.data[q.capacity-1]
}
// EnQueue put element into queue
func (q *CircularQueue[T]) EnQueue(value T) error {
// Enqueue put element into queue
func (q *CircularQueue[T]) Enqueue(value T) error {
if q.IsFull() {
return errors.New("queue is full!")
}
q.data[q.rear] = value
q.rear = (q.rear + 1) % q.size
q.rear = (q.rear + 1) % q.capacity
return nil
}
// DeQueue remove head element of queue and return it, if queue is empty, return nil and error
func (q *CircularQueue[T]) DeQueue() (*T, error) {
// Dequeue remove head element of queue and return it, if queue is empty, return nil and error
func (q *CircularQueue[T]) Dequeue() (*T, error) {
if q.IsEmpty() {
return nil, errors.New("queue is empty")
}
@@ -89,7 +89,7 @@ func (q *CircularQueue[T]) DeQueue() (*T, error) {
headItem := q.data[q.front]
var t T
q.data[q.front] = t
q.front = (q.front + 1) % q.size
q.front = (q.front + 1) % q.capacity
return &headItem, nil
}
@@ -99,7 +99,7 @@ func (q *CircularQueue[T]) Clear() {
q.data = []T{}
q.front = 0
q.rear = 0
q.size = 0
q.capacity = 0
}
// Contain checks if the value is in queue or not

View File

@@ -6,47 +6,47 @@ import (
"github.com/duke-git/lancet/v2/internal"
)
func TestCircularQueue_EnQueue(t *testing.T) {
assert := internal.NewAssert(t, "TestCircularQueue_EnQueue")
func TestCircularQueue_Enqueue(t *testing.T) {
assert := internal.NewAssert(t, "TestCircularQueue_Enqueue")
queue := NewCircularQueue[int](6)
queue.EnQueue(1)
queue.EnQueue(2)
queue.EnQueue(3)
queue.EnQueue(4)
queue.EnQueue(5)
queue.Enqueue(1)
queue.Enqueue(2)
queue.Enqueue(3)
queue.Enqueue(4)
queue.Enqueue(5)
queue.Print()
// assert.Equal([]int{1, 2, 3, 4, 5}, queue.Data())
assert.Equal(5, queue.Length())
assert.Equal([]int{1, 2, 3, 4, 5}, queue.Data())
assert.Equal(5, queue.Size())
err := queue.EnQueue(6)
err := queue.Enqueue(6)
assert.IsNotNil(err)
}
func TestCircularQueue_DeQueue(t *testing.T) {
func TestCircularQueue_Dequeue(t *testing.T) {
assert := internal.NewAssert(t, "TestCircularQueue_DeQueue")
queue := NewCircularQueue[int](6)
assert.Equal(true, queue.IsEmpty())
queue.EnQueue(1)
queue.EnQueue(2)
queue.EnQueue(3)
queue.EnQueue(4)
queue.EnQueue(5)
queue.Enqueue(1)
queue.Enqueue(2)
queue.Enqueue(3)
queue.Enqueue(4)
queue.Enqueue(5)
val, err := queue.DeQueue()
val, err := queue.Dequeue()
assert.IsNil(err)
assert.Equal(1, *val)
assert.Equal(false, queue.IsFull())
val, _ = queue.DeQueue()
val, _ = queue.Dequeue()
queue.Print()
assert.Equal(2, *val)
queue.EnQueue(6)
queue.Enqueue(6)
queue.Print()
assert.Equal(false, queue.IsFull())
}
@@ -55,24 +55,24 @@ func TestCircularQueue_Front(t *testing.T) {
assert := internal.NewAssert(t, "TestCircularQueue_Front")
queue := NewCircularQueue[int](6)
queue.EnQueue(1)
queue.EnQueue(2)
queue.EnQueue(3)
queue.EnQueue(4)
queue.EnQueue(5)
queue.Enqueue(1)
queue.Enqueue(2)
queue.Enqueue(3)
queue.Enqueue(4)
queue.Enqueue(5)
queue.Print()
queue.DeQueue()
queue.DeQueue()
queue.EnQueue(6)
queue.EnQueue(7)
queue.Dequeue()
queue.Dequeue()
queue.Enqueue(6)
queue.Enqueue(7)
queue.Print()
val := queue.Front()
assert.Equal(3, val)
assert.Equal(5, queue.Length())
assert.Equal(5, queue.Size())
}
func TestCircularQueue_Back(t *testing.T) {
@@ -81,19 +81,19 @@ func TestCircularQueue_Back(t *testing.T) {
queue := NewCircularQueue[int](6)
assert.Equal(true, queue.IsEmpty())
queue.EnQueue(1)
queue.EnQueue(2)
queue.EnQueue(3)
queue.EnQueue(4)
queue.EnQueue(5)
queue.Enqueue(1)
queue.Enqueue(2)
queue.Enqueue(3)
queue.Enqueue(4)
queue.Enqueue(5)
queue.Print()
assert.Equal(5, queue.Back())
queue.DeQueue()
queue.DeQueue()
queue.EnQueue(6)
queue.EnQueue(7)
queue.Dequeue()
queue.Dequeue()
queue.Enqueue(6)
queue.Enqueue(7)
queue.Print()
assert.Equal(7, queue.Back())
@@ -103,7 +103,7 @@ func TestCircularQueue_Contain(t *testing.T) {
assert := internal.NewAssert(t, "TestCircularQueue_Contain")
queue := NewCircularQueue[int](2)
queue.EnQueue(1)
queue.Enqueue(1)
assert.Equal(true, queue.Contain(1))
assert.Equal(false, queue.Contain(2))
}
@@ -113,34 +113,34 @@ func TestCircularQueue_Clear(t *testing.T) {
queue := NewCircularQueue[int](3)
assert.Equal(true, queue.IsEmpty())
assert.Equal(0, queue.Length())
assert.Equal(0, queue.Size())
queue.EnQueue(1)
queue.Enqueue(1)
assert.Equal(false, queue.IsEmpty())
assert.Equal(1, queue.Length())
assert.Equal(1, queue.Size())
queue.Clear()
assert.Equal(true, queue.IsEmpty())
assert.Equal(0, queue.Length())
assert.Equal(0, queue.Size())
}
func TestCircularQueue_Data(t *testing.T) {
assert := internal.NewAssert(t, "TestCircularQueue_Data")
queue := NewCircularQueue[int](6)
queue.EnQueue(1)
queue.EnQueue(2)
queue.EnQueue(3)
queue.EnQueue(4)
queue.EnQueue(5)
queue.Enqueue(1)
queue.Enqueue(2)
queue.Enqueue(3)
queue.Enqueue(4)
queue.Enqueue(5)
queue.Print()
assert.Equal([]int{1, 2, 3, 4, 5}, queue.Data())
queue.DeQueue()
queue.DeQueue()
queue.EnQueue(6)
queue.EnQueue(7)
queue.Dequeue()
queue.Dequeue()
queue.Enqueue(6)
queue.Enqueue(7)
queue.Print()
assert.Equal([]int{3, 4, 5, 6, 7}, queue.Data())

View File

@@ -3,6 +3,7 @@ package datastructure
import (
"errors"
"fmt"
"reflect"
"github.com/duke-git/lancet/v2/datastructure"
)
@@ -19,7 +20,7 @@ func NewLinkedQueue[T any]() *LinkedQueue[T] {
return &LinkedQueue[T]{head: nil, tail: nil, length: 0}
}
// Data return queue data
// Data return slice of queue data
func (q *LinkedQueue[T]) Data() []T {
res := []T{}
current := q.head
@@ -41,8 +42,8 @@ func (q *LinkedQueue[T]) IsEmpty() bool {
return q.length == 0
}
// EnQueue add element into queue
func (q *LinkedQueue[T]) EnQueue(value T) {
// Enqueue put element into queue
func (q *LinkedQueue[T]) Enqueue(value T) {
newNode := datastructure.NewQueueNode(value)
if q.IsEmpty() {
@@ -55,8 +56,8 @@ func (q *LinkedQueue[T]) EnQueue(value T) {
q.length++
}
// DeQueue delete head element of queue then return it, if queue is empty, return nil and error
func (q *LinkedQueue[T]) DeQueue() (*T, error) {
// Dequeue delete head element of queue then return it, if queue is empty, return nil and error
func (q *LinkedQueue[T]) Dequeue() (*T, error) {
if q.IsEmpty() {
return nil, errors.New("queue is empty")
}
@@ -102,3 +103,15 @@ func (q *LinkedQueue[T]) Print() {
info += " ]"
fmt.Println(info)
}
// Contain checks if the value is in queue or not
func (q *LinkedQueue[T]) Contain(value T) bool {
current := q.head
for current != nil {
if reflect.DeepEqual(current.Value, value) {
return true
}
current = current.Next
}
return false
}

View File

@@ -6,13 +6,13 @@ import (
"github.com/duke-git/lancet/v2/internal"
)
func TestLinkedQueue_EnQueue(t *testing.T) {
assert := internal.NewAssert(t, "TestLinkedQueue_EnQueue")
func TestLinkedQueue_Enqueue(t *testing.T) {
assert := internal.NewAssert(t, "TestLinkedQueue_Enqueue")
queue := NewLinkedQueue[int]()
queue.EnQueue(1)
queue.EnQueue(2)
queue.EnQueue(3)
queue.Enqueue(1)
queue.Enqueue(2)
queue.Enqueue(3)
queue.Print()
@@ -20,15 +20,15 @@ func TestLinkedQueue_EnQueue(t *testing.T) {
assert.Equal(3, queue.Size())
}
func TestLinkedQueue_DeQueue(t *testing.T) {
func TestLinkedQueue_Dequeue(t *testing.T) {
assert := internal.NewAssert(t, "TestLinkedQueue_DeQueue")
queue := NewLinkedQueue[int]()
queue.EnQueue(1)
queue.EnQueue(2)
queue.EnQueue(3)
queue.Enqueue(1)
queue.Enqueue(2)
queue.Enqueue(3)
val, _ := queue.DeQueue()
val, _ := queue.Dequeue()
queue.Print()
@@ -43,9 +43,9 @@ func TestLinkedQueue_Front(t *testing.T) {
_, err := queue.Front()
assert.IsNotNil(err)
queue.EnQueue(1)
queue.EnQueue(2)
queue.EnQueue(3)
queue.Enqueue(1)
queue.Enqueue(2)
queue.Enqueue(3)
val, err := queue.Front()
assert.Equal(1, *val)
@@ -59,9 +59,9 @@ func TestLinkedQueue_Back(t *testing.T) {
_, err := queue.Back()
assert.IsNotNil(err)
queue.EnQueue(1)
queue.EnQueue(2)
queue.EnQueue(3)
queue.Enqueue(1)
queue.Enqueue(2)
queue.Enqueue(3)
val, err := queue.Back()
assert.Equal(3, *val)
@@ -74,11 +74,24 @@ func TestLinkedQueue_Clear(t *testing.T) {
queue := NewLinkedQueue[int]()
assert.Equal(true, queue.IsEmpty())
queue.EnQueue(1)
queue.EnQueue(2)
queue.EnQueue(3)
queue.Enqueue(1)
queue.Enqueue(2)
queue.Enqueue(3)
assert.Equal(false, queue.IsEmpty())
queue.Clear()
assert.Equal(true, queue.IsEmpty())
}
func TestLinkedQueue_Contain(t *testing.T) {
assert := internal.NewAssert(t, "TestLinkedQueue_Contain")
queue := NewLinkedQueue[int]()
queue.Enqueue(1)
queue.Enqueue(2)
queue.Enqueue(3)
assert.Equal(true, queue.Contain(1))
assert.Equal(false, queue.Contain(4))
}

View File

@@ -0,0 +1,113 @@
package datastructure
import (
"errors"
"github.com/duke-git/lancet/v2/lancetconstraints"
)
// PriorityQueue is a priority queue implemented by binary heap tree
// type T should implements Compare function in lancetconstraints.Comparator interface.
type PriorityQueue[T any] struct {
items []T
size int
comparator lancetconstraints.Comparator
}
// NewPriorityQueue return a pointer of PriorityQueue
// param `comparator` is used to compare values in the queue
func NewPriorityQueue[T any](capacity int, comparator lancetconstraints.Comparator) *PriorityQueue[T] {
return &PriorityQueue[T]{
items: make([]T, capacity+1),
size: 0,
comparator: comparator,
}
}
// IsEmpty checks if the queue is empty or not
func (q *PriorityQueue[T]) IsEmpty() bool {
return q.size == 0
}
// Size get number of items in the queue
func (q *PriorityQueue[T]) Size() int {
return q.size
}
// IsFull checks if the queue capacity is full or not
func (q *PriorityQueue[T]) IsFull() bool {
return q.size == len(q.items)-1
}
// Data return a slice of queue data
func (q *PriorityQueue[T]) Data() []T {
data := make([]T, q.size)
for i := 1; i < q.size+1; i++ {
data[i-1] = q.items[i]
}
return data
}
// Enqueue insert value into queue
func (q *PriorityQueue[T]) Enqueue(val T) error {
if q.IsFull() {
return errors.New("queue is already full.")
}
q.size++
q.items[q.size] = val
q.swim(q.size)
return nil
}
// Dequeue delete and return max value in queue
func (q *PriorityQueue[T]) Dequeue() (T, bool) {
var val T
if q.IsEmpty() {
return val, false
}
max := q.items[1]
q.swap(1, q.size)
q.size--
q.sink(1)
//set zero value for rest values of the queue
q.items[q.size+1] = val
return max, true
}
// swim when child's key is larger than parent's key, exchange them.
func (q *PriorityQueue[T]) swim(index int) {
for index > 1 && q.comparator.Compare(q.items[index/2], q.items[index]) < 0 {
q.swap(index, index/2)
index = index / 2
}
}
// sink when parent's key smaller than child's key, exchange parent's key with larger child's key.
func (q *PriorityQueue[T]) sink(index int) {
for 2*index <= q.size {
j := 2 * index
// get larger child node index
if j < q.size && q.comparator.Compare(q.items[j], q.items[j+1]) < 0 {
j++
}
// if parent larger than child, stop
if !(q.comparator.Compare(q.items[index], q.items[j]) < 0) {
break
}
q.swap(index, j)
index = j
}
}
// swap the two values at index i and j
func (q *PriorityQueue[T]) swap(i, j int) {
q.items[i], q.items[j] = q.items[j], q.items[i]
}

View File

@@ -0,0 +1,64 @@
package datastructure
import (
"testing"
"github.com/duke-git/lancet/v2/internal"
)
type intComparator struct{}
func (c *intComparator) Compare(v1, v2 any) int {
val1, _ := v1.(int)
val2, _ := v2.(int)
if val1 < val2 {
return -1
} else if val1 > val2 {
return 1
}
return 0
}
func TestPriorityQueue_Enqueue(t *testing.T) {
assert := internal.NewAssert(t, "TestPriorityQueue_Enqueue")
comparator := &intComparator{}
pq := NewPriorityQueue[int](10, comparator)
assert.Equal(true, pq.IsEmpty())
assert.Equal(false, pq.IsFull())
for i := 1; i < 11; i++ {
pq.Enqueue(i)
}
assert.Equal(true, pq.IsFull())
queueData := pq.Data()
assert.Equal([]int{10, 9, 6, 7, 8, 2, 5, 1, 4, 3}, queueData)
}
func TestPriorityQueue_Dequeue(t *testing.T) {
assert := internal.NewAssert(t, "TestPriorityQueue_Dequeue")
comparator := &intComparator{}
pq := NewPriorityQueue[int](10, comparator)
_, ok := pq.Dequeue()
assert.Equal(false, ok)
for i := 1; i < 11; i++ {
pq.Enqueue(i)
}
assert.Equal(10, pq.Size())
val, ok := pq.Dequeue()
assert.Equal(true, ok)
assert.Equal(10, val)
assert.Equal([]int{9, 8, 6, 7, 3, 2, 5, 1, 4}, pq.Data())
assert.Equal(9, pq.Size())
}

View File

@@ -1,7 +1,7 @@
package datastructure
// Set is a data container, like slice, but element of set is not duplicate
type Set[T comparable] map[T]bool
type Set[T comparable] map[T]struct{}
// NewSet return a instance of set
func NewSet[T comparable](values ...T) Set[T] {
@@ -13,7 +13,7 @@ func NewSet[T comparable](values ...T) Set[T] {
// Add value to set
func (s Set[T]) Add(values ...T) {
for _, v := range values {
s[v] = true
s[v] = struct{}{}
}
}
@@ -23,7 +23,7 @@ func (s Set[T]) Contain(value T) bool {
return ok
}
// Contain checks if set contains other set
// ContainAll checks if set contains other set
func (s Set[T]) ContainAll(other Set[T]) bool {
for k := range other {
_, ok := s[k]
@@ -103,3 +103,34 @@ func (s Set[T]) Intersection(other Set[T]) Set[T] {
return set
}
// SymmetricDifference creates a new set whose element is in set1 or set2, but not in both sets
func (s Set[T]) SymmetricDifference(other Set[T]) Set[T] {
set := NewSet[T]()
s.Iterate(func(value T) {
if !other.Contain(value) {
set.Add(value)
}
})
other.Iterate(func(value T) {
if !s.Contain(value) {
set.Add(value)
}
})
return set
}
// Minus creates an set of whose element in origin set but not in compared set
func (s Set[T]) Minus(comparedSet Set[T]) Set[T] {
set := NewSet[T]()
s.Iterate(func(value T) {
if !comparedSet.Contain(value) {
set.Add(value)
}
})
return set
}

View File

@@ -127,3 +127,23 @@ func TestSet_Intersection(t *testing.T) {
assert.Equal(expected, intersectionSet)
}
func TestSet_SymmetricDifference(t *testing.T) {
assert := internal.NewAssert(t, "TestSet_SymmetricDifference")
set1 := NewSet(1, 2, 3)
set2 := NewSet(2, 3, 4, 5)
assert.Equal(NewSet(1, 4, 5), set1.SymmetricDifference(set2))
}
func TestSet_Minus(t *testing.T) {
assert := internal.NewAssert(t, "TestSet_Minus")
set1 := NewSet(1, 2, 3)
set2 := NewSet(2, 3, 4, 5)
set3 := NewSet(2, 3)
assert.Equal(NewSet(1), set1.Minus(set2))
assert.Equal(NewSet(4, 5), set2.Minus(set3))
}

View File

@@ -47,7 +47,7 @@ func (s *ArrayStack[T]) Pop() (*T, error) {
return &topItem, nil
}
// Peak return the top element of stack then return it
// Peak return the top element of stack
func (s *ArrayStack[T]) Peak() (*T, error) {
if s.IsEmpty() {
return nil, errors.New("stack is empty")

View File

@@ -12,29 +12,31 @@ import (
// In BSTree: leftNode < rootNode < rightNode
// type T should implements Compare function in lancetconstraints.Comparator interface.
type BSTree[T any] struct {
root *datastructure.TreeNode[T]
root *datastructure.TreeNode[T]
comparator lancetconstraints.Comparator
}
// NewBSTree create a BSTree pointer
func NewBSTree[T any](rootData T) *BSTree[T] {
// param `comparator` is used to compare values in the tree
func NewBSTree[T any](rootData T, comparator lancetconstraints.Comparator) *BSTree[T] {
root := datastructure.NewTreeNode(rootData)
return &BSTree[T]{root}
return &BSTree[T]{root, comparator}
}
// InsertNode insert data into BSTree
func (t *BSTree[T]) InsertNode(data T, comparator lancetconstraints.Comparator) {
func (t *BSTree[T]) Insert(data T) {
root := t.root
newNode := datastructure.NewTreeNode(data)
if root == nil {
t.root = newNode
} else {
insertTreeNode(root, newNode, comparator)
insertTreeNode(root, newNode, t.comparator)
}
}
// DeletetNode delete data into BSTree
func (t *BSTree[T]) DeletetNode(data T, comparator lancetconstraints.Comparator) {
deleteTreeNode(t.root, data, comparator)
func (t *BSTree[T]) Delete(data T) {
deleteTreeNode(t.root, data, t.comparator)
}
// NodeLevel get node level in BSTree
@@ -75,6 +77,29 @@ func (t *BSTree[T]) Depth() int {
return calculateDepth(t.root, 0)
}
// IsSubTree checks if the tree `t` has `subTree` or not
func (t *BSTree[T]) HasSubTree(subTree *BSTree[T]) bool {
return hasSubTree(t.root, subTree.root, t.comparator)
}
func hasSubTree[T any](superTreeRoot, subTreeRoot *datastructure.TreeNode[T],
comparator lancetconstraints.Comparator) bool {
result := false
if superTreeRoot != nil && subTreeRoot != nil {
if comparator.Compare(superTreeRoot.Value, subTreeRoot.Value) == 0 {
result = isSubTree(superTreeRoot, subTreeRoot, comparator)
}
if !result {
result = hasSubTree(superTreeRoot.Left, subTreeRoot, comparator)
}
if !result {
result = hasSubTree(superTreeRoot.Right, subTreeRoot, comparator)
}
}
return result
}
// Print the bstree structure
func (t *BSTree[T]) Print() {
maxLevel := t.NodeLevel(t.root)

View File

@@ -20,14 +20,13 @@ func (c *intComparator) Compare(v1, v2 any) int {
return 0
}
func TestBSTree_InsertNode(t *testing.T) {
bstree := NewBSTree(6)
func TestBSTree_Insert(t *testing.T) {
bstree := NewBSTree(6, &intComparator{})
comparator := &intComparator{}
bstree.InsertNode(7, comparator)
bstree.InsertNode(5, comparator)
bstree.InsertNode(2, comparator)
bstree.InsertNode(4, comparator)
bstree.Insert(7)
bstree.Insert(5)
bstree.Insert(2)
bstree.Insert(4)
bstree.Print()
}
@@ -35,13 +34,12 @@ func TestBSTree_InsertNode(t *testing.T) {
func TestBSTree_PreOrderTraverse(t *testing.T) {
assert := internal.NewAssert(t, "TestBSTree_PreOrderTraverse")
bstree := NewBSTree(6)
bstree := NewBSTree(6, &intComparator{})
comparator := &intComparator{}
bstree.InsertNode(7, comparator)
bstree.InsertNode(5, comparator)
bstree.InsertNode(2, comparator)
bstree.InsertNode(4, comparator)
bstree.Insert(7)
bstree.Insert(5)
bstree.Insert(2)
bstree.Insert(4)
acturl := bstree.PreOrderTraverse()
t.Log(acturl)
@@ -51,13 +49,12 @@ func TestBSTree_PreOrderTraverse(t *testing.T) {
func TestBSTree_PostOrderTraverse(t *testing.T) {
assert := internal.NewAssert(t, "TestBSTree_PostOrderTraverse")
bstree := NewBSTree(6)
bstree := NewBSTree(6, &intComparator{})
comparator := &intComparator{}
bstree.InsertNode(7, comparator)
bstree.InsertNode(5, comparator)
bstree.InsertNode(2, comparator)
bstree.InsertNode(4, comparator)
bstree.Insert(7)
bstree.Insert(5)
bstree.Insert(2)
bstree.Insert(4)
acturl := bstree.PostOrderTraverse()
t.Log(acturl)
@@ -67,13 +64,12 @@ func TestBSTree_PostOrderTraverse(t *testing.T) {
func TestBSTree_InOrderTraverse(t *testing.T) {
assert := internal.NewAssert(t, "TestBSTree_InOrderTraverse")
bstree := NewBSTree(6)
bstree := NewBSTree(6, &intComparator{})
comparator := &intComparator{}
bstree.InsertNode(7, comparator)
bstree.InsertNode(5, comparator)
bstree.InsertNode(2, comparator)
bstree.InsertNode(4, comparator)
bstree.Insert(7)
bstree.Insert(5)
bstree.Insert(2)
bstree.Insert(4)
acturl := bstree.InOrderTraverse()
t.Log(acturl)
@@ -83,13 +79,12 @@ func TestBSTree_InOrderTraverse(t *testing.T) {
func TestBSTree_LevelOrderTraverse(t *testing.T) {
assert := internal.NewAssert(t, "TestBSTree_LevelOrderTraverse")
bstree := NewBSTree(6)
bstree := NewBSTree(6, &intComparator{})
comparator := &intComparator{}
bstree.InsertNode(7, comparator)
bstree.InsertNode(5, comparator)
bstree.InsertNode(2, comparator)
bstree.InsertNode(4, comparator)
bstree.Insert(7)
bstree.Insert(5)
bstree.Insert(2)
bstree.Insert(4)
bstree.Print()
@@ -98,20 +93,19 @@ func TestBSTree_LevelOrderTraverse(t *testing.T) {
assert.Equal([]int{6, 5, 7, 2, 4}, acturl)
}
func TestBSTree_DeletetNode(t *testing.T) {
assert := internal.NewAssert(t, "TestBSTree_DeletetNode")
func TestBSTree_Delete(t *testing.T) {
assert := internal.NewAssert(t, "TestBSTree_Delete")
bstree := NewBSTree(6)
bstree := NewBSTree(6, &intComparator{})
comparator := &intComparator{}
bstree.InsertNode(7, comparator)
bstree.InsertNode(5, comparator)
bstree.InsertNode(2, comparator)
bstree.InsertNode(4, comparator)
bstree.Insert(7)
bstree.Insert(5)
bstree.Insert(2)
bstree.Insert(4)
bstree.Print()
bstree.DeletetNode(4, comparator)
bstree.Delete(4)
bstree.Print()
acturl1 := bstree.InOrderTraverse()
t.Log(acturl1)
@@ -128,15 +122,36 @@ func TestBSTree_DeletetNode(t *testing.T) {
func TestBSTree_Depth(t *testing.T) {
assert := internal.NewAssert(t, "TestBSTree_Depth")
bstree := NewBSTree(6)
bstree := NewBSTree(6, &intComparator{})
comparator := &intComparator{}
bstree.InsertNode(7, comparator)
bstree.InsertNode(5, comparator)
bstree.InsertNode(2, comparator)
bstree.InsertNode(4, comparator)
bstree.Insert(7)
bstree.Insert(5)
bstree.Insert(2)
bstree.Insert(4)
bstree.Print()
assert.Equal(bstree.Depth(), 4)
}
func TestBSTree_IsSubTree(t *testing.T) {
assert := internal.NewAssert(t, "TestBSTree_IsSubTree")
superTree := NewBSTree(8, &intComparator{})
superTree.Insert(4)
superTree.Insert(5)
superTree.Insert(6)
superTree.Insert(9)
superTree.Insert(4)
superTree.Print()
subTree := NewBSTree(5, &intComparator{})
subTree.Insert(4)
subTree.Insert(6)
subTree.Print()
assert.Equal(true, superTree.HasSubTree(subTree))
assert.Equal(false, subTree.HasSubTree(superTree))
}

View File

@@ -11,7 +11,7 @@ import (
func preOrderTraverse[T any](node *datastructure.TreeNode[T]) []T {
data := []T{}
if node != nil {
data = append(data, node.Data)
data = append(data, node.Value)
data = append(data, preOrderTraverse(node.Left)...)
data = append(data, preOrderTraverse(node.Right)...)
}
@@ -23,7 +23,7 @@ func postOrderTraverse[T any](node *datastructure.TreeNode[T]) []T {
if node != nil {
data = append(data, preOrderTraverse(node.Left)...)
data = append(data, preOrderTraverse(node.Right)...)
data = append(data, node.Data)
data = append(data, node.Value)
}
return data
}
@@ -32,7 +32,7 @@ func inOrderTraverse[T any](node *datastructure.TreeNode[T]) []T {
data := []T{}
if node != nil {
data = append(data, inOrderTraverse(node.Left)...)
data = append(data, node.Data)
data = append(data, node.Value)
data = append(data, inOrderTraverse(node.Right)...)
}
return data
@@ -43,7 +43,7 @@ func preOrderPrint[T any](node *datastructure.TreeNode[T]) {
return
}
fmt.Printf("%v, ", node.Data)
fmt.Printf("%v, ", node.Value)
preOrderPrint(node.Left)
preOrderPrint(node.Right)
}
@@ -55,7 +55,7 @@ func postOrderPrint[T any](node *datastructure.TreeNode[T]) {
preOrderPrint(node.Left)
preOrderPrint(node.Right)
fmt.Printf("%v, ", node.Data)
fmt.Printf("%v, ", node.Value)
}
func inOrderPrint[T any](node *datastructure.TreeNode[T]) {
@@ -64,7 +64,7 @@ func inOrderPrint[T any](node *datastructure.TreeNode[T]) {
}
inOrderPrint(node.Left)
fmt.Printf("%v, ", node.Data)
fmt.Printf("%v, ", node.Value)
inOrderPrint(node.Right)
}
@@ -76,7 +76,7 @@ func levelOrderTraverse[T any](root *datastructure.TreeNode[T], traversal *[]T)
for len(q) != 0 {
n, q = q[0], q[1:]
*traversal = append(*traversal, n.Data)
*traversal = append(*traversal, n.Value)
if n.Left != nil {
q = append(q, n.Left)
}
@@ -87,7 +87,7 @@ func levelOrderTraverse[T any](root *datastructure.TreeNode[T], traversal *[]T)
}
func insertTreeNode[T any](rootNode, newNode *datastructure.TreeNode[T], comparator lancetconstraints.Comparator) {
if comparator.Compare(newNode.Data, rootNode.Data) == -1 {
if comparator.Compare(newNode.Value, rootNode.Value) == -1 {
if rootNode.Left == nil {
rootNode.Left = newNode
} else {
@@ -107,9 +107,9 @@ func deleteTreeNode[T any](node *datastructure.TreeNode[T], data T, comparator l
if node == nil {
return nil
}
if comparator.Compare(data, node.Data) == -1 {
if comparator.Compare(data, node.Value) == -1 {
node.Left = deleteTreeNode(node.Left, data, comparator)
} else if comparator.Compare(data, node.Data) == 1 {
} else if comparator.Compare(data, node.Value) == 1 {
node.Right = deleteTreeNode(node.Right, data, comparator)
} else {
if node.Left == nil {
@@ -150,7 +150,7 @@ func printTreeNodes[T any](nodes []*datastructure.TreeNode[T], level, maxLevel i
newNodes := []*datastructure.TreeNode[T]{}
for _, node := range nodes {
if node != nil {
fmt.Printf("%v", node.Data)
fmt.Printf("%v", node.Value)
newNodes = append(newNodes, node.Left)
newNodes = append(newNodes, node.Right)
} else {
@@ -216,6 +216,20 @@ func calculateDepth[T any](node *datastructure.TreeNode[T], depth int) int {
return max(calculateDepth(node.Left, depth+1), calculateDepth(node.Right, depth+1))
}
func isSubTree[T any](superTreeRoot, subTreeRoot *datastructure.TreeNode[T], comparator lancetconstraints.Comparator) bool {
if subTreeRoot == nil {
return true
}
if superTreeRoot == nil {
return false
}
if comparator.Compare(superTreeRoot.Value, subTreeRoot.Value) != 0 {
return false
}
result := isSubTree(superTreeRoot.Left, subTreeRoot.Left, comparator) && isSubTree(superTreeRoot.Right, subTreeRoot.Right, comparator)
return result
}
func max(a, b int) int {
if a > b {
return a

View File

@@ -4,24 +4,24 @@
// Package datetime implements some functions to format date and time.
// Note:
// 1. `format` param in FormatTimeToStr function should be as flow:
//"yyyy-mm-dd hh:mm:ss"
//"yyyy-mm-dd hh:mm"
//"yyyy-mm-dd hh"
//"yyyy-mm-dd"
//"yyyy-mm"
//"mm-dd"
//"dd-mm-yy hh:mm:ss"
//"yyyy/mm/dd hh:mm:ss"
//"yyyy/mm/dd hh:mm"
//"yyyy/mm/dd hh"
//"yyyy/mm/dd"
//"yyyy/mm"
//"mm/dd"
//"dd/mm/yy hh:mm:ss"
//"yyyy"
//"mm"
//"hh:mm:ss"
//"mm:ss"
// "yyyy-mm-dd hh:mm:ss"
// "yyyy-mm-dd hh:mm"
// "yyyy-mm-dd hh"
// "yyyy-mm-dd"
// "yyyy-mm"
// "mm-dd"
// "dd-mm-yy hh:mm:ss"
// "yyyy/mm/dd hh:mm:ss"
// "yyyy/mm/dd hh:mm"
// "yyyy/mm/dd hh"
// "yyyy/mm/dd"
// "yyyy/mm"
// "mm/dd"
// "dd/mm/yy hh:mm:ss"
// "yyyy"
// "mm"
// "hh:mm:ss"
// "mm:ss"
package datetime
import (
@@ -147,16 +147,32 @@ func EndOfDay(t time.Time) time.Time {
return time.Date(y, m, d, 23, 59, 59, int(time.Second-time.Nanosecond), t.Location())
}
// BeginOfWeek return beginning week, week begin from Sunday
func BeginOfWeek(t time.Time) time.Time {
y, m, d := t.AddDate(0, 0, 0-int(BeginOfDay(t).Weekday())).Date()
return time.Date(y, m, d, 0, 0, 0, 0, t.Location())
// BeginOfWeek return beginning week, default week begin from Sunday
func BeginOfWeek(t time.Time, beginFrom ...time.Weekday) time.Time {
var beginFromWeekday = time.Sunday
if len(beginFrom) > 0 {
beginFromWeekday = beginFrom[0]
}
y, m, d := t.AddDate(0, 0, int(beginFromWeekday-t.Weekday())).Date()
beginOfWeek := time.Date(y, m, d, 0, 0, 0, 0, t.Location())
if beginOfWeek.After(t) {
return beginOfWeek.AddDate(0, 0, -7)
}
return beginOfWeek
}
// EndOfWeek return end week time, week end with Saturday
func EndOfWeek(t time.Time) time.Time {
y, m, d := BeginOfWeek(t).AddDate(0, 0, 7).Add(-time.Nanosecond).Date()
return time.Date(y, m, d, 23, 59, 59, int(time.Second-time.Nanosecond), t.Location())
// EndOfWeek return end week time, default week end with Saturday
func EndOfWeek(t time.Time, endWith ...time.Weekday) time.Time {
var endWithWeekday = time.Saturday
if len(endWith) > 0 {
endWithWeekday = endWith[0]
}
y, m, d := t.AddDate(0, 0, int(endWithWeekday-t.Weekday())).Date()
var endWithWeek = time.Date(y, m, d, 23, 59, 59, int(time.Second-time.Nanosecond), t.Location())
if endWithWeek.Before(t) {
endWithWeek = endWithWeek.AddDate(0, 0, 7)
}
return endWithWeek
}
// BeginOfMonth return beginning of month

View File

@@ -5,7 +5,7 @@ Package algorithm implements some basic algorithm. eg. sort, search.
## Source
- [https://github.com/duke-git/lancet/blob/main/algorithm/sorter.go](https://github.com/duke-git/lancet/blob/main/algorithm/sorter.go)
- [https://github.com/duke-git/lancet/blob/main/algorithm/sort.go](https://github.com/duke-git/lancet/blob/main/algorithm/sort.go)
- [https://github.com/duke-git/lancet/blob/main/algorithm/search.go](https://github.com/duke-git/lancet/blob/main/algorithm/search.go)
- [https://github.com/duke-git/lancet/blob/main/algorithm/lru_cache.go](https://github.com/duke-git/lancet/blob/main/algorithm/lru_cache.go)
@@ -22,18 +22,20 @@ import (
## Index
- [BubbleSort](#BubbleSort)
- [CountSort](#CountSort)
- [HeapSort](#HeapSort)
- [InsertionSort](#InsertionSort)
- [MergeSort](#MergeSort)
- [QuickSort](#QuickSort)
- [SelectionSort](#SelectionSort)
- [ShellSort](#ShellSort)
- [QuickSort](#QuickSort)
- [HeapSort](#HeapSort)
- [MergeSort](#MergeSort)
- [CountSort](#CountSort)
- [BinarySearch](#BinarySearch)
- [BinaryIterativeSearch](#BinaryIterativeSearch)
- [LinearSearch](#LinearSearch)
- [LRUCache](#LRUCache)
<div STYLE="page-break-after: always;"></div>
## Documentation
@@ -46,7 +48,7 @@ import (
<b>Signature:</b>
```go
func BubbleSort[T any](slice []T, comparator lancetconstraints.Comparator) []T
func BubbleSort[T any](slice []T, comparator lancetconstraints.Comparator)
```
<b>Example:</b>
@@ -76,9 +78,9 @@ func main() {
intSlice := []int{2, 1, 5, 3, 6, 4}
comparator := &intComparator{}
sortedSlice := algorithm.BubbleSort(intSlice, comparator)
algorithm.BubbleSort(intSlice, comparator)
fmt.Println(sortedSlice) //[]int{1, 2, 3, 4, 5, 6}
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6}
}
```
@@ -91,7 +93,7 @@ func main() {
<b>Signature:</b>
```go
func InsertionSort[T any](slice []T, comparator lancetconstraints.Comparator) []T
func InsertionSort[T any](slice []T, comparator lancetconstraints.Comparator)
```
<b>Example:</b>
@@ -141,9 +143,9 @@ func main() {
{Name: "e", Age: 28},
}
comparator := &peopleAgeComparator{}
sortedPeople := algorithm.InsertionSort(peoples, comparator)
algorithm.InsertionSort(peoples, comparator)
fmt.Println(sortedSlice) //[{d 8} {b 10} {c 17} {a 20} {e 28}]
fmt.Println(peoples) //[{d 8} {b 10} {c 17} {a 20} {e 28}]
}
```
@@ -156,7 +158,7 @@ func main() {
<b>Signature:</b>
```go
func SelectionSort[T any](slice []T, comparator lancetconstraints.Comparator) []T
func SelectionSort[T any](slice []T, comparator lancetconstraints.Comparator)
```
<b>Example:</b>
@@ -186,9 +188,9 @@ func main() {
intSlice := []int{2, 1, 5, 3, 6, 4}
comparator := &intComparator{}
sortedSlice := algorithm.SelectionSort(intSlice, comparator)
algorithm.SelectionSort(intSlice, comparator)
fmt.Println(sortedSlice) //[]int{1, 2, 3, 4, 5, 6}
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6}
}
```
@@ -201,7 +203,7 @@ func main() {
<b>Signature:</b>
```go
func ShellSort[T any](slice []T, comparator lancetconstraints.Comparator) []T
func ShellSort[T any](slice []T, comparator lancetconstraints.Comparator)
```
<b>Example:</b>
@@ -231,9 +233,9 @@ func main() {
intSlice := []int{2, 1, 5, 3, 6, 4}
comparator := &intComparator{}
sortedSlice := algorithm.ShellSort(intSlice, comparator)
algorithm.ShellSort(intSlice, comparator)
fmt.Println(sortedSlice) //[]int{1, 2, 3, 4, 5, 6}
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6}
}
```
@@ -246,7 +248,7 @@ func main() {
<b>Signature:</b>
```go
func QuickSort[T any](slice []T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) []T
func QuickSort[T any](slice []T comparator lancetconstraints.Comparator)
```
<b>Example:</b>
@@ -276,9 +278,9 @@ func main() {
intSlice := []int{2, 1, 5, 3, 6, 4}
comparator := &intComparator{}
sortedSlice := algorithm.QuickSort(intSlice, 0, len(intSlice)-1, comparator)
algorithm.QuickSort(intSlice, comparator)
fmt.Println(sortedSlice) //[]int{1, 2, 3, 4, 5, 6}
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6}
}
```
@@ -291,7 +293,7 @@ func main() {
<b>Signature:</b>
```go
func HeapSort[T any](slice []T, comparator lancetconstraints.Comparator) []T
func HeapSort[T any](slice []T, comparator lancetconstraints.Comparator)
```
<b>Example:</b>
@@ -321,9 +323,9 @@ func main() {
intSlice := []int{2, 1, 5, 3, 6, 4}
comparator := &intComparator{}
sortedSlice := algorithm.HeapSort(intSlice, comparator)
algorithm.HeapSort(intSlice, comparator)
fmt.Println(sortedSlice) //[]int{1, 2, 3, 4, 5, 6}
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6}
}
```
@@ -336,7 +338,7 @@ func main() {
<b>Signature:</b>
```go
func MergeSort[T any](slice []T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) []T
func MergeSort[T any](slice []T, comparator lancetconstraints.Comparator)
```
<b>Example:</b>
@@ -366,9 +368,9 @@ func main() {
intSlice := []int{2, 1, 5, 3, 6, 4}
comparator := &intComparator{}
sortedSlice := algorithm.MergeSort(intSlice, 0, len(intSlice)-1, comparator)
algorithm.MergeSort(intSlice, comparator)
fmt.Println(sortedSlice) //[]int{1, 2, 3, 4, 5, 6}
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6}
}
```
@@ -582,8 +584,8 @@ import (
func main() {
cache := algorithm.NewLRUCache[int, int](2)
cache.Put(1, 1)
cache.Put(2, 2)
cache.Put(1, 1)
cache.Put(2, 2)
_, ok := cache.Get(0) // ok -> false

View File

@@ -5,7 +5,7 @@ algorithm算法包实现一些基本算法sortsearchlrucache。
## 源码
- [https://github.com/duke-git/lancet/blob/main/algorithm/sorter.go](https://github.com/duke-git/lancet/blob/main/algorithm/sorter.go)
- [https://github.com/duke-git/lancet/blob/main/algorithm/sort.go](https://github.com/duke-git/lancet/blob/main/algorithm/sort.go)
- [https://github.com/duke-git/lancet/blob/main/algorithm/search.go](https://github.com/duke-git/lancet/blob/main/algorithm/search.go)
- [https://github.com/duke-git/lancet/blob/main/algorithm/lru_cache.go](https://github.com/duke-git/lancet/blob/main/algorithm/lru_cache.go)
@@ -21,19 +21,22 @@ import (
<div STYLE="page-break-after: always;"></div>
## 目录
- [BubbleSort](#BubbleSort)
- [CountSort](#CountSort)
- [HeapSort](#HeapSort)
- [InsertionSort](#InsertionSort)
- [MergeSort](#MergeSort)
- [QuickSort](#QuickSort)
- [SelectionSort](#SelectionSort)
- [ShellSort](#ShellSort)
- [QuickSort](#QuickSort)
- [HeapSort](#HeapSort)
- [MergeSort](#MergeSort)
- [CountSort](#CountSort)
- [BinarySearch](#BinarySearch)
- [BinaryIterativeSearch](#BinaryIterativeSearch)
- [LinearSearch](#LinearSearch)
- [LRUCache](#LRUCache)
<div STYLE="page-break-after: always;"></div>
## 文档
@@ -46,7 +49,7 @@ import (
<b>函数签名:</b>
```go
func BubbleSort[T any](slice []T, comparator lancetconstraints.Comparator) []T
func BubbleSort[T any](slice []T, comparator lancetconstraints.Comparator)
```
<b>Example:</b>
@@ -76,9 +79,9 @@ func main() {
intSlice := []int{2, 1, 5, 3, 6, 4}
comparator := &intComparator{}
sortedSlice := algorithm.BubbleSort(intSlice, comparator)
algorithm.BubbleSort(intSlice, comparator)
fmt.Println(sortedSlice) //[]int{1, 2, 3, 4, 5, 6}
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6}
}
```
@@ -91,7 +94,7 @@ func main() {
<b>函数签名:</b>
```go
func InsertionSort[T any](slice []T, comparator lancetconstraints.Comparator) []T
func InsertionSort[T any](slice []T, comparator lancetconstraints.Comparator)
```
<b>Example:</b>
@@ -141,9 +144,9 @@ func main() {
{Name: "e", Age: 28},
}
comparator := &peopleAgeComparator{}
sortedPeople := algorithm.InsertionSort(peoples, comparator)
algorithm.InsertionSort(peoples, comparator)
fmt.Println(sortedSlice) //[{d 8} {b 10} {c 17} {a 20} {e 28}]
fmt.Println(intSlice) //[{d 8} {b 10} {c 17} {a 20} {e 28}]
}
```
@@ -156,7 +159,7 @@ func main() {
<b>函数签名:</b>
```go
func SelectionSort[T any](slice []T, comparator lancetconstraints.Comparator) []T
func SelectionSort[T any](slice []T, comparator lancetconstraints.Comparator)
```
<b>Example:</b>
@@ -186,9 +189,9 @@ func main() {
intSlice := []int{2, 1, 5, 3, 6, 4}
comparator := &intComparator{}
sortedSlice := algorithm.SelectionSort(intSlice, comparator)
algorithm.SelectionSort(intSlice, comparator)
fmt.Println(sortedSlice) //[]int{1, 2, 3, 4, 5, 6}
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6}
}
```
@@ -201,7 +204,7 @@ func main() {
<b>函数签名:</b>
```go
func ShellSort[T any](slice []T, comparator lancetconstraints.Comparator) []T
func ShellSort[T any](slice []T, comparator lancetconstraints.Comparator)
```
<b>Example:</b>
@@ -231,9 +234,9 @@ func main() {
intSlice := []int{2, 1, 5, 3, 6, 4}
comparator := &intComparator{}
sortedSlice := algorithm.ShellSort(intSlice, comparator)
algorithm.ShellSort(intSlice, comparator)
fmt.Println(sortedSlice) //[]int{1, 2, 3, 4, 5, 6}
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6}
}
```
@@ -246,7 +249,7 @@ func main() {
<b>函数签名:</b>
```go
func QuickSort[T any](slice []T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) []T
func QuickSort[T any](slice []T, comparator lancetconstraints.Comparator) []T
```
<b>Example:</b>
@@ -276,9 +279,9 @@ func main() {
intSlice := []int{2, 1, 5, 3, 6, 4}
comparator := &intComparator{}
sortedSlice := algorithm.QuickSort(intSlice, 0, len(intSlice)-1, comparator)
algorithm.QuickSort(intSlice, comparator)
fmt.Println(sortedSlice) //[]int{1, 2, 3, 4, 5, 6}
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6}
}
```
@@ -321,9 +324,9 @@ func main() {
intSlice := []int{2, 1, 5, 3, 6, 4}
comparator := &intComparator{}
sortedSlice := algorithm.HeapSort(intSlice, comparator)
algorithm.HeapSort(intSlice, comparator)
fmt.Println(sortedSlice) //[]int{1, 2, 3, 4, 5, 6}
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6}
}
```
@@ -336,7 +339,7 @@ func main() {
<b>函数签名:</b>
```go
func MergeSort[T any](slice []T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) []T
func MergeSort[T any](slice []T, comparator lancetconstraints.Comparator)
```
<b>Example:</b>
@@ -366,9 +369,9 @@ func main() {
intSlice := []int{2, 1, 5, 3, 6, 4}
comparator := &intComparator{}
sortedSlice := algorithm.MergeSort(intSlice, 0, len(intSlice)-1, comparator)
algorithm.MergeSort(intSlice, comparator)
fmt.Println(sortedSlice) //[]int{1, 2, 3, 4, 5, 6}
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6}
}
```
@@ -582,8 +585,8 @@ import (
func main() {
cache := algorithm.NewLRUCache[int, int](2)
cache.Put(1, 1)
cache.Put(2, 2)
cache.Put(1, 1)
cache.Put(2, 2)
_, ok := cache.Get(0) // ok -> false

390
docs/concurrency.md Normal file
View File

@@ -0,0 +1,390 @@
# Concurrency
Package concurrency contain some functions to support concurrent programming. eg, goroutine, channel, async.
<div STYLE="page-break-after: always;"></div>
## Source:
- [https://github.com/duke-git/lancet/blob/main/concurrency/channel.go](https://github.com/duke-git/lancet/blob/main/concurrency/channel.go)
<div STYLE="page-break-after: always;"></div>
## Usage:
```go
import (
"github.com/duke-git/lancet/v2/concurrency"
)
```
<div STYLE="page-break-after: always;"></div>
## Index
### Channel
- [NewChannel](#NewChannel)
- [Bridge](#Bridge)
- [FanIn](#FanIn)
- [Generate](#Generate)
- [Or](#Or)
- [OrDone](#OrDone)
- [Repeat](#Repeat)
- [RepeatFn](#RepeatFn)
- [Take](#Take)
- [Tee](#Tee)
<div STYLE="page-break-after: always;"></div>
## Documentation
## Channel
### <span id="NewChannel">NewChannel</span>
<p>return a Channel pointer instance.</p>
<b>Signature:</b>
```go
type Channel struct {}
func NewChannel() *Channel
```
<b>Example:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/concurrency"
)
func main() {
c := concurrency.NewChannel()
}
```
### <span id="Bridge">Bridge</span>
<p>Link multiple channels into one channel until cancel the context.</p>
<b>Signature:</b>
```go
func (c *Channel) Bridge(ctx context.Context, chanStream <-chan <-chan any) <-chan any
```
<b>Example:</b>
```go
package main
import (
"context"
"fmt"
"github.com/duke-git/lancet/v2/concurrency"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
c := concurrency.NewChannel()
genVals := func() <-chan <-chan any {
chanStream := make(chan (<-chan any))
go func() {
defer close(chanStream)
for i := 0; i < 10; i++ {
stream := make(chan any, 1)
stream <- i
close(stream)
chanStream <- stream
}
}()
return chanStream
}
index := 0
for val := range c.Bridge(ctx, genVals()) {
fmt.Printf("%v ", val) //0 1 2 3 4 5 6 7 8 9
}
}
```
### <span id="FanIn">FanIn</span>
<p>merge multiple channels into one channel until cancel the context.</p>
<b>Signature:</b>
```go
func (c *Channel) FanIn(ctx context.Context, channels ...<-chan any) <-chan any
```
<b>Example:</b>
```go
package main
import (
"context"
"fmt"
"github.com/duke-git/lancet/v2/concurrency"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
c := concurrency.NewChannel()
channels := make([]<-chan any, 3)
for i := 0; i < 3; i++ {
channels[i] = c.Take(ctx, c.Repeat(ctx, i), 3)
}
mergedChannel := c.FanIn(ctx, channels...)
for val := range mergedChannel {
fmt.Println("\t%d\n", val) //1,2,1,0,0,1,0,2,2 (order not for sure)
}
}
```
### <span id="Repeat">Repeat</span>
<p>Return a chan, put param `values` into the chan repeatly until cancel the context.</p>
<b>Signature:</b>
```go
func (c *Channel) Repeat(ctx context.Context, values ...any) <-chan any
```
<b>Example:</b>
```go
package main
import (
"context"
"fmt"
"github.com/duke-git/lancet/v2/concurrency"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
c := concurrency.NewChannel()
intStream := c.Take(ctx, c.Repeat(ctx, 1, 2), 5)
for v := range intStream {
fmt.Println(v) //1, 2, 1, 2, 1
}
}
```
### <span id="RepeatFn">RepeatFn</span>
<p>Return a chan, excutes fn repeatly, and put the result into retruned chan until cancel context.</p>
<b>Signature:</b>
```go
func (c *Channel) RepeatFn(ctx context.Context, fn func() any) <-chan any
```
<b>Example:</b>
```go
package main
import (
"context"
"fmt"
"github.com/duke-git/lancet/v2/concurrency"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
fn := func() any {
s := "a"
return s
}
c := concurrency.NewChannel()
dataStream := c.Take(ctx, c.RepeatFn(ctx, fn), 3)
for v := range dataStream {
fmt.Println(v) //a, a, a
}
}
```
### <span id="Or">Or</span>
<p>Read one or more channels into one channel, will close when any readin channel is closed.</p>
<b>Signature:</b>
```go
func (c *Channel) Or(channels ...<-chan any) <-chan any
```
<b>Example:</b>
```go
package main
import (
"context"
"fmt"
"github.com/duke-git/lancet/v2/concurrency"
)
func main() {
sig := func(after time.Duration) <-chan any {
c := make(chan interface{})
go func() {
defer close(c)
time.Sleep(after)
}()
return c
}
start := time.Now()
c := concurrency.NewChannel()
<-c.Or(
sig(1*time.Second),
sig(2*time.Second),
sig(3*time.Second),
sig(4*time.Second),
sig(5*time.Second),
)
fmt.Println("done after %v", time.Since(start)) //1.003s
}
```
### <span id="OrDone">OrDone</span>
<p>Read a channel into another channel, will close until cancel context.</p>
<b>Signature:</b>
```go
func (c *Channel) OrDone(ctx context.Context, channel <-chan any) <-chan any
```
<b>Example:</b>
```go
package main
import (
"context"
"fmt"
"github.com/duke-git/lancet/v2/concurrency"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
c := concurrency.NewChannel()
intStream := c.Take(ctx, c.Repeat(ctx, 1), 3)
for val := range c.OrDone(ctx, intStream) {
fmt.Println(val) //1
}
}
```
### <span id="Take">Take</span>
<p>Return a chan whose values are tahken from another chan until cancel context.</p>
<b>Signature:</b>
```go
func (c *Channel) Take(ctx context.Context, valueStream <-chan any, number int) <-chan any
```
<b>Example:</b>
```go
package main
import (
"context"
"fmt"
"github.com/duke-git/lancet/v2/concurrency"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
numbers := make(chan any, 5)
numbers <- 1
numbers <- 2
numbers <- 3
numbers <- 4
numbers <- 5
defer close(numbers)
c := concurrency.NewChannel()
intStream := c.Take(ctx, numbers, 3)
for val := range intStream {
fmt.Println(val) //1, 2, 3
}
}
```
### <span id="Tee">Tee</span>
<p>Split one chanel into two channels until cancel context.</p>
<b>Signature:</b>
```go
func (c *Channel) Tee(ctx context.Context, in <-chan any) (<-chan any, <-chan any)
```
<b>Example:</b>
```go
package main
import (
"context"
"fmt"
"github.com/duke-git/lancet/v2/concurrency"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
c := concurrency.NewChannel()
inStream := c.Take(ctx, c.Repeat(ctx, 1, 2), 4)
out1, out2 := c.Tee(ctx, inStream)
for val := range out1 {
fmt.Println(val) //1
fmt.Println(<-out2) //1
}
}
```

390
docs/concurrency_zh-CN.md Normal file
View File

@@ -0,0 +1,390 @@
# Concurrency
并发包包含一些支持并发编程的功能。例如goroutine, channel, async等。
<div STYLE="page-break-after: always;"></div>
## 源码:
- [https://github.com/duke-git/lancet/blob/main/concurrency/channel.go](https://github.com/duke-git/lancet/blob/main/concurrency/channel.go)
<div STYLE="page-break-after: always;"></div>
## 用法:
```go
import (
"github.com/duke-git/lancet/v2/concurrency"
)
```
<div STYLE="page-break-after: always;"></div>
## 目录
### Channel
- [NewChannel](#NewChannel)
- [Bridge](#Bridge)
- [FanIn](#FanIn)
- [Generate](#Generate)
- [Or](#Or)
- [OrDone](#OrDone)
- [Repeat](#Repeat)
- [RepeatFn](#RepeatFn)
- [Take](#Take)
- [Tee](#Tee)
<div STYLE="page-break-after: always;"></div>
## 文档
### Channel
### <span id="NewChannel">NewChannel</span>
<p>返回一个 Channel 指针实例</p>
<b>函数签名:</b>
```go
type Channel struct {}
func NewChannel() *Channel
```
<b>例子:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/concurrency"
)
func main() {
c := concurrency.NewChannel()
}
```
### <span id="Bridge">Bridge</span>
<p>将多个通道链接到一个通道,直到取消上下文。</p>
<b>函数签名:</b>
```go
func (c *Channel) Bridge(ctx context.Context, chanStream <-chan <-chan any) <-chan any
```
<b>例子:</b>
```go
package main
import (
"context"
"fmt"
"github.com/duke-git/lancet/v2/concurrency"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
c := concurrency.NewChannel()
genVals := func() <-chan <-chan any {
chanStream := make(chan (<-chan any))
go func() {
defer close(chanStream)
for i := 0; i < 10; i++ {
stream := make(chan any, 1)
stream <- i
close(stream)
chanStream <- stream
}
}()
return chanStream
}
index := 0
for val := range c.Bridge(ctx, genVals()) {
fmt.Printf("%v ", val) //0 1 2 3 4 5 6 7 8 9
}
}
```
### <span id="FanIn">FanIn</span>
<p>将多个通道合并为一个通道,直到取消上下文</p>
<b>函数签名:</b>
```go
func (c *Channel) FanIn(ctx context.Context, channels ...<-chan any) <-chan any
```
<b>例子:</b>
```go
package main
import (
"context"
"fmt"
"github.com/duke-git/lancet/v2/concurrency"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
c := concurrency.NewChannel()
channels := make([]<-chan any, 3)
for i := 0; i < 3; i++ {
channels[i] = c.Take(ctx, c.Repeat(ctx, i), 3)
}
mergedChannel := c.FanIn(ctx, channels...)
for val := range mergedChannel {
fmt.Println("\t%d\n", val) //1,2,1,0,0,1,0,2,2 (order not for sure)
}
}
```
### <span id="Repeat">Repeat</span>
<p>返回一个chan将参数`values`重复放入chan直到取消上下文。</p>
<b>函数签名:</b>
```go
func (c *Channel) Repeat(ctx context.Context, values ...any) <-chan any
```
<b>例子:</b>
```go
package main
import (
"context"
"fmt"
"github.com/duke-git/lancet/v2/concurrency"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
c := concurrency.NewChannel()
intStream := c.Take(ctx, c.Repeat(ctx, 1, 2), 5)
for v := range intStream {
fmt.Println(v) //1, 2, 1, 2, 1
}
}
```
### <span id="RepeatFn">RepeatFn</span>
<p>返回一个chan重复执行函数fn并将结果放入返回的chan直到取消上下文。</p>
<b>函数签名:</b>
```go
func (c *Channel) RepeatFn(ctx context.Context, fn func() any) <-chan any
```
<b>例子:</b>
```go
package main
import (
"context"
"fmt"
"github.com/duke-git/lancet/v2/concurrency"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
fn := func() any {
s := "a"
return s
}
c := concurrency.NewChannel()
dataStream := c.Take(ctx, c.RepeatFn(ctx, fn), 3)
for v := range dataStream {
fmt.Println(v) //a, a, a
}
}
```
### <span id="Or">Or</span>
<p>将一个或多个通道读取到一个通道中,当任何读取通道关闭时将结束读取。</p>
<b>函数签名:</b>
```go
func (c *Channel) Or(channels ...<-chan any) <-chan any
```
<b>例子:</b>
```go
package main
import (
"context"
"fmt"
"github.com/duke-git/lancet/v2/concurrency"
)
func main() {
sig := func(after time.Duration) <-chan any {
c := make(chan interface{})
go func() {
defer close(c)
time.Sleep(after)
}()
return c
}
start := time.Now()
c := concurrency.NewChannel()
<-c.Or(
sig(1*time.Second),
sig(2*time.Second),
sig(3*time.Second),
sig(4*time.Second),
sig(5*time.Second),
)
fmt.Println("done after %v", time.Since(start)) //1.003s
}
```
### <span id="OrDone">OrDone</span>
<p>将一个通道读入另一个通道,直到取消上下文。</p>
<b>函数签名:</b>
```go
func (c *Channel) OrDone(ctx context.Context, channel <-chan any) <-chan any
```
<b>例子:</b>
```go
package main
import (
"context"
"fmt"
"github.com/duke-git/lancet/v2/concurrency"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
c := concurrency.NewChannel()
intStream := c.Take(ctx, c.Repeat(ctx, 1), 3)
for val := range c.OrDone(ctx, intStream) {
fmt.Println(val) //1
}
}
```
### <span id="Take">Take</span>
<p>返回一个chan其值从另一个chan获取直到取消上下文。</p>
<b>函数签名:</b>
```go
func (c *Channel) Take(ctx context.Context, valueStream <-chan any, number int) <-chan any
```
<b>例子:</b>
```go
package main
import (
"context"
"fmt"
"github.com/duke-git/lancet/v2/concurrency"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
numbers := make(chan any, 5)
numbers <- 1
numbers <- 2
numbers <- 3
numbers <- 4
numbers <- 5
defer close(numbers)
c := concurrency.NewChannel()
intStream := c.Take(ctx, numbers, 3)
for val := range intStream {
fmt.Println(val) //1, 2, 3
}
}
```
### <span id="Tee">Tee</span>
<p>将一个通道分成两个通道,直到取消上下文。</p>
<b>函数签名:</b>
```go
func (c *Channel) Tee(ctx context.Context, in <-chan any) (<-chan any, <-chan any)
```
<b>例子:</b>
```go
package main
import (
"context"
"fmt"
"github.com/duke-git/lancet/v2/concurrency"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
c := concurrency.NewChannel()
inStream := c.Take(ctx, c.Repeat(ctx, 1, 2), 4)
out1, out2 := c.Tee(ctx, inStream)
for val := range out1 {
fmt.Println(val) //1
fmt.Println(<-out2) //1
}
}
```

264
docs/condition.md Normal file
View File

@@ -0,0 +1,264 @@
# Condition
Package condition contains some functions for conditional judgment. eg. And, Or, TernaryOperator... The implementation of this package refers to the implementation of carlmjohnson's truthy package, you may find more useful information in [truthy](https://github.com/carlmjohnson/truthy), thanks carlmjohnson.
<div STYLE="page-break-after: always;"></div>
## Source:
- [https://github.com/duke-git/lancet/blob/main/condition/condition.go](https://github.com/duke-git/lancet/blob/main/condition/condition.go)
<div STYLE="page-break-after: always;"></div>
## Usage:
```go
import (
"github.com/duke-git/lancet/v2/condition"
)
```
<div STYLE="page-break-after: always;"></div>
## Index
- [Bool](#Bool)
- [And](#And)
- [Or](#Or)
- [Xor](#Generate)
- [Nor](#Nor)
- [Nand](#Nand)
- [TernaryOperator](#TernaryOperator)
<div STYLE="page-break-after: always;"></div>
## Documentation
### <span id="Bool">Bool</span>
<p>Returns the truthy value of anything.<br/>
If the value's type has a Bool() bool method, the method is called and returned.<br/>
If the type has an IsZero() bool method, the opposite value is returned.<br/>
Slices and maps are truthy if they have a length greater than zero.<br/>
All other types are truthy if they are not their zero value.</p>
<b>Signature:</b>
```go
func Bool[T any](value T) bool
```
<b>Example:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/condition"
)
func main() {
// bool
fmt.Println(condition.Bool(false)) // false
fmt.Println(condition.Bool(true)) // true
// integer
fmt.Println(condition.Bool(0)) // false
fmt.Println(condition.Bool(1)) // true
// float
fmt.Println(condition.Bool(0.0)) // false
fmt.Println(condition.Bool(0.1)) // true
// string
fmt.Println(condition.Bool("")) // false
fmt.Println(condition.Bool(" ")) // true
fmt.Println(condition.Bool("0")) // true
// slice
var nums [2]int
fmt.Println(condition.Bool(nums)) // false
nums = [2]int{0, 1}
fmt.Println(condition.Bool(nums)) // true
// map
fmt.Println(condition.Bool(map[string]string{})) // false
fmt.Println(condition.Bool(map[string]string{"a": "a"})) // true
// struct
fmt.Println(condition.Bool(struct{}{})) // false
fmt.Println(condition.Bool(time.Now())) // true
}
```
### <span id="And">And</span>
<p>Returns true if both a and b are truthy.</p>
<b>Signature:</b>
```go
func And[T, U any](a T, b U) bool
```
<b>Example:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/condition"
)
func main() {
fmt.Println(condition.And(0, 1)) // false
fmt.Println(condition.And(0, "")) // false
fmt.Println(condition.And(0, "0")) // false
fmt.Println(condition.And(1, "0")) // true
}
```
### <span id="Or">Or</span>
<p>Returns false iff neither a nor b is truthy.</p>
<b>Signature:</b>
```go
func Or[T, U any](a T, b U) bool
```
<b>Example:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/condition"
)
func main() {
fmt.Println(condition.Or(0, "")) // false
fmt.Println(condition.Or(0, 1)) // true
fmt.Println(condition.Or(0, "0")) // true
fmt.Println(condition.Or(1, "0")) // true
}
```
### <span id="Xor">Xor</span>
<p>Returns true iff a or b but not both is truthy.</p>
<b>Signature:</b>
```go
func Xor[T, U any](a T, b U) bool
```
<b>Example:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/condition"
)
func main() {
fmt.Println(condition.Xor(0, 0)) // false
fmt.Println(condition.Xor(0, 1)) // true
fmt.Println(condition.Xor(1, 0)) // true
fmt.Println(condition.Xor(1, 1)) // false
}
```
### <span id="Nor">Nor</span>
<p>Returns true iff neither a nor b is truthy.</p>
<b>Signature:</b>
```go
func Nor[T, U any](a T, b U) bool
```
<b>Example:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/condition"
)
func main() {
fmt.Println(condition.Nor(0, 0)) // true
fmt.Println(condition.Nor(0, 1)) // false
fmt.Println(condition.Nor(1, 0)) // false
fmt.Println(condition.Nor(1, 1)) // true
}
```
### <span id="Nand">Nand</span>
<p>Returns false iff both a and b are truthy</p>
<b>Signature:</b>
```go
func Nand[T, U any](a T, b U) bool
```
<b>Example:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/condition"
)
func main() {
fmt.Println(condition.Nand(0, 0)) // true
fmt.Println(condition.Nand(0, 1)) // true
fmt.Println(condition.Nand(1, 0)) // true
fmt.Println(condition.Nand(1, 1)) // false
}
```
### <span id="TernaryOperator">TernaryOperator</span>
<p>Checks the value of param `isTrue`, if true return ifValue else return elseValue</p>
<b>Signature:</b>
```go
func TernaryOperator[T, U any](isTrue T, ifValue U, elseValue U) U
```
<b>Example:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/condition"
)
func main() {
trueValue := "1"
falseValue := "0"
fmt.Println(condition.TernaryOperator(true, trueValue, falseValue)) // "1"
}
```

263
docs/condition_zh-CN.md Normal file
View File

@@ -0,0 +1,263 @@
# Condition
condition包含一些用于条件判断的函数。这个包的实现参考了carlmjohnson的truthy包的实现更多有用的信息可以在[truthy](https://github.com/carlmjohnson/truthy)中找到感谢carlmjohnson。
<div STYLE="page-break-after: always;"></div>
## 源码:
- [https://github.com/duke-git/lancet/blob/main/condition/condition.go](https://github.com/duke-git/lancet/blob/main/condition/condition.go)
<div STYLE="page-break-after: always;"></div>
## 用法:
```go
import (
"github.com/duke-git/lancet/v2/condition"
)
```
<div STYLE="page-break-after: always;"></div>
## Index
- [Bool](#Bool)
- [And](#And)
- [Or](#Or)
- [Xor](#Generate)
- [Nor](#Nor)
- [Nand](#Nand)
- [TernaryOperator](#TernaryOperator)
<div STYLE="page-break-after: always;"></div>
## 目录
### <span id="Bool">Bool</span>
<p>返回传入参数的bool值.<br/>
如果出入类型参数含有Bool方法, 会调用该方法并返回<br/>
如果传入类型参数有IsZero方法, 返回IsZero方法返回值的取反<br/>
slices和map的length大于0时返回true否则返回false<br/>
其他类型会判断是否是零值</p>
<b>函数签名:</b>
```go
func Bool[T any](value T) bool
```
<b>例子:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/condition"
)
func main() {
// bool
fmt.Println(condition.Bool(false)) // false
fmt.Println(condition.Bool(true)) // true
// integer
fmt.Println(condition.Bool(0)) // false
fmt.Println(condition.Bool(1)) // true
// float
fmt.Println(condition.Bool(0.0)) // false
fmt.Println(condition.Bool(0.1)) // true
// string
fmt.Println(condition.Bool("")) // false
fmt.Println(condition.Bool(" ")) // true
fmt.Println(condition.Bool("0")) // true
// slice
var nums [2]int
fmt.Println(condition.Bool(nums)) // false
nums = [2]int{0, 1}
fmt.Println(condition.Bool(nums)) // true
// map
fmt.Println(condition.Bool(map[string]string{})) // false
fmt.Println(condition.Bool(map[string]string{"a": "a"})) // true
// struct
fmt.Println(condition.Bool(struct{}{})) // false
fmt.Println(condition.Bool(time.Now())) // true
}
```
### <span id="And">And</span>
<p>逻辑且操作当切仅当a和b都为true时返回true</p>
<b>函数签名:</b>
```go
func And[T, U any](a T, b U) bool
```
<b>例子:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/condition"
)
func main() {
fmt.Println(condition.And(0, 1)) // false
fmt.Println(condition.And(0, "")) // false
fmt.Println(condition.And(0, "0")) // false
fmt.Println(condition.And(1, "0")) // true
}
```
### <span id="Or">Or</span>
<p>逻辑或操作当切仅当a和b都为false时返回false</p>
<b>函数签名:</b>
```go
func Or[T, U any](a T, b U) bool
```
<b>例子:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/condition"
)
func main() {
fmt.Println(condition.Or(0, "")) // false
fmt.Println(condition.Or(0, 1)) // true
fmt.Println(condition.Or(0, "0")) // true
fmt.Println(condition.Or(1, "0")) // true
}
```
### <span id="Xor">Xor</span>
<p>逻辑异或操作a和b相同返回falsea和b不相同返回true</p>
<b>函数签名:</b>
```go
func Xor[T, U any](a T, b U) bool
```
<b>例子:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/condition"
)
func main() {
fmt.Println(condition.Xor(0, 0)) // false
fmt.Println(condition.Xor(0, 1)) // true
fmt.Println(condition.Xor(1, 0)) // true
fmt.Println(condition.Xor(1, 1)) // false
}
```
### <span id="Nor">Nor</span>
<p>异或的取反操作</p>
<b>函数签名:</b>
```go
func Nor[T, U any](a T, b U) bool
```
<b>例子:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/condition"
)
func main() {
fmt.Println(condition.Nor(0, 0)) // true
fmt.Println(condition.Nor(0, 1)) // false
fmt.Println(condition.Nor(1, 0)) // false
fmt.Println(condition.Nor(1, 1)) // true
}
```
### <span id="Nand">Nand</span>
<p>如果a和b都为真返回false否则返回true</p>
<b>函数签名:</b>
```go
func Nand[T, U any](a T, b U) bool
```
<b>例子:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/condition"
)
func main() {
fmt.Println(condition.Nand(0, 0)) // true
fmt.Println(condition.Nand(0, 1)) // true
fmt.Println(condition.Nand(1, 0)) // true
fmt.Println(condition.Nand(1, 1)) // false
}
```
### <span id="TernaryOperator">TernaryOperator</span>
<p>三元运算符</p>
<b>函数签名:</b>
```go
func TernaryOperator[T, U any](isTrue T, ifValue U, elseValue U) U
```
<b>例子:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/condition"
)
func main() {
trueValue := "1"
falseValue := "0"
fmt.Println(condition.TernaryOperator(true, trueValue, falseValue)) // "1"
}
```

View File

@@ -24,10 +24,18 @@ import (
- [ToBool](#ToBool)
- [ToBytes](#ToBytes)
- [ToChar](#ToChar)
- [ToChannel](#ToChannel)
- [ToFloat](#ToFloat)
- [ToInt](#ToInt)
- [ToJson](#ToJson)
- [ToMap](#ToMap)
- [ToPointer](#ToPointer)
- [ToString](#ToString)
- [StructToMap](#StructToMap)
- [MapToSlice](#MapToSlice)
- [EncodeByte](#EncodeByte)
- [DecodeByte](#DecodeByte)
<div STYLE="page-break-after: always;"></div>
@@ -191,6 +199,43 @@ func main() {
```
### <span id="ToChannel">ToChannel</span>
<p>Convert a collection of elements to a read-only channels.</p>
<b>Signature:</b>
```go
func ToChannel[T any](array []T) <-chan T
```
<b>Example:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/convertor"
)
func main() {
ch := convertor.ToChannel([]int{1, 2, 3})
val1, _ := <-ch
fmt.Println(val1) //1
val2, _ := <-ch
fmt.Println(val2) //2
val3, _ := <-ch
fmt.Println(val3) //3
_, ok := <-ch
fmt.Println(ok) //false
}
```
### <span id="ToFloat">ToFloat</span>
@@ -286,14 +331,14 @@ func main() {
### <span id="ToString">ToString</span>
### <span id="ToMap">ToMap</span>
<p>Convert interface to string. </p>
<p>Convert a slice or an array of structs to a map based on iteratee function. </p>
<b>Signature:</b>
```go
func ToString(value any) string
func ToMap[T any, K comparable, V any](array []T, iteratee func(T) (K, V)) map[K]V
```
<b>Example:</b>
@@ -306,9 +351,46 @@ import (
)
func main() {
fmt.Printf("%q", convertor.ToString(1)) //"1"
fmt.Printf("%q", convertor.ToString(1.1)) //"1.1"
fmt.Printf("%q", convertor.ToString([]int{1, 2, 3})) //"[1,2,3]"
type Message struct {
name string
code int
}
messages := []Message{
{name: "Hello", code: 100},
{name: "Hi", code: 101},
}
result := convertor.ToMap(messages, func(msg Message) (int, string) {
return msg.code, msg.name
})
fmt.Println(result) //{100: "Hello", 101: "Hi"}
}
```
### <span id="ToPointer">ToPointer</span>
<p>Returns a pointer to passed value. </p>
<b>Signature:</b>
```go
func ToPointer[T any](value T) *T
```
<b>Example:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/convertor"
)
func main() {
result := convertor.ToPointer(123)
fmt.Println(*result) //123
}
```
@@ -346,4 +428,90 @@ func main() {
fmt.Printf("type: %T, value: %s", pm, pm) //type: map[string]interface {}, value: map[name:test]
}
```
### <span id="MapToSlice">MapToSlice</span>
<p>Convert a map to a slice based on iteratee function.</p>
<b>Signature:</b>
```go
func MapToSlice[T any, K comparable, V any](aMap map[K]V, iteratee func(K, V) T) []T
```
<b>Example:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/convertor"
)
func main() {
aMap := map[string]int{"a": 1, "b": 2, "c": 3}
result := MapToSlice(aMap, func(key string, value int) string {
return key + ":" + strconv.Itoa(value)
})
fmt.Println(result) //[]string{"a:1", "b:2", "c:3"}
}
```
### <span id="EncodeByte">EncodeByte</span>
<p>Encode data to byte slice.</p>
<b>Signature:</b>
```go
func EncodeByte(data any) ([]byte, error)
```
<b>Example:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/convertor"
)
func main() {
byteData, _ := convertor.EncodeByte("abc")
fmt.Println(byteData) //[]byte{6, 12, 0, 3, 97, 98, 99}
}
```
### <span id="DecodeByte">DecodeByte</span>
<p>Decode byte data to target object. target should be a pointer instance.</p>
<b>Signature:</b>
```go
func DecodeByte(data []byte, target any) error
```
<b>Example:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/convertor"
)
func main() {
var result string
byteData := []byte{6, 12, 0, 3, 97, 98, 99}
convertor.DecodeByte(byteData, &result)
fmt.Println(result) //"abc"
}
```

View File

@@ -26,10 +26,18 @@ import (
- [ToBool](#ToBool)
- [ToBytes](#ToBytes)
- [ToChar](#ToChar)
- [ToChannel](#ToChannel)
- [ToFloat](#ToFloat)
- [ToInt](#ToInt)
- [ToJson](#ToJson)
- [ToMap](#ToMap)
- [ToPointer](#ToPointer)
- [ToString](#ToString)
- [StructToMap](#StructToMap)
- [MapToSlice](#MapToSlice)
- [EncodeByte](#EncodeByte)
- [DecodeByte](#DecodeByte)
<div STYLE="page-break-after: always;"></div>
@@ -194,6 +202,44 @@ func main() {
### <span id="ToChannel">ToChannel</span>
<p>将切片转为只读channel</p>
<b>函数签名:</b>
```go
func ToChannel[T any](array []T) <-chan T
```
<b>例子:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/convertor"
)
func main() {
ch := convertor.ToChannel([]int{1, 2, 3})
val1, _ := <-ch
fmt.Println(val1) //1
val2, _ := <-ch
fmt.Println(val2) //2
val3, _ := <-ch
fmt.Println(val3) //3
_, ok := <-ch
fmt.Println(ok) //false
}
```
### <span id="ToFloat">ToFloat</span>
<p>将interface转成float64类型如果参数无法转换会返回0和error</p>
@@ -229,7 +275,7 @@ func main() {
### <span id="ToInt">ToInt</span>
<p>将interface转成intt64类型如果参数无法转换会返回0和error</p>
<p>将interface转成int64类型如果参数无法转换会返回0和error</p>
<b>函数签名:</b>
@@ -288,6 +334,71 @@ func main() {
### <span id="ToMap">ToMap</span>
<p>将切片转为map</p>
<b>函数签名:</b>
```go
func ToMap[T any, K comparable, V any](array []T, iteratee func(T) (K, V)) map[K]V
```
<b>例子:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/convertor"
)
func main() {
type Message struct {
name string
code int
}
messages := []Message{
{name: "Hello", code: 100},
{name: "Hi", code: 101},
}
result := convertor.ToMap(messages, func(msg Message) (int, string) {
return msg.code, msg.name
})
fmt.Println(result) //{100: "Hello", 101: "Hi"}
}
```
### <span id="ToPointer">ToPointer</span>
<p>返回传入值的指针</p>
<b>函数签名:</b>
```go
func ToPointer[T any](value T) *T
```
<b>例子:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/convertor"
)
func main() {
result := convertor.ToPointer(123)
fmt.Println(*result) //123
}
```
### <span id="ToString">ToString</span>
<p>将interface转成字符串</p>
@@ -348,4 +459,91 @@ func main() {
fmt.Printf("type: %T, value: %s", pm, pm) //type: map[string]interface {}, value: map[name:test]
}
```
### <span id="MapToSlice">MapToSlice</span>
<p>map中key和value执行函数iteratee后转为切片</p>
<b>函数签名:</b>
```go
func MapToSlice[T any, K comparable, V any](aMap map[K]V, iteratee func(K, V) T) []T
```
<b>例子:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/convertor"
)
func main() {
aMap := map[string]int{"a": 1, "b": 2, "c": 3}
result := MapToSlice(aMap, func(key string, value int) string {
return key + ":" + strconv.Itoa(value)
})
fmt.Println(result) //[]string{"a:1", "b:2", "c:3"}
}
```
### <span id="EncodeByte">EncodeByte</span>
<p>将data编码成字节切片</p>
<b>函数签名:</b>
```go
func EncodeByte(data any) ([]byte, error)
```
<b>例子:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/convertor"
)
func main() {
byteData, _ := convertor.EncodeByte("abc")
fmt.Println(byteData) //[]byte{6, 12, 0, 3, 97, 98, 99}
}
```
### <span id="DecodeByte">DecodeByte</span>
<p>解码字节切片到目标对象,目标对象需要传入一个指针实例子</p>
<b>函数签名:</b>
```go
func DecodeByte(data []byte, target any) error
```
<b>例子:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/convertor"
)
func main() {
var result string
byteData := []byte{6, 12, 0, 3, 97, 98, 99}
convertor.DecodeByte(byteData, &result)
fmt.Println(result) //"abc"
}
```

View File

@@ -85,7 +85,7 @@ import (
func main() {
data := "hello world"
key := "abcdefghijklmnop"
key := "abcdefghijklmnop"
encrypted := cryptor.AesEcbEncrypt([]byte(data), []byte(key))
fmt.Println(string(encrypted))
@@ -115,9 +115,9 @@ import (
func main() {
data := "hello world"
key := "abcdefghijklmnop"
key := "abcdefghijklmnop"
encrypted := cryptor.AesEcbEncrypt([]byte(data), []byte(key))
decrypted := cryptor.AesEcbDecrypt(encrypted, []byte(key))
decrypted := cryptor.AesEcbDecrypt(encrypted, []byte(key))
fmt.Println(string(decrypted)) //hello world
}
```
@@ -145,7 +145,7 @@ import (
func main() {
data := "hello world"
key := "abcdefghijklmnop"
key := "abcdefghijklmnop"
encrypted := cryptor.AesCbcEncrypt([]byte(data), []byte(key))
fmt.Println(string(encrypted))
@@ -176,9 +176,9 @@ import (
func main() {
data := "hello world"
key := "abcdefghijklmnop"
key := "abcdefghijklmnop"
encrypted := cryptor.AesCbcEncrypt([]byte(data), []byte(key))
decrypted := cryptor.AesCbcDecrypt(encrypted, []byte(key))
decrypted := cryptor.AesCbcDecrypt(encrypted, []byte(key))
fmt.Println(string(decrypted)) //hello world
}
```
@@ -207,7 +207,7 @@ import (
func main() {
data := "hello world"
key := "abcdefghijklmnop"
key := "abcdefghijklmnop"
encrypted := cryptor.AesCtrCrypt([]byte(data), []byte(key))
decrypted := cryptor.AesCtrCrypt(encrypted, []byte(key))
@@ -239,7 +239,7 @@ import (
func main() {
data := "hello world"
key := "abcdefghijklmnop"
key := "abcdefghijklmnop"
encrypted := cryptor.AesCfbEncrypt([]byte(data), []byte(key))
fmt.Println(string(encrypted))
}
@@ -271,7 +271,7 @@ func main() {
data := "hello world"
key := "abcdefghijklmnop"
encrypted := cryptor.AesCfbEncrypt([]byte(data), []byte(key))
decrypted := cryptor.AesCfbDecrypt(encrypted, []byte(key))
decrypted := cryptor.AesCfbDecrypt(encrypted, []byte(key))
fmt.Println(string(decrypted)) //hello world
}
```
@@ -300,7 +300,7 @@ import (
func main() {
data := "hello world"
key := "abcdefghijklmnop"
key := "abcdefghijklmnop"
encrypted := cryptor.AesOfbEncrypt([]byte(data), []byte(key))
fmt.Println(string(encrypted))
}
@@ -330,9 +330,9 @@ import (
func main() {
data := "hello world"
key := "abcdefghijklmnop"
key := "abcdefghijklmnop"
encrypted := cryptor.AesOfbEncrypt([]byte(data), []byte(key))
decrypted := cryptor.AesOfbDecrypt(encrypted, []byte(key))
decrypted := cryptor.AesOfbDecrypt(encrypted, []byte(key))
fmt.Println(string(decrypted)) //hello world
}
@@ -360,7 +360,7 @@ import (
)
func main() {
base64Str := cryptor.Base64StdEncode("hello world")
base64Str := cryptor.Base64StdEncode("hello world")
fmt.Println(base64Str) //aGVsbG8gd29ybGQ=
}
```
@@ -417,7 +417,7 @@ import (
func main() {
data := "hello world"
key := "abcdefgh"
key := "abcdefgh"
encrypted := cryptor.DesEcbEncrypt([]byte(data), []byte(key))
fmt.Println(string(encrypted))
@@ -448,9 +448,9 @@ import (
func main() {
data := "hello world"
key := "abcdefgh"
key := "abcdefgh"
encrypted := cryptor.DesEcbEncrypt([]byte(data), []byt(key)
decrypted := cryptor.DesEcbDecrypt(encrypted, []byte(key))
decrypted := cryptor.DesEcbDecrypt(encrypted, []byte(key))
fmt.Println(string(decrypted)) //hello world
}
@@ -480,7 +480,7 @@ import (
func main() {
data := "hello world"
key := "abcdefgh"
key := "abcdefgh"
encrypted := cryptor.DesCbcEncrypt([]byte(data), []byt(key)
fmt.Println(string(encrypted))
@@ -511,7 +511,7 @@ import (
func main() {
data := "hello world"
key := "abcdefgh"
key := "abcdefgh"
encrypted := cryptor.DesCbcEncrypt([]byte(data), []byt(key)
decrypted := cryptor.DesCbcDecrypt(encrypted, []byte(key))
@@ -543,7 +543,7 @@ import (
func main() {
data := "hello world"
key := "abcdefgh"
key := "abcdefgh"
encrypted := cryptor.DesCtrCrypt([]byte(data), []byte(key))
decrypted := cryptor.DesCtrCrypt(encrypted, []byte(key))
@@ -575,7 +575,7 @@ import (
func main() {
data := "hello world"
key := "abcdefgh"
key := "abcdefgh"
encrypted := cryptor.DesCfbEncrypt([]byte(data), []byt(key)
fmt.Println(string(encrypted))
}
@@ -605,9 +605,9 @@ import (
func main() {
data := "hello world"
key := "abcdefgh"
key := "abcdefgh"
encrypted := cryptor.DesCfbEncrypt([]byte(data), []byt(key)
decrypted := cryptor.DesCfbDecrypt(encrypted, []byte(key))
decrypted := cryptor.DesCfbDecrypt(encrypted, []byte(key))
fmt.Println(string(decrypted)) //hello world
}
```
@@ -636,7 +636,7 @@ import (
func main() {
data := "hello world"
key := "abcdefgh"
key := "abcdefgh"
encrypted := cryptor.DesOfbEncrypt([]byte(data), []byte(key))
fmt.Println(string(encrypted))
}
@@ -666,9 +666,9 @@ import (
func main() {
data := "hello world"
key := "abcdefgh"
key := "abcdefgh"
encrypted := cryptor.DesOfbEncrypt([]byte(data), []byte(key))
decrypted := cryptor.DesOfbDecrypt(encrypted, []byte(key))
decrypted := cryptor.DesOfbDecrypt(encrypted, []byte(key))
fmt.Println(string(decrypted)) //hello world
}
@@ -725,8 +725,8 @@ import (
)
func main() {
s := cryptor.HmacSha1("hello world", "12345"))
fmt.Println(s) //3826f812255d8683f051ee97346d1359234d5dbd
s := cryptor.HmacSha1("hello world", "12345"))
fmt.Println(s) //3826f812255d8683f051ee97346d1359234d5dbd
}
```
@@ -753,8 +753,8 @@ import (
)
func main() {
s := cryptor.HmacSha256("hello world", "12345"))
fmt.Println(s) //9dce2609f2d67d41f74c7f9efc8ccd44370d41ad2de52982627588dfe7289ab8
s := cryptor.HmacSha256("hello world", "12345"))
fmt.Println(s) //9dce2609f2d67d41f74c7f9efc8ccd44370d41ad2de52982627588dfe7289ab8
}
```
@@ -781,8 +781,8 @@ import (
)
func main() {
s := cryptor.HmacSha512("hello world", "12345"))
fmt.Println(s)
s := cryptor.HmacSha512("hello world", "12345"))
fmt.Println(s)
//5b1563ac4e9b49c9ada8ccb232588fc4f0c30fd12f756b3a0b95af4985c236ca60925253bae10ce2c6bf9af1c1679b51e5395ff3d2826c0a2c7c0d72225d4175
}
```
@@ -810,8 +810,8 @@ import (
)
func main() {
s := cryptor.Md5String("hello"))
fmt.Println(s) //5d41402abc4b2a76b9719d911017c592
s := cryptor.Md5String("hello"))
fmt.Println(s) //5d41402abc4b2a76b9719d911017c592
}
```
@@ -838,8 +838,8 @@ import (
)
func main() {
s := cryptor.Md5File("./main.go"))
fmt.Println(s)
s := cryptor.Md5File("./main.go"))
fmt.Println(s)
}
```
@@ -866,8 +866,8 @@ import (
)
func main() {
s := cryptor.Sha1("hello world"))
fmt.Println(s) //2aae6c35c94fcfb415dbe95f408b9ce91ee846ed
s := cryptor.Sha1("hello world"))
fmt.Println(s) //2aae6c35c94fcfb415dbe95f408b9ce91ee846ed
}
```
@@ -894,8 +894,8 @@ import (
)
func main() {
s := cryptor.Sha256("hello world"))
fmt.Println(s) //b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9
s := cryptor.Sha256("hello world"))
fmt.Println(s) //b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9
}
```
@@ -922,7 +922,7 @@ import (
)
func main() {
s := cryptor.Sha512("hello world"))
s := cryptor.Sha512("hello world"))
fmt.Println(s) //309ecc489c12d6eb4cc40f50c902f2b4d0ed77ee511a7c7a9bcd3ca86d4cd86f989dd35bc5ff499670da34255b45b0cfd830e81f605dcf7dc5542e93ae9cd76f
}
```
@@ -950,10 +950,10 @@ import (
)
func main() {
err := cryptor.GenerateRsaKey(4096, "rsa_private.pem", "rsa_public.pem")
err := cryptor.GenerateRsaKey(4096, "rsa_private.pem", "rsa_public.pem")
if err != nil {
fmt.Println(err)
}
}
}
```
@@ -980,14 +980,14 @@ import (
)
func main() {
err := cryptor.GenerateRsaKey(4096, "rsa_private.pem", "rsa_public.pem")
err := cryptor.GenerateRsaKey(4096, "rsa_private.pem", "rsa_public.pem")
if err != nil {
fmt.Println(err)
}
data := []byte("hello world")
encrypted := cryptor.RsaEncrypt(data, "rsa_public.pem")
decrypted := cryptor.RsaDecrypt(encrypted, "rsa_private.pem")
encrypted := cryptor.RsaEncrypt(data, "rsa_public.pem")
decrypted := cryptor.RsaDecrypt(encrypted, "rsa_private.pem")
fmt.Println(string(decrypted)) //hello world
}
@@ -1016,14 +1016,14 @@ import (
)
func main() {
err := cryptor.GenerateRsaKey(4096, "rsa_private.pem", "rsa_public.pem")
err := cryptor.GenerateRsaKey(4096, "rsa_private.pem", "rsa_public.pem")
if err != nil {
fmt.Println(err)
}
}
data := []byte("hello world")
encrypted := cryptor.RsaEncrypt(data, "rsa_public.pem")
decrypted := cryptor.RsaDecrypt(encrypted, "rsa_private.pem")
encrypted := cryptor.RsaEncrypt(data, "rsa_public.pem")
decrypted := cryptor.RsaDecrypt(encrypted, "rsa_private.pem")
fmt.Println(string(decrypted)) //hello world
}

View File

@@ -85,7 +85,7 @@ import (
func main() {
data := "hello world"
key := "abcdefghijklmnop"
key := "abcdefghijklmnop"
encrypted := cryptor.AesEcbEncrypt([]byte(data), []byte(key))
fmt.Println(string(encrypted))
@@ -115,9 +115,9 @@ import (
func main() {
data := "hello world"
key := "abcdefghijklmnop"
key := "abcdefghijklmnop"
encrypted := cryptor.AesEcbEncrypt([]byte(data), []byte(key))
decrypted := cryptor.AesEcbDecrypt(encrypted, []byte(key))
decrypted := cryptor.AesEcbDecrypt(encrypted, []byte(key))
fmt.Println(string(decrypted)) //hello world
}
```
@@ -145,7 +145,7 @@ import (
func main() {
data := "hello world"
key := "abcdefghijklmnop"
key := "abcdefghijklmnop"
encrypted := cryptor.AesCbcEncrypt([]byte(data), []byte(key))
fmt.Println(string(encrypted))
@@ -176,9 +176,9 @@ import (
func main() {
data := "hello world"
key := "abcdefghijklmnop"
key := "abcdefghijklmnop"
encrypted := cryptor.AesCbcEncrypt([]byte(data), []byte(key))
decrypted := cryptor.AesCbcDecrypt(encrypted, []byte(key))
decrypted := cryptor.AesCbcDecrypt(encrypted, []byte(key))
fmt.Println(string(decrypted)) //hello world
}
@@ -208,7 +208,7 @@ import (
func main() {
data := "hello world"
key := "abcdefghijklmnop"
key := "abcdefghijklmnop"
encrypted := cryptor.AesCtrCrypt([]byte(data), []byte(key))
decrypted := cryptor.AesCtrCrypt(encrypted, []byte(key))
@@ -240,7 +240,7 @@ import (
func main() {
data := "hello world"
key := "abcdefghijklmnop"
key := "abcdefghijklmnop"
encrypted := cryptor.AesCfbEncrypt([]byte(data), []byte(key))
fmt.Println(string(encrypted))
}
@@ -270,9 +270,9 @@ import (
func main() {
data := "hello world"
key := "abcdefghijklmnop"
key := "abcdefghijklmnop"
encrypted := cryptor.AesCfbEncrypt([]byte(data), []byte(key))
decrypted := cryptor.AesCfbDecrypt(encrypted, []byte(key))
decrypted := cryptor.AesCfbDecrypt(encrypted, []byte(key))
fmt.Println(string(decrypted)) //hello world
}
```
@@ -301,7 +301,7 @@ import (
func main() {
data := "hello world"
key := "abcdefghijklmnop"
key := "abcdefghijklmnop"
encrypted := cryptor.AesOfbEncrypt([]byte(data), []byte(key))
fmt.Println(string(encrypted))
}
@@ -331,9 +331,9 @@ import (
func main() {
data := "hello world"
key := "abcdefghijklmnop"
key := "abcdefghijklmnop"
encrypted := cryptor.AesOfbEncrypt([]byte(data), []byte(key))
decrypted := cryptor.AesOfbDecrypt(encrypted, []byte(key))
decrypted := cryptor.AesOfbDecrypt(encrypted, []byte(key))
fmt.Println(string(decrypted)) //hello world
}
```
@@ -360,7 +360,7 @@ import (
)
func main() {
base64Str := cryptor.Base64StdEncode("hello world")
base64Str := cryptor.Base64StdEncode("hello world")
fmt.Println(base64Str) //aGVsbG8gd29ybGQ=
}
```
@@ -388,7 +388,7 @@ import (
)
func main() {
str := cryptor.Base64StdDecode("aGVsbG8gd29ybGQ=")
str := cryptor.Base64StdDecode("aGVsbG8gd29ybGQ=")
fmt.Println(str) //hello world
}
```
@@ -417,7 +417,7 @@ import (
func main() {
data := "hello world"
key := "abcdefgh"
key := "abcdefgh"
encrypted := cryptor.DesEcbEncrypt([]byte(data), []byte(key))
fmt.Println(string(encrypted))
@@ -448,7 +448,7 @@ import (
func main() {
data := "hello world"
key := "abcdefgh"
key := "abcdefgh"
encrypted := cryptor.DesEcbEncrypt([]byte(data), []byte(key))
decrypted := cryptor.DesEcbDecrypt(encrypted, []byte(key))
@@ -480,7 +480,7 @@ import (
func main() {
data := "hello world"
key := "abcdefgh"
key := "abcdefgh"
encrypted := cryptor.DesCbcEncrypt([]byte(data), []byte(key))
fmt.Println(string(encrypted))
@@ -511,7 +511,7 @@ import (
func main() {
data := "hello world"
key := "abcdefgh"
key := "abcdefgh"
encrypted := cryptor.DesCbcEncrypt([]byte(data), []byte(key))
decrypted := cryptor.DesCbcDecrypt(encrypted, []byte(key))
@@ -543,7 +543,7 @@ import (
func main() {
data := "hello world"
key := "abcdefgh"
key := "abcdefgh"
encrypted := cryptor.DesCtrCrypt([]byte(data), []byte(key))
decrypted := cryptor.DesCtrCrypt(encrypted, []byte(key))
@@ -575,7 +575,7 @@ import (
func main() {
data := "hello world"
key := "abcdefgh"
key := "abcdefgh"
encrypted := cryptor.DesCfbEncrypt([]byte(data), []byte(key))
fmt.Println(string(encrypted))
}
@@ -605,7 +605,7 @@ import (
func main() {
data := "hello world"
key := "abcdefgh"
key := "abcdefgh"
encrypted := cryptor.DesCfbEncrypt([]byte(data), []byte(key))
decrypted := cryptor.DesCfbDecrypt(encrypted, []byte(key))
fmt.Println(string(decrypted)) //hello world
@@ -636,7 +636,7 @@ import (
func main() {
data := "hello world"
key := "abcdefgh"
key := "abcdefgh"
encrypted := cryptor.DesOfbEncrypt([]byte(data), []byte(key))
fmt.Println(string(encrypted))
}
@@ -666,7 +666,7 @@ import (
func main() {
data := "hello world"
key := "abcdefgh"
key := "abcdefgh"
encrypted := cryptor.DesOfbEncrypt([]byte(data), []byte(key))
decrypted := cryptor.DesOfbDecrypt(encrypted, []byte(key))
fmt.Println(string(decrypted)) //hello world
@@ -696,8 +696,8 @@ import (
)
func main() {
s := cryptor.HmacMd5("hello world", "12345"))
fmt.Println(s) //5f4c9faaff0a1ad3007d9ddc06abe36d
s := cryptor.HmacMd5("hello world", "12345"))
fmt.Println(s) //5f4c9faaff0a1ad3007d9ddc06abe36d
}
```
@@ -724,8 +724,8 @@ import (
)
func main() {
s := cryptor.HmacSha1("hello world", "12345"))
fmt.Println(s) //3826f812255d8683f051ee97346d1359234d5dbd
s := cryptor.HmacSha1("hello world", "12345"))
fmt.Println(s) //3826f812255d8683f051ee97346d1359234d5dbd
}
```
@@ -752,8 +752,8 @@ import (
)
func main() {
s := cryptor.HmacSha256("hello world", "12345"))
fmt.Println(s) //9dce2609f2d67d41f74c7f9efc8ccd44370d41ad2de52982627588dfe7289ab8
s := cryptor.HmacSha256("hello world", "12345"))
fmt.Println(s) //9dce2609f2d67d41f74c7f9efc8ccd44370d41ad2de52982627588dfe7289ab8
}
```
@@ -780,8 +780,8 @@ import (
)
func main() {
s := cryptor.HmacSha512("hello world", "12345"))
fmt.Println(s)
s := cryptor.HmacSha512("hello world", "12345"))
fmt.Println(s)
//5b1563ac4e9b49c9ada8ccb232588fc4f0c30fd12f756b3a0b95af4985c236ca60925253bae10ce2c6bf9af1c1679b51e5395ff3d2826c0a2c7c0d72225d4175
}
```
@@ -809,8 +809,8 @@ import (
)
func main() {
s := cryptor.Md5String("hello"))
fmt.Println(s) //5d41402abc4b2a76b9719d911017c592
s := cryptor.Md5String("hello"))
fmt.Println(s) //5d41402abc4b2a76b9719d911017c592
}
```
@@ -837,8 +837,8 @@ import (
)
func main() {
s := cryptor.Md5File("./main.go"))
fmt.Println(s)
s := cryptor.Md5File("./main.go"))
fmt.Println(s)
}
```
@@ -865,8 +865,8 @@ import (
)
func main() {
s := cryptor.Sha1("hello world"))
fmt.Println(s) //2aae6c35c94fcfb415dbe95f408b9ce91ee846ed
s := cryptor.Sha1("hello world"))
fmt.Println(s) //2aae6c35c94fcfb415dbe95f408b9ce91ee846ed
}
```
@@ -893,8 +893,8 @@ import (
)
func main() {
s := cryptor.Sha256("hello world"))
fmt.Println(s) //b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9
s := cryptor.Sha256("hello world"))
fmt.Println(s) //b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9
}
```
@@ -921,8 +921,8 @@ import (
)
func main() {
s := cryptor.Sha512("hello world"))
fmt.Println(s) //309ecc489c12d6eb4cc40f50c902f2b4d0ed77ee511a7c7a9bcd3ca86d4cd86f989dd35bc5ff499670da34255b45b0cfd830e81f605dcf7dc5542e93ae9cd76f
s := cryptor.Sha512("hello world"))
fmt.Println(s) //309ecc489c12d6eb4cc40f50c902f2b4d0ed77ee511a7c7a9bcd3ca86d4cd86f989dd35bc5ff499670da34255b45b0cfd830e81f605dcf7dc5542e93ae9cd76f
}
```
@@ -949,10 +949,10 @@ import (
)
func main() {
err := cryptor.GenerateRsaKey(4096, "rsa_private.pem", "rsa_public.pem")
if err != nil {
fmt.Println(err)
}
err := cryptor.GenerateRsaKey(4096, "rsa_private.pem", "rsa_public.pem")
if err != nil {
fmt.Println(err)
}
}
```
@@ -979,11 +979,11 @@ import (
)
func main() {
err := cryptor.GenerateRsaKey(4096, "rsa_private.pem", "rsa_public.pem")
if err != nil {
fmt.Println(err)
}
data := []byte("hello world")
err := cryptor.GenerateRsaKey(4096, "rsa_private.pem", "rsa_public.pem")
if err != nil {
fmt.Println(err)
}
data := []byte("hello world")
encrypted := cryptor.RsaEncrypt(data, "rsa_public.pem")
decrypted := cryptor.RsaDecrypt(encrypted, "rsa_private.pem")
fmt.Println(string(decrypted)) //hello world
@@ -1013,11 +1013,11 @@ import (
)
func main() {
err := cryptor.GenerateRsaKey(4096, "rsa_private.pem", "rsa_public.pem")
if err != nil {
fmt.Println(err)
}
data := []byte("hello world")
err := cryptor.GenerateRsaKey(4096, "rsa_private.pem", "rsa_public.pem")
if err != nil {
fmt.Println(err)
}
data := []byte("hello world")
encrypted := cryptor.RsaEncrypt(data, "rsa_public.pem")
decrypted := cryptor.RsaDecrypt(encrypted, "rsa_private.pem")
fmt.Println(string(decrypted)) //hello world

View File

@@ -0,0 +1,314 @@
# HashMap
HashMap is a key value map data structure.
<div STYLE="page-break-after: always;"></div>
## Source
- [https://github.com/duke-git/lancet/blob/main/datastructure/hashmap/hashmap.go](https://github.com/duke-git/lancet/blob/main/datastructure/hashmap/hashmap.go)
<div STYLE="page-break-after: always;"></div>
## Usage
```go
import (
hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
)
```
<div STYLE="page-break-after: always;"></div>
## Index
- [NewHashMap](#NewHashMap)
- [NewHashMapWithCapacity](#NewHashMapWithCapacity)
- [Get](#Get)
- [Put](#Put)
- [Delete](#Delete)
- [Contains](#Contains)
- [Iterate](#Iterate)
- [Keys](#Keys)
- [Values](#Values)
<div STYLE="page-break-after: always;"></div>
## Documentation
### <span id="NewHashMap">NewHashMap</span>
<p>Make a HashMap instance with default capacity is 1 << 10.</p>
<b>Signature:</b>
```go
func NewHashMap() *HashMap
```
<b>Example:</b>
```go
package main
import (
"fmt"
hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
)
func main() {
hm := heap.NewHashMap()
fmt.Println(hm)
}
```
### <span id="NewHashMap">NewHashMap</span>
<p>Make a HashMap instance with given size and capacity.</p>
<b>Signature:</b>
```go
func NewHashMapWithCapacity(size, capacity uint64) *HashMap
```
<b>Example:</b>
```go
package main
import (
"fmt"
hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
)
func main() {
hm := heap.NewHashMapWithCapacity(uint64(100), uint64(1000))
fmt.Println(hm)
}
```
### <span id="Get">Get</span>
<p>Get the value of given key in hashmap</p>
<b>Signature:</b>
```go
func (hm *HashMap) Get(key any) any
```
<b>Example:</b>
```go
package main
import (
"fmt"
hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
)
func main() {
hm := heap.NewHashMap()
val := hm.Get("a")
fmt.Println(val) //nil
}
```
### <span id="Put">Put</span>
<p>Put new key value in hashmap, then return value</p>
<b>Signature:</b>
```go
func (hm *HashMap) Put(key any, value any) any
```
<b>Example:</b>
```go
package main
import (
"fmt"
hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
)
func main() {
hm := heap.NewHashMap()
hm.Put("a", 1)
val := hm.Get("a")
fmt.Println(val) //1
}
```
### <span id="Delete">Delete</span>
<p>Delete key-value item by given key in hashmap.</p>
<b>Signature:</b>
```go
func (hm *HashMap) Delete(key any)
```
<b>Example:</b>
```go
package main
import (
"fmt"
hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
)
func main() {
hm := heap.NewHashMap()
hm.Put("a", 1)
val := hm.Get("a")
fmt.Println(val) //1
hm.Delete("a")
val = hm.Get("a")
fmt.Println(val) //nil
}
```
### <span id="Contains">Contains</span>
<p>Checks if given key is in hashmap or not.</p>
<b>Signature:</b>
```go
func (hm *HashMap) Contains(key any) bool
```
<b>Example:</b>
```go
package main
import (
"fmt"
hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
)
func main() {
hm := heap.NewHashMap()
hm.Put("a", 1)
fmt.Println(hm.Contains("a")) //true
fmt.Println(hm.Contains("b")) //false
}
```
### <span id="Iterate">Iterate</span>
<p>Executes iteratee funcation for every key and value pair of hashmap.</p>
<b>Signature:</b>
```go
func (hm *HashMap) Iterate(iteratee func(key, value any))
```
<b>Example:</b>
```go
package main
import (
"fmt"
hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
)
func main() {
hm := heap.NewHashMap()
hm.Put("a", 1)
hm.Put("b", 2)
hm.Put("c", 3)
hm.Iterate(func(key, value any) {
fmt.Println(key)
fmt.Println(value)
})
}
```
### <span id="Keys">Keys</span>
<p>Return a slice of the hashmap's keys (random order).</p>
<b>Signature:</b>
```go
func (hm *HashMap) Keys() []any
```
<b>Example:</b>
```go
package main
import (
"fmt"
hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
)
func main() {
hm := heap.NewHashMap()
hm.Put("a", 1)
hm.Put("b", 2)
hm.Put("c", 3)
keys := hm.Keys()
fmt.Println(keys) //[]interface{"a", "b", "c"}
}
```
### <span id="Values">Values</span>
<p>Return a slice of the hashmap's values (random order).</p>
<b>Signature:</b>
```go
func (hm *HashMap) Values() []any
```
<b>Example:</b>
```go
package main
import (
"fmt"
hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
)
func main() {
hm := heap.NewHashMap()
hm.Put("a", 1)
hm.Put("b", 2)
hm.Put("c", 3)
values := hm.Values()
fmt.Println(values) //[]interface{2, 1, 3}
}
```

View File

@@ -0,0 +1,308 @@
# HashMap
HashMap 数据结构实现
<div STYLE="page-break-after: always;"></div>
## 源码
- [https://github.com/duke-git/lancet/blob/main/datastructure/hashmap/hashmap.go](https://github.com/duke-git/lancet/blob/main/datastructure/hashmap/hashmap.go)
<div STYLE="page-break-after: always;"></div>
## 用法
```go
import (
hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
)
```
<div STYLE="page-break-after: always;"></div>
## 目录
- [NewHashMap](#NewHashMap)
- [NewHashMapWithCapacity](#NewHashMapWithCapacity)
- [Get](#Get)
- [Put](#Put)
- [Delete](#Delete)
- [Contains](#Contains)
- [Iterate](#Iterate)
- [Keys](#Keys)
- [Values](#Values)
<div STYLE="page-break-after: always;"></div>
## API 文档
### <span id="NewHashMap">NewHashMap</span>
<p>新建默认容量1 << 10的HashMap指针实例</p>
<b>函数签名:</b>
```go
func NewHashMap() *HashMap
```
<b>例子:</b>
```go
package main
import (
"fmt"
hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
)
func main() {
hm := heap.NewHashMap()
fmt.Println(hm)
}
```
### <span id="NewHashMapWithCapacity">NewHashMapWithCapacity</span>
<p>新建指定容量和长度的HashMap指针实例.</p>
<b>函数签名:</b>
```go
func NewHashMapWithCapacity(size, capacity uint64) *HashMap
```
<b>例子:</b>
```go
package main
import (
"fmt"
hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
)
func main() {
hm := heap.NewHashMapWithCapacity(uint64(100), uint64(1000))
fmt.Println(hm)
}
```
### <span id="Get">Get</span>
<p>在hashmap中根据key获取值</p>
<b>函数签名:</b>
```go
func (hm *HashMap) Get(key any) any
```
<b>例子:</b>
```go
package main
import (
"fmt"
hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
)
func main() {
hm := heap.NewHashMap()
val := hm.Get("a")
fmt.Println(val) //nil
}
```
### <span id="Put">Put</span>
<p>将key-value放入hashmap中</p>
<b>函数签名:</b>
```go
func (hm *HashMap) Put(key any, value any) any
```
<b>例子:</b>
```go
package main
import (
"fmt"
hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
)
func main() {
hm := heap.NewHashMap()
hm.Put("a", 1)
val := hm.Get("a")
fmt.Println(val) //1
}
```
### <span id="Delete">Delete</span>
<p>将指定的key从hashmap中删除</p>
<b>函数签名:</b>
```go
func (hm *HashMap) Delete(key any)
```
<b>例子:</b>
```go
package main
import (
"fmt"
hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
)
func main() {
hm := heap.NewHashMap()
hm.Put("a", 1)
val := hm.Get("a")
fmt.Println(val) //1
hm.Delete("a")
val = hm.Get("a")
fmt.Println(val) //nil
}
```
### <span id="Contains">Contains</span>
<p>判断hashmap中是否包含指定的key</p>
<b>函数签名:</b>
```go
func (hm *HashMap) Contains(key any) bool
```
<b>例子:</b>
```go
package main
import (
"fmt"
hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
)
func main() {
hm := heap.NewHashMap()
hm.Put("a", 1)
fmt.Println(hm.Contains("a")) //true
fmt.Println(hm.Contains("b")) //false
}
```
### <span id="Iterate">Iterate</span>
<p>迭代hashmap对每个key和value执行iteratee函数</p>
<b>函数签名:</b>
```go
func (hm *HashMap) Iterate(iteratee func(key, value any))
```
<b>例子:</b>
```go
package main
import (
"fmt"
hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
)
func main() {
hm := heap.NewHashMap()
hm.Put("a", 1)
hm.Put("b", 2)
hm.Put("c", 3)
hm.Iterate(func(key, value any) {
fmt.Println(key)
fmt.Println(value)
})
}
```
### <span id="Keys">Keys</span>
<p>返回hashmap所有key的切片 (随机顺序)</p>
<b>函数签名:</b>
```go
func (hm *HashMap) Keys() []any
```
<b>例子:</b>
```go
package main
import (
"fmt"
hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
)
func main() {
hm := heap.NewHashMap()
hm.Put("a", 1)
hm.Put("b", 2)
hm.Put("c", 3)
keys := hm.Keys()
fmt.Println(keys) //[]interface{"a", "b", "c"}
}
```
### <span id="Values">Values</span>
<p>返回hashmap所有值的切片 (随机顺序).</p>
<b>函数签名:</b>
```go
func (hm *HashMap) Values() []any
```
<b>例子:</b>
```go
package main
import (
"fmt"
hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
)
func main() {
hm := heap.NewHashMap()
hm.Put("a", 1)
hm.Put("b", 2)
hm.Put("c", 3)
values := hm.Values()
fmt.Println(values) //[]interface{2, 1, 3}
}
```

364
docs/datastructure/heap.md Normal file
View File

@@ -0,0 +1,364 @@
# Heap
Heap is a binary heap tree implemented by slice.
<div STYLE="page-break-after: always;"></div>
## Source
- [https://github.com/duke-git/lancet/blob/main/datastructure/heap/maxheap.go](https://github.com/duke-git/lancet/blob/main/datastructure/heap/maxheap.go)
<div STYLE="page-break-after: always;"></div>
## Usage
```go
import (
heap "github.com/duke-git/lancet/v2/datastructure/heap"
)
```
<div STYLE="page-break-after: always;"></div>
## Index
- [MaxHeap](#MaxHeap)
- [Push](#Push)
- [Pop](#Pop)
- [Peek](#Peek)
- [Data](#Data)
- [Size](#Size)
<div STYLE="page-break-after: always;"></div>
## Documentation
### 1. MaxHeap
MaxHeap is a binary heap tree implemented by slice, The key of the root node is both greater than or equal to the key value of the left subtree and greater than or equal to the key value of the right subtree.
### <span id="NewMaxHeap">NewMaxHeap</span>
<p>Return a NewMaxHeap pointer instance.</p>
<b>Signature:</b>
```go
type MaxHeap[T any] struct {
data []T
comparator lancetconstraints.Comparator
}
func NewMaxHeap[T any](comparator lancetconstraints.Comparator) *MaxHeap[T]
```
<b>Example:</b>
```go
package main
import (
"fmt"
heap "github.com/duke-git/lancet/v2/datastructure/heap"
)
type intComparator struct{}
func (c *intComparator) Compare(v1, v2 any) int {
val1, _ := v1.(int)
val2, _ := v2.(int)
if val1 < val2 {
return -1
} else if val1 > val2 {
return 1
}
return 0
}
func main() {
maxHeap := heap.NewMaxHeap[int](&intComparator{})
fmt.Println(maxHeap)
}
```
### <span id="Push">Push</span>
<p>Push value into the heap</p>
<b>Signature:</b>
```go
func (h *MaxHeap[T]) Push(value T)
```
<b>Example:</b>
```go
package main
import (
"fmt"
heap "github.com/duke-git/lancet/v2/datastructure/heap"
)
type intComparator struct{}
func (c *intComparator) Compare(v1, v2 any) int {
val1, _ := v1.(int)
val2, _ := v2.(int)
if val1 < val2 {
return -1
} else if val1 > val2 {
return 1
}
return 0
}
func main() {
maxHeap := heap.NewMaxHeap[int](&intComparator{})
values := []int{6, 5, 2, 4, 7, 10, 12, 1, 3, 8, 9, 11}
for _, v := range values {
maxHeap.Push(v)
}
fmt.Println(maxHeap.Data()) //[]int{12, 9, 11, 4, 8, 10, 7, 1, 3, 5, 6, 2}
}
```
### <span id="Pop">Pop</span>
<p>Pop return the largest value, and remove it from the heap if heap is empty, return zero value and fasle</p>
<b>Signature:</b>
```go
func (h *MaxHeap[T]) Pop() (T, bool)
```
<b>Example:</b>
```go
package main
import (
"fmt"
heap "github.com/duke-git/lancet/v2/datastructure/heap"
)
type intComparator struct{}
func (c *intComparator) Compare(v1, v2 any) int {
val1, _ := v1.(int)
val2, _ := v2.(int)
if val1 < val2 {
return -1
} else if val1 > val2 {
return 1
}
return 0
}
func main() {
maxHeap := heap.NewMaxHeap[int](&intComparator{})
values := []int{6, 5, 2, 4, 7, 10, 12, 1, 3, 8, 9, 11}
for _, v := range values {
maxHeap.Push(v)
}
val, ok := maxHeap.Pop()
fmt.Println(val) //12
fmt.Println(ok) //true
}
```
### <span id="Peek">Peek</span>
<p>Return the largest element from the heap without removing it, if heap is empty, it returns zero value and false.</p>
<b>Signature:</b>
```go
func (h *MaxHeap[T]) Peek() (T, bool)
```
<b>Example:</b>
```go
package main
import (
"fmt"
heap "github.com/duke-git/lancet/v2/datastructure/heap"
)
type intComparator struct{}
func (c *intComparator) Compare(v1, v2 any) int {
val1, _ := v1.(int)
val2, _ := v2.(int)
if val1 < val2 {
return -1
} else if val1 > val2 {
return 1
}
return 0
}
func main() {
maxHeap := heap.NewMaxHeap[int](&intComparator{})
values := []int{6, 5, 2, 4, 7, 10, 12, 1, 3, 8, 9, 11}
for _, v := range values {
maxHeap.Push(v)
}
val, ok := maxHeap.Peek()
fmt.Println(val) //12
fmt.Println(maxHeap.Size()) //12
}
```
### <span id="Data">Data</span>
<p>Return all element of the heap</p>
<b>Signature:</b>
```go
func (h *MaxHeap[T]) Data() []T
```
<b>Example:</b>
```go
package main
import (
"fmt"
heap "github.com/duke-git/lancet/v2/datastructure/heap"
)
type intComparator struct{}
func (c *intComparator) Compare(v1, v2 any) int {
val1, _ := v1.(int)
val2, _ := v2.(int)
if val1 < val2 {
return -1
} else if val1 > val2 {
return 1
}
return 0
}
func main() {
maxHeap := heap.NewMaxHeap[int](&intComparator{})
values := []int{6, 5, 2, 4, 7, 10, 12, 1, 3, 8, 9, 11}
for _, v := range values {
maxHeap.Push(v)
}
fmt.Println(maxHeap.Data()) //[]int{12, 9, 11, 4, 8, 10, 7, 1, 3, 5, 6, 2}
}
```
### <span id="Size">Size</span>
<p>Return the number of elements in the heap</p>
<b>Signature:</b>
```go
func (h *MaxHeap[T]) Size() int
```
<b>Example:</b>
```go
package main
import (
"fmt"
heap "github.com/duke-git/lancet/v2/datastructure/heap"
)
type intComparator struct{}
func (c *intComparator) Compare(v1, v2 any) int {
val1, _ := v1.(int)
val2, _ := v2.(int)
if val1 < val2 {
return -1
} else if val1 > val2 {
return 1
}
return 0
}
func main() {
maxHeap := heap.NewMaxHeap[int](&intComparator{})
values := []int{6, 5, 2}
for _, v := range values {
maxHeap.Push(v)
}
fmt.Println(maxHeap.Size()) //3
}
```
### <span id="PrintStructure">PrintStructure</span>
<p>Print the tree structure of the heap</p>
<b>Signature:</b>
```go
func (h *MaxHeap[T]) PrintStructure()
```
<b>Example:</b>
```go
package main
import (
"fmt"
heap "github.com/duke-git/lancet/v2/datastructure/heap"
)
type intComparator struct{}
func (c *intComparator) Compare(v1, v2 any) int {
val1, _ := v1.(int)
val2, _ := v2.(int)
if val1 < val2 {
return -1
} else if val1 > val2 {
return 1
}
return 0
}
func main() {
maxHeap := heap.NewMaxHeap[int](&intComparator{})
values := []int{6, 5, 2, 4, 7, 10, 12, 1, 3, 8, 9, 11}
for _, v := range values {
maxHeap.Push(v)
}
fmt.Println(maxHeap.PrintStructure())
// 12
// 9 11
// 4 8 10 7
// 1 3 5 6 2
}
```

View File

@@ -0,0 +1,364 @@
# Heap
堆,切片实现的二叉堆数据结构。
<div STYLE="page-break-after: always;"></div>
## 源码
- [https://github.com/duke-git/lancet/blob/main/datastructure/heap/maxheap.go](https://github.com/duke-git/lancet/blob/main/datastructure/heap/maxheap.go)
<div STYLE="page-break-after: always;"></div>
## 用法
```go
import (
heap "github.com/duke-git/lancet/v2/datastructure/heap"
)
```
<div STYLE="page-break-after: always;"></div>
## 目录
- [MaxHeap](#MaxHeap)
- [Push](#Push)
- [Pop](#Pop)
- [Peek](#Peek)
- [Data](#Data)
- [Size](#Size)
<div STYLE="page-break-after: always;"></div>
## API文档
### 1. MaxHeap
MaxHeap是通过slice实现的二叉堆树根节点的key既大于等于左子树的key值且大于等于右子树的key值。
### <span id="NewMaxHeap">NewMaxHeap</span>
<p>返回NewMaxHeap指针实例</p>
<b>函数签名:</b>
```go
type MaxHeap[T any] struct {
data []T
comparator lancetconstraints.Comparator
}
func NewMaxHeap[T any](comparator lancetconstraints.Comparator) *MaxHeap[T]
```
<b>例子:</b>
```go
package main
import (
"fmt"
heap "github.com/duke-git/lancet/v2/datastructure/heap"
)
type intComparator struct{}
func (c *intComparator) Compare(v1, v2 any) int {
val1, _ := v1.(int)
val2, _ := v2.(int)
if val1 < val2 {
return -1
} else if val1 > val2 {
return 1
}
return 0
}
func main() {
maxHeap := heap.NewMaxHeap[int](&intComparator{})
fmt.Println(maxHeap)
}
```
### <span id="Push">Push</span>
<p>向堆中插入数据</p>
<b>函数签名:</b>
```go
func (h *MaxHeap[T]) Push(value T)
```
<b>例子:</b>
```go
package main
import (
"fmt"
heap "github.com/duke-git/lancet/v2/datastructure/heap"
)
type intComparator struct{}
func (c *intComparator) Compare(v1, v2 any) int {
val1, _ := v1.(int)
val2, _ := v2.(int)
if val1 < val2 {
return -1
} else if val1 > val2 {
return 1
}
return 0
}
func main() {
maxHeap := heap.NewMaxHeap[int](&intComparator{})
values := []int{6, 5, 2, 4, 7, 10, 12, 1, 3, 8, 9, 11}
for _, v := range values {
maxHeap.Push(v)
}
fmt.Println(maxHeap.Data()) //[]int{12, 9, 11, 4, 8, 10, 7, 1, 3, 5, 6, 2}
}
```
### <span id="Pop">Pop</span>
<p>返回堆中最大值并将其从堆中删除如果堆为空返回零值并返回false</p>
<b>函数签名:</b>
```go
func (h *MaxHeap[T]) Pop() (T, bool)
```
<b>例子:</b>
```go
package main
import (
"fmt"
heap "github.com/duke-git/lancet/v2/datastructure/heap"
)
type intComparator struct{}
func (c *intComparator) Compare(v1, v2 any) int {
val1, _ := v1.(int)
val2, _ := v2.(int)
if val1 < val2 {
return -1
} else if val1 > val2 {
return 1
}
return 0
}
func main() {
maxHeap := heap.NewMaxHeap[int](&intComparator{})
values := []int{6, 5, 2, 4, 7, 10, 12, 1, 3, 8, 9, 11}
for _, v := range values {
maxHeap.Push(v)
}
val, ok := maxHeap.Pop()
fmt.Println(val) //12
fmt.Println(ok) //true
}
```
### <span id="Peek">Peek</span>
<p>返回堆中最大值如果堆为空返回零值并返回false</p>
<b>函数签名:</b>
```go
func (h *MaxHeap[T]) Peek() (T, bool)
```
<b>例子:</b>
```go
package main
import (
"fmt"
heap "github.com/duke-git/lancet/v2/datastructure/heap"
)
type intComparator struct{}
func (c *intComparator) Compare(v1, v2 any) int {
val1, _ := v1.(int)
val2, _ := v2.(int)
if val1 < val2 {
return -1
} else if val1 > val2 {
return 1
}
return 0
}
func main() {
maxHeap := heap.NewMaxHeap[int](&intComparator{})
values := []int{6, 5, 2, 4, 7, 10, 12, 1, 3, 8, 9, 11}
for _, v := range values {
maxHeap.Push(v)
}
val, ok := maxHeap.Peek()
fmt.Println(val) //12
fmt.Println(maxHeap.Size()) //12
}
```
### <span id="Data">Data</span>
<p>返回堆中全部元素的切片</p>
<b>函数签名:</b>
```go
func (h *MaxHeap[T]) Data() []T
```
<b>例子:</b>
```go
package main
import (
"fmt"
heap "github.com/duke-git/lancet/v2/datastructure/heap"
)
type intComparator struct{}
func (c *intComparator) Compare(v1, v2 any) int {
val1, _ := v1.(int)
val2, _ := v2.(int)
if val1 < val2 {
return -1
} else if val1 > val2 {
return 1
}
return 0
}
func main() {
maxHeap := heap.NewMaxHeap[int](&intComparator{})
values := []int{6, 5, 2, 4, 7, 10, 12, 1, 3, 8, 9, 11}
for _, v := range values {
maxHeap.Push(v)
}
fmt.Println(maxHeap.Data()) //[]int{12, 9, 11, 4, 8, 10, 7, 1, 3, 5, 6, 2}
}
```
### <span id="Size">Size</span>
<p>返回堆中元素的数量</p>
<b>函数签名:</b>
```go
func (h *MaxHeap[T]) Size() int
```
<b>例子:</b>
```go
package main
import (
"fmt"
heap "github.com/duke-git/lancet/v2/datastructure/heap"
)
type intComparator struct{}
func (c *intComparator) Compare(v1, v2 any) int {
val1, _ := v1.(int)
val2, _ := v2.(int)
if val1 < val2 {
return -1
} else if val1 > val2 {
return 1
}
return 0
}
func main() {
maxHeap := heap.NewMaxHeap[int](&intComparator{})
values := []int{6, 5, 2}
for _, v := range values {
maxHeap.Push(v)
}
fmt.Println(maxHeap.Size()) //3
}
```
### <span id="PrintStructure">PrintStructure</span>
<p>打印堆的树形结构</p>
<b>函数签名:</b>
```go
func (h *MaxHeap[T]) PrintStructure()
```
<b>例子:</b>
```go
package main
import (
"fmt"
heap "github.com/duke-git/lancet/v2/datastructure/heap"
)
type intComparator struct{}
func (c *intComparator) Compare(v1, v2 any) int {
val1, _ := v1.(int)
val2, _ := v2.(int)
if val1 < val2 {
return -1
} else if val1 > val2 {
return 1
}
return 0
}
func main() {
maxHeap := heap.NewMaxHeap[int](&intComparator{})
values := []int{6, 5, 2, 4, 7, 10, 12, 1, 3, 8, 9, 11}
for _, v := range values {
maxHeap.Push(v)
}
fmt.Println(maxHeap.PrintStructure())
// 12
// 9 11
// 4 8 10 7
// 1 3 5 6 2
}
```

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

884
docs/datastructure/list.md Normal file
View File

@@ -0,0 +1,884 @@
# List
List is a linear table, implemented with slice.
<div STYLE="page-break-after: always;"></div>
## Source
- [https://github.com/duke-git/lancet/blob/main/datastructure/list/list.go](https://github.com/duke-git/lancet/blob/main/datastructure/list/list.go)
<div STYLE="page-break-after: always;"></div>
## Usage
```go
import (
list "github.com/duke-git/lancet/v2/datastructure/list"
)
```
<div STYLE="page-break-after: always;"></div>
## Index
- [NewList](#NewList)
- [Contain](#Contain)
- [Data](#Data)
- [ValueOf](#ValueOf)
- [IndexOf](#IndexOf)
- [LastIndexOf](#LastIndexOf)
- [IndexOfFunc](#IndexOfFunc)
- [LastIndexOfFunc](#LastIndexOfFunc)
- [Push](#Push)
- [PopFirst](#PopFirst)
- [PopLast](#PopLast)
- [DeleteAt](#DeleteAt)
- [InsertAt](#InsertAt)
- [UpdateAt](#UpdateAt)
- [Equal](#Equal)
- [IsEmpty](#IsEmpty)
- [Clear](#Clear)
- [Clone](#Clone)
- [Merge](#Merge)
- [Size](#Size)
- [Cap](#Cap)
- [Swap](#Swap)
- [Reverse](#Reverse)
- [Unique](#Unique)
- [Union](#Union)
- [Intersection](#Intersection)
- [SubList](#SubList)
- [DeleteIf](#DeleteIf)
<div STYLE="page-break-after: always;"></div>
## Documentation
### <span id="NewList">NewList</span>
<p>List is a linear table, implemented with slice.
NewList function return a list pointer</p>
<b>Signature:</b>
```go
type List[T any] struct {
data []T
}
func NewList[T any](data []T) *List[T]
```
<b>Example:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
li := list.NewList([]int{1, 2, 3})
fmt.Println(li)
}
```
### <span id="Contain">Contain</span>
<p>Check if the value in the list or not</p>
<b>Signature:</b>
```go
func (l *List[T]) Contain(value T) bool
```
<b>Example:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
li := list.NewList([]int{1, 2, 3})
fmt.Println(li.Contain(1)) //true
fmt.Println(li.Contain(0)) //false
}
```
### <span id="Data">Data</span>
<p>Return slice of list data</p>
<b>Signature:</b>
```go
func (l *List[T]) Data() []T
```
<b>Example:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
li := list.NewList([]int{1, 2, 3})
data := li.Data()
fmt.Println(data) //[]int{1, 2, 3}
}
```
### <span id="ValueOf">ValueOf</span>
<p>Return the value pointer at index in list</p>
<b>Signature:</b>
```go
func (l *List[T]) ValueOf(index int) (*T, bool)
```
<b>Example:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
li := list.NewList([]int{1, 2, 3})
v, ok := li.ValueOf(0)
fmt.Println(*v) //1
fmt.Println(ok) //true
}
```
### <span id="IndexOf">IndexOf</span>
<p>Returns the index of value in the list. if not found return -1</p>
<b>Signature:</b>
```go
func (l *List[T]) IndexOf(value T) int
```
<b>Example:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
li := list.NewList([]int{1, 2, 3})
fmt.Println(li.IndexOf(1)) //0
fmt.Println(li.IndexOf(0)) //-1
}
```
### <span id="LastIndexOf">LastIndexOf</span>
<p> Returns the index of the last occurrence of the value in this list if not found return -1</p>
<b>Signature:</b>
```go
func (l *List[T]) LastIndexOf(value T) int
```
<b>Example:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
li := list.NewList([]int{1, 2, 3, 1})
fmt.Println(li.LastIndexOf(1)) // 3
fmt.Println(li.LastIndexOf(0)) //-1
}
```
### <span id="IndexOfFunc">IndexOfFunc</span>
<p> IndexOfFunc returns the first index satisfying f(v). if not found return -1</p>
<b>Signature:</b>
```go
func (l *List[T]) IndexOfFunc(f func(T) bool) int
```
<b>Example:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
li := list.NewList([]int{1, 2, 3})
fmt.Println(li.IndexOfFunc(func(a int) bool { return a == 1 })) //0
fmt.Println(li.IndexOfFunc(func(a int) bool { return a == 0 })) //-1
}
```
### <span id="LastIndexOfFunc">LastIndexOfFunc</span>
<p>LastIndexOfFunc returns the index of the last occurrence of the value in this list satisfying f(data[i]). if not found return -1</p>
<b>Signature:</b>
```go
func (l *List[T]) LastIndexOfFunc(f func(T) bool) int
```
<b>Example:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
li := list.NewList([]int{1, 2, 3, 1})
fmt.Println(li.LastIndexOfFunc(func(a int) bool { return a == 1 })) // 3
fmt.Println(li.LastIndexOfFunc(func(a int) bool { return a == 0 })) //-1
}
```
### <span id="Push">Push</span>
<p>Append value to the list</p>
<b>Signature:</b>
```go
func (l *List[T]) Push(value T)
```
<b>Example:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
li := list.NewList([]int{1, 2, 3})
li.Push(4)
fmt.Println(li.Data()) //[]int{1, 2, 3, 4}
}
```
### <span id="PopFirst">PopFirst</span>
<p>Delete the first value of list and return it</p>
<b>Signature:</b>
```go
func (l *List[T]) PopFirst() (*T, bool)
```
<b>Example:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
li := list.NewList([]int{1, 2, 3})
v, ok := li.PopFirst()
fmt.Println(*v) //1
fmt.Println(ok) //true
fmt.Println(li.Data()) //2, 3
}
```
### <span id="PopLast">PopFirst</span>
<p>Delete the last value of list and return it</p>
<b>Signature:</b>
```go
func (l *List[T]) PopLast() (*T, bool)
```
<b>Example:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
li := list.NewList([]int{1, 2, 3})
v, ok := li.PopLast()
fmt.Println(*v) //3
fmt.Println(ok) //true
fmt.Println(li.Data()) //1, 2
}
```
### <span id="DeleteAt">DeleteAt</span>
<p>Delete the value of list at index, if index is not between 0 and length of list data, do nothing</p>
<b>Signature:</b>
```go
func (l *List[T]) DeleteAt(index int)
```
<b>Example:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
li := list.NewList([]int{1, 2, 3, 4})
li.DeleteAt(-1)
fmt.Println(li.Data()) //1,2,3,4
li.DeleteAt(4)
fmt.Println(li.Data()) //1,2,3,4
li.DeleteAt(0)
fmt.Println(li.Data()) //2,3,4
li.DeleteAt(2)
fmt.Println(li.Data()) //2,3
}
```
### <span id="InsertAt">InsertAt</span>
<p>Insert value into list at index, if index is not between 0 and length of list data, do nothing</p>
<b>Signature:</b>
```go
func (l *List[T]) InsertAt(index int, value T)
```
<b>Example:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
li := list.NewList([]int{1, 2, 3})
li.InsertAt(-1, 0)
fmt.Println(li.Data()) //1,2,3
li.InsertAt(4, 0)
fmt.Println(li.Data()) //1,2,3
li.InsertAt(3, 4)
fmt.Println(li.Data()) //1,2,3,4
// li.InsertAt(2, 4)
// fmt.Println(li.Data()) //1,2,4,3
}
```
### <span id="UpdateAt">UpdateAt</span>
<p>Update value of list at index, index shoud between 0 and list size - 1</p>
<b>Signature:</b>
```go
func (l *List[T]) UpdateAt(index int, value T)
```
<b>Example:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
li := list.NewList([]int{1, 2, 3})
li.UpdateAt(-1, 0)
fmt.Println(li.Data()) //1,2,3
li.UpdateAt(2, 4)
fmt.Println(li.Data()) //1,2,4
li.UpdateAt(3, 5)
fmt.Println(li.Data()) //1,2,4
}
```
### <span id="Equal">Equal</span>
<p>Compare a list to another list, use reflect.DeepEqual on every element</p>
<b>Signature:</b>
```go
func (l *List[T]) Equal(other *List[T]) bool
```
<b>Example:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
li1 := list.NewList([]int{1, 2, 3, 4})
li2 := list.NewList([]int{1, 2, 3, 4})
li3 := list.NewList([]int{1, 2, 3})
fmt.Println(li1.Equal(li2)) //true
fmt.Println(li1.Equal(li3)) //false
}
```
### <span id="IsEmpty">IsEmpty</span>
<p>Check if a list is empty or not</p>
<b>Signature:</b>
```go
func (l *List[T]) IsEmpty() bool
```
<b>Example:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
li1 := list.NewList([]int{1, 2, 3})
li2 := list.NewList([]int{})
fmt.Println(li1.IsEmpty()) //false
fmt.Println(li2.IsEmpty()) //true
}
```
### <span id="Clear">Clear</span>
<p>Clear the data of list</p>
<b>Signature:</b>
```go
func (l *List[T]) Clear()
```
<b>Example:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
li := list.NewList([]int{1, 2, 3})
li.Clear()
fmt.Println(li.Data()) // empty
}
```
### <span id="Clone">Clone</span>
<p>Return a copy of list</p>
<b>Signature:</b>
```go
func (l *List[T]) Clone() *List[T]
```
<b>Example:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
li := list.NewList([]int{1, 2, 3})
cloneList := li.Clone()
fmt.Println(cloneList.Data()) // 1,2,3
}
```
### <span id="Merge">Merge</span>
<p>Merge two list, return new list, don't change original list</p>
<b>Signature:</b>
```go
func (l *List[T]) Merge(other *List[T]) *List[T]
```
<b>Example:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
li1 := list.NewList([]int{1, 2, 3, 4})
li2 := list.NewList([]int{4, 5, 6})
li3 := li1.Merge(li2)
fmt.Println(li3.Data()) //1, 2, 3, 4, 4, 5, 6
}
```
### <span id="Size">Size</span>
<p>Return number of list data items</p>
<b>Signature:</b>
```go
func (l *List[T]) Size() int
```
<b>Example:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
li := list.NewList([]int{1, 2, 3, 4})
fmt.Println(li.Size()) //4
}
```
### <span id="Cap">Cap</span>
<p>Cap return cap of the inner data</p>
<b>Signature:</b>
```go
func (l *List[T]) Cap() int
```
<b>Example:</b>
```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
}
```
### <span id="Swap">Swap</span>
<p>Swap the value at two index in list</p>
<b>Signature:</b>
```go
func (l *List[T]) Swap(i, j int)
```
<b>Example:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
li := list.NewList([]int{1, 2, 3, 4})
li.Swap(0, 3)
fmt.Println(li.Data()) //4, 2, 3, 1
}
```
### <span id="Reverse">Reverse</span>
<p>Reverse the data item order of list</p>
<b>Signature:</b>
```go
func (l *List[T]) Reverse()
```
<b>Example:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
li := list.NewList([]int{1, 2, 3, 4})
li.Reverse()
fmt.Println(li.Data()) //4, 3, 2, 1
}
```
### <span id="Unique">Unique</span>
<p>Remove duplicate items in list</p>
<b>Signature:</b>
```go
func (l *List[T]) Unique()
```
<b>Example:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
li := list.NewList([]int{1, 2, 2, 3, 4})
li.Unique()
fmt.Println(li.Data()) //1,2,3,4
}
```
### <span id="Union">Union</span>
<p>Creates a new list contain all elements in list l and other, remove duplicate element</p>
<b>Signature:</b>
```go
func (l *List[T]) Union(other *List[T]) *List[T]
```
<b>Example:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
li1 := list.NewList([]int{1, 2, 3, 4})
li2 := list.NewList([]int{4, 5, 6})
li3 := li1.Union(li2)
fmt.Println(li3.Data()) //1,2,3,4,5,6
}
```
### <span id="Intersection">Intersection</span>
<p>Creates a new list whose element both be contained in list l and other</p>
<b>Signature:</b>
```go
func (l *List[T]) Intersection(other *List[T]) *List[T]
```
<b>Example:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
li1 := list.NewList([]int{1, 2, 3, 4})
li2 := list.NewList([]int{4, 5, 6})
li3 := li1.Intersection(li2)
fmt.Println(li3.Data()) //4
}
```
### <span id="SubList">SubList</span>
<p>SubList returns a sub list of the original list between the specified fromIndex, inclusive, and toIndex, exclusive.</p>
<b>Signature:</b>
```go
func (l *List[T]) SubList(fromIndex, toIndex int) *List[T]
```
<b>Example:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
l := list.NewList([]int{1, 2, 3, 4, 5, 6})
fmt.Println(l.SubList(2, 5)) // []int{3, 4, 5}
}
```
### <span id="DeleteIf">DeleteIf</span>
<p>DeleteIf delete all satisfying f(data[i]), returns count of removed elements</p>
<b>Signature:</b>
```go
func (l *List[T]) DeleteIf(f func(T) bool) int
```
<b>Example:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
l := list.NewList([]int{1, 1, 1, 1, 2, 3, 1, 1, 4, 1, 1, 1, 1, 1, 1})
fmt.Println(l.DeleteIf(func(a int) bool { return a == 1 })) // 12
fmt.Println(l.Data()) // []int{2, 3, 4}
}
```

View File

@@ -0,0 +1,882 @@
# List
List是线性表数据结构, 用go切片实现。
<div STYLE="page-break-after: always;"></div>
## 源码
- [https://github.com/duke-git/lancet/blob/main/datastructure/list/list.go](https://github.com/duke-git/lancet/blob/main/datastructure/list/list.go)
<div STYLE="page-break-after: always;"></div>
## 用法
```go
import (
"github.com/duke-git/lancet/v2/datastructure"
)
```
<div STYLE="page-break-after: always;"></div>
## 目录
- [NewList](#NewList)
- [Contain](#Contain)
- [Data](#Data)
- [ValueOf](#ValueOf)
- [IndexOf](#IndexOf)
- [LastIndexOf](#LastIndexOf)
- [IndexOfFunc](#IndexOfFunc)
- [LastIndexOfFunc](#LastIndexOfFunc)
- [Push](#Push)
- [PopFirst](#PopFirst)
- [PopLast](#PopLast)
- [DeleteAt](#DeleteAt)
- [InsertAt](#InsertAt)
- [UpdateAt](#UpdateAt)
- [Equal](#Equal)
- [IsEmpty](#IsEmpty)
- [Clear](#Clear)
- [Clone](#Clone)
- [Merge](#Merge)
- [Size](#Size)
- [Cap](#Cap)
- [Swap](#Swap)
- [Reverse](#Reverse)
- [Unique](#Unique)
- [Union](#Union)
- [Intersection](#Intersection)
- [SubList](#SubList)
- [DeleteIf](#DeleteIf)
<div STYLE="page-break-after: always;"></div>
## 文档
### <span id="NewList">NewList</span>
<p>返回List指针实例</p>
<b>函数签名:</b>
```go
type List[T any] struct {
data []T
}
func NewList[T any](data []T) *List[T]
```
<b>例子:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
li := list.NewList([]int{1, 2, 3})
fmt.Println(li)
}
```
### <span id="Contain">Contain</span>
<p>判断列表中是否包含特定值</p>
<b>函数签名:</b>
```go
func (l *List[T]) Contain(value T) bool
```
<b>例子:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
li := list.NewList([]int{1, 2, 3})
fmt.Println(li.Contain(1)) //true
fmt.Println(li.Contain(0)) //false
}
```
### <span id="Data">Data</span>
<p>返回List中所有数据切片</p>
<b>函数签名:</b>
```go
func (l *List[T]) Data() []T
```
<b>例子:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
li := list.NewList([]int{1, 2, 3})
data := li.Data()
fmt.Println(data) //[]int{1, 2, 3}
}
```
### <span id="ValueOf">ValueOf</span>
<p>返回列表中索引处的值指针</p>
<b>函数签名:</b>
```go
func (l *List[T]) ValueOf(index int) (*T, bool)
```
<b>例子:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
li := list.NewList([]int{1, 2, 3})
v, ok := li.ValueOf(0)
fmt.Println(*v) //1
fmt.Println(ok) //true
}
```
### <span id="IndexOf">IndexOf</span>
<p>返回列表中值的索引,如果没有找到返回-1</p>
<b>函数签名:</b>
```go
func (l *List[T]) IndexOf(value T) int
```
<b>例子:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
li := list.NewList([]int{1, 2, 3})
fmt.Println(li.IndexOf(1)) //0
fmt.Println(li.IndexOf(0)) //-1
}
```
### <span id="LastIndexOf">LastIndexOf</span>
<p>返回列表中最后一次出现的值的索引。如果未找到,则返回-1</p>
<b>函数签名:</b>
```go
func (l *List[T]) LastIndexOf(value T) int
```
<b>例子:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
li := list.NewList([]int{1, 2, 3, 1})
fmt.Println(li.LastIndexOf(1)) // 3
fmt.Println(li.LastIndexOf(0)) //-1
}
```
### <span id="IndexOfFunc">IndexOfFunc</span>
<p>返回第一个符合函数条件的元素的索引。如果未找到,则返回-1</p>
<b>函数签名:</b>
```go
func (l *List[T]) IndexOfFunc(f func(T) bool) int
```
<b>例子:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
li := list.NewList([]int{1, 2, 3})
fmt.Println(li.IndexOfFunc(func(a int) bool { return a == 1 })) //0
fmt.Println(li.IndexOfFunc(func(a int) bool { return a == 0 })) //-1
}
```
### <span id="LastIndexOfFunc">LastIndexOfFunc</span>
<p>返回最后一个符合函数条件的元素的索引。如果未找到,则返回-1</p>
<b>函数签名:</b>
```go
func (l *List[T]) LastIndexOfFunc(f func(T) bool) int
```
<b>例子:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
li := list.NewList([]int{1, 2, 3, 1})
fmt.Println(li.LastIndexOfFunc(func(a int) bool { return a == 1 })) // 3
fmt.Println(li.LastIndexOfFunc(func(a int) bool { return a == 0 })) //-1
}
```
### <span id="Push">Push</span>
<p>将值附加到列表末尾</p>
<b>函数签名:</b>
```go
func (l *List[T]) Push(value T)
```
<b>例子:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
li := list.NewList([]int{1, 2, 3})
li.Push(4)
fmt.Println(li.Data()) //[]int{1, 2, 3, 4}
}
```
### <span id="PopFirst">PopFirst</span>
<p>删除列表的第一个值并返回该值</p>
<b>函数签名:</b>
```go
func (l *List[T]) PopFirst() (*T, bool)
```
<b>例子:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
li := list.NewList([]int{1, 2, 3})
v, ok := li.PopFirst()
fmt.Println(*v) //1
fmt.Println(ok) //true
fmt.Println(li.Data()) //2, 3
}
```
### <span id="PopLast">PopFirst</span>
<p>删除列表的最后一个值并返回该值</p>
<b>函数签名:</b>
```go
func (l *List[T]) PopLast() (*T, bool)
```
<b>例子:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
li := list.NewList([]int{1, 2, 3})
v, ok := li.PopLast()
fmt.Println(*v) //3
fmt.Println(ok) //true
fmt.Println(li.Data()) //1, 2
}
```
### <span id="DeleteAt">DeleteAt</span>
<p>删除索引处列表的值如果索引不在0和列表数据长度之间则不执行任何操作</p>
<b>函数签名:</b>
```go
func (l *List[T]) DeleteAt(index int)
```
<b>例子:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
li := list.NewList([]int{1, 2, 3, 4})
li.DeleteAt(-1)
fmt.Println(li.Data()) //1,2,3,4
li.DeleteAt(4)
fmt.Println(li.Data()) //1,2,3,4
li.DeleteAt(0)
fmt.Println(li.Data()) //2,3,4
li.DeleteAt(2)
fmt.Println(li.Data()) //2,3
}
```
### <span id="InsertAt">InsertAt</span>
<p>在索引处插入值到列表中,如果索引不在 0 和列表数据长度之间,则不执行任何操作</p>
<b>函数签名:</b>
```go
func (l *List[T]) InsertAt(index int, value T)
```
<b>例子:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
li := list.NewList([]int{1, 2, 3})
li.InsertAt(-1, 0)
fmt.Println(li.Data()) //1,2,3
li.InsertAt(4, 0)
fmt.Println(li.Data()) //1,2,3
li.InsertAt(3, 4)
fmt.Println(li.Data()) //1,2,3,4
// li.InsertAt(2, 4)
// fmt.Println(li.Data()) //1,2,4,3
}
```
### <span id="UpdateAt">UpdateAt</span>
<p>更新索引处列表的值索引应该在0和列表数据长度-1之间</p>
<b>函数签名:</b>
```go
func (l *List[T]) UpdateAt(index int, value T)
```
<b>例子:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
li := list.NewList([]int{1, 2, 3})
li.UpdateAt(-1, 0)
fmt.Println(li.Data()) //1,2,3
li.UpdateAt(2, 4)
fmt.Println(li.Data()) //1,2,4
li.UpdateAt(3, 5)
fmt.Println(li.Data()) //1,2,4
}
```
### <span id="Equal">Equal</span>
<p>比较一个列表和另一个列表,在每个元素上使用 reflect.DeepEqual</p>
<b>函数签名:</b>
```go
func (l *List[T]) Equal(other *List[T]) bool
```
<b>例子:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
li1 := list.NewList([]int{1, 2, 3, 4})
li2 := list.NewList([]int{1, 2, 3, 4})
li3 := list.NewList([]int{1, 2, 3})
fmt.Println(li1.Equal(li2)) //true
fmt.Println(li1.Equal(li3)) //false
}
```
### <span id="IsEmpty">IsEmpty</span>
<p>判断列表是否为空</p>
<b>函数签名:</b>
```go
func (l *List[T]) IsEmpty() bool
```
<b>例子:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
li1 := list.NewList([]int{1, 2, 3})
li2 := list.NewList([]int{})
fmt.Println(li1.IsEmpty()) //false
fmt.Println(li2.IsEmpty()) //true
}
```
### <span id="Clear">Clear</span>
<p>清空列表数据</p>
<b>函数签名:</b>
```go
func (l *List[T]) Clear()
```
<b>例子:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
li := list.NewList([]int{1, 2, 3})
li.Clear()
fmt.Println(li.Data()) // empty
}
```
### <span id="Clone">Clone</span>
<p>返回列表的一个拷贝</p>
<b>函数签名:</b>
```go
func (l *List[T]) Clone() *List[T]
```
<b>例子:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
li := list.NewList([]int{1, 2, 3})
cloneList := li.Clone()
fmt.Println(cloneList.Data()) // 1,2,3
}
```
### <span id="Merge">Merge</span>
<p>合并两个列表,返回新的列表</p>
<b>函数签名:</b>
```go
func (l *List[T]) Merge(other *List[T]) *List[T]
```
<b>例子:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
li1 := list.NewList([]int{1, 2, 3, 4})
li2 := list.NewList([]int{4, 5, 6})
li3 := li1.Merge(li2)
fmt.Println(li3.Data()) //1, 2, 3, 4, 4, 5, 6
}
```
### <span id="Size">Size</span>
<p>返回列表数据项的数量</p>
<b>函数签名:</b>
```go
func (l *List[T]) Size() int
```
<b>例子:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
li := list.NewList([]int{1, 2, 3, 4})
fmt.Println(li.Size()) //4
}
```
### <span id="Cap">Cap</span>
<p>返回列表数据容量</p>
<b>函数签名:</b>
```go
func (l *List[T]) Cap() int
```
<b>例子:</b>
```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
}
```
### <span id="Swap">Swap</span>
<p>交换列表中两个索引位置的值</p>
<b>函数签名:</b>
```go
func (l *List[T]) Swap(i, j int)
```
<b>例子:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
li := list.NewList([]int{1, 2, 3, 4})
li.Swap(0, 3)
fmt.Println(li.Data()) //4, 2, 3, 1
}
```
### <span id="Reverse">Reverse</span>
<p>反转列表的数据项顺序</p>
<b>函数签名:</b>
```go
func (l *List[T]) Reverse()
```
<b>例子:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
li := list.NewList([]int{1, 2, 3, 4})
li.Reverse()
fmt.Println(li.Data()) //4, 3, 2, 1
}
```
### <span id="Unique">Unique</span>
<p>列表去除重复数据项</p>
<b>函数签名:</b>
```go
func (l *List[T]) Unique()
```
<b>例子:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
li := list.NewList([]int{1, 2, 2, 3, 4})
li.Unique()
fmt.Println(li.Data()) //1,2,3,4
}
```
### <span id="Union">Union</span>
<p>两个列表取并集,去除重复数据项</p>
<b>函数签名:</b>
```go
func (l *List[T]) Union(other *List[T]) *List[T]
```
<b>例子:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
li1 := list.NewList([]int{1, 2, 3, 4})
li2 := list.NewList([]int{4, 5, 6})
li3 := li1.Union(li2)
fmt.Println(li3.Data()) //1,2,3,4,5,6
}
```
### <span id="Intersection">Intersection</span>
<p>两个列表取交集</p>
<b>函数签名:</b>
```go
func (l *List[T]) Intersection(other *List[T]) *List[T]
```
<b>例子:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
li1 := list.NewList([]int{1, 2, 3, 4})
li2 := list.NewList([]int{4, 5, 6})
li3 := li1.Intersection(li2)
fmt.Println(li3.Data()) //4
}
```
### <span id="SubList">SubList</span>
<p>SubList returns a sub list of the original list between the specified fromIndex, inclusive, and toIndex, exclusive.</p>
<b>函数签名:</b>
```go
func (l *List[T]) SubList(fromIndex, toIndex int) *List[T]
```
<b>例子:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
l := list.NewList([]int{1, 2, 3, 4, 5, 6})
fmt.Println(l.SubList(2, 5)) // []int{3, 4, 5}
}
```
### <span id="DeleteIf">DeleteIf</span>
<p>删除列表中所有符合函数调用函数返回true)的元素,返回删除元素的数量</p>
<b>函数签名:</b>
```go
func (l *List[T]) DeleteIf(f func(T) bool) int
```
<b>例子:</b>
```go
package main
import (
"fmt"
list "github.com/duke-git/lancet/v2/datastructure/list"
)
func main() {
l := list.NewList([]int{1, 1, 1, 1, 2, 3, 1, 1, 4, 1, 1, 1, 1, 1, 1})
fmt.Println(l.DeleteIf(func(a int) bool { return a == 1 })) // 12
fmt.Println(l.Data()) // []int{2, 3, 4}
}
```

1387
docs/datastructure/queue.md Normal file

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

489
docs/datastructure/set.md Normal file
View File

@@ -0,0 +1,489 @@
# Set
Set is a data container, like list, but elements of set is not duplicate.
<div STYLE="page-break-after: always;"></div>
## Source
- [https://github.com/duke-git/lancet/blob/main/datastructure/set/set.go](https://github.com/duke-git/lancet/blob/main/datastructure/set/set.go)
<div STYLE="page-break-after: always;"></div>
## Usage
```go
import (
set "github.com/duke-git/lancet/v2/datastructure/set"
)
```
<div STYLE="page-break-after: always;"></div>
## Index
- [NewSet](#NewSet)
- [Values](#Values)
- [Add](#Add)
- [Delete](#Delete)
- [Contain](#Contain)
- [ContainAll](#ContainAll)
- [Clone](#Clone)
- [Size](#Size)
- [Equal](#Equal)
- [Iterate](#Iterate)
- [IsEmpty](#IsEmpty)
- [Union](#Union)
- [Intersection](#Intersection)
- [SymmetricDifference](#SymmetricDifference)
- [Minus](#Minus)
<div STYLE="page-break-after: always;"></div>
## Documentation
### <span id="NewSet">NewSet</span>
<p>Make a Set instance</p>
<b>Signature:</b>
```go
type Set[T comparable] map[T]bool
func NewSet[T comparable](values ...T) Set[T]
```
<b>Example:</b>
```go
package main
import (
"fmt"
set "github.com/duke-git/lancet/v2/datastructure/set"
)
func main() {
st := set.NewSet[int](1,2,2,3)
fmt.Println(st.Values()) //1,2,3
}
```
### <span id="Values">Values</span>
<p>Return slice of all set data</p>
<b>Signature:</b>
```go
func (s Set[T]) Values() []T
```
<b>Example:</b>
```go
package main
import (
"fmt"
set "github.com/duke-git/lancet/v2/datastructure/set"
)
func main() {
st := set.NewSet[int](1,2,2,3)
fmt.Println(st.Values()) //1,2,3
}
```
### <span id="Add">Add</span>
<p>Add value to set</p>
<b>Signature:</b>
```go
func (s Set[T]) Add(values ...T)
```
<b>Example:</b>
```go
package main
import (
"fmt"
set "github.com/duke-git/lancet/v2/datastructure/set"
)
func main() {
st := set.NewSet[int]()
st.Add(1, 2, 3)
fmt.Println(st.Values()) //1,2,3
}
```
### <span id="Delete">Delete</span>
<p>Delete value in set</p>
<b>Signature:</b>
```go
func (s Set[T]) Delete(values ...T)
```
<b>Example:</b>
```go
package main
import (
"fmt"
set "github.com/duke-git/lancet/v2/datastructure/set"
)
func main() {
st := set.NewSet[int]()
st.Add(1, 2, 3)
set.Delete(3)
fmt.Println(st.Values()) //1,2
}
```
### <span id="Contain">Contain</span>
<p>Check if value is in set or not</p>
<b>Signature:</b>
```go
func (s Set[T]) Contain(value T) bool
```
<b>Example:</b>
```go
package main
import (
"fmt"
set "github.com/duke-git/lancet/v2/datastructure/set"
)
func main() {
st := set.NewSet[int]()
st.Add(1, 2, 3)
fmt.Println(st.Contain(1)) //true
fmt.Println(st.Contain(4)) //false
}
```
### <span id="ContainAll">ContainAll</span>
<p>Checks if set contains another set</p>
<b>Signature:</b>
```go
func (s Set[T]) ContainAll(other Set[T]) bool
```
<b>Example:</b>
```go
package main
import (
"fmt"
set "github.com/duke-git/lancet/v2/datastructure/set"
)
func main() {
set1 := set.NewSet(1, 2, 3)
set2 := set.NewSet(1, 2)
set3 := set.NewSet(1, 2, 3, 4)
fmt.Println(set1.ContainAll(set2)) //true
fmt.Println(set1.ContainAll(set3)) //false
}
```
### <span id="Size">Size</span>
<p>Get the number of elements in set</p>
<b>Signature:</b>
```go
func (s Set[T]) Size() int
```
<b>Example:</b>
```go
package main
import (
"fmt"
set "github.com/duke-git/lancet/v2/datastructure/set"
)
func main() {
set1 := set.NewSet(1, 2, 3)
fmt.Println(set1.Size()) //3
}
```
### <span id="Clone">Clone</span>
<p>Make a copy of set</p>
<b>Signature:</b>
```go
func (s Set[T]) Clone() Set[T]
```
<b>Example:</b>
```go
package main
import (
"fmt"
set "github.com/duke-git/lancet/v2/datastructure/set"
)
func main() {
set1 := set.NewSet(1, 2, 3)
set2 := set1.Clone()
fmt.Println(set1.Size() == set2.Size()) //true
fmt.Println(set1.ContainAll(set2)) //true
}
```
### <span id="Equal">Equal</span>
<p>Check if two sets has same elements or not</p>
<b>Signature:</b>
```go
func (s Set[T]) Equal(other Set[T]) bool
```
<b>Example:</b>
```go
package main
import (
"fmt"
set "github.com/duke-git/lancet/v2/datastructure/set"
)
func main() {
set1 := set.NewSet(1, 2, 3)
set2 := set.NewSet(1, 2, 3)
set3 := set.NewSet(1, 2, 3, 4)
fmt.Println(set1.Equal(set2)) //true
fmt.Println(set1.Equal(set3)) //false
}
```
### <span id="Iterate">Iterate</span>
<p>Call function by every element of set</p>
<b>Signature:</b>
```go
func (s Set[T]) Iterate(fn func(value T))
```
<b>Example:</b>
```go
package main
import (
"fmt"
set "github.com/duke-git/lancet/v2/datastructure/set"
)
func main() {
set1 := set.NewSet(1, 2, 3)
arr := []int{}
set.Iterate(func(value int) {
arr = append(arr, value)
})
fmt.Println(arr) //1,2,3
}
```
### <span id="IsEmpty">IsEmpty</span>
<p>Check if the set is empty or not</p>
<b>Signature:</b>
```go
func (s Set[T]) IsEmpty() bool
```
<b>Example:</b>
```go
package main
import (
"fmt"
set "github.com/duke-git/lancet/v2/datastructure/set"
)
func main() {
set1 := set.NewSet(1, 2, 3)
set2 := set.NewSet()
fmt.Println(set1.IsEmpty()) //false
fmt.Println(set2.IsEmpty()) //true
}
```
### <span id="Union">Union</span>
<p>Create a new set contain all element of set s and other</p>
<b>Signature:</b>
```go
func (s Set[T]) Union(other Set[T]) Set[T]
```
<b>Example:</b>
```go
package main
import (
"fmt"
set "github.com/duke-git/lancet/v2/datastructure/set"
)
func main() {
set1 := set.NewSet(1, 2, 3)
set2 := set.NewSet(2, 3, 4, 5)
set3 := set1.Union(set2)
fmt.Println(set3.Values()) //1,2,3,4,5
}
```
### <span id="Intersection">Intersection</span>
<p>Create a new set whose element both be contained in set s and other</p>
<b>Signature:</b>
```go
func (s Set[T]) Intersection(other Set[T]) Set[T]
```
<b>Example:</b>
```go
package main
import (
"fmt"
set "github.com/duke-git/lancet/v2/datastructure/set"
)
func main() {
set1 := set.NewSet(1, 2, 3)
set2 := set.NewSet(2, 3, 4, 5)
set3 := set1.Intersection(set2)
fmt.Println(set3.Values()) //2,3
}
```
### <span id="SymmetricDifference">SymmetricDifference</span>
<p>Create a new set whose element is in set1 or set2, but not in both set1 and set2</p>
<b>Signature:</b>
```go
func (s Set[T]) SymmetricDifference(other Set[T]) Set[T]
```
<b>Example:</b>
```go
package main
import (
"fmt"
set "github.com/duke-git/lancet/v2/datastructure/set"
)
func main() {
set1 := set.NewSet(1, 2, 3)
set2 := set.NewSet(2, 3, 4, 5)
set3 := set1.SymmetricDifference(set2)
fmt.Println(set3.Values()) //1,4,5
}
```
### <span id="Minus">Minus</span>
<p>Create an set of whose element in origin set but not in compared set</p>
<b>Signature:</b>
```go
func (s Set[T]) Minus(comparedSet Set[T]) Set[T]
```
<b>Example:</b>
```go
package main
import (
"fmt"
set "github.com/duke-git/lancet/v2/datastructure/set"
)
func main() {
set1 := set.NewSet(1, 2, 3)
set2 := set.NewSet(2, 3, 4, 5)
set3 := set.NewSet(2, 3)
res1 := set1.Minus(set2)
fmt.Println(res1.Values()) //1
res2 := set2.Minus(set3)
fmt.Println(res2.Values()) //4,5
}
```

View File

@@ -0,0 +1,489 @@
# Set
Set集合数据结构类似列表。Set中元素不重复。
<div STYLE="page-break-after: always;"></div>
## 源码
- [https://github.com/duke-git/lancet/blob/main/datastructure/set/set.go](https://github.com/duke-git/lancet/blob/main/datastructure/set/set.go)
<div STYLE="page-break-after: always;"></div>
## 用法
```go
import (
set "github.com/duke-git/lancet/v2/datastructure/set"
)
```
<div STYLE="page-break-after: always;"></div>
## 目录
- [NewSet](#NewSet)
- [Values](#Values)
- [Add](#Add)
- [Delete](#Delete)
- [Contain](#Contain)
- [ContainAll](#ContainAll)
- [Clone](#Clone)
- [Size](#Size)
- [Equal](#Equal)
- [Iterate](#Iterate)
- [IsEmpty](#IsEmpty)
- [Union](#Union)
- [Intersection](#Intersection)
- [SymmetricDifference](#SymmetricDifference)
- [Minus](#Minus)
<div STYLE="page-break-after: always;"></div>
## 文档
### <span id="NewSet">NewSet</span>
<p>返回Set结构体对象</p>
<b>函数签名:</b>
```go
type Set[T comparable] map[T]bool
func NewSet[T comparable](values ...T) Set[T]
```
<b>例子:</b>
```go
package main
import (
"fmt"
set "github.com/duke-git/lancet/v2/datastructure/set"
)
func main() {
st := set.NewSet[int](1,2,2,3)
fmt.Println(st.Values()) //1,2,3
}
```
### <span id="Values">Values</span>
<p>获取集合中所有元素的切片</p>
<b>函数签名:</b>
```go
func (s Set[T]) Values() []T
```
<b>例子:</b>
```go
package main
import (
"fmt"
set "github.com/duke-git/lancet/v2/datastructure/set"
)
func main() {
st := set.NewSet[int](1,2,2,3)
fmt.Println(st.Values()) //1,2,3
}
```
### <span id="Add">Add</span>
<p>向集合中添加元素</p>
<b>函数签名:</b>
```go
func (s Set[T]) Add(values ...T)
```
<b>例子:</b>
```go
package main
import (
"fmt"
set "github.com/duke-git/lancet/v2/datastructure/set"
)
func main() {
st := set.NewSet[int]()
st.Add(1, 2, 3)
fmt.Println(st.Values()) //1,2,3
}
```
### <span id="Delete">Delete</span>
<p>删除集合中元素</p>
<b>函数签名:</b>
```go
func (s Set[T]) Delete(values ...T)
```
<b>例子:</b>
```go
package main
import (
"fmt"
set "github.com/duke-git/lancet/v2/datastructure/set"
)
func main() {
st := set.NewSet[int]()
st.Add(1, 2, 3)
set.Delete(3)
fmt.Println(st.Values()) //1,2
}
```
### <span id="Contain">Contain</span>
<p>判断集合是否包含某个值</p>
<b>函数签名:</b>
```go
func (s Set[T]) Contain(value T) bool
```
<b>例子:</b>
```go
package main
import (
"fmt"
set "github.com/duke-git/lancet/v2/datastructure/set"
)
func main() {
st := set.NewSet[int]()
st.Add(1, 2, 3)
fmt.Println(st.Contain(1)) //true
fmt.Println(st.Contain(4)) //false
}
```
### <span id="ContainAll">ContainAll</span>
<p>判断集合是否包含另一个集合</p>
<b>函数签名:</b>
```go
func (s Set[T]) ContainAll(other Set[T]) bool
```
<b>例子:</b>
```go
package main
import (
"fmt"
set "github.com/duke-git/lancet/v2/datastructure/set"
)
func main() {
set1 := set.NewSet(1, 2, 3)
set2 := set.NewSet(1, 2)
set3 := set.NewSet(1, 2, 3, 4)
fmt.Println(set1.ContainAll(set2)) //true
fmt.Println(set1.ContainAll(set3)) //false
}
```
### <span id="Size">Size</span>
<p>获取集合中元素的个数</p>
<b>函数签名:</b>
```go
func (s Set[T]) Size() int
```
<b>例子:</b>
```go
package main
import (
"fmt"
set "github.com/duke-git/lancet/v2/datastructure/set"
)
func main() {
set1 := set.NewSet(1, 2, 3)
fmt.Println(set1.Size()) //3
}
```
### <span id="Clone">Clone</span>
<p>克隆一个集合</p>
<b>函数签名:</b>
```go
func (s Set[T]) Clone() Set[T]
```
<b>例子:</b>
```go
package main
import (
"fmt"
set "github.com/duke-git/lancet/v2/datastructure/set"
)
func main() {
set1 := set.NewSet(1, 2, 3)
set2 := set1.Clone()
fmt.Println(set1.Size() == set2.Size()) //true
fmt.Println(set1.ContainAll(set2)) //true
}
```
### <span id="Equal">Equal</span>
<p>比较两个集合是否相等,包含相同元素为相等</p>
<b>函数签名:</b>
```go
func (s Set[T]) Equal(other Set[T]) bool
```
<b>例子:</b>
```go
package main
import (
"fmt"
set "github.com/duke-git/lancet/v2/datastructure/set"
)
func main() {
set1 := set.NewSet(1, 2, 3)
set2 := set.NewSet(1, 2, 3)
set3 := set.NewSet(1, 2, 3, 4)
fmt.Println(set1.Equal(set2)) //true
fmt.Println(set1.Equal(set3)) //false
}
```
### <span id="Iterate">Iterate</span>
<p>迭代结合,在每个元素上调用函数</p>
<b>函数签名:</b>
```go
func (s Set[T]) Iterate(fn func(value T))
```
<b>例子:</b>
```go
package main
import (
"fmt"
set "github.com/duke-git/lancet/v2/datastructure/set"
)
func main() {
set1 := set.NewSet(1, 2, 3)
arr := []int{}
set.Iterate(func(value int) {
arr = append(arr, value)
})
fmt.Println(arr) //1,2,3
}
```
### <span id="IsEmpty">IsEmpty</span>
<p>判断集合是否为空</p>
<b>函数签名:</b>
```go
func (s Set[T]) IsEmpty() bool
```
<b>例子:</b>
```go
package main
import (
"fmt"
set "github.com/duke-git/lancet/v2/datastructure/set"
)
func main() {
set1 := set.NewSet(1, 2, 3)
set2 := set.NewSet()
fmt.Println(set1.IsEmpty()) //false
fmt.Println(set2.IsEmpty()) //true
}
```
### <span id="Union">Union</span>
<p>求两个集合的并集</p>
<b>函数签名:</b>
```go
func (s Set[T]) Union(other Set[T]) Set[T]
```
<b>例子:</b>
```go
package main
import (
"fmt"
set "github.com/duke-git/lancet/v2/datastructure/set"
)
func main() {
set1 := set.NewSet(1, 2, 3)
set2 := set.NewSet(2, 3, 4, 5)
set3 := set1.Union(set2)
fmt.Println(set3.Values()) //1,2,3,4,5
}
```
### <span id="Intersection">Intersection</span>
<p>求两个集合的交集</p>
<b>函数签名:</b>
```go
func (s Set[T]) Intersection(other Set[T]) Set[T]
```
<b>例子:</b>
```go
package main
import (
"fmt"
set "github.com/duke-git/lancet/v2/datastructure/set"
)
func main() {
set1 := set.NewSet(1, 2, 3)
set2 := set.NewSet(2, 3, 4, 5)
set3 := set1.Intersection(set2)
fmt.Println(set3.Values()) //2,3
}
```
### <span id="SymmetricDifference">SymmetricDifference</span>
<p>返回一个集合,其中元素在第一个集合或第二个集合中,且不同时存在于两个集合中</p>
<b>函数签名:</b>
```go
func (s Set[T]) SymmetricDifference(other Set[T]) Set[T]
```
<b>例子:</b>
```go
package main
import (
"fmt"
set "github.com/duke-git/lancet/v2/datastructure/set"
)
func main() {
set1 := set.NewSet(1, 2, 3)
set2 := set.NewSet(2, 3, 4, 5)
set3 := set1.SymmetricDifference(set2)
fmt.Println(set3.Values()) //1,4,5
}
```
### <span id="Minus">Minus</span>
<p>创建一个集合,其元素在原始集中但不在比较集中</p>
<b>函数签名:</b>
```go
func (s Set[T]) Minus(comparedSet Set[T]) Set[T]
```
<b>例子:</b>
```go
package main
import (
"fmt"
set "github.com/duke-git/lancet/v2/datastructure/set"
)
func main() {
set1 := set.NewSet(1, 2, 3)
set2 := set.NewSet(2, 3, 4, 5)
set3 := set.NewSet(2, 3)
res1 := set1.Minus(set2)
fmt.Println(res1.Values()) //1
res2 := set2.Minus(set3)
fmt.Println(res2.Values()) //4,5
}
```

611
docs/datastructure/stack.md Normal file
View File

@@ -0,0 +1,611 @@
# Stack
Stack is an abstract data type that serves as a collection of elements. Elements follow the LIFO principle. FIFO is last-in, first-out, meaning that the most recently produced items are recorded as sold first.
<div STYLE="page-break-after: always;"></div>
## Source
- [https://github.com/duke-git/lancet/blob/main/datastructure/stack/arraystack.go](https://github.com/duke-git/lancet/blob/main/datastructure/stack/arraystack.go)
- [https://github.com/duke-git/lancet/blob/main/datastructure/stack/linkedstack.go](https://github.com/duke-git/lancet/blob/main/datastructure/stack/linkedstack.go)
<div STYLE="page-break-after: always;"></div>
## Usage
```go
import (
stack "github.com/duke-git/lancet/v2/datastructure/stack"
)
```
<div STYLE="page-break-after: always;"></div>
## Index
### 1. ArrayStack
- [NewArrayStack](#NewArrayStack)
- [Push](#ArrayStack_Push)
- [Pop](#ArrayStack_Pop)
- [Peak](#ArrayStack_Peak)
- [Data](#ArrayStack_Data)
- [Size](#ArrayStack_Size)
- [IsEmpty](#ArrayStack_IsEmpty)
- [Clear](#ArrayStack_Clear)
### 2. LinkedStack
- [NewLinkedStack](#NewLinkedStack)
- [Push](#LinkedStack_Push)
- [Pop](#LinkedStack_Pop)
- [Peak](#LinkedStack_Peak)
- [Data](#LinkedStack_Data)
- [Size](#LinkedStack_Size)
- [IsEmpty](#LinkedStack_IsEmpty)
- [Clear](#LinkedStack_Clear)
- [Print](#LinkedStack_Print)
<div STYLE="page-break-after: always;"></div>
## Documentation
### 1. ArrayStack
ArrayStack is a stack implemented by slice.
### <span id="NewArrayStack">NewArrayStack</span>
<p>Return a empty ArrayStack pointer</p>
<b>Signature:</b>
```go
type ArrayStack[T any] struct {
data []T
length int
}
func NewArrayStack[T any]() *ArrayStack[T]
```
<b>Example:</b>
```go
package main
import (
"fmt"
stack "github.com/duke-git/lancet/v2/datastructure/stack"
)
func main() {
sk := stack.NewArrayStack[int]()
fmt.Println(sk)
}
```
### <span id="ArrayStack_Push">Push</span>
<p>Push element into array stack</p>
<b>Signature:</b>
```go
func (s *ArrayStack[T]) Push(value T)
```
<b>Example:</b>
```go
package main
import (
"fmt"
stack "github.com/duke-git/lancet/v2/datastructure/stack"
)
func main() {
sk := stack.NewArrayStack[int]()
sk.Push(1)
sk.Push(2)
sk.Push(3)
fmt.Println(sk.Data()) //[]int{3, 2, 1}
}
```
### <span id="ArrayStack_Pop">Pop</span>
<p>Delete the top element of stack then return it, if stack is empty, return nil and error</p>
<b>Signature:</b>
```go
func (s *ArrayStack[T]) Pop() (*T, error)
```
<b>Example:</b>
```go
package main
import (
"fmt"
stack "github.com/duke-git/lancet/v2/datastructure/stack"
)
func main() {
sk := stack.NewArrayStack[int]()
sk.Push(1)
sk.Push(2)
sk.Push(3)
val, err := sk.Pop()
fmt.Println(err) //nil
fmt.Println(*val) //3
fmt.Println(sk.Data()) //[]int{2, 1}
}
```
### <span id="ArrayStack_Peak">Peak</span>
<p>Return the top element of array stack</p>
<b>Signature:</b>
```go
func (s *ArrayStack[T]) Peak() (*T, error)
```
<b>Example:</b>
```go
package main
import (
"fmt"
stack "github.com/duke-git/lancet/v2/datastructure/stack"
)
func main() {
sk := stack.NewArrayStack[int]()
sk.Push(1)
sk.Push(2)
sk.Push(3)
val, err := sk.Peak()
fmt.Println(err) //nil
fmt.Println(*val) //3
fmt.Println(sk.Data()) //[]int{3, 2, 1}
}
```
### <span id="ArrayStack_Data">Data</span>
<p>Return a slice of all data in array stack</p>
<b>Signature:</b>
```go
func (s *ArrayStack[T]) Data() []T
```
<b>Example:</b>
```go
package main
import (
"fmt"
stack "github.com/duke-git/lancet/v2/datastructure/stack"
)
func main() {
sk := stack.NewArrayStack[int]()
sk.Push(1)
sk.Push(2)
sk.Push(3)
fmt.Println(sk.Data()) //[]int{3, 2, 1}
}
```
### <span id="ArrayStack_Size">Size</span>
<p>Return number of elements in array stack</p>
<b>Signature:</b>
```go
func (s *ArrayStack[T]) Size() int
```
<b>Example:</b>
```go
package main
import (
"fmt"
stack "github.com/duke-git/lancet/v2/datastructure/stack"
)
func main() {
sk := stack.NewArrayStack[int]()
sk.Push(1)
sk.Push(2)
sk.Push(3)
fmt.Println(sk.Size()) //3
}
```
### <span id="ArrayStack_IsEmpty">IsEmpty</span>
<p>Check if array stack is empty or not</p>
<b>Signature:</b>
```go
func (s *ArrayStack[T]) IsEmpty() bool
```
<b>Example:</b>
```go
package main
import (
"fmt"
stack "github.com/duke-git/lancet/v2/datastructure/stack"
)
func main() {
sk := stack.NewArrayStack[int]()
fmt.Println(sk.IsEmpty()) //true
sk.Push(1)
sk.Push(2)
sk.Push(3)
fmt.Println(sk.IsEmpty()) //false
}
```
### <span id="ArrayStack_Clear">Clear</span>
<p>Clear all elments in array stack</p>
<b>Signature:</b>
```go
func (s *ArrayStack[T]) Clear()
```
<b>Example:</b>
```go
package main
import (
"fmt"
stack "github.com/duke-git/lancet/v2/datastructure/stack"
)
func main() {
sk := stack.NewArrayStack[int]()
sk.Push(1)
sk.Push(2)
sk.Push(3)
sk.Clear()
fmt.Println(sk.Data()) //[]int{}
}
```
### 2. LinkedStack
LinkedStack is a stack implemented by linked list.
### <span id="NewLinkedStack">NewLinkedStack</span>
<p>Return a empty LinkedStack pointer</p>
<b>Signature:</b>
```go
type StackNode[T any] struct {
Value T
Next *StackNode[T]
}
type LinkedStack[T any] struct {
top *datastructure.StackNode[T]
length int
}
func NewLinkedStack[T any]() *LinkedStack[T]
```
<b>Example:</b>
```go
package main
import (
"fmt"
stack "github.com/duke-git/lancet/v2/datastructure/stack"
)
func main() {
sk := stack.NewLinkedStack[int]()
fmt.Println(sk)
}
```
### <span id="LinkedStack_Push">Push</span>
<p>Push element into linked stack</p>
<b>Signature:</b>
```go
func (s *LinkedStack[T]) Push(value T)
```
<b>Example:</b>
```go
package main
import (
"fmt"
stack "github.com/duke-git/lancet/v2/datastructure/stack"
)
func main() {
sk := stack.NewLinkedStack[int]()
sk.Push(1)
sk.Push(2)
sk.Push(3)
fmt.Println(sk.Data()) //[]int{3, 2, 1}
}
```
### <span id="LinkedStack_Pop">Pop</span>
<p>Delete the top element of stack then return it, if stack is empty, return nil and error</p>
<b>Signature:</b>
```go
func (s *LinkedStack[T]) Pop() (*T, error)
```
<b>Example:</b>
```go
package main
import (
"fmt"
stack "github.com/duke-git/lancet/v2/datastructure/stack"
)
func main() {
sk := stack.NewLinkedStack[int]()
sk.Push(1)
sk.Push(2)
sk.Push(3)
val, err := sk.Pop()
fmt.Println(err) //nil
fmt.Println(*val) //3
fmt.Println(sk.Data()) //[]int{2, 1}
}
```
### <span id="LinkedStack_Peak">Peak</span>
<p>Return the top element of linked stack</p>
<b>Signature:</b>
```go
func (s *LinkedStack[T]) Peak() (*T, error)
```
<b>Example:</b>
```go
package main
import (
"fmt"
stack "github.com/duke-git/lancet/v2/datastructure/stack"
)
func main() {
sk := stack.NewLinkedStack[int]()
sk.Push(1)
sk.Push(2)
sk.Push(3)
val, err := sk.Peak()
fmt.Println(err) //nil
fmt.Println(*val) //3
fmt.Println(sk.Data()) //[]int{3, 2, 1}
}
```
### <span id="LinkedStack_Data">Data</span>
<p>Return a slice of all data in linked stack</p>
<b>Signature:</b>
```go
func (s *LinkedStack[T]) Data() []T
```
<b>Example:</b>
```go
package main
import (
"fmt"
stack "github.com/duke-git/lancet/v2/datastructure/stack"
)
func main() {
sk := stack.NewLinkedStack[int]()
sk.Push(1)
sk.Push(2)
sk.Push(3)
fmt.Println(sk.Data()) //[]int{3, 2, 1}
}
```
### <span id="LinkedStack_Size">Size</span>
<p>Return number of elements in linked stack</p>
<b>Signature:</b>
```go
func (s *LinkedStack[T]) Size() int
```
<b>Example:</b>
```go
package main
import (
"fmt"
stack "github.com/duke-git/lancet/v2/datastructure/stack"
)
func main() {
sk := stack.NewLinkedStack[int]()
sk.Push(1)
sk.Push(2)
sk.Push(3)
fmt.Println(sk.Size()) //3
}
```
### <span id="LinkedStack_IsEmpty">IsEmpty</span>
<p>Check if linked stack is empty or not</p>
<b>Signature:</b>
```go
func (s *LinkedStack[T]) IsEmpty() bool
```
<b>Example:</b>
```go
package main
import (
"fmt"
stack "github.com/duke-git/lancet/v2/datastructure/stack"
)
func main() {
sk := stack.NewLinkedStack[int]()
fmt.Println(sk.IsEmpty()) //true
sk.Push(1)
sk.Push(2)
sk.Push(3)
fmt.Println(sk.IsEmpty()) //false
}
```
### <span id="LinkedStack_Clear">Clear</span>
<p>Clear all elments in linked stack</p>
<b>Signature:</b>
```go
func (s *LinkedStack[T]) Clear()
```
<b>Example:</b>
```go
package main
import (
"fmt"
stack "github.com/duke-git/lancet/v2/datastructure/stack"
)
func main() {
sk := stack.NewLinkedStack[int]()
sk.Push(1)
sk.Push(2)
sk.Push(3)
sk.Clear()
fmt.Println(sk.Data()) //[]int{}
}
```
### <span id="LinkedStack_Print">Print</span>
<p>Print the structure of a linked stack</p>
<b>Signature:</b>
```go
func (s *LinkedStack[T]) Print()
```
<b>Example:</b>
```go
package main
import (
"fmt"
stack "github.com/duke-git/lancet/v2/datastructure/stack"
)
func main() {
sk := stack.NewLinkedStack[int]()
sk.Push(1)
sk.Push(2)
sk.Push(3)
sk.Print() //[ &{Value:3 Next:0xc000010260}, &{Value:2 Next:0xc000010250}, &{Value:1 Next:<nil>}, ]
}
```

View File

@@ -0,0 +1,611 @@
# Stack
栈数据结构包括ArrayStack数组栈和LinkedStack链表栈
<div STYLE="page-break-after: always;"></div>
## 源码
- [https://github.com/duke-git/lancet/blob/main/datastructure/stack/arraystack.go](https://github.com/duke-git/lancet/blob/main/datastructure/stack/arraystack.go)
- [https://github.com/duke-git/lancet/blob/main/datastructure/stack/linkedstack.go](https://github.com/duke-git/lancet/blob/main/datastructure/stack/linkedstack.go)
<div STYLE="page-break-after: always;"></div>
## 用法
```go
import (
stack "github.com/duke-git/lancet/v2/datastructure/stack"
)
```
<div STYLE="page-break-after: always;"></div>
## 目录
### 1. ArrayStack数组栈
- [NewArrayStack](#NewArrayStack)
- [Push](#ArrayStack_Push)
- [Pop](#ArrayStack_Pop)
- [Peak](#ArrayStack_Peak)
- [Data](#ArrayStack_Data)
- [Size](#ArrayStack_Size)
- [IsEmpty](#ArrayStack_IsEmpty)
- [Clear](#ArrayStack_Clear)
### 2. LinkedStack链表栈
- [NewLinkedStack](#NewLinkedStack)
- [Push](#LinkedStack_Push)
- [Pop](#LinkedStack_Pop)
- [Peak](#LinkedStack_Peak)
- [Data](#LinkedStack_Data)
- [Size](#LinkedStack_Size)
- [IsEmpty](#LinkedStack_IsEmpty)
- [Clear](#LinkedStack_Clear)
- [Print](#LinkedStack_Print)
<div STYLE="page-break-after: always;"></div>
## 文档
### 1. ArrayStack
用切片实现栈结构
### <span id="NewArrayStack">NewArrayStack</span>
<p>返回ArrayStack指针实例</p>
<b>函数签名:</b>
```go
type ArrayStack[T any] struct {
data []T
length int
}
func NewArrayStack[T any]() *ArrayStack[T]
```
<b>例子:</b>
```go
package main
import (
"fmt"
stack "github.com/duke-git/lancet/v2/datastructure/stack"
)
func main() {
sk := stack.NewArrayStack[int]()
fmt.Println(sk)
}
```
### <span id="ArrayStack_Push">Push</span>
<p>将元素加入数组栈</p>
<b>函数签名:</b>
```go
func (s *ArrayStack[T]) Push(value T)
```
<b>例子:</b>
```go
package main
import (
"fmt"
stack "github.com/duke-git/lancet/v2/datastructure/stack"
)
func main() {
sk := stack.NewArrayStack[int]()
sk.Push(1)
sk.Push(2)
sk.Push(3)
fmt.Println(sk.Data()) //[]int{3, 2, 1}
}
```
### <span id="ArrayStack_Pop">Pop</span>
<p>删除栈顶元素并返回该元素指针</p>
<b>函数签名:</b>
```go
func (s *ArrayStack[T]) Pop() (*T, error)
```
<b>例子:</b>
```go
package main
import (
"fmt"
stack "github.com/duke-git/lancet/v2/datastructure/stack"
)
func main() {
sk := stack.NewArrayStack[int]()
sk.Push(1)
sk.Push(2)
sk.Push(3)
val, err := sk.Pop()
fmt.Println(err) //nil
fmt.Println(*val) //3
fmt.Println(sk.Data()) //[]int{2, 1}
}
```
### <span id="ArrayStack_Peak">Peak</span>
<p>返回栈顶元素指针</p>
<b>函数签名:</b>
```go
func (s *ArrayStack[T]) Peak() (*T, error)
```
<b>例子:</b>
```go
package main
import (
"fmt"
stack "github.com/duke-git/lancet/v2/datastructure/stack"
)
func main() {
sk := stack.NewArrayStack[int]()
sk.Push(1)
sk.Push(2)
sk.Push(3)
val, err := sk.Peak()
fmt.Println(err) //nil
fmt.Println(*val) //3
fmt.Println(sk.Data()) //[]int{3, 2, 1}
}
```
### <span id="ArrayStack_Data">Data</span>
<p>返回栈中所有元素组成的切片</p>
<b>函数签名:</b>
```go
func (s *ArrayStack[T]) Data() []T
```
<b>例子:</b>
```go
package main
import (
"fmt"
stack "github.com/duke-git/lancet/v2/datastructure/stack"
)
func main() {
sk := stack.NewArrayStack[int]()
sk.Push(1)
sk.Push(2)
sk.Push(3)
fmt.Println(sk.Data()) //[]int{3, 2, 1}
}
```
### <span id="ArrayStack_Size">Size</span>
<p>返回栈中元素的数量</p>
<b>函数签名:</b>
```go
func (s *ArrayStack[T]) Size() int
```
<b>例子:</b>
```go
package main
import (
"fmt"
stack "github.com/duke-git/lancet/v2/datastructure/stack"
)
func main() {
sk := stack.NewArrayStack[int]()
sk.Push(1)
sk.Push(2)
sk.Push(3)
fmt.Println(sk.Size()) //3
}
```
### <span id="ArrayStack_IsEmpty">IsEmpty</span>
<p>判断栈是否为空</p>
<b>函数签名:</b>
```go
func (s *ArrayStack[T]) IsEmpty() bool
```
<b>例子:</b>
```go
package main
import (
"fmt"
stack "github.com/duke-git/lancet/v2/datastructure/stack"
)
func main() {
sk := stack.NewArrayStack[int]()
fmt.Println(sk.IsEmpty()) //true
sk.Push(1)
sk.Push(2)
sk.Push(3)
fmt.Println(sk.IsEmpty()) //false
}
```
### <span id="ArrayStack_Clear">Clear</span>
<p>清空栈元素,使栈为空</p>
<b>函数签名:</b>
```go
func (s *ArrayStack[T]) Clear()
```
<b>例子:</b>
```go
package main
import (
"fmt"
stack "github.com/duke-git/lancet/v2/datastructure/stack"
)
func main() {
sk := stack.NewArrayStack[int]()
sk.Push(1)
sk.Push(2)
sk.Push(3)
sk.Clear()
fmt.Println(sk.Data()) //[]int{}
}
```
### 2. LinkedStack
链表实现的栈结构。
### <span id="NewLinkedStack">NewLinkedStack</span>
<p>返回LinkedStack指针实例</p>
<b>函数签名:</b>
```go
type StackNode[T any] struct {
Value T
Next *StackNode[T]
}
type LinkedStack[T any] struct {
top *datastructure.StackNode[T]
length int
}
func NewLinkedStack[T any]() *LinkedStack[T]
```
<b>例子:</b>
```go
package main
import (
"fmt"
stack "github.com/duke-git/lancet/v2/datastructure/stack"
)
func main() {
sk := stack.NewLinkedStack[int]()
fmt.Println(sk)
}
```
### <span id="LinkedStack_Push">Push</span>
<p>将元素加入链表栈</p>
<b>函数签名:</b>
```go
func (s *LinkedStack[T]) Push(value T)
```
<b>例子:</b>
```go
package main
import (
"fmt"
stack "github.com/duke-git/lancet/v2/datastructure/stack"
)
func main() {
sk := stack.NewLinkedStack[int]()
sk.Push(1)
sk.Push(2)
sk.Push(3)
fmt.Println(sk.Data()) //[]int{3, 2, 1}
}
```
### <span id="LinkedStack_Pop">Pop</span>
<p>删除栈顶元素并返回该元素指针</p>
<b>函数签名:</b>
```go
func (s *LinkedStack[T]) Pop() (*T, error)
```
<b>例子:</b>
```go
package main
import (
"fmt"
stack "github.com/duke-git/lancet/v2/datastructure/stack"
)
func main() {
sk := stack.NewLinkedStack[int]()
sk.Push(1)
sk.Push(2)
sk.Push(3)
val, err := sk.Pop()
fmt.Println(err) //nil
fmt.Println(*val) //3
fmt.Println(sk.Data()) //[]int{2, 1}
}
```
### <span id="LinkedStack_Peak">Peak</span>
<p>返回栈顶元素指针</p>
<b>函数签名:</b>
```go
func (s *LinkedStack[T]) Peak() (*T, error)
```
<b>例子:</b>
```go
package main
import (
"fmt"
stack "github.com/duke-git/lancet/v2/datastructure/stack"
)
func main() {
sk := stack.NewLinkedStack[int]()
sk.Push(1)
sk.Push(2)
sk.Push(3)
val, err := sk.Peak()
fmt.Println(err) //nil
fmt.Println(*val) //3
fmt.Println(sk.Data()) //[]int{3, 2, 1}
}
```
### <span id="LinkedStack_Data">Data</span>
<p>返回栈中所有元素组成的切片</p>
<b>函数签名:</b>
```go
func (s *LinkedStack[T]) Data() []T
```
<b>例子:</b>
```go
package main
import (
"fmt"
stack "github.com/duke-git/lancet/v2/datastructure/stack"
)
func main() {
sk := stack.NewLinkedStack[int]()
sk.Push(1)
sk.Push(2)
sk.Push(3)
fmt.Println(sk.Data()) //[]int{3, 2, 1}
}
```
### <span id="LinkedStack_Size">Size</span>
<p>返回栈中元素的数量</p>
<b>函数签名:</b>
```go
func (s *LinkedStack[T]) Size() int
```
<b>例子:</b>
```go
package main
import (
"fmt"
stack "github.com/duke-git/lancet/v2/datastructure/stack"
)
func main() {
sk := stack.NewLinkedStack[int]()
sk.Push(1)
sk.Push(2)
sk.Push(3)
fmt.Println(sk.Size()) //3
}
```
### <span id="LinkedStack_IsEmpty">IsEmpty</span>
<p>判断栈是否为空</p>
<b>函数签名:</b>
```go
func (s *LinkedStack[T]) IsEmpty() bool
```
<b>例子:</b>
```go
package main
import (
"fmt"
stack "github.com/duke-git/lancet/v2/datastructure/stack"
)
func main() {
sk := stack.NewLinkedStack[int]()
fmt.Println(sk.IsEmpty()) //true
sk.Push(1)
sk.Push(2)
sk.Push(3)
fmt.Println(sk.IsEmpty()) //false
}
```
### <span id="LinkedStack_Clear">Clear</span>
<p>清空栈元素,使栈为空</p>
<b>函数签名:</b>
```go
func (s *LinkedStack[T]) Clear()
```
<b>例子:</b>
```go
package main
import (
"fmt"
stack "github.com/duke-git/lancet/v2/datastructure/stack"
)
func main() {
sk := stack.NewLinkedStack[int]()
sk.Push(1)
sk.Push(2)
sk.Push(3)
sk.Clear()
fmt.Println(sk.Data()) //[]int{}
}
```
### <span id="LinkedStack_Print">Print</span>
<p>打印链表栈结构</p>
<b>函数签名:</b>
```go
func (s *LinkedStack[T]) Print()
```
<b>例子:</b>
```go
package main
import (
"fmt"
stack "github.com/duke-git/lancet/v2/datastructure/stack"
)
func main() {
sk := stack.NewLinkedStack[int]()
sk.Push(1)
sk.Push(2)
sk.Push(3)
sk.Print() //[ &{Value:3 Next:0xc000010260}, &{Value:2 Next:0xc000010250}, &{Value:1 Next:<nil>}, ]
}
```

527
docs/datastructure/tree.md Normal file
View File

@@ -0,0 +1,527 @@
# Tree
Tree is a collection of tree nodes. Each tree node has a value, a left pointer point to left node and a right pointer point to right node.
<div STYLE="page-break-after: always;"></div>
## Source
- [https://github.com/duke-git/lancet/blob/main/datastructure/tree/bstree.go](https://github.com/duke-git/lancet/blob/main/datastructure/tree/bstree.go)
<div STYLE="page-break-after: always;"></div>
## Usage
```go
import (
tree "github.com/duke-git/lancet/v2/datastructure/tree"
)
```
<div STYLE="page-break-after: always;"></div>
## Index
### 1. BSTree
- [NewBSTree](#NewBSTree)
- [Insert](#BSTree_Insert)
- [Delete](#BSTree_Delete)
- [PreOrderTraverse](#BSTree_PreOrderTraverse)
- [InOrderTraverse](#BSTree_InOrderTraverse)
- [PostOrderTraverse](#BSTree_PostOrderTraverse)
- [LevelOrderTraverse](#BSTree_LevelOrderTraverse)
- [Depth](#BSTree_Depth)
- [HasSubTree](#BSTree_HasSubTree)
- [Print](#BSTree_Print)
<div STYLE="page-break-after: always;"></div>
## Documentation
## 1. BSTree
BSTree is a binary search tree data structure in which each node has at two children, which are referred to as the left child and the right child. In BSTree: leftNode < rootNode < rightNode. Type T should implements Compare function in lancetconstraints.Comparator interface.
### <span id="NewBSTree">NewBSTree</span>
<p>Make a BSTree pointer instance</p>
<b>Signature:</b>
```go
func NewBSTree[T any](rootData T, comparator lancetconstraints.Comparator) *BSTree[T]
type BSTree[T any] struct {
root *datastructure.TreeNode[T]
comparator lancetconstraints.Comparator
}
type TreeNode[T any] struct {
Value T
Left *TreeNode[T]
Right *TreeNode[T]
}
```
<b>Example:</b>
```go
package main
import (
"fmt"
tree "github.com/duke-git/lancet/v2/datastructure/tree"
)
type intComparator struct{}
func (c *intComparator) Compare(v1, v2 any) int {
val1, _ := v1.(int)
val2, _ := v2.(int)
if val1 < val2 {
return -1
} else if val1 > val2 {
return 1
}
return 0
}
func main() {
bstree := tree.NewBSTree(6, &intComparator{})
fmt.Println(bstree) //
}
```
### <span id="BSTree_Insert">Insert</span>
<p>Insert value into binary search tree</p>
<b>Signature:</b>
```go
func (t *BSTree[T]) Insert(data T)
```
<b>Example:</b>
```go
package main
import (
"fmt"
tree "github.com/duke-git/lancet/v2/datastructure/tree"
)
type intComparator struct{}
func (c *intComparator) Compare(v1, v2 any) int {
val1, _ := v1.(int)
val2, _ := v2.(int)
if val1 < val2 {
return -1
} else if val1 > val2 {
return 1
}
return 0
}
func main() {
bstree := tree.NewBSTree(6, &intComparator{})
bstree.Insert(7)
bstree.Insert(5)
bstree.Insert(2)
bstree.Insert(4)
fmt.Println(bstree.PreOrderTraverse()) //6, 5, 2, 4, 7
}
```
### <span id="BSTree_Delete">Delete</span>
<p>Delete value of binary search tree</p>
<b>Signature:</b>
```go
func (t *BSTree[T]) Delete(data T)
```
<b>Example:</b>
```go
package main
import (
"fmt"
tree "github.com/duke-git/lancet/v2/datastructure/tree"
)
type intComparator struct{}
func (c *intComparator) Compare(v1, v2 any) int {
val1, _ := v1.(int)
val2, _ := v2.(int)
if val1 < val2 {
return -1
} else if val1 > val2 {
return 1
}
return 0
}
func main() {
bstree := tree.NewBSTree(6, &intComparator{})
bstree.Insert(7)
bstree.Insert(5)
bstree.Insert(2)
bstree.Insert(4)
bstree.Delete(4)
fmt.Println(bstree.PreOrderTraverse()) //2, 5, 6, 7
}
```
### <span id="BSTree_PreOrderTraverse">PreOrderTraverse</span>
<p>Traverse tree nodes in pre order</p>
<b>Signature:</b>
```go
func (t *BSTree[T]) PreOrderTraverse() []T
```
<b>Example:</b>
```go
package main
import (
"fmt"
tree "github.com/duke-git/lancet/v2/datastructure/tree"
)
type intComparator struct{}
func (c *intComparator) Compare(v1, v2 any) int {
val1, _ := v1.(int)
val2, _ := v2.(int)
if val1 < val2 {
return -1
} else if val1 > val2 {
return 1
}
return 0
}
func main() {
bstree := tree.NewBSTree(6, &intComparator{})
bstree.Insert(7)
bstree.Insert(5)
bstree.Insert(2)
bstree.Insert(4)
fmt.Println(bstree.PreOrderTraverse()) //6, 5, 2, 4, 7
}
```
### <span id="BSTree_InOrderTraverse">InOrderTraverse</span>
<p>Traverse tree nodes in middle order</p>
<b>Signature:</b>
```go
func (t *BSTree[T]) InOrderTraverse() []T
```
<b>Example:</b>
```go
package main
import (
"fmt"
tree "github.com/duke-git/lancet/v2/datastructure/tree"
)
type intComparator struct{}
func (c *intComparator) Compare(v1, v2 any) int {
val1, _ := v1.(int)
val2, _ := v2.(int)
if val1 < val2 {
return -1
} else if val1 > val2 {
return 1
}
return 0
}
func main() {
bstree := tree.NewBSTree(6, &intComparator{})
bstree.Insert(7)
bstree.Insert(5)
bstree.Insert(2)
bstree.Insert(4)
fmt.Println(bstree.InOrderTraverse()) //2, 4, 5, 6, 7
}
```
### <span id="BSTree_PostOrderTraverse">PostOrderTraverse</span>
<p>Traverse tree nodes in post order</p>
<b>Signature:</b>
```go
func (t *BSTree[T]) PostOrderTraverse() []T
```
<b>Example:</b>
```go
package main
import (
"fmt"
tree "github.com/duke-git/lancet/v2/datastructure/tree"
)
type intComparator struct{}
func (c *intComparator) Compare(v1, v2 any) int {
val1, _ := v1.(int)
val2, _ := v2.(int)
if val1 < val2 {
return -1
} else if val1 > val2 {
return 1
}
return 0
}
func main() {
bstree := tree.NewBSTree(6, &intComparator{})
bstree.Insert(7)
bstree.Insert(5)
bstree.Insert(2)
bstree.Insert(4)
fmt.Println(bstree.PostOrderTraverse()) //5, 2, 4, 7, 6
}
```
### <span id="BSTree_LevelOrderTraverse">LevelOrderTraverse</span>
<p>Traverse tree nodes in node level order</p>
<b>Signature:</b>
```go
func (t *BSTree[T]) LevelOrderTraverse() []T
```
<b>Example:</b>
```go
package main
import (
"fmt"
tree "github.com/duke-git/lancet/v2/datastructure/tree"
)
type intComparator struct{}
func (c *intComparator) Compare(v1, v2 any) int {
val1, _ := v1.(int)
val2, _ := v2.(int)
if val1 < val2 {
return -1
} else if val1 > val2 {
return 1
}
return 0
}
func main() {
bstree := tree.NewBSTree(6, &intComparator{})
bstree.Insert(7)
bstree.Insert(5)
bstree.Insert(2)
bstree.Insert(4)
fmt.Println(bstree.LevelOrderTraverse()) //6, 5, 7, 2, 4
}
```
### <span id="BSTree_Depth">Depth</span>
<p>Get the depth of a binary saerch tree</p>
<b>Signature:</b>
```go
func (t *BSTree[T]) Depth() int
```
<b>Example:</b>
```go
package main
import (
"fmt"
tree "github.com/duke-git/lancet/v2/datastructure/tree"
)
type intComparator struct{}
func (c *intComparator) Compare(v1, v2 any) int {
val1, _ := v1.(int)
val2, _ := v2.(int)
if val1 < val2 {
return -1
} else if val1 > val2 {
return 1
}
return 0
}
func main() {
bstree := tree.NewBSTree(6, &intComparator{})
bstree.Insert(7)
bstree.Insert(5)
bstree.Insert(2)
bstree.Insert(4)
fmt.Println(bstree.Depth()) //4
}
```
### <span id="BSTree_HasSubTree">HasSubTree</span>
<p>Check if the given tree is sub tree of origin tree or not</p>
<b>Signature:</b>
```go
func (t *BSTree[T]) HasSubTree(subTree *BSTree[T]) bool
```
<b>Example:</b>
```go
package main
import (
"fmt"
tree "github.com/duke-git/lancet/v2/datastructure/tree"
)
type intComparator struct{}
func (c *intComparator) Compare(v1, v2 any) int {
val1, _ := v1.(int)
val2, _ := v2.(int)
if val1 < val2 {
return -1
} else if val1 > val2 {
return 1
}
return 0
}
func main() {
superTree := tree.NewBSTree(8, &intComparator{})
superTree.Insert(4)
superTree.Insert(5)
superTree.Insert(6)
superTree.Insert(9)
superTree.Insert(4)
subTree := tree.NewBSTree(5, &intComparator{})
subTree.Insert(4)
subTree.Insert(6)
fmt.Println(superTree.HasSubTree(subTree)) //true
fmt.Println(subTree.HasSubTree(superTree)) //false
}
```
### <span id="BSTree_Print">Print</span>
<p>Print the structure of binary saerch tree</p>
<b>Signature:</b>
```go
func (t *BSTree[T]) Print()
```
<b>Example:</b>
```go
package main
import (
"fmt"
tree "github.com/duke-git/lancet/v2/datastructure/tree"
)
type intComparator struct{}
func (c *intComparator) Compare(v1, v2 any) int {
val1, _ := v1.(int)
val2, _ := v2.(int)
if val1 < val2 {
return -1
} else if val1 > val2 {
return 1
}
return 0
}
func main() {
bstree := tree.NewBSTree(6, &intComparator{})
bstree.Insert(7)
bstree.Insert(5)
bstree.Insert(2)
bstree.Insert(4)
fmt.Println(bstree.Print())
// 6
// / \
// / \
// / \
// / \
// 5 7
// /
// /
// 2
// \
// 4
}
```

View File

@@ -0,0 +1,527 @@
# Tree
树是树节点的集合。 每个树节点都有一个值,一个指向左节点的左指针和一个指向右节点的右指针。
<div STYLE="page-break-after: always;"></div>
## 源码
- [https://github.com/duke-git/lancet/blob/main/datastructure/tree/bstree.go](https://github.com/duke-git/lancet/blob/main/datastructure/tree/bstree.go)
<div STYLE="page-break-after: always;"></div>
## 用法
```go
import (
tree "github.com/duke-git/lancet/v2/datastructure/tree"
)
```
<div STYLE="page-break-after: always;"></div>
## 目录
### 1. BSTree
- [NewBSTree](#NewBSTree)
- [Insert](#BSTree_Insert)
- [Delete](#BSTree_Delete)
- [PreOrderTraverse](#BSTree_PreOrderTraverse)
- [InOrderTraverse](#BSTree_InOrderTraverse)
- [PostOrderTraverse](#BSTree_PostOrderTraverse)
- [LevelOrderTraverse](#BSTree_LevelOrderTraverse)
- [Depth](#BSTree_Depth)
- [HasSubTree](#BSTree_HasSubTree)
- [Print](#BSTree_Print)
<div STYLE="page-break-after: always;"></div>
## 文档
## 1. BSTree
BSTree是一种二叉搜索树数据结构其中每个节点有两个孩子分别称为左孩子和右孩子。 在 BSTree 中leftNode < rootNode < rightNode。 T类型应该实现lancetconstraints.Comparator。
### <span id="NewBSTree">NewBSTree</span>
<p>返回BSTree指针实例</p>
<b>函数签名:</b>
```go
func NewBSTree[T any](rootData T, comparator lancetconstraints.Comparator) *BSTree[T]
type BSTree[T any] struct {
root *datastructure.TreeNode[T]
comparator lancetconstraints.Comparator
}
type TreeNode[T any] struct {
Value T
Left *TreeNode[T]
Right *TreeNode[T]
}
```
<b>例子:</b>
```go
package main
import (
"fmt"
tree "github.com/duke-git/lancet/v2/datastructure/tree"
)
type intComparator struct{}
func (c *intComparator) Compare(v1, v2 any) int {
val1, _ := v1.(int)
val2, _ := v2.(int)
if val1 < val2 {
return -1
} else if val1 > val2 {
return 1
}
return 0
}
func main() {
bstree := tree.NewBSTree(6, &intComparator{})
fmt.Println(bstree) //
}
```
### <span id="BSTree_Insert">Insert</span>
<p>将值插入二叉搜索树</p>
<b>函数签名:</b>
```go
func (t *BSTree[T]) Insert(data T)
```
<b>例子:</b>
```go
package main
import (
"fmt"
tree "github.com/duke-git/lancet/v2/datastructure/tree"
)
type intComparator struct{}
func (c *intComparator) Compare(v1, v2 any) int {
val1, _ := v1.(int)
val2, _ := v2.(int)
if val1 < val2 {
return -1
} else if val1 > val2 {
return 1
}
return 0
}
func main() {
bstree := tree.NewBSTree(6, &intComparator{})
bstree.Insert(7)
bstree.Insert(5)
bstree.Insert(2)
bstree.Insert(4)
fmt.Println(bstree.PreOrderTraverse()) //6, 5, 2, 4, 7
}
```
### <span id="BSTree_Delete">Delete</span>
<p>删除插入二叉搜索树中指定的值</p>
<b>函数签名:</b>
```go
func (t *BSTree[T]) Delete(data T)
```
<b>例子:</b>
```go
package main
import (
"fmt"
tree "github.com/duke-git/lancet/v2/datastructure/tree"
)
type intComparator struct{}
func (c *intComparator) Compare(v1, v2 any) int {
val1, _ := v1.(int)
val2, _ := v2.(int)
if val1 < val2 {
return -1
} else if val1 > val2 {
return 1
}
return 0
}
func main() {
bstree := tree.NewBSTree(6, &intComparator{})
bstree.Insert(7)
bstree.Insert(5)
bstree.Insert(2)
bstree.Insert(4)
bstree.Delete(4)
fmt.Println(bstree.PreOrderTraverse()) //2, 5, 6, 7
}
```
### <span id="BSTree_PreOrderTraverse">PreOrderTraverse</span>
<p>按前序遍历树节点</p>
<b>函数签名:</b>
```go
func (t *BSTree[T]) PreOrderTraverse() []T
```
<b>例子:</b>
```go
package main
import (
"fmt"
tree "github.com/duke-git/lancet/v2/datastructure/tree"
)
type intComparator struct{}
func (c *intComparator) Compare(v1, v2 any) int {
val1, _ := v1.(int)
val2, _ := v2.(int)
if val1 < val2 {
return -1
} else if val1 > val2 {
return 1
}
return 0
}
func main() {
bstree := tree.NewBSTree(6, &intComparator{})
bstree.Insert(7)
bstree.Insert(5)
bstree.Insert(2)
bstree.Insert(4)
fmt.Println(bstree.PreOrderTraverse()) //6, 5, 2, 4, 7
}
```
### <span id="BSTree_InOrderTraverse">InOrderTraverse</span>
<p>按中序遍历树节点</p>
<b>函数签名:</b>
```go
func (t *BSTree[T]) InOrderTraverse() []T
```
<b>例子:</b>
```go
package main
import (
"fmt"
tree "github.com/duke-git/lancet/v2/datastructure/tree"
)
type intComparator struct{}
func (c *intComparator) Compare(v1, v2 any) int {
val1, _ := v1.(int)
val2, _ := v2.(int)
if val1 < val2 {
return -1
} else if val1 > val2 {
return 1
}
return 0
}
func main() {
bstree := tree.NewBSTree(6, &intComparator{})
bstree.Insert(7)
bstree.Insert(5)
bstree.Insert(2)
bstree.Insert(4)
fmt.Println(bstree.InOrderTraverse()) //2, 4, 5, 6, 7
}
```
### <span id="BSTree_PostOrderTraverse">PostOrderTraverse</span>
<p>按后序遍历树节点</p>
<b>函数签名:</b>
```go
func (t *BSTree[T]) PostOrderTraverse() []T
```
<b>例子:</b>
```go
package main
import (
"fmt"
tree "github.com/duke-git/lancet/v2/datastructure/tree"
)
type intComparator struct{}
func (c *intComparator) Compare(v1, v2 any) int {
val1, _ := v1.(int)
val2, _ := v2.(int)
if val1 < val2 {
return -1
} else if val1 > val2 {
return 1
}
return 0
}
func main() {
bstree := tree.NewBSTree(6, &intComparator{})
bstree.Insert(7)
bstree.Insert(5)
bstree.Insert(2)
bstree.Insert(4)
fmt.Println(bstree.PostOrderTraverse()) //5, 2, 4, 7, 6
}
```
### <span id="BSTree_LevelOrderTraverse">LevelOrderTraverse</span>
<p>按节点层次遍历树节点</p>
<b>函数签名:</b>
```go
func (t *BSTree[T]) LevelOrderTraverse() []T
```
<b>例子:</b>
```go
package main
import (
"fmt"
tree "github.com/duke-git/lancet/v2/datastructure/tree"
)
type intComparator struct{}
func (c *intComparator) Compare(v1, v2 any) int {
val1, _ := v1.(int)
val2, _ := v2.(int)
if val1 < val2 {
return -1
} else if val1 > val2 {
return 1
}
return 0
}
func main() {
bstree := tree.NewBSTree(6, &intComparator{})
bstree.Insert(7)
bstree.Insert(5)
bstree.Insert(2)
bstree.Insert(4)
fmt.Println(bstree.LevelOrderTraverse()) //6, 5, 7, 2, 4
}
```
### <span id="BSTree_Depth">Depth</span>
<p>获取树的深度</p>
<b>函数签名:</b>
```go
func (t *BSTree[T]) Depth() int
```
<b>例子:</b>
```go
package main
import (
"fmt"
tree "github.com/duke-git/lancet/v2/datastructure/tree"
)
type intComparator struct{}
func (c *intComparator) Compare(v1, v2 any) int {
val1, _ := v1.(int)
val2, _ := v2.(int)
if val1 < val2 {
return -1
} else if val1 > val2 {
return 1
}
return 0
}
func main() {
bstree := tree.NewBSTree(6, &intComparator{})
bstree.Insert(7)
bstree.Insert(5)
bstree.Insert(2)
bstree.Insert(4)
fmt.Println(bstree.Depth()) //4
}
```
### <span id="BSTree_HasSubTree">HasSubTree</span>
<p>判断给定树是否是子树</p>
<b>函数签名:</b>
```go
func (t *BSTree[T]) HasSubTree(subTree *BSTree[T]) bool
```
<b>例子:</b>
```go
package main
import (
"fmt"
tree "github.com/duke-git/lancet/v2/datastructure/tree"
)
type intComparator struct{}
func (c *intComparator) Compare(v1, v2 any) int {
val1, _ := v1.(int)
val2, _ := v2.(int)
if val1 < val2 {
return -1
} else if val1 > val2 {
return 1
}
return 0
}
func main() {
superTree := tree.NewBSTree(8, &intComparator{})
superTree.Insert(4)
superTree.Insert(5)
superTree.Insert(6)
superTree.Insert(9)
superTree.Insert(4)
subTree := tree.NewBSTree(5, &intComparator{})
subTree.Insert(4)
subTree.Insert(6)
fmt.Println(superTree.HasSubTree(subTree)) //true
fmt.Println(subTree.HasSubTree(superTree)) //false
}
```
### <span id="BSTree_Print">Print</span>
<p>打印树结构</p>
<b>函数签名:</b>
```go
func (t *BSTree[T]) Print()
```
<b>例子:</b>
```go
package main
import (
"fmt"
tree "github.com/duke-git/lancet/v2/datastructure/tree"
)
type intComparator struct{}
func (c *intComparator) Compare(v1, v2 any) int {
val1, _ := v1.(int)
val2, _ := v2.(int)
if val1 < val2 {
return -1
} else if val1 > val2 {
return 1
}
return 0
}
func main() {
bstree := tree.NewBSTree(6, &intComparator{})
bstree.Insert(7)
bstree.Insert(5)
bstree.Insert(2)
bstree.Insert(4)
fmt.Println(bstree.Print())
// 6
// / \
// / \
// / \
// / \
// 5 7
// /
// /
// 2
// \
// 4
}
```

View File

@@ -251,7 +251,7 @@ func main() {
<b>Signature:</b>
```go
func BeginOfWeek(t time.Time) time.Time
func BeginOfWeek(t time.Time, beginFrom ...time.Weekday) time.Time
```
<b>Example:</b>
@@ -414,7 +414,7 @@ func main() {
<b>Signature:</b>
```go
func EndOfWeek(t time.Time) time.Time
func EndOfWeek(t time.Time, endWith ...time.Weekday) time.Time
```
<b>Example:</b>
@@ -712,7 +712,7 @@ func main() {
```go
type theTime struct {
unix int64
unix int64
}
func NewUnix(unix int64) *theTime
```
@@ -741,7 +741,7 @@ func main() {
```go
type theTime struct {
unix int64
unix int64
}
func NewFormat(t string) (*theTime, error)
```
@@ -771,7 +771,7 @@ func main() {
```go
type theTime struct {
unix int64
unix int64
}
func NewISO8601(iso8601 string) (*theTime, error)
```

View File

@@ -243,12 +243,12 @@ func main() {
### <span id="BeginOfWeek">BeginOfWeek</span>
<p>返回指定时间的星期开始时间</p>
<p>返回指定时间的每周开始时间,默认开始时间星期日</p>
<b>函数签名:</b>
```go
func BeginOfWeek(t time.Time) time.Time
func BeginOfWeek(t time.Time, beginFrom ...time.Weekday) time.Time
```
<b>例子:</b>
@@ -406,12 +406,12 @@ func main() {
### <span id="EndOfWeek">EndOfWeek</span>
<p>返回指定时间的星期结束时间</p>
<p>返回指定时间的星期结束时间,默认结束时间星期六</p>
<b>函数签名:</b>
```go
func EndOfWeek(t time.Time) time.Time
func EndOfWeek(t time.Time, endWith ...time.Weekday) time.Time
```
<b>例子:</b>
@@ -680,7 +680,7 @@ func main() {
```go
type theTime struct {
unix int64
unix int64
}
func NewUnixNow() *theTime
```
@@ -708,7 +708,7 @@ func main() {
```go
type theTime struct {
unix int64
unix int64
}
func NewUnix(unix int64) *theTime
```
@@ -737,7 +737,7 @@ func main() {
```go
type theTime struct {
unix int64
unix int64
}
func NewFormat(t string) (*theTime, error)
```
@@ -767,7 +767,7 @@ func main() {
```go
type theTime struct {
unix int64
unix int64
}
func NewISO8601(iso8601 string) (*theTime, error)
```

View File

@@ -21,18 +21,20 @@ import (
## Index
- [ClearFile](#ClearFile)
- [CreateFile](#CreateFile)
- [CreateDir](#CreateDir)
- [CopyFile](#CopyFile)
- [FileMode](#FileMode)
- [MiMeType](#MiMeType)
- [IsExist](#IsExist)
- [IsLink](#IsLink)
- [IsDir](#IsDir)
- [ListFileNames](#ListFileNames)
- [RemoveFile](#RemoveFile)
- [ReadFileToString](#ReadFileToString)
- [ReadFileByLine](#ReadFileByLine)
- [Zip](#Zip)
- [UnZip](#UnZip)
- [UnZip](#UnZip)
<div STYLE="page-break-after: always;"></div>
@@ -92,6 +94,32 @@ func main() {
```
### <span id="CreateDir">CreateDir</span>
<p>Create directory in absolute path. param `absPath` like /a/, /a/b/.</p>
<b>Signature:</b>
```go
func CreateDir(absPath string) error
```
<b>Example:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/fileutil"
)
func main() {
err := fileutil.CreateDir("/a/")
fmt.Println(err)
}
```
### <span id="CopyFile">CopyFile</span>
<p>Copy src file to dest file. If dest file exist will overwrite it.</p>
@@ -112,9 +140,9 @@ import (
func main() {
err := fileutil.CopyFile("./test.txt", "./test_copy.txt")
if err != nil {
fmt.Println(err)
}
if err != nil {
fmt.Println(err)
}
}
```
@@ -140,9 +168,9 @@ import (
func main() {
mode, err := fileutil.FileMode("./test.txt")
if err != nil {
fmt.Println(err)
}
if err != nil {
fmt.Println(err)
}
fmt.Println(mode)
}
```
@@ -310,8 +338,8 @@ import (
func main() {
err := fileutil.RemoveFile("./test.txt")
if err != nil {
fmt.Println(err)
}
fmt.Println(err)
}
}
```
@@ -337,12 +365,12 @@ import (
func main() {
path := "./test.txt"
fileutil.CreateFile(path)
fileutil.CreateFile(path)
f, _ := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, 0777)
f.WriteString("hello world")
f, _ := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, 0777)
f.WriteString("hello world")
content, _ := fileutil.ReadFileToString(path)
content, _ := fileutil.ReadFileToString(path)
fmt.Println(content) //hello world
}
```
@@ -370,13 +398,13 @@ import (
func main() {
path := "./text.txt"
fileutil.CreateFile(path)
fileutil.CreateFile(path)
f, _ := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, 0777)
defer f.Close()
f.WriteString("hello\nworld")
f, _ := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, 0777)
defer f.Close()
f.WriteString("hello\nworld")
contents, _ := fileutil.ReadFileByLine(path)
contents, _ := fileutil.ReadFileByLine(path)
fmt.Println(contents) //[]string{"hello", "world"}
}
```
@@ -402,7 +430,7 @@ import (
)
func main() {
err := fileutil.Zip("./test.txt", "./test.zip")
err := fileutil.Zip("./test.txt", "./test.zip")
if err != nil {
fmt.Println(err)
}
@@ -431,7 +459,7 @@ import (
)
func main() {
err := fileutil.Zip("./test.zip", "./unzip/test.txt")
err := fileutil.Zip("./test.zip", "./unzip/test.txt")
if err != nil {
fmt.Println(err)
}

View File

@@ -21,6 +21,7 @@ import (
## 目录
- [ClearFile](#ClearFile)
- [CreateFile](#CreateFile)
- [CreateDir](#CreateDir)
- [CopyFile](#CopyFile)
- [FileMode](#FileMode)
- [MiMeType](#MiMeType)
@@ -92,6 +93,32 @@ func main() {
```
### <span id="CreateDir">CreateDir</span>
<p>使用绝对路径创建嵌套目录,例如/a/, /a/b/</p>
<b>函数签名:</b>
```go
func CreateDir(absPath string) error
```
<b>Example:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/fileutil"
)
func main() {
err := fileutil.CreateDir("/a/")
fmt.Println(err)
}
```
### <span id="CopyFile">CopyFile</span>
<p>拷贝文件,会覆盖原有的拷贝文件</p>
@@ -112,9 +139,9 @@ import (
func main() {
err := fileutil.CopyFile("./test.txt", "./test_copy.txt")
if err != nil {
fmt.Println(err)
}
if err != nil {
fmt.Println(err)
}
}
```
@@ -140,9 +167,9 @@ import (
func main() {
mode, err := fileutil.FileMode("./test.txt")
if err != nil {
fmt.Println(err)
}
if err != nil {
fmt.Println(err)
}
fmt.Println(mode)
}
```
@@ -310,8 +337,8 @@ import (
func main() {
err := fileutil.RemoveFile("./test.txt")
if err != nil {
fmt.Println(err)
}
fmt.Println(err)
}
}
```
@@ -337,12 +364,12 @@ import (
func main() {
path := "./test.txt"
fileutil.CreateFile(path)
fileutil.CreateFile(path)
f, _ := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, 0777)
f.WriteString("hello world")
f, _ := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, 0777)
f.WriteString("hello world")
content, _ := fileutil.ReadFileToString(path)
content, _ := fileutil.ReadFileToString(path)
fmt.Println(content) //hello world
}
```
@@ -370,13 +397,13 @@ import (
func main() {
path := "./text.txt"
fileutil.CreateFile(path)
fileutil.CreateFile(path)
f, _ := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, 0777)
defer f.Close()
f.WriteString("hello\nworld")
f, _ := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, 0777)
defer f.Close()
f.WriteString("hello\nworld")
contents, _ := fileutil.ReadFileByLine(path)
contents, _ := fileutil.ReadFileByLine(path)
fmt.Println(contents) //[]string{"hello", "world"}
}
```
@@ -402,7 +429,7 @@ import (
)
func main() {
err := fileutil.Zip("./test.txt", "./test.zip")
err := fileutil.Zip("./test.txt", "./test.zip")
if err != nil {
fmt.Println(err)
}
@@ -431,7 +458,7 @@ import (
)
func main() {
err := fileutil.Zip("./test.zip", "./unzip/test.txt")
err := fileutil.Zip("./test.zip", "./unzip/test.txt")
if err != nil {
fmt.Println(err)
}

View File

@@ -26,6 +26,7 @@ import (
- [Compose](#Compose)
- [Debounced](#Debounced)
- [Delay](#Delay)
- [Pipeline](#Pipeline)
- [Watcher](#Watcher)
<div STYLE="page-break-after: always;"></div>
@@ -101,8 +102,6 @@ import (
)
func main() {
assert := internal.NewAssert(t, "TestBefore")
arr := []string{"a", "b", "c", "d", "e"}
f := function.Before(3, func(i int) int {
return i
@@ -120,7 +119,7 @@ func main() {
}
expected := []int64{0, 1, 2, 2, 2}
assert.Equal(expected, res)
fmt.Println(res) // 0, 1, 2, 2, 2
}
```
@@ -302,6 +301,43 @@ func main() {
```
### <span id="Pipeline">Pipeline</span>
<p>Pipeline takes a list of functions and returns a function whose param will be passed into
the functions one by one.</p>
<b>Signature:</b>
```go
func Pipeline[T any](funcs ...func(T) T) func(T) T
```
<b>Example:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/function"
)
func main() {
addOne := func(x int) int {
return x + 1
}
double := func(x int) int {
return 2 * x
}
square := func(x int) int {
return x * x
}
f := Pipeline(addOne, double, square)
fmt.Println(f(2)) //36
}
```
### <span id="Watcher">Watcher</span>

View File

@@ -26,6 +26,7 @@ import (
- [Compose](#Compose)
- [Debounced](#Debounced)
- [Delay](#Delay)
- [Pipeline](#Pipeline)
- [Watcher](#Watcher)
<div STYLE="page-break-after: always;"></div>
@@ -101,8 +102,6 @@ import (
)
func main() {
assert := internal.NewAssert(t, "TestBefore")
arr := []string{"a", "b", "c", "d", "e"}
f := function.Before(3, func(i int) int {
return i
@@ -119,8 +118,7 @@ func main() {
appendStr(i, arr[i], f)
}
expected := []int64{0, 1, 2, 2, 2}
assert.Equal(expected, res)
fmt.Println(res) // 0, 1, 2, 2, 2
}
```
@@ -297,7 +295,45 @@ func main() {
time.Sleep(5 * time.Second)
close(stop)
fmt.Println(res) //[* * * * *]
fmt.Println(res) //[* * * * *]
}
```
### <span id="Pipeline">Pipeline</span>
<p>执行函数pipeline.</p>
<b>函数签名:</b>
```go
func Pipeline[T any](funcs ...func(T) T) func(T) T
```
<b>例子:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/function"
)
func main() {
addOne := func(x int) int {
return x + 1
}
double := func(x int) int {
return 2 * x
}
square := func(x int) int {
return x * x
}
f := Pipeline(addOne, double, square)
fmt.Println(f(2)) //36
}
```

352
docs/maputil.md Normal file
View File

@@ -0,0 +1,352 @@
# Maputil
Package maputil includes some functions to manipulate map.
<div STYLE="page-break-after: always;"></div>
## Source:
- [https://github.com/duke-git/lancet/blob/main/maputil/map.go](https://github.com/duke-git/lancet/blob/main/maputil/map.go)
<div STYLE="page-break-after: always;"></div>
## Example:
```go
import (
"github.com/duke-git/lancet/v2/maputil"
)
```
<div STYLE="page-break-after: always;"></div>
## Index
- [ForEach](#ForEach)
- [Filter](#Filter)
- [Intersect](#Intersect)
- [Keys](#Keys)
- [Merge](#Merge)
- [Minus](#Minus)
- [Values](#Values)
- [IsDisjoint](#IsDisjoint)
<div STYLE="page-break-after: always;"></div>
## Documentation
### <span id="ForEach">ForEach</span>
<p>Executes iteratee funcation for every key and value pair in map.</p>
<b>Signature:</b>
```go
func ForEach[K comparable, V any](m map[K]V, iteratee func(key K, value V))
```
<b>Example:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/maputil"
)
func main() {
m := map[string]int{
"a": 1,
"b": 2,
"c": 3,
"d": 4,
}
var sum int
maputil.ForEach(m, func(_ string, value int) {
sum += value
})
fmt.Println(sum) // 10
}
```
### <span id="Filter">Filter</span>
<p>Iterates over map, return a new map contains all key and value pairs pass the predicate function.</p>
<b>Signature:</b>
```go
func Filter[K comparable, V any](m map[K]V, predicate func(key K, value V) bool) map[K]V
```
<b>Example:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/maputil"
)
func main() {
m := map[string]int{
"a": 1,
"b": 2,
"c": 3,
"d": 4,
"e": 5,
}
isEven := func(_ string, value int) bool {
return value%2 == 0
}
maputil.Filter(m, func(_ string, value int) {
sum += value
})
res := maputil.Filter(m, isEven)
fmt.Println(res) // map[string]int{"b": 2, "d": 4,}
}
```
### <span id="Intersect">Intersect</span>
<p>Iterates over maps, return a new map of key and value pairs in all given maps.</p>
<b>Signature:</b>
```go
func Intersect[K comparable, V any](maps ...map[K]V) map[K]V
```
<b>Example:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/maputil"
)
func main() {
m1 := map[string]int{
"a": 1,
"b": 2,
"c": 3,
}
m2 := map[string]int{
"a": 1,
"b": 2,
"c": 6,
"d": 7,
}
m3 := map[string]int{
"a": 1,
"b": 9,
"e": 9,
}
fmt.Println(maputil.Intersect(m1)) // map[string]int{"a": 1, "b": 2, "c": 3}
fmt.Println(maputil.Intersect(m1, m2)) // map[string]int{"a": 1, "b": 2}
fmt.Println(maputil.Intersect(m1, m2, m3)) // map[string]int{"a": 1}
}
```
### <span id="Keys">Keys</span>
<p>Returns a slice of the map's keys.</p>
<b>Signature:</b>
```go
func Keys[K comparable, V any](m map[K]V) []K
```
<b>Example:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/maputil"
)
func main() {
m := map[int]string{
1: "a",
2: "a",
3: "b",
4: "c",
5: "d",
}
keys := maputil.Keys(m)
sort.Ints(keys)
fmt.Println(keys) // []int{1, 2, 3, 4, 5}
}
```
### <span id="Merge">Merge</span>
<p>Merge maps, next key will overwrite previous key.</p>
<b>Signature:</b>
```go
func Merge[K comparable, V any](maps ...map[K]V) map[K]V
```
<b>Example:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/maputil"
)
func main() {
m1 := map[int]string{
1: "a",
2: "b",
}
m2 := map[int]string{
1: "1",
3: "2",
}
fmt.Println(maputil.Merge(m1, m2)) // map[int]string{1:"1", 2:"b", 3:"2",}
}
```
### <span id="Minus">Minus</span>
<p>Creates an map of whose key in mapA but not in mapB.</p>
<b>Signature:</b>
```go
func Minus[K comparable, V any](mapA, mapB map[K]V) map[K]V
```
<b>Example:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/maputil"
)
func main() {
m1 := map[string]int{
"a": 1,
"b": 2,
"c": 3,
}
m2 := map[string]int{
"a": 11,
"b": 22,
"d": 33,
}
fmt.Println(maputil.Minus(m1, m2)) //map[string]int{"c": 3}
}
```
### <span id="Values">Values</span>
<p>Returns a slice of the map's values.</p>
<b>Signature:</b>
```go
func Values[K comparable, V any](m map[K]V) []V
```
<b>Example:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/maputil"
)
func main() {
m := map[int]string{
1: "a",
2: "a",
3: "b",
4: "c",
5: "d",
}
values := maputil.Values(m)
sort.Strings(values)
fmt.Println(values) // []string{"a", "a", "b", "c", "d"}
}
```
### <span id="IsDisjoint">IsDisjoint</span>
<p>Checks two maps are disjoint if they have no keys in common</p>
<b>Signature:</b>
```go
func IsDisjoint[K comparable, V any](mapA, mapB map[K]V) bool
```
<b>Example:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/maputil"
)
func main() {
m1 := map[int]string{
1: "a",
2: "a",
3: "b",
4: "c",
5: "d",
}
m2 := map[int]string{
1: "a",
2: "a",
3: "b",
4: "c",
5: "d",
}
m3 := map[int]string{
6: "a",
}
ok := maputil.IsDisjoint(m2, m1)
fmt.Println(ok) // false
ok = maputil.IsDisjoint(m2, m3)
fmt.Println(ok) // true
}
```

352
docs/maputil_zh-CN.md Normal file
View File

@@ -0,0 +1,352 @@
# Maputil
maputil包包括一些操作map的函数。
<div STYLE="page-break-after: always;"></div>
## 源码:
- [https://github.com/duke-git/lancet/blob/main/maputil/map.go](https://github.com/duke-git/lancet/blob/main/maputil/map.go)
<div STYLE="page-break-after: always;"></div>
## 用法:
```go
import (
"github.com/duke-git/lancet/v2/maputil"
)
```
<div STYLE="page-break-after: always;"></div>
## 目录:
- [ForEach](#ForEach)
- [Filter](#Filter)
- [Intersect](#Intersect)
- [Keys](#Keys)
- [Merge](#Merge)
- [Minus](#Minus)
- [Values](#Values)
- [IsDisjoint](#IsDisjoint)
<div STYLE="page-break-after: always;"></div>
## API文档:
### <span id="ForEach">ForEach</span>
<p>对map中的每对key和value执行iteratee函数</p>
<b>函数签名:</b>
```go
func ForEach[K comparable, V any](m map[K]V, iteratee func(key K, value V))
```
<b>例子:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/maputil"
)
func main() {
m := map[string]int{
"a": 1,
"b": 2,
"c": 3,
"d": 4,
}
var sum int
maputil.ForEach(m, func(_ string, value int) {
sum += value
})
fmt.Println(sum) // 10
}
```
### <span id="Filter">Filter</span>
<p>迭代map中的每对key和value, 返回符合predicate函数的key, value</p>
<b>函数签名:</b>
```go
func Filter[K comparable, V any](m map[K]V, predicate func(key K, value V) bool) map[K]V
```
<b>例子:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/maputil"
)
func main() {
m := map[string]int{
"a": 1,
"b": 2,
"c": 3,
"d": 4,
"e": 5,
}
isEven := func(_ string, value int) bool {
return value%2 == 0
}
maputil.Filter(m, func(_ string, value int) {
sum += value
})
res := maputil.Filter(m, isEven)
fmt.Println(res) // map[string]int{"b": 2, "d": 4,}
}
```
### <span id="Intersect">Intersect</span>
<p>多个map的交集操作</p>
<b>函数签名:</b>
```go
func Intersect[K comparable, V any](maps ...map[K]V) map[K]V
```
<b>例子:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/maputil"
)
func main() {
m1 := map[string]int{
"a": 1,
"b": 2,
"c": 3,
}
m2 := map[string]int{
"a": 1,
"b": 2,
"c": 6,
"d": 7,
}
m3 := map[string]int{
"a": 1,
"b": 9,
"e": 9,
}
fmt.Println(maputil.Intersect(m1)) // map[string]int{"a": 1, "b": 2, "c": 3}
fmt.Println(maputil.Intersect(m1, m2)) // map[string]int{"a": 1, "b": 2}
fmt.Println(maputil.Intersect(m1, m2, m3)) // map[string]int{"a": 1}
}
```
### <span id="Keys">Keys</span>
<p>返回map中所有key的切片</p>
<b>函数签名:</b>
```go
func Keys[K comparable, V any](m map[K]V) []K
```
<b>例子:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/maputil"
)
func main() {
m := map[int]string{
1: "a",
2: "a",
3: "b",
4: "c",
5: "d",
}
keys := maputil.Keys(m)
sort.Ints(keys)
fmt.Println(keys) // []int{1, 2, 3, 4, 5}
}
```
### <span id="Merge">Merge</span>
<p>合并多个maps, 相同的key会被后来的key覆盖</p>
<b>函数签名:</b>
```go
func Merge[K comparable, V any](maps ...map[K]V) map[K]V
```
<b>例子:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/maputil"
)
func main() {
m1 := map[int]string{
1: "a",
2: "b",
}
m2 := map[int]string{
1: "1",
3: "2",
}
fmt.Println(maputil.Merge(m1, m2)) // map[int]string{1:"1", 2:"b", 3:"2",}
}
```
### <span id="Minus">Minus</span>
<p>返回一个map其中的key存在于mapA不存在于mapB.</p>
<b>函数签名:</b>
```go
func Minus[K comparable, V any](mapA, mapB map[K]V) map[K]V
```
<b>例子:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/maputil"
)
func main() {
m1 := map[string]int{
"a": 1,
"b": 2,
"c": 3,
}
m2 := map[string]int{
"a": 11,
"b": 22,
"d": 33,
}
fmt.Println(maputil.Minus(m1, m2)) //map[string]int{"c": 3}
}
```
### <span id="Values">Values</span>
<p>返回map中所有value的切片</p>
<b>函数签名:</b>
```go
func Values[K comparable, V any](m map[K]V) []V
```
<b>例子:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/maputil"
)
func main() {
m := map[int]string{
1: "a",
2: "a",
3: "b",
4: "c",
5: "d",
}
values := maputil.Values(m)
sort.Strings(values)
fmt.Println(values) // []string{"a", "a", "b", "c", "d"}
}
```
### <span id="IsDisjoint">IsDisjoint</span>
<p>验证两个map是否具有不同的key</p>
<b>函数签名:</b>
```go
func IsDisjoint[K comparable, V any](mapA, mapB map[K]V) bool
```
<b>例子:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/maputil"
)
func main() {
m1 := map[int]string{
1: "a",
2: "a",
3: "b",
4: "c",
5: "d",
}
m2 := map[int]string{
1: "a",
2: "a",
3: "b",
4: "c",
5: "d",
}
m3 := map[int]string{
6: "a",
}
ok := maputil.IsDisjoint(m2, m1)
fmt.Println(ok) // false
ok = maputil.IsDisjoint(m2, m3)
fmt.Println(ok) // true
}
```

View File

@@ -25,7 +25,9 @@ import (
- [Fibonacci](#Fibonacci)
- [Factorial](#Factorial)
- [Max](#Max)
- [MaxBy](#MaxBy)
- [Min](#Min)
- [MinBy](#MaxBy)
- [Percent](#Percent)
- [RoundToFloat](#RoundToFloat)
@@ -177,6 +179,45 @@ func main() {
### <span id="MaxBy">MaxBy</span>
<p>Return the maximum value of a slice using the given comparator function.</p>
<b>Signature:</b>
```go
func MaxBy[T any](slice []T, comparator func(T, T) bool) T
```
<b>Example:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/mathutil"
)
func main() {
res1 := mathutil.MaxBy([]string{"a", "ab", "abc"}, func(v1, v2 string) bool {
return len(v1) > len(v2)
})
fmt.Println(res1) //abc
res2 := mathutil.MaxBy([]string{"abd", "abc", "ab"}, func(v1, v2 string) bool {
return len(v1) > len(v2)
})
fmt.Println(res2) //abd
res3 := mathutil.MaxBy([]string{}, func(v1, v2 string) bool {
return len(v1) > len(v2)
})
fmt.Println(res3) //“”
}
```
### <span id="Min">Min</span>
<p>Return min value of numbers.</p>
@@ -204,6 +245,45 @@ func main() {
### <span id="MinBy">MinBy</span>
<p>Return the minimum value of a slice using the given comparator function.</p>
<b>Signature:</b>
```go
func MinBy[T any](slice []T, comparator func(T, T) bool) T
```
<b>Example:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/mathutil"
)
func main() {
res1 := mathutil.MinBy([]string{"a", "ab", "abc"}, func(v1, v2 string) bool {
return len(v1) < len(v2)
})
fmt.Println(res1) //a
res2 := mathutil.MinBy([]string{"ab", "ac", "abc"}, func(v1, v2 string) bool {
return len(v1) < len(v2)
})
fmt.Println(res2) //ab
res3 := mathutil.MinBy([]string{}, func(v1, v2 string) bool {
return len(v1) < len(v2)
})
fmt.Println(res3) //“”
}
```
### <span id="Percent">Percent</span>
<p>calculate the percentage of val to total, retain n decimal places.</p>

View File

@@ -25,7 +25,9 @@ import (
- [Fibonacci](#Fibonacci)
- [Factorial](#Factorial)
- [Max](#Max)
- [MaxBy](#MaxBy)
- [Min](#Min)
- [MinBy](#MaxBy)
- [Percent](#Percent)
- [RoundToFloat](#RoundToFloat)
@@ -174,6 +176,45 @@ func main() {
### <span id="MaxBy">MaxBy</span>
<p>使用给定的比较器函数返回切片的最大值</p>
<b>函数签名:</b>
```go
func MaxBy[T any](slice []T, comparator func(T, T) bool) T
```
<b>例子:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/mathutil"
)
func main() {
res1 := mathutil.MaxBy([]string{"a", "ab", "abc"}, func(v1, v2 string) bool {
return len(v1) > len(v2)
})
fmt.Println(res1) //abc
res2 := mathutil.MaxBy([]string{"abd", "abc", "ab"}, func(v1, v2 string) bool {
return len(v1) > len(v2)
})
fmt.Println(res2) //abd
res3 := mathutil.MaxBy([]string{}, func(v1, v2 string) bool {
return len(v1) > len(v2)
})
fmt.Println(res3) //“”
}
```
### <span id="Min">Min</span>
<p>返回参数中的最小数</p>
@@ -201,6 +242,45 @@ func main() {
### <span id="MinBy">MinBy</span>
<p>使用给定的比较器函数返回切片的最小值</p>
<b>函数签名:</b>
```go
func MinBy[T any](slice []T, comparator func(T, T) bool) T
```
<b>例子:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/mathutil"
)
func main() {
res1 := mathutil.MinBy([]string{"a", "ab", "abc"}, func(v1, v2 string) bool {
return len(v1) < len(v2)
})
fmt.Println(res1) //a
res2 := mathutil.MinBy([]string{"ab", "ac", "abc"}, func(v1, v2 string) bool {
return len(v1) < len(v2)
})
fmt.Println(res2) //ab
res3 := mathutil.MinBy([]string{}, func(v1, v2 string) bool {
return len(v1) < len(v2)
})
fmt.Println(res3) //“”
}
```
### <span id="Percent">Percent</span>
<p>计算百分比保留n位小数</p>

View File

@@ -7,6 +7,8 @@ Package netutil contains functions to get net information and send http request.
- [https://github.com/duke-git/lancet/blob/main/netutil/net.go](https://github.com/duke-git/lancet/blob/main/netutil/net.go)
- [https://github.com/duke-git/lancet/blob/main/netutil/http_client.go](https://github.com/duke-git/lancet/blob/main/netutil/http_client.go)
- [https://github.com/duke-git/lancet/blob/main/netutil/http.go](https://github.com/duke-git/lancet/blob/main/netutil/http.go)
<div STYLE="page-break-after: always;"></div>
@@ -22,17 +24,26 @@ import (
## Index
- [ConvertMapToQueryString](#ConvertMapToQueryString)
- [EncodeUrl](#EncodeUrl)
- [GetInternalIp](#GetInternalIp)
- [GetIps](#GetIps)
- [GetMacAddrs](#GetMacAddrs)
- [GetPublicIpInfo](#GetPublicIpInfo)
- [GetRequestPublicIp](#GetRequestPublicIp)
- [IsPublicIP](#IsPublicIP)
- [HttpGet](#HttpGet)
- [HttpDelete](#HttpDelete)
- [HttpPost](#HttpPost)
- [HttpPut](#HttpPut)
- [IsInternalIP](#IsInternalIP)
- [HttpRequest](#HttpRequest)
- [HttpClient](#HttpClient)
- [SendRequest](#SendRequest)
- [DecodeResponse](#DecodeResponse)
- [StructToUrlValues](#StructToUrlValues)
- [HttpPatch](#HttpPatch)
- [HttpGet<sup>Deprecated</sup>](#HttpGet)
- [HttpDelete<sup>Deprecated</sup>](#HttpDelete)
- [HttpPost<sup>Deprecated</sup>](#HttpPost)
- [HttpPut<sup>Deprecated</sup>](#HttpPut)
- [HttpPatch<sup>Deprecated</sup>](#HttpPatch)
- [ParseHttpResponse](#ParseHttpResponse)
<div STYLE="page-break-after: always;"></div>
@@ -72,6 +83,36 @@ func main() {
### <span id="EncodeUrl">EncodeUrl</span>
<p>Encode url query string values.</p>
<b>Signature:</b>
```go
func EncodeUrl(urlStr string) (string, error)
```
<b>Example:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/netutil"
)
func main() {
urlAddr := "http://www.lancet.com?a=1&b=[2]"
encodedUrl, err := netutil.EncodeUrl(urlAddr)
if err != nil {
fmt.Println(err)
}
fmt.Println(encodedUrl) //http://www.lancet.com?a=1&b=%5B2%5D
}
```
### <span id="GetInternalIp">GetInternalIp</span>
<p>Get internal ip information.</p>
@@ -199,8 +240,51 @@ func main() {
### <span id="GetRequestPublicIp">GetRequestPublicIp</span>
<p>Get http request public ip.</p>
<b>Signature:</b>
```go
func GetRequestPublicIp(req *http.Request) string
```
<b>Example:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/netutil"
)
func main() {
ip := "36.112.24.10"
request1 := http.Request{
Method: "GET",
Header: http.Header{
"X-Forwarded-For": {ip},
},
}
publicIp1 := netutil.GetRequestPublicIp(&request1)
fmt.Println(publicIp1) //36.112.24.10
request2 := http.Request{
Method: "GET",
Header: http.Header{
"X-Real-Ip": {ip},
},
}
publicIp2 := netutil.GetRequestPublicIp(&request2)
fmt.Println(publicIp2) //36.112.24.10
}
```
### <span id="IsPublicIP">IsPublicIP</span>
<p>Checks if a ip is public or not.</p>
<p>Checks if an ip is public or not.</p>
<b>Signature:</b>
@@ -230,14 +314,268 @@ func main() {
### <span id="HttpGet">HttpGet</span>
### <span id="IsInternalIP">IsInternalIP</span>
<p>Checks if an ip is intranet or not.</p>
<b>Signature:</b>
```go
func IsInternalIP(IP net.IP) bool
```
<b>Example:</b>
```go
package main
import (
"fmt"
"net"
"github.com/duke-git/lancet/v2/netutil"
)
func main() {
ip1 := net.ParseIP("127.0.0.1")
ip2 := net.ParseIP("36.112.24.10")
fmt.Println(netutil.IsInternalIP(ip1)) //true
fmt.Println(netutil.IsInternalIP(ip2)) //false
}
```
### <span id="HttpRequest">HttpRequest</span>
<p>HttpRequest is a struct used to abstract HTTP request entity.</p>
<b>Signature:</b>
```go
type HttpRequest struct {
RawURL string
Method string
Headers http.Header
QueryParams url.Values
FormData url.Values
Body []byte
}
```
<b>Example:</b>
```go
package main
import (
"fmt"
"net"
"github.com/duke-git/lancet/v2/netutil"
)
func main() {
header := http.Header{}
header.Add("Content-Type", "multipart/form-data")
postData := url.Values{}
postData.Add("userId", "1")
postData.Add("title", "testItem")
request := &netutil.HttpRequest{
RawURL: "https://jsonplaceholder.typicode.com/todos",
Method: "POST",
Headers: header,
FormData: postData,
}
}
```
### <span id="HttpClient">HttpClient</span>
<p>HttpClient is a struct used to send HTTP request. It can be instanced with some configurations or none config.</p>
<b>Signature:</b>
```go
type HttpClient struct {
*http.Client
TLS *tls.Config
Request *http.Request
Config HttpClientConfig
}
type HttpClientConfig struct {
SSLEnabled bool
TLSConfig *tls.Config
Compressed bool
HandshakeTimeout time.Duration
ResponseTimeout time.Duration
Verbose bool
}
func NewHttpClient() *HttpClient
func NewHttpClientWithConfig(config *HttpClientConfig) *HttpClient
```
<b>Example:</b>
```go
package main
import (
"fmt"
"net"
"time"
"github.com/duke-git/lancet/v2/netutil"
)
func main() {
httpClientCfg := netutil.HttpClientConfig{
SSLEnabled: true,
HandshakeTimeout:10 * time.Second
}
httpClient := netutil.NewHttpClientWithConfig(&httpClientCfg)
}
```
### <span id="SendRequest">SendRequest</span>
<p>Use HttpClient to send HTTP request.</p>
<b>Signature:</b>
```go
func (client *HttpClient) SendRequest(request *HttpRequest) (*http.Response, error)
```
<b>Example:</b>
```go
package main
import (
"fmt"
"net"
"time"
"github.com/duke-git/lancet/v2/netutil"
)
func main() {
request := &netutil.HttpRequest{
RawURL: "https://jsonplaceholder.typicode.com/todos/1",
Method: "GET",
}
httpClient := netutil.NewHttpClient()
resp, err := httpClient.SendRequest(request)
if err != nil || resp.StatusCode != 200 {
log.Fatal(err)
}
type Todo struct {
UserId int `json:"userId"`
Id int `json:"id"`
Title string `json:"title"`
Completed bool `json:"completed"`
}
var todo Todo
httpClient.DecodeResponse(resp, &todo)
fmt.Println(todo.Id) //1
}
```
### <span id="DecodeResponse">DecodeResponse</span>
<p>Decode http response into target object.</p>
<b>Signature:</b>
```go
func (client *HttpClient) DecodeResponse(resp *http.Response, target any) error
```
<b>Example:</b>
```go
package main
import (
"fmt"
"net"
"time"
"github.com/duke-git/lancet/v2/netutil"
)
func main() {
request := &netutil.HttpRequest{
RawURL: "https://jsonplaceholder.typicode.com/todos/1",
Method: "GET",
}
httpClient := netutil.NewHttpClient()
resp, err := httpClient.SendRequest(request)
if err != nil || resp.StatusCode != 200 {
log.Fatal(err)
}
type Todo struct {
UserId int `json:"userId"`
Id int `json:"id"`
Title string `json:"title"`
Completed bool `json:"completed"`
}
var todo Todo
httpClient.DecodeResponse(resp, &todo)
fmt.Println(todo.Id) //1
}
```
### <span id="StructToUrlValues">StructToUrlValues</span>
<p>Convert struct to url values, only convert the field which is exported and has `json` tag.</p>
<b>Signature:</b>
```go
func StructToUrlValues(targetStruct any) url.Values
```
<b>Example:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/netutil"
)
func main() {
type TodoQuery struct {
Id int `json:"id"`
UserId int `json:"userId"`
}
todoQuery := TodoQuery{
Id: 1,
UserId: 2,
}
todoValues := netutil.StructToUrlValues(todoQuery)
fmt.Println(todoValues.Get("id")) //1
fmt.Println(todoValues.Get("userId")) //2
}
```
### <span id="HttpGet">HttpGet (Deprecated: use SendRequest for replacement)</span>
<p>Send http get request.</p>
<b>Signature:</b>
```go
// params[0] is header which type should be http.Header or map[string]string,
// params[1] is query param which type should be url.Values or map[string]any,
// params[1] is query param which type should be url.Values or map[string]string,
// params[2] is post body which type should be []byte.
// params[3] is http client which type should be http.Client.
func HttpGet(url string, params ...any) (*http.Response, error)
@@ -272,14 +610,14 @@ func main() {
### <span id="HttpPost">HttpPost</span>
### <span id="HttpPost">HttpPost (Deprecated: use SendRequest for replacement)</span>
<p>Send http post request.</p>
<b>Signature:</b>
```go
// params[0] is header which type should be http.Header or map[string]string,
// params[1] is query param which type should be url.Values or map[string]any,
// params[1] is query param which type should be url.Values or map[string]string,
// params[2] is post body which type should be []byte.
// params[3] is http client which type should be http.Client.
func HttpPost(url string, params ...any) (*http.Response, error)
@@ -321,14 +659,14 @@ func main() {
### <span id="HttpPut">HttpPut</span>
### <span id="HttpPut">HttpPut (Deprecated: use SendRequest for replacement)</span>
<p>Send http put request.</p>
<b>Signature:</b>
```go
// params[0] is header which type should be http.Header or map[string]string,
// params[1] is query param which type should be url.Values or map[string]any,
// params[1] is query param which type should be url.Values or map[string]string,
// params[2] is post body which type should be []byte.
// params[3] is http client which type should be http.Client.
func HttpPut(url string, params ...any) (*http.Response, error)
@@ -371,14 +709,14 @@ func main() {
### <span id="HttpDelete">HttpDelete</span>
### <span id="HttpDelete">HttpDelete (Deprecated: use SendRequest for replacement)</span>
<p>Send http delete request.</p>
<b>Signature:</b>
```go
// params[0] is header which type should be http.Header or map[string]string,
// params[1] is query param which type should be url.Values or map[string]any,
// params[1] is query param which type should be url.Values or map[string]string,
// params[2] is post body which type should be []byte.
// params[3] is http client which type should be http.Client.
func HttpDelete(url string, params ...any) (*http.Response, error)
@@ -410,14 +748,14 @@ func main() {
### <span id="HttpPatch">HttpPatch</span>
### <span id="HttpPatch">HttpPatch (Deprecated: use SendRequest for replacement)</span>
<p>Send http patch request.</p>
<b>Signature:</b>
```go
// params[0] is header which type should be http.Header or map[string]string,
// params[1] is query param which type should be url.Values or map[string]any,
// params[1] is query param which type should be url.Values or map[string]string,
// params[2] is post body which type should be []byte.
// params[3] is http client which type should be http.Client.
func HttpPatch(url string, params ...any) (*http.Response, error)

View File

@@ -6,6 +6,9 @@ netutil网络包支持获取ip地址发送http请求。
## 源码:
- [https://github.com/duke-git/lancet/blob/main/netutil/net.go](https://github.com/duke-git/lancet/blob/main/netutil/net.go)
- [https://github.com/duke-git/lancet/blob/main/netutil/http_client.go](https://github.com/duke-git/lancet/blob/main/netutil/http_client.go)
- [https://github.com/duke-git/lancet/blob/main/netutil/http.go](https://github.com/duke-git/lancet/blob/main/netutil/http.go)
<div STYLE="page-break-after: always;"></div>
@@ -21,17 +24,27 @@ import (
## 目录
- [ConvertMapToQueryString](#ConvertMapToQueryString)
- [EncodeUrl](#EncodeUrl)
- [GetInternalIp](#GetInternalIp)
- [GetIps](#GetIps)
- [GetMacAddrs](#GetMacAddrs)
- [GetPublicIpInfo](#GetPublicIpInfo)
- [IsPublicIP](#IsPublicIP)
- [HttpGet](#HttpGet)
- [HttpDelete](#HttpDelete)
- [HttpPost](#HttpPost)
- [HttpPut](#HttpPut)
- [GetRequestPublicIp](#GetRequestPublicIp)
- [IsPublicIP](#IsPublicIP)
- [IsInternalIP](#IsInternalIP)
- [HttpRequest](#HttpRequest)
- [HttpClient](#HttpClient)
- [SendRequest](#SendRequest)
- [DecodeResponse](#DecodeResponse)
- [StructToUrlValues](#StructToUrlValues)
- [HttpGet<sup>Deprecated</sup>](#HttpGet)
- [HttpDelete<sup>Deprecated</sup>](#HttpDelete)
- [HttpPost<sup>Deprecated</sup>](#HttpPost)
- [HttpPut<sup>Deprecated</sup>](#HttpPut)
- [HttpPatch<sup>Deprecated</sup>](#HttpPatch)
- [HttpPatch](#HttpPatch)
- [ParseHttpResponse](#ParseHttpResponse)
<div STYLE="page-break-after: always;"></div>
@@ -71,6 +84,37 @@ func main() {
### <span id="EncodeUrl">EncodeUrl</span>
<p>编码url query string的值</p>
<b>函数签名:</b>
```go
func EncodeUrl(urlStr string) (string, error)
```
<b>例子:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/netutil"
)
func main() {
urlAddr := "http://www.lancet.com?a=1&b=[2]"
encodedUrl, err := netutil.EncodeUrl(urlAddr)
if err != nil {
fmt.Println(err)
}
fmt.Println(encodedUrl) //http://www.lancet.com?a=1&b=%5B2%5D
}
```
### <span id="GetInternalIp">GetInternalIp</span>
<p>获取内部ip</p>
@@ -197,6 +241,50 @@ func main() {
### <span id="GetRequestPublicIp">GetRequestPublicIp</span>
<p>获取http请求ip</p>
<b>函数签名:</b>
```go
func GetRequestPublicIp(req *http.Request) string
```
<b>例子:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/netutil"
)
func main() {
ip := "36.112.24.10"
request1 := http.Request{
Method: "GET",
Header: http.Header{
"X-Forwarded-For": {ip},
},
}
publicIp1 := netutil.GetRequestPublicIp(&request1)
fmt.Println(publicIp1) //36.112.24.10
request2 := http.Request{
Method: "GET",
Header: http.Header{
"X-Real-Ip": {ip},
},
}
publicIp2 := netutil.GetRequestPublicIp(&request2)
fmt.Println(publicIp2) //36.112.24.10
}
```
### <span id="IsPublicIP">IsPublicIP</span>
<p>判断ip是否是公共ip</p>
@@ -227,15 +315,268 @@ func main() {
### <span id="IsInternalIP">IsInternalIP</span>
<p>判断ip是否是局域网ip.</p>
### <span id="HttpGet">HttpGet</span>
<b>函数签名:</b>
```go
func IsInternalIP(IP net.IP) bool
```
<b>例子:</b>
```go
package main
import (
"fmt"
"net"
"github.com/duke-git/lancet/v2/netutil"
)
func main() {
ip1 := net.ParseIP("127.0.0.1")
ip2 := net.ParseIP("36.112.24.10")
fmt.Println(netutil.IsInternalIP(ip1)) //true
fmt.Println(netutil.IsInternalIP(ip2)) //false
}
```
### <span id="HttpRequest">HttpRequest</span>
<p>HttpRequest用于抽象HTTP请求实体的结构</p>
<b>函数签名:</b>
```go
type HttpRequest struct {
RawURL string
Method string
Headers http.Header
QueryParams url.Values
FormData url.Values
Body []byte
}
```
<b>例子:</b>
```go
package main
import (
"fmt"
"net"
"github.com/duke-git/lancet/v2/netutil"
)
func main() {
header := http.Header{}
header.Add("Content-Type", "multipart/form-data")
postData := url.Values{}
postData.Add("userId", "1")
postData.Add("title", "testItem")
request := &netutil.HttpRequest{
RawURL: "https://jsonplaceholder.typicode.com/todos",
Method: "POST",
Headers: header,
FormData: postData,
}
}
```
### <span id="HttpClient">HttpClient</span>
<p>HttpClient是用于发送HTTP请求的结构体。它可以用一些配置参数或无配置实例化.</p>
<b>函数签名:</b>
```go
type HttpClient struct {
*http.Client
TLS *tls.Config
Request *http.Request
Config HttpClientConfig
}
type HttpClientConfig struct {
SSLEnabled bool
TLSConfig *tls.Config
Compressed bool
HandshakeTimeout time.Duration
ResponseTimeout time.Duration
Verbose bool
}
func NewHttpClient() *HttpClient
func NewHttpClientWithConfig(config *HttpClientConfig) *HttpClient
```
<b>例子:</b>
```go
package main
import (
"fmt"
"net"
"time"
"github.com/duke-git/lancet/v2/netutil"
)
func main() {
httpClientCfg := netutil.HttpClientConfig{
SSLEnabled: true,
HandshakeTimeout:10 * time.Second
}
httpClient := netutil.NewHttpClientWithConfig(&httpClientCfg)
}
```
### <span id="SendRequest">SendRequest</span>
<p>HttpClient发送http请求</p>
<b>函数签名:</b>
```go
func (client *HttpClient) SendRequest(request *HttpRequest) (*http.Response, error)
```
<b>例子:</b>
```go
package main
import (
"fmt"
"net"
"time"
"github.com/duke-git/lancet/v2/netutil"
)
func main() {
request := &netutil.HttpRequest{
RawURL: "https://jsonplaceholder.typicode.com/todos/1",
Method: "GET",
}
httpClient := netutil.NewHttpClient()
resp, err := httpClient.SendRequest(request)
if err != nil || resp.StatusCode != 200 {
log.Fatal(err)
}
type Todo struct {
UserId int `json:"userId"`
Id int `json:"id"`
Title string `json:"title"`
Completed bool `json:"completed"`
}
var todo Todo
httpClient.DecodeResponse(resp, &todo)
fmt.Println(todo.Id) //1
}
```
### <span id="DecodeResponse">DecodeResponse</span>
<p>解析http响应体到目标结构体</p>
<b>函数签名:</b>
```go
func (client *HttpClient) DecodeResponse(resp *http.Response, target any) error
```
<b>例子:</b>
```go
package main
import (
"fmt"
"net"
"time"
"github.com/duke-git/lancet/v2/netutil"
)
func main() {
request := &netutil.HttpRequest{
RawURL: "https://jsonplaceholder.typicode.com/todos/1",
Method: "GET",
}
httpClient := netutil.NewHttpClient()
resp, err := httpClient.SendRequest(request)
if err != nil || resp.StatusCode != 200 {
log.Fatal(err)
}
type Todo struct {
UserId int `json:"userId"`
Id int `json:"id"`
Title string `json:"title"`
Completed bool `json:"completed"`
}
var todo Todo
httpClient.DecodeResponse(resp, &todo)
fmt.Println(todo.Id) //1
}
```
### <span id="StructToUrlValues">StructToUrlValues</span>
<p>将结构体转为url values, 仅转化结构体导出字段并且包含`json` tag.</p>
<b>函数签名:</b>
```go
func StructToUrlValues(targetStruct any) url.Values
```
<b>例子:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/netutil"
)
func main() {
type TodoQuery struct {
Id int `json:"id"`
UserId int `json:"userId"`
}
todoQuery := TodoQuery{
Id: 1,
UserId: 2,
}
todoValues := netutil.StructToUrlValues(todoQuery)
fmt.Println(todoValues.Get("id")) //1
fmt.Println(todoValues.Get("userId")) //2
}
```
### <span id="HttpGet">HttpGet (Deprecated: use SendRequest for replacement)</span>
<p>发送http get请求</p>
<b>函数签名:</b>
```go
// params[0] http请求header类型必须是http.Header或者map[string]string
// params[1] http查询字符串类型必须是url.Values或者map[string]any
// params[1] http查询字符串类型必须是url.Values或者map[string]string
// params[2] post请求体类型必须是[]byte
// params[3] http client类型必须是http.Client
func HttpGet(url string, params ...any) (*http.Response, error)
@@ -270,14 +611,14 @@ func main() {
### <span id="HttpPost">HttpPost</span>
### <span id="HttpPost">HttpPost (Deprecated: use SendRequest for replacement)</span>
<p>发送http post请求</p>
<b>函数签名:</b>
```go
// params[0] http请求header类型必须是http.Header或者map[string]string
// params[1] http查询字符串类型必须是url.Values或者map[string]any
// params[1] http查询字符串类型必须是url.Values或者map[string]string
// params[2] post请求体类型必须是[]byte
// params[3] http client类型必须是http.Client
func HttpPost(url string, params ...any) (*http.Response, error)
@@ -319,19 +660,19 @@ func main() {
### <span id="HttpPut">HttpPut</span>
### <span id="HttpPut">HttpPut (Deprecated: use SendRequest for replacement)</span>
<p>发送http put请求</p>
<b>函数签名:</b>
```go
// params[0] http请求header类型必须是http.Header或者map[string]string
// params[1] http查询字符串类型必须是url.Values或者map[string]any
// params[1] http查询字符串类型必须是url.Values或者map[string]string
// params[2] post请求体类型必须是[]byte
// params[3] http client类型必须是http.Client
func HttpPut(url string, params ...any) (*http.Response, error)
```
<b>Example:</b>
<b>例子:</b>
```go
package main
@@ -369,14 +710,14 @@ func main() {
### <span id="HttpDelete">HttpDelete</span>
### <span id="HttpDelete">HttpDelete (Deprecated: use SendRequest for replacement)</span>
<p>发送http delete请求</p>
<b>函数签名:</b>
```go
// params[0] http请求header类型必须是http.Header或者map[string]string
// params[1] http查询字符串类型必须是url.Values或者map[string]any
// params[1] http查询字符串类型必须是url.Values或者map[string]string
// params[2] post请求体类型必须是[]byte
// params[3] http client类型必须是http.Client
func HttpDelete(url string, params ...any) (*http.Response, error)
@@ -408,14 +749,14 @@ func main() {
### <span id="HttpPatch">HttpPatch</span>
### <span id="HttpPatch">HttpPatch (Deprecated: use SendRequest for replacement)</span>
<p>发送http patch请求</p>
<b>函数签名:</b>
```go
// params[0] http请求header类型必须是http.Header或者map[string]string
// params[1] http查询字符串类型必须是url.Values或者map[string]any
// params[1] http查询字符串类型必须是url.Values或者map[string]string
// params[2] post请求体类型必须是[]byte
// params[3] http client类型必须是http.Client
func HttpPatch(url string, params ...any) (*http.Response, error)

View File

@@ -97,8 +97,8 @@ import (
func main() {
var number int
var increaseNumber retry.RetryFunc
increaseNumber = func() error {
increaseNumber := func() error {
number++
if number == 3 {
return nil
@@ -139,6 +139,7 @@ import (
func main() {
var number int
increaseNumber := func() error {
number++
if number == 3 {

View File

@@ -99,8 +99,7 @@ import (
func main() {
var number int
var increaseNumber retry.RetryFunc
increaseNumber = func() error {
increaseNumber := func() error {
number++
if number == 3 {
return nil

View File

@@ -20,6 +20,7 @@ import (
<div STYLE="page-break-after: always;"></div>
## Index
- [AppendIfAbsent](#AppendIfAbsent)
- [Contain](#Contain)
- [ContainSubSlice](#ContainSubSlice)
- [Chunk](#Chunk)
@@ -31,10 +32,13 @@ import (
- [DifferenceWith](#DifferenceWith)
- [DeleteAt](#DeleteAt)
- [Drop](#Drop)
- [Equal](#Equal)
- [EqualWith](#EqualWith)
- [Every](#Every)
- [Filter](#Filter)
- [Find](#Find)
- [FindLast](#FindLast)
- [Flatten](#Flatten)
- [FlattenDeep](#FlattenDeep)
- [ForEach](#ForEach)
@@ -44,14 +48,22 @@ import (
- [InterfaceSlice](#InterfaceSlice)
- [Intersection](#Intersection)
- [InsertAt](#InsertAt)
- [IndexOf](#IndexOf)
- [LastIndexOf](#LastIndexOf)
- [Map](#Map)
- [Reverse](#Reverse)
- [Reduce](#Reduce)
- [Replace](#Replace)
- [ReplaceAll](#ReplaceAll)
- [Shuffle](#Shuffle)
- [SortByField](#SortByField)
- [Some](#Some)
- [StringSlice](#StringSlice)
- [SymmetricDifference](#SymmetricDifference)
- [ToSlice](#ToSlice)
- [ToSlicePointer](#ToSlicePointer)
- [Unique](#Unique)
- [UniqueBy](#UniqueBy)
- [Union](#Union)
- [UpdateAt](#UpdateAt)
- [Without](#Without)
@@ -60,13 +72,40 @@ import (
## Documentation
### <span id="AppendIfAbsent">AppendIfAbsent</span>
<p>If slice doesn't contain the value, append it to the slice.</p>
<b>Signature:</b>
```go
func AppendIfAbsent[T comparable](slice []T, value T) []T
```
<b>Example:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
strs := []string{"a", "b"}
res1 := slice.AppendIfAbsent(strs, "a")
fmt.Println(res1) //[]string{"a", "b"}
res2 := slice.AppendIfAbsent(strs, "cannot")
fmt.Println(res2"}
}
```
### <span id="Contain">Contain</span>
<p>Check if the value is in the slice or not.</p>
<b>Signature:</b>
```go
func Contain[T any](slice []T, value T) bool
func Contain[T comparable](slice []T, value T) bool
```
<b>Example:</b>
@@ -89,7 +128,7 @@ func main() {
<b>Signature:</b>
```go
func ContainSubSlice[T any](slice, subslice []T) bool
func ContainSubSlice[T comparable](slice, subslice []T) bool
```
<b>Example:</b>
@@ -247,7 +286,7 @@ func main() {
<b>Signature:</b>
```go
func DifferenceBy[T any](slice []T, comparedSlice []T, iteratee func(index int, t T) T) []T
func DifferenceBy[T comparable](slice []T, comparedSlice []T, iteratee func(index int, item T) T) []T
```
<b>Example:</b>
@@ -357,6 +396,68 @@ func main() {
### <span id="Equal">Equal</span>
<p>Check if two slices are equal: the same length and all elements' order and value are equal.</p>
<b>Signature:</b>
```go
func Equal[T comparable](slice1, slice2 []T) bool
```
<b>Example:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
slice1 := []int{1, 2, 3}
slice2 := []int{1, 2, 3}
slice3 := []int{3, 2, 1}
res1 := slice.Equal(slice1, slice2)
res2 := slice.Equal(slice1, slice3)
fmt.Println(res1) //true
fmt.Println(res2) //false
}
```
### <span id="EqualWith">EqualWith</span>
<p>Check if two slices are equal with comparator func.</p>
<b>Signature:</b>
```go
func EqualWith[T, U any](slice1 []T, slice2 []U, comparator func(T, U) bool) bool
```
<b>Example:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
slice1 := []int{1, 2, 3}
slice2 := []int{2, 4, 6}
isDouble := func(a, b int) bool {
return b == a*2
}
res := slice.EqualWith(slice1, slice2, isDouble)
fmt.Println(res) //true
}
```
### <span id="Every">Every</span>
<p>Return true if all of the values in the slice pass the predicate function.</p>
@@ -364,7 +465,7 @@ func main() {
<b>Signature:</b>
```go
func Every[T any](slice []T, predicate func(index int, t T) bool) bool
func Every[T any](slice []T, predicate func(index int, item T) bool) bool
```
<b>Example:</b>
@@ -394,7 +495,7 @@ func main() {
<b>Signature:</b>
```go
func Filter[T any](slice []T, predicate func(index int, t T) bool) []T
func Filter[T any](slice []T, predicate func(index int, item T) bool) []T
```
<b>Example:</b>
@@ -423,7 +524,7 @@ func main() {
<b>Signature:</b>
```go
func Find[T any](slice []T, predicate func(index int, t T) bool) (*T, bool)
func Find[T any](slice []T, predicate func(index int, item T) bool) (*T, bool)
```
<b>Example:</b>
@@ -454,7 +555,7 @@ func main() {
<b>Signature:</b>
```go
func FindLast[T any](slice []T, predicate func(index int, t T) bool) (*T, bool)
func FindLast[T any](slice []T, predicate func(index int, item T) bool) (*T, bool)
```
<b>Example:</b>
@@ -478,6 +579,31 @@ func main() {
### <span id="Flatten">Flatten</span>
<p>Flatten slice with one level.</p>
<b>Signature:</b>
```go
func Flatten(slice any) any
```
<b>Example:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
arr := [][][]string{{{"a", "b"}}, {{"c", "d"}}}
res := slice.Flatten(arr)
fmt.Println(res) //{{"a", "b"}, {"c", "d"}}
}
```
### <span id="FlattenDeep">FlattenDeep</span>
<p>flattens slice recursive.</p>
@@ -497,7 +623,7 @@ import (
func main() {
arr := [][][]string{{{"a", "b"}}, {{"c", "d"}}}
res := slice.FlattenDeep(arr)
fmt.Println(res) //[]string{"a", "b", "c", "d"}
fmt.Println(res) //{"a", "b", "c", "d"}
}
```
@@ -511,7 +637,7 @@ func main() {
<b>Signature:</b>
```go
func ForEach[T any](slice []T, iteratee func(index int, t T))
func ForEach[T any](slice []T, iteratee func(index int, item T))
```
<b>Example:</b>
@@ -540,7 +666,7 @@ func main() {
<b>Signature:</b>
```go
func GroupBy[T any](slice []T, groupFn func(index int, t T) bool) ([]T, []T)
func GroupBy[T any](slice []T, groupFn func(index int, item T) bool) ([]T, []T)
```
<b>Example:</b>
@@ -651,7 +777,7 @@ func main() {
<b>Signature:</b>
```go
func Intersection[T any](slices ...[]T) []T
func Intersection[T comparable](slices ...[]T) []T
```
<b>Example:</b>
@@ -703,13 +829,70 @@ func main() {
### <span id="IndexOf">IndexOf</span>
<p>Returns the index at which the first occurrence of a value is found in a slice or return -1 if the value cannot be found.</p>
<b>Signature:</b>
```go
func IndexOf[T comparable](slice []T, value T) int
```
<b>Example:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
arr := []string{"a", "a", "b", "c"}
res1 := slice.IndexOf(arr, "a")
fmt.Println(res1) //0
res2 := slice.IndexOf(arr, "d")
fmt.Println(res2) //-1
}
```
### <span id="LastIndexOf">LastIndexOf</span>
<p>Returns the index at which the last occurrence of a value is found in a slice or return -1 if the value cannot be found.</p>
<b>Signature:</b>
```go
func LastIndexOf[T comparable](slice []T, value T) int
```
<b>Example:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
arr := []string{"a", "a", "b", "c"}
res1 := slice.LastIndexOf(arr, "a")
fmt.Println(res1) //1
res2 := slice.LastIndexOf(arr, "d")
fmt.Println(res2) //-1
}
```
### <span id="Map">Map</span>
<p>Creates an slice of values by running each element in slice thru function.</p>
<b>Signature:</b>
```go
func Map[T any, U any](slice []T, iteratee func(index int, t T) U) []U
func Map[T any, U any](slice []T, iteratee func(index int, item T) U) []U
```
<b>Example:</b>
@@ -763,7 +946,7 @@ func main() {
<b>Signature:</b>
```go
func Reduce[T any](slice []T, iteratee func(index int, t1, t2 T) T, initial T) T
func Reduce[T any](slice []T, iteratee func(index int, item1, item2 T) T, initial T) T
```
<b>Example:</b>
@@ -785,6 +968,60 @@ func main() {
### <span id="Replace">Replace</span>
<p>Returns a copy of the slice with the first n non-overlapping instances of old replaced by new.</p>
<b>Signature:</b>
```go
func Replace[T comparable](slice []T, old T, new T, n int) []T
```
<b>Example:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
strs := []string{"a", "b", "a", "c", "d", "a"}
fmt.Println(slice.Replace(strs, "a", "x", 0)) //{"a", "b", "a", "c", "d", "a"}
fmt.Println(slice.Replace(strs, "a", "x", 1)) //{"x", "b", "a", "c", "d", "a"}
fmt.Println(slice.Replace(strs, "a", "x", -1)) //{"x", "b", "x", "c", "d", "x"}
}
```
### <span id="ReplaceAll">ReplaceAll</span>
<p>Returns a copy of the slice with the first n non-overlapping instances of old replaced by new.</p>
<b>Signature:</b>
```go
func ReplaceAll[T comparable](slice []T, old T, new T) []T
```
<b>Example:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
strs := []string{"a", "b", "a", "c", "d", "a"}
fmt.Println(slice.ReplaceAll(strs, "a", "x")) //{"x", "b", "x", "c", "d", "x"}
fmt.Println(slice.Replace(strs, "e", "x")) //{"a", "b", "a", "c", "d", "a"}
}
```
### <span id="Shuffle">Shuffle</span>
<p>Creates an slice of shuffled values.</p>
@@ -860,7 +1097,7 @@ func main() {
<b>Signature:</b>
```go
func Some[T any](slice []T, predicate func(index int, t T) bool) bool
func Some[T any](slice []T, predicate func(index int, item T) bool) bool
```
<b>Example:</b>
@@ -909,13 +1146,91 @@ func main() {
### <span id="SymmetricDifference">SymmetricDifference</span>
<p>Create a slice whose element is in given slices, but not in both slices.</p>
<b>Signature:</b>
```go
func SymmetricDifference[T comparable](slices ...[]T) []T
```
<b>Example:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
s1 := []int{1, 2, 3}
s2 := []int{1, 2, 4}
s3 := []int{1, 2, 3, 5}
fmt.Println(slice.SymmetricDifference(s1)) //[]int{1, 2, 3}
fmt.Println(slice.SymmetricDifference(s1, s2)) //[]int{3, 4}
fmt.Println(slice.SymmetricDifference(s1, s2, s3)) //[]int{3, 4, 5}
}
```
### <span id="ToSlice">ToSlice</span>
<p>Returns a slices of a variable parameter transformation</p>
<b>Signature:</b>
```go
func ToSlice[T any](value ...T) []T
```
<b>Example:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
res := slice.ToSlice("a", "b")
fmt.Println(res) //{"a", "b"}
}
```
### <span id="ToSlicePointer">ToSlicePointer</span>
<p>Returns a pointer to the slices of a variable parameter transformation</p>
<b>Signature:</b>
```go
func ToSlicePointer[T any](value ...T) []*T
```
<b>Example:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
str1 := "a"
str2 := "b"
res := slice.ToSlicePointer(str1, str2)
fmt.Println(res) // res -> []*string{&str1, &str2}
}
```
### <span id="Unique">Unique</span>
<p>Remove duplicate elements in slice.</p>
<b>Signature:</b>
```go
func Unique[T any](slice []T) []T
func Unique[T comparable](slice []T) []T
```
<b>Example:</b>
@@ -933,13 +1248,39 @@ func main() {
### <span id="Union">Unique</span>
### <span id="UniqueBy">UniqueBy</span>
<p>Call iteratee func with every item of slice, then remove duplicated.</p>
<b>Signature:</b>
```go
func UniqueBy[T comparable](slice []T, iteratee func(item T) T) []T
```
<b>Example:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/slice"
)
func main() {
res := slice.UniqueBy([]int{1, 2, 3, 4, 5, 6}, func(val int) int {
return val % 4
})
fmt.Println(res) //[]int{1, 2, 3, 0}
}
```
### <span id="Union">Union</span>
<p>Creates a slice of unique values, in order, from all given slices. using == for equality comparisons.</p>
<b>Signature:</b>
```go
func Union[T any](slices ...[]T) []T
func Union[T comparable](slices ...[]T) []T
```
<b>Example:</b>
@@ -993,7 +1334,7 @@ func main() {
<b>Signature:</b>
```go
func Without[T any](slice []T, values ...T) []T
func Without[T comparable](slice []T, values ...T) []T
```
<b>Example:</b>

View File

@@ -20,6 +20,7 @@ import (
<div STYLE="page-break-after: always;"></div>
## 目录
- [AppendIfAbsent](#AppendIfAbsent)
- [Contain](#Contain)
- [ContainSubSlice](#ContainSubSlice)
- [Chunk](#Chunk)
@@ -32,9 +33,12 @@ import (
- [DeleteAt](#DeleteAt)
- [Drop](#Drop)
- [Every](#Every)
- [Equal](#Equal)
- [EqualWith](#EqualWith)
- [Filter](#Filter)
- [Find](#Find)
- [FindLast](#FindLast)
- [Flatten](#Flatten)
- [FlattenDeep](#FlattenDeep)
- [ForEach](#ForEach)
@@ -44,14 +48,22 @@ import (
- [InterfaceSlice](#InterfaceSlice)
- [Intersection](#Intersection)
- [InsertAt](#InsertAt)
- [IndexOf](#IndexOf)
- [LastIndexOf](#LastIndexOf)
- [Map](#Map)
- [Reverse](#Reverse)
- [Reduce](#Reduce)
- [Replace](#Replace)
- [ReplaceAll](#ReplaceAll)
- [Shuffle](#Shuffle)
- [SortByField](#SortByField)
- [Some](#Some)
- [StringSlice](#StringSlice)
- [SymmetricDifference](#SymmetricDifference)
- [ToSlice](#ToSlice)
- [ToSlicePointer](#ToSlicePointer)
- [Unique](#Unique)
- [UniqueBy](#UniqueBy)
- [Union](#Union)
- [UpdateAt](#UpdateAt)
- [Without](#Without)
@@ -60,13 +72,41 @@ import (
## 文档
### <span id="AppendIfAbsent">AppendIfAbsent</span>
<p>当前切片中不包含值时,将该值追加到切片中</p>
<b>函数签名:</b>
```go
func AppendIfAbsent[T comparable](slice []T, value T) []T
```
<b>例子:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
strs := []string{"a", "b"}
res1 := slice.AppendIfAbsent(strs, "a")
fmt.Println(res1) //[]string{"a", "b"}
res2 := slice.AppendIfAbsent(strs, "cannot")
fmt.Println(res2"}
}
```
### <span id="Contain">Contain</span>
<p>判断slice是否包含value</p>
<b>函数签名:</b>
```go
func Contain[T any](slice []T, value T) bool
func Contain[T comparable](slice []T, value T) bool
```
<b>例子:</b>
@@ -89,7 +129,7 @@ func main() {
<b>函数签名:</b>
```go
func ContainSubSlice[T any](slice, subslice []T) bool
func ContainSubSlice[T comparable](slice, subslice []T) bool
```
<b>例子:</b>
@@ -247,7 +287,7 @@ func main() {
<b>函数签名:</b>
```go
func DifferenceBy[T any](slice []T, comparedSlice []T, iteratee func(index int, t T) T) []T
func DifferenceBy[T comparable](slice []T, comparedSlice []T, iteratee func(index int, item T) T) []T
```
<b>例子:</b>
@@ -366,7 +406,7 @@ func main() {
<b>函数签名:</b>
```go
func Every[T any](slice []T, predicate func(index int, t T) bool) bool
func Every[T any](slice []T, predicate func(index int, item T) bool) bool
```
<b>例子:</b>
@@ -390,13 +430,76 @@ func main() {
### <span id="Filter">Filter</span>
<p>返回与函数匹配的所有元素。 函数签名应该是 func(index int, value any) bool</p>
### <span id="Equal">Equal</span>
<p>检查两个切片是否相等,相等条件:切片长度相同,元素顺序和值都相同</p>
<b>函数签名:</b>
```go
func Filter[T any](slice []T, predicate func(index int, t T) bool) []T
func Equal[T comparable](slice1, slice2 []T) bool
```
<b>例子:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
slice1 := []int{1, 2, 3}
slice2 := []int{1, 2, 3}
slice3 := []int{3, 2, 1}
res1 := slice.Equal(slice1, slice2)
res2 := slice.Equal(slice1, slice3)
fmt.Println(res1) //true
fmt.Println(res2) //false
}
```
### <span id="EqualWith">EqualWith</span>
<p>检查两个切片是否相等相等条件对两个切片的元素调用比较函数comparator返回true</p>
<b>函数签名:</b>
```go
func EqualWith[T, U any](slice1 []T, slice2 []U, comparator func(T, U) bool) bool
```
<b>例子:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
slice1 := []int{1, 2, 3}
slice2 := []int{2, 4, 6}
isDouble := func(a, b int) bool {
return b == a*2
}
res := slice.EqualWith(slice1, slice2, isDouble)
fmt.Println(res) //true
}
```
### <span id="Filter">Filter</span>
<p>返回切片中通过predicate函数真值测试的所有元素</p>
<b>函数签名:</b>
```go
func Filter[T any](slice []T, predicate func(index int, item T) bool) []T
```
<b>例子:</b>
@@ -420,12 +523,12 @@ func main() {
### <span id="Find">Find</span>
<p>遍历slice的元素,返回第一个通过function真值测试的元素</p>
<p>遍历切片的元素,返回第一个通过predicate函数真值测试的元素</p>
<b>函数签名:</b>
```go
func Find[T any](slice []T, predicate func(index int, t T) bool) (*T, bool)
func Find[T any](slice []T, predicate func(index int, item T) bool) (*T, bool)
```
<b>例子:</b>
@@ -451,12 +554,12 @@ func main() {
### <span id="FindLast">FindLast</span>
<p>从头到尾遍历 slice 的元素,返回最后一个通过函数真值测试的元素。</p>
<p>从头到尾遍历slice的元素返回最后一个通过predicate函数真值测试的元素。</p>
<b>函数签名:</b>
```go
func FindLast[T any](slice []T, predicate func(index int, t T) bool) (*T, bool)
func FindLast[T any](slice []T, predicate func(index int, item T) bool) (*T, bool)
```
<b>例子:</b>
@@ -479,6 +582,30 @@ func main() {
```
### <span id="Flatten">Flatten</span>
<p>将切片压平一层</p>
<b>函数签名:</b>
```go
func Flatten(slice any) any
```
<b>例子:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
arr := [][][]string{{{"a", "b"}}, {{"c", "d"}}}
res := slice.Flatten(arr)
fmt.Println(res) //{{"a", "b"}, {"c", "d"}}
}
```
### <span id="FlattenDeep">FlattenDeep</span>
<p>flattens slice recursive.</p>
@@ -508,12 +635,12 @@ func main() {
### <span id="ForEach">ForEach</span>
<p>遍历slice的元素并为每个元素调用函数</p>
<p>遍历切片的元素并为每个元素调用iteratee函数</p>
<b>函数签名:</b>
```go
func ForEach[T any](slice []T, iteratee func(index int, t T))
func ForEach[T any](slice []T, iteratee func(index int, item T))
```
<b>例子:</b>
@@ -542,7 +669,7 @@ func main() {
<b>函数签名:</b>
```go
func GroupBy[T any](slice []T, groupFn func(index int, t T) bool) ([]T, []T)
func GroupBy[T any](slice []T, groupFn func(index int, item T) bool) ([]T, []T)
```
<b>例子:</b>
@@ -651,7 +778,7 @@ func main() {
<b>函数签名:</b>
```go
func Intersection[T any](slices ...[]T) []T
func Intersection[T comparable](slices ...[]T) []T
```
<b>例子:</b>
@@ -703,13 +830,69 @@ func main() {
### <span id="IndexOf">IndexOf</span>
<p>返回在切片中找到值的第一个匹配项的索引,如果找不到值,则返回-1</p>
<b>函数签名:</b>
```go
func IndexOf[T comparable](slice []T, value T) int
```
<b>例子:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
arr := []string{"a", "a", "b", "c"}
res1 := slice.IndexOf(arr, "a")
fmt.Println(res1) //0
res2 := slice.IndexOf(arr, "d")
fmt.Println(res2) //-1
}
```
### <span id="LastIndexOf">LastIndexOf</span>
<p>返回在切片中找到最后一个值的索引,如果找不到该值,则返回-1</p>
<b>函数签名:</b>
```go
func LastIndexOf[T comparable](slice []T, value T) int
```
<b>例子:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
arr := []string{"a", "a", "b", "c"}
res1 := slice.LastIndexOf(arr, "a")
fmt.Println(res1) //1
res2 := slice.LastIndexOf(arr, "d")
fmt.Println(res2) //-1
}
```
### <span id="Map">Map</span>
<p>通过运行函数slice中的每个元素来创建一个新切片</p>
<b>函数签名:</b>
```go
func Map[T any, U any](slice []T, iteratee func(index int, t T) U) []U
func Map[T any, U any](slice []T, iteratee func(index int, item T) U) []U
```
<b>例子:</b>
@@ -758,12 +941,12 @@ func main() {
### <span id="Reduce">Reduce</span>
<p>将slice中的元素依次运行函数,返回运行结果</p>
<p>将切片中的元素依次运行iteratee函数,返回运行结果</p>
<b>函数签名:</b>
```go
func Reduce[T any](slice []T, iteratee func(index int, t1, t2 T) T, initial T) T
func Reduce[T any](slice []T, iteratee func(index int, item1, item2 T) T, initial T) T
```
<b>例子:</b>
@@ -785,6 +968,61 @@ func main() {
### <span id="Replace">Replace</span>
<p>返回切片的副本其中前n个不重叠的old替换为new</p>
<b>函数签名:</b>
```go
func Replace[T comparable](slice []T, old T, new T, n int) []T
```
<b>例子:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
strs := []string{"a", "b", "a", "c", "d", "a"}
fmt.Println(slice.Replace(strs, "a", "x", 0)) //{"a", "b", "a", "c", "d", "a"}
fmt.Println(slice.Replace(strs, "a", "x", 1)) //{"x", "b", "a", "c", "d", "a"}
fmt.Println(slice.Replace(strs, "a", "x", -1)) //{"x", "b", "x", "c", "d", "x"}
}
```
### <span id="ReplaceAll">ReplaceAll</span>
<p>返回切片的副本将其中old全部替换为new</p>
<b>函数签名:</b>
```go
func ReplaceAll[T comparable](slice []T, old T, new T) []T
```
<b>例子:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
strs := []string{"a", "b", "a", "c", "d", "a"}
fmt.Println(slice.ReplaceAll(strs, "a", "x")) //{"x", "b", "x", "c", "d", "x"}
fmt.Println(slice.Replace(strs, "e", "x")) //{"a", "b", "a", "c", "d", "a"}
}
```
### <span id="Shuffle">Shuffle</span>
<p>随机打乱切片中的元素顺序</p>
@@ -860,7 +1098,7 @@ func main() {
<b>函数签名:</b>
```go
func Some[T any](slice []T, predicate func(index int, t T) bool) bool
func Some[T any](slice []T, predicate func(index int, item T) bool) bool
```
<b>例子:</b>
@@ -909,13 +1147,91 @@ func main() {
### <span id="SymmetricDifference">SymmetricDifference</span>
<p>返回一个切片,其中的元素存在于参数切片中,但不同时存储在于参数切片中(交集取反)</p>
<b>函数签名:</b>
```go
func SymmetricDifference[T comparable](slices ...[]T) []T
```
<b>例子:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
s1 := []int{1, 2, 3}
s2 := []int{1, 2, 4}
s3 := []int{1, 2, 3, 5}
fmt.Println(slice.SymmetricDifference(s1)) //[]int{1, 2, 3}
fmt.Println(slice.SymmetricDifference(s1, s2)) //[]int{3, 4}
fmt.Println(slice.SymmetricDifference(s1, s2, s3)) //[]int{3, 4, 5}
}
```
### <span id="ToSlice">ToSlice</span>
<p>将可变参数转为切片</p>
<b>函数签名:</b>
```go
func ToSlice[T any](value ...T) []T
```
<b>例子:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
res := slice.ToSlice("a", "b")
fmt.Println(res) //{"a", "b"}
}
```
### <span id="ToSlicePointer">ToSlicePointer</span>
<p>将可变参数转为指针切片</p>
<b>函数签名:</b>
```go
func ToSlicePointer[T any](value ...T) []*T
```
<b>例子:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
)
func main() {
str1 := "a"
str2 := "b"
res := slice.ToSlicePointer(str1, str2)
fmt.Println(res) // res -> []*string{&str1, &str2}
}
```
### <span id="Unique">Unique</span>
<p>删除切片中的重复元素</p>
<b>函数签名:</b>
```go
func Unique[T any](slice []T) []T
func Unique[T comparable](slice []T) []T
```
<b>例子:</b>
@@ -933,13 +1249,39 @@ func main() {
### <span id="Union">Unique</span>
<p>从所有给定的切片按顺序创建一个唯一值切片。 使用 == 进行相等比较。</p>
### <span id="UniqueBy">UniqueBy</span>
<p>对切片的每个元素调用iteratee函数然后删除重复元素</p>
<b>函数签名:</b>
```go
func Union[T any](slices ...[]T) []T
func UniqueBy[T comparable](slice []T, iteratee func(item T) T) []T
```
<b>例子:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/slice"
)
func main() {
res := slice.UniqueBy([]int{1, 2, 3, 4, 5, 6}, func(val int) int {
return val % 4
})
fmt.Println(res) //[]int{1, 2, 3, 0}
}
```
### <span id="Union">Union</span>
<p>从所有给定的切片按顺序创建一个唯一值切片,使用==进行相等比较</p>
<b>函数签名:</b>
```go
func Union[T comparable](slices ...[]T) []T
```
<b>例子:</b>
@@ -961,7 +1303,7 @@ func main() {
### <span id="UpdateAt">UpdateAt</span>
<p>更新索引处的切片元素。 如果 param index < 0 或 index >= len(slice),将返回错误</p>
<p>更新索引处的切片元素。 如果index < 0或 index >= len(slice),将返回错误</p>
<b>函数签名:</b>
@@ -993,7 +1335,7 @@ func main() {
<b>函数签名:</b>
```go
func Without[T any](slice []T, values ...T) []T
func Without[T comparable](slice []T, values ...T) []T
```
<b>例子:</b>

View File

@@ -32,8 +32,9 @@ import (
- [UpperFirst](#UpperFirst)
- [PadEnd](#PadEnd)
- [PadStart](#PadStart)
- [ReverseStr](#ReverseStr)
- [Reverse](#Reverse)
- [SnakeCase](#SnakeCase)
- [SplitEx](#SplitEx)
- [Wrap](#Wrap)
- [Unwrap](#Unwrap)
@@ -429,13 +430,13 @@ func main() {
### <span id="ReverseStr">ReverseStr</span>
### <span id="Reverse">Reverse</span>
<p>Return string whose char order is reversed to the given string.</p>
<b>Signature:</b>
```go
func ReverseStr(s string) string
func Reverse(s string) string
```
<b>Example:</b>
@@ -493,6 +494,43 @@ func main() {
### <span id="SplitEx">SplitEx</span>
<p>Split a given string whether the result contains empty string.</p>
<b>Signature:</b>
```go
func SplitEx(s, sep string, removeEmptyString bool) []string
```
<b>Example:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/strutil"
)
func main() {
arr1 := strutil.SplitEx(" a b c ", "", true)
fmt.Println(arr1) //[]string{}
arr2 := strutil.SplitEx(" a b c ", " ", false)
fmt.Println(arr2) //[]string{"", "a", "b", "c", ""}
arr3 := strutil.SplitEx(" a b c ", " ", true)
fmt.Println(arr3) //[]string{"a", "b", "c"}
arr4 := strutil.SplitEx(" a = b = c = ", " = ", false)
fmt.Println(arr4) //[]string{" a", "b", "c", ""}
arr5 := strutil.SplitEx(" a = b = c = ", " = ", true)
fmt.Println(arr5) //[]string{" a", "b", "c"}
}
```
### <span id="Wrap">Wrap</span>
<p>Wrap a string with another string.</p>

View File

@@ -32,8 +32,9 @@ import (
- [UpperFirst](#UpperFirst)
- [PadEnd](#PadEnd)
- [PadStart](#PadStart)
- [ReverseStr](#ReverseStr)
- [Reverse](#Reverse)
- [SnakeCase](#SnakeCase)
- [SplitEx](#SplitEx)
- [Wrap](#Wrap)
- [Unwrap](#Unwrap)
@@ -430,13 +431,13 @@ func main() {
### <span id="ReverseStr">ReverseStr</span>
### <span id="Reverse">Reverse</span>
<p>返回字符顺序与给定字符串相反的字符串</p>
<b>函数签名:</b>
```go
func ReverseStr(s string) string
func Reverse(s string) string
```
<b>例子:</b>
@@ -493,6 +494,41 @@ func main() {
### <span id="SplitEx">SplitEx</span>
<p>分割字符串为切片removeEmptyString参数指定是否去除空字符串</p>
<b>函数签名:</b>
```go
func SplitEx(s, sep string, removeEmptyString bool) []string
```
<b>例子:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/strutil"
)
func main() {
arr1 := strutil.SplitEx(" a b c ", "", true)
fmt.Println(arr1) //[]string{}
arr2 := strutil.SplitEx(" a b c ", " ", false)
fmt.Println(arr2) //[]string{"", "a", "b", "c", ""}
arr3 := strutil.SplitEx(" a b c ", " ", true)
fmt.Println(arr3) //[]string{"a", "b", "c"}
arr4 := strutil.SplitEx(" a = b = c = ", " = ", false)
fmt.Println(arr4) //[]string{" a", "b", "c", ""}
arr5 := strutil.SplitEx(" a = b = c = ", " = ", true)
fmt.Println(arr5) //[]string{" a", "b", "c"}
}
```
### <span id="Wrap">Wrap</span>
<p>用另一个字符串包裹一个字符串</p>

View File

@@ -28,6 +28,7 @@ import (
- [RemoveOsEnv](#RemoveOsEnv)
- [CompareOsEnv](#CompareOsEnv)
- [ExecCommand](#ExecCommand)
- [GetOsBits](#GetOsBits)
<div STYLE="page-break-after: always;"></div>
@@ -210,7 +211,7 @@ func main() {
### <span id="ExecCommand">CompareOsEnv</span>
<p>use shell /bin/bash -c(linux) or cmd (windows) to execute command.</p>
<p>Use shell /bin/bash -c(linux) or cmd (windows) to execute command.</p>
<b>Signature:</b>
@@ -236,7 +237,27 @@ func main() {
### <span id="GetOsBits">GetOsBits</span>
<p>Get current os bits, 32bit or 64bit. return 32 or 64</p>
<b>Signature:</b>
```go
func GetOsBits() int
```
<b>Example:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/system"
)
func main() {
osBit := system.GetOsBits()
fmt.Println(osBit)
}
```

View File

@@ -26,8 +26,10 @@ import (
- [GetOsEnv](#GetOsEnv)
- [SetOsEnv](#SetOsEnv)
- [RemoveOsEnv](#RemoveOsEnv)
- [CompareOsEnv](#CompareOsEnv)
- [ExecCommand](#ExecCommand)
- [GetOsBits](#GetOsBits)
<div STYLE="page-break-after: always;"></div>
@@ -209,7 +211,7 @@ func main() {
### <span id="ExecCommand">CompareOsEnv</span>
### <span id="ExecCommand">ExecCommand</span>
<p>使用shell /bin/bash -c(linux) 或 cmd (windows) 执行shell命令</p>
<b>Signature:</b>
@@ -236,6 +238,30 @@ func main() {
### <span id="GetOsBits">GetOsBits</span>
<p>获取当前操作系统位数返回32或64</p>
<b>函数签名:</b>
```go
func GetOsBits() int
```
<b>例子:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/system"
)
func main() {
osBit := system.GetOsBits()
fmt.Println(osBit)
}
```

View File

@@ -47,6 +47,7 @@ import (
- [IsStrongPassword](#IsStrongPassword)
- [IsUrl](#IsUrl)
- [IsWeakPassword](#IsWeakPassword)
- [IsZeroValue](#IsZeroValue)
<div STYLE="page-break-after: always;"></div>
@@ -765,7 +766,7 @@ func main() {
### <span id="IsWeakPassword">IsWeakPassword</span>
<p>Check if the string is weak passwordonly letter or only number or letter + number
<p>Checks if the string is weak passwordonly letter or only number or letter + number
.</p>
<b>Signature:</b>
@@ -792,6 +793,36 @@ func main() {
### <span id="IsZeroValue">IsZeroValue</span>
<p>Checks if passed value is a zero value.</p>
<b>Signature:</b>
```go
func IsZeroValue(value any) bool
```
<b>Example:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/validator"
)
func main() {
fmt.Println(validator.IsZeroValue(nil)) //true
fmt.Println(validator.IsZeroValue(0)) //true
fmt.Println(validator.IsZeroValue("")) //true
fmt.Println(validator.IsZeroValue([]int)) //true
fmt.Println(validator.IsZeroValue(interface{})) //true
fmt.Println(validator.IsZeroValue("0")) //false
fmt.Println(validator.IsZeroValue("nil")) //false
}
```

View File

@@ -47,6 +47,7 @@ import (
- [IsStrongPassword](#IsStrongPassword)
- [IsUrl](#IsUrl)
- [IsWeakPassword](#IsWeakPassword)
- [IsZeroValue](#IsZeroValue)
<div STYLE="page-break-after: always;"></div>
@@ -792,8 +793,31 @@ func main() {
### <span id="IsZeroValue">IsZeroValue</span>
<p>判断传入的参数值是否为零值</p>
<b>函数签名:</b>
```go
func IsZeroValue(value any) bool
```
<b>例子:</b>
```go
import (
"fmt"
"github.com/duke-git/lancet/v2/validator"
)
func main() {
fmt.Println(validator.IsZeroValue(nil)) //true
fmt.Println(validator.IsZeroValue(0)) //true
fmt.Println(validator.IsZeroValue("")) //true
fmt.Println(validator.IsZeroValue([]int)) //true
fmt.Println(validator.IsZeroValue(interface{})) //true
fmt.Println(validator.IsZeroValue("0")) //false
fmt.Println(validator.IsZeroValue("nil")) //false
}
```

View File

@@ -46,7 +46,7 @@ import (
)
func main() {
_, err := strconv.Atoi("4o2")
_, err := strconv.Atoi("4o2")
defer func() {
v := recover()
fmt.Println(err.Error()) // err.Error() == v.(*strconv.NumError).Error()

View File

@@ -46,7 +46,7 @@ import (
)
func main() {
_, err := strconv.Atoi("4o2")
_, err := strconv.Atoi("4o2")
defer func() {
v := recover()
fmt.Println(err.Error()) // err.Error() == v.(*strconv.NumError).Error()

View File

@@ -13,6 +13,7 @@ import (
"io/ioutil"
"net/http"
"os"
"path"
"path/filepath"
"strings"
)
@@ -40,6 +41,11 @@ func CreateFile(path string) bool {
return true
}
// CreateDir create directory in absolute path. param `absPath` like /a/, /a/b/
func CreateDir(absPath string) error {
return os.MkdirAll(path.Dir(absPath), os.ModePerm)
}
// IsDir checks if the path is directory or not
func IsDir(path string) bool {
file, err := os.Stat(path)
@@ -110,7 +116,7 @@ func ReadFileByLine(path string) ([]string, error) {
}
defer f.Close()
res := make([]string, 0)
result := make([]string, 0)
buf := bufio.NewReader(f)
for {
@@ -122,10 +128,10 @@ func ReadFileByLine(path string) ([]string, error) {
if err != nil {
continue
}
res = append(res, l)
result = append(result, l)
}
return res, nil
return result, nil
}
// ListFileNames return all file names in the path
@@ -144,14 +150,14 @@ func ListFileNames(path string) ([]string, error) {
return []string{}, nil
}
res := []string{}
result := []string{}
for i := 0; i < sz; i++ {
if !fs[i].IsDir() {
res = append(res, fs[i].Name())
result = append(result, fs[i].Name())
}
}
return res, nil
return result, nil
}
// Zip create zip file, fpath could be a single file or a directory

View File

@@ -25,6 +25,7 @@ func TestCreateFile(t *testing.T) {
f := "./text.txt"
if CreateFile(f) {
file, err := os.Open(f)
defer file.Close()
assert.IsNil(err)
assert.Equal(f, file.Name())
} else {
@@ -33,6 +34,27 @@ func TestCreateFile(t *testing.T) {
os.Remove(f)
}
func TestCreateDir(t *testing.T) {
assert := internal.NewAssert(t, "TestCreateDir")
pwd, err := os.Getwd()
if err != nil {
t.Error(err)
t.FailNow()
}
dirPath := pwd + "/a/"
err = CreateDir(dirPath)
if err != nil {
t.Error(err)
t.FailNow()
}
assert.Equal(true, IsExist(dirPath))
os.Remove(dirPath)
assert.Equal(false, IsExist(dirPath))
}
func TestIsDir(t *testing.T) {
assert := internal.NewAssert(t, "TestIsDir")
@@ -90,6 +112,7 @@ func TestReadFileToString(t *testing.T) {
CreateFile(path)
f, _ := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, 0777)
defer f.Close()
f.WriteString("hello world")
content, _ := ReadFileToString(path)
@@ -197,6 +220,7 @@ func TestMiMeType(t *testing.T) {
assert := internal.NewAssert(t, "TestMiMeType")
f, _ := os.Open("./file.go")
defer f.Close()
assert.Equal("text/plain; charset=utf-8", MiMeType(f))
assert.Equal("text/plain; charset=utf-8", MiMeType("./file.go"))
}

View File

@@ -27,16 +27,16 @@ func After(n int, fn any) func(args ...any) []reflect.Value {
func Before(n int, fn any) func(args ...any) []reflect.Value {
// Catch programming error while constructing the closure
mustBeFunction(fn)
var res []reflect.Value
var result []reflect.Value
return func(args ...any) []reflect.Value {
if n > 0 {
res = unsafeInvokeFunc(fn, args...)
result = unsafeInvokeFunc(fn, args...)
}
if n <= 0 {
fn = nil
}
n--
return res
return result
}
}
@@ -113,3 +113,15 @@ func Schedule(d time.Duration, fn any, args ...any) chan bool {
return quit
}
// Pipeline takes a list of functions and returns a function whose param will be passed into
// the functions one by one.
func Pipeline[T any](funcs ...func(T) T) func(T) T {
return func(arg T) (result T) {
result = arg
for _, fn := range funcs {
result = fn(result)
}
return
}
}

View File

@@ -116,7 +116,7 @@ func TestDebounced(t *testing.T) {
}
func TestSchedule(t *testing.T) {
assert := internal.NewAssert(t, "TestSchedule")
// assert := internal.NewAssert(t, "TestSchedule")
var res []string
appendStr := func(s string) {
@@ -127,6 +127,28 @@ func TestSchedule(t *testing.T) {
time.Sleep(5 * time.Second)
close(stop)
expected := []string{"*", "*", "*", "*", "*"}
assert.Equal(expected, res)
t.Log(res)
// todo: in github action, for now, this test is not working sometimes
// res maybe [* * * * *] or [* * * * * *]
// expected := []string{"*", "*", "*", "*", "*"}
// assert.Equal(expected, res)
}
func TestPipeline(t *testing.T) {
assert := internal.NewAssert(t, "TestPipeline")
addOne := func(x int) int {
return x + 1
}
double := func(x int) int {
return 2 * x
}
square := func(x int) int {
return x * x
}
f := Pipeline(addOne, double, square)
assert.Equal(36, f(2))
}

View File

@@ -29,9 +29,10 @@ func TestWatcher(t *testing.T) {
assert.Equal(int64(0), w.stopTime)
}
func longRunningTask() {
var slice []int64
func longRunningTask() []int64 {
var data []int64
for i := 0; i < 10000000; i++ {
slice = append(slice, int64(i))
data = append(data, int64(i))
}
return data
}

View File

@@ -14,5 +14,5 @@ type Comparator interface {
// Number contains all types of number and uintptr, used for generics constraint
type Number interface {
int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | uintptr | float32 | float64
~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr | ~float32 | ~float64
}

BIN
logo.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 63 KiB

After

Width:  |  Height:  |  Size: 18 KiB

120
maputil/map.go Normal file
View File

@@ -0,0 +1,120 @@
// Copyright 2021 dudaodong@gmail.com. All rights reserved.
// Use of this source code is governed by MIT license
// Package maputil includes some functions to manipulate map.
package maputil
import "reflect"
// Keys returns a slice of the map's keys
func Keys[K comparable, V any](m map[K]V) []K {
keys := make([]K, len(m))
var i int
for k := range m {
keys[i] = k
i++
}
return keys
}
// Values returns a slice of the map's values
func Values[K comparable, V any](m map[K]V) []V {
values := make([]V, len(m))
var i int
for _, v := range m {
values[i] = v
i++
}
return values
}
// Merge maps, next key will overwrite previous key
func Merge[K comparable, V any](maps ...map[K]V) map[K]V {
result := make(map[K]V, 0)
for _, m := range maps {
for k, v := range m {
result[k] = v
}
}
return result
}
// ForEach executes iteratee funcation for every key and value pair in map
func ForEach[K comparable, V any](m map[K]V, iteratee func(key K, value V)) {
for k, v := range m {
iteratee(k, v)
}
}
// Filter iterates over map, return a new map contains all key and value pairs pass the predicate function
func Filter[K comparable, V any](m map[K]V, predicate func(key K, value V) bool) map[K]V {
result := make(map[K]V)
for k, v := range m {
if predicate(k, v) {
result[k] = v
}
}
return result
}
// Intersect iterates over maps, return a new map of key and value pairs in all given maps
func Intersect[K comparable, V any](maps ...map[K]V) map[K]V {
if len(maps) == 0 {
return map[K]V{}
}
if len(maps) == 1 {
return maps[0]
}
var result map[K]V
reducer := func(m1, m2 map[K]V) map[K]V {
m := make(map[K]V)
for k, v1 := range m1 {
if v2, ok := m2[k]; ok && reflect.DeepEqual(v1, v2) {
m[k] = v1
}
}
return m
}
reduceMaps := make([]map[K]V, 2, 2)
result = reducer(maps[0], maps[1])
for i := 2; i < len(maps); i++ {
reduceMaps[0] = result
reduceMaps[1] = maps[i]
result = reducer(reduceMaps[0], reduceMaps[1])
}
return result
}
// Minus creates an map of whose key in mapA but not in mapB
func Minus[K comparable, V any](mapA, mapB map[K]V) map[K]V {
result := make(map[K]V)
for k, v := range mapA {
if _, ok := mapB[k]; !ok {
result[k] = v
}
}
return result
}
// IsDisjoint two map are disjoint if they have no keys in common
func IsDisjoint[K comparable, V any](mapA, mapB map[K]V) bool {
for k := range mapA {
if _, ok := mapB[k]; ok {
return false
}
}
return true
}

172
maputil/map_test.go Normal file
View File

@@ -0,0 +1,172 @@
package maputil
import (
"sort"
"testing"
"github.com/duke-git/lancet/v2/internal"
)
func TestKeys(t *testing.T) {
assert := internal.NewAssert(t, "TestKeys")
m := map[int]string{
1: "a",
2: "a",
3: "b",
4: "c",
5: "d",
}
keys := Keys(m)
sort.Ints(keys)
assert.Equal([]int{1, 2, 3, 4, 5}, keys)
}
func TestValues(t *testing.T) {
assert := internal.NewAssert(t, "TestValues")
m := map[int]string{
1: "a",
2: "a",
3: "b",
4: "c",
5: "d",
}
values := Values(m)
sort.Strings(values)
assert.Equal([]string{"a", "a", "b", "c", "d"}, values)
}
func TestMerge(t *testing.T) {
assert := internal.NewAssert(t, "TestMerge")
m1 := map[int]string{
1: "a",
2: "b",
}
m2 := map[int]string{
1: "1",
3: "2",
}
expected := map[int]string{
1: "1",
2: "b",
3: "2",
}
acturl := Merge(m1, m2)
assert.Equal(expected, acturl)
}
func TestForEach(t *testing.T) {
assert := internal.NewAssert(t, "TestForEach")
m := map[string]int{
"a": 1,
"b": 2,
"c": 3,
"d": 4,
}
var sum int
ForEach(m, func(_ string, value int) {
sum += value
})
assert.Equal(10, sum)
}
func TestFilter(t *testing.T) {
assert := internal.NewAssert(t, "TestFilter")
m := map[string]int{
"a": 1,
"b": 2,
"c": 3,
"d": 4,
"e": 5,
}
isEven := func(_ string, value int) bool {
return value%2 == 0
}
acturl := Filter(m, isEven)
assert.Equal(map[string]int{
"b": 2,
"d": 4,
}, acturl)
}
func TestIntersect(t *testing.T) {
assert := internal.NewAssert(t, "TestIntersect")
m1 := map[string]int{
"a": 1,
"b": 2,
"c": 3,
}
m2 := map[string]int{
"a": 1,
"b": 2,
"c": 6,
"d": 7,
}
m3 := map[string]int{
"a": 1,
"b": 9,
"e": 9,
}
assert.Equal(map[string]int{"a": 1, "b": 2, "c": 3}, Intersect(m1))
assert.Equal(map[string]int{"a": 1, "b": 2}, Intersect(m1, m2))
assert.Equal(map[string]int{"a": 1}, Intersect(m1, m2, m3))
}
func TestMinus(t *testing.T) {
assert := internal.NewAssert(t, "TestMinus")
m1 := map[string]int{
"a": 1,
"b": 2,
"c": 3,
}
m2 := map[string]int{
"a": 11,
"b": 22,
"d": 33,
}
assert.Equal(map[string]int{"c": 3}, Minus(m1, m2))
}
func TestIsDisjoint(t *testing.T) {
assert := internal.NewAssert(t, "TestMinus")
m1 := map[string]int{
"a": 1,
"b": 2,
"c": 3,
}
m2 := map[string]int{
"d": 22,
}
assert.Equal(true, IsDisjoint(m1, m2))
m3 := map[string]int{
"a": 22,
}
assert.Equal(false, IsDisjoint(m1, m3))
}

View File

@@ -57,9 +57,9 @@ func Percent(val, total float64, n int) float64 {
return float64(0)
}
tmp := val / total * 100
res := RoundToFloat(tmp, n)
result := RoundToFloat(tmp, n)
return res
return result
}
// RoundToString round up to n decimal places
@@ -67,8 +67,8 @@ func RoundToString(x float64, n int) string {
tmp := math.Pow(10.0, float64(n))
x *= tmp
x = math.Round(x)
res := strconv.FormatFloat(x/tmp, 'f', n, 64)
return res
result := strconv.FormatFloat(x/tmp, 'f', n, 64)
return result
}
// RoundToFloat round up to n decimal places
@@ -89,8 +89,8 @@ func TruncRound(x float64, n int) float64 {
} else {
newFloat = temp[0] + "." + temp[1][:n]
}
res, _ := strconv.ParseFloat(newFloat, 64)
return res
result, _ := strconv.ParseFloat(newFloat, 64)
return result
}
// Max return max value of params
@@ -106,6 +106,27 @@ func Max[T lancetconstraints.Number](numbers ...T) T {
return max
}
// MaxBy search the maximum value of a slice using the given comparator function.
func MaxBy[T any](slice []T, comparator func(T, T) bool) T {
var max T
if len(slice) == 0 {
return max
}
max = slice[0]
for i := 1; i < len(slice); i++ {
val := slice[i]
if comparator(val, max) {
max = val
}
}
return max
}
// Min return min value of params
func Min[T lancetconstraints.Number](numbers ...T) T {
min := numbers[0]
@@ -119,6 +140,27 @@ func Min[T lancetconstraints.Number](numbers ...T) T {
return min
}
// MinBy search the minimum value of a slice using the given comparator function.
func MinBy[T any](slice []T, comparator func(T, T) bool) T {
var min T
if len(slice) == 0 {
return min
}
min = slice[0]
for i := 1; i < len(slice); i++ {
val := slice[i]
if comparator(val, min) {
min = val
}
}
return min
}
// Average return average value of params
func Average[T lancetconstraints.Number](numbers ...T) T {
var sum T

View File

@@ -87,6 +87,28 @@ func TestMax(t *testing.T) {
assert.Equal(Max(0, 0), 0)
assert.Equal(Max(1, 2, 3), 3)
assert.Equal(Max(1.2, 1.4, 1.1, 1.4), 1.4)
type Integer int
assert.Equal(Max(Integer(1), Integer(0)), Integer(1))
}
func TestMaxBy(t *testing.T) {
assert := internal.NewAssert(t, "MaxBy")
res1 := MaxBy([]string{"a", "ab", "abc"}, func(v1, v2 string) bool {
return len(v1) > len(v2)
})
assert.Equal("abc", res1)
res2 := MaxBy([]string{"abd", "abc", "ab"}, func(v1, v2 string) bool {
return len(v1) > len(v2)
})
assert.Equal("abd", res2)
res3 := MaxBy([]string{}, func(v1, v2 string) bool {
return len(v1) > len(v2)
})
assert.Equal("", res3)
}
func TestMin(t *testing.T) {
@@ -96,3 +118,22 @@ func TestMin(t *testing.T) {
assert.Equal(Min(1, 2, 3), 1)
assert.Equal(Min(1.2, 1.4, 1.1, 1.4), 1.1)
}
func TestMinBy(t *testing.T) {
assert := internal.NewAssert(t, "TestMinBy")
res1 := MinBy([]string{"a", "ab", "abc"}, func(v1, v2 string) bool {
return len(v1) < len(v2)
})
assert.Equal("a", res1)
res2 := MinBy([]string{"ab", "ac", "abc"}, func(v1, v2 string) bool {
return len(v1) < len(v2)
})
assert.Equal("ab", res2)
res3 := MinBy([]string{}, func(v1, v2 string) bool {
return len(v1) < len(v2)
})
assert.Equal("", res3)
}

View File

@@ -6,7 +6,8 @@
// HttpGet, HttpPost, HttpDelete, HttpPut, HttpPatch, function param `url` is required.
// HttpGet, HttpPost, HttpDelete, HttpPut, HttpPatch, function param `params` is variable, the order is:
// params[0] is header which type should be http.Header or map[string]string,
// params[1] is query param which type should be url.Values or map[string]any,
// params[1] is query string param which type should be url.Values or map[string]string, when content-type header is
// multipart/form-data or application/x-www-form-urlencoded
// params[2] is post body which type should be []byte.
// params[3] is http client which type should be http.Client.
package netutil

228
netutil/http_client.go Normal file
View File

@@ -0,0 +1,228 @@
package netutil
import (
"bytes"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"reflect"
"regexp"
"strings"
"time"
"github.com/duke-git/lancet/v2/slice"
)
// HttpRequest struct is a composed http request
type HttpRequest struct {
RawURL string
Method string
Headers http.Header
QueryParams url.Values
FormData url.Values
Body []byte
}
// HttpClientConfig contains some configurations for http client
type HttpClientConfig struct {
SSLEnabled bool
TLSConfig *tls.Config
Compressed bool
HandshakeTimeout time.Duration
ResponseTimeout time.Duration
Verbose bool
}
// defaultHttpClientConfig defalut client config
var defaultHttpClientConfig = &HttpClientConfig{
Compressed: false,
HandshakeTimeout: 20 * time.Second,
ResponseTimeout: 40 * time.Second,
}
// HttpClient is used for sending http request
type HttpClient struct {
*http.Client
TLS *tls.Config
Request *http.Request
Config HttpClientConfig
}
// NewHttpClient make a HttpClient instance
func NewHttpClient() *HttpClient {
client := &HttpClient{
Client: &http.Client{
Transport: &http.Transport{
TLSHandshakeTimeout: defaultHttpClientConfig.HandshakeTimeout,
ResponseHeaderTimeout: defaultHttpClientConfig.ResponseTimeout,
DisableCompression: !defaultHttpClientConfig.Compressed,
},
},
Config: *defaultHttpClientConfig,
}
return client
}
// NewHttpClientWithConfig make a HttpClient instance with pass config
func NewHttpClientWithConfig(config *HttpClientConfig) *HttpClient {
if config == nil {
config = defaultHttpClientConfig
}
client := &HttpClient{
Client: &http.Client{
Transport: &http.Transport{
TLSHandshakeTimeout: config.HandshakeTimeout,
ResponseHeaderTimeout: config.ResponseTimeout,
DisableCompression: !config.Compressed,
},
},
Config: *config,
}
if config.SSLEnabled {
client.TLS = config.TLSConfig
}
return client
}
// SendRequest send http request
func (client *HttpClient) SendRequest(request *HttpRequest) (*http.Response, error) {
err := validateRequest(request)
if err != nil {
return nil, err
}
rawUrl := request.RawURL
req, err := http.NewRequest(request.Method, rawUrl, bytes.NewBuffer(request.Body))
if err != nil {
return nil, err
}
client.setTLS(rawUrl)
client.setHeader(req, request.Headers)
client.setQueryParam(req, rawUrl, request.QueryParams)
if request.FormData != nil {
client.setFormData(req, request.FormData)
}
client.Request = req
resp, err := client.Client.Do(req)
if err != nil {
return nil, err
}
return resp, nil
}
// DecodeResponse decode response into target object
func (client *HttpClient) DecodeResponse(resp *http.Response, target any) error {
if resp == nil {
return errors.New("invalid target param")
}
defer resp.Body.Close()
return json.NewDecoder(resp.Body).Decode(target)
}
// setTLS set http client transport TLSClientConfig
func (client *HttpClient) setTLS(rawUrl string) {
if strings.HasPrefix(rawUrl, "https") {
if transport, ok := client.Client.Transport.(*http.Transport); ok {
transport.TLSClientConfig = client.TLS
}
}
}
// setHeader set http rquest header
func (client *HttpClient) setHeader(req *http.Request, headers http.Header) {
if headers == nil {
headers = make(http.Header)
}
if _, ok := headers["Accept"]; !ok {
headers["Accept"] = []string{"*/*"}
}
if _, ok := headers["Accept-Encoding"]; !ok && client.Config.Compressed {
headers["Accept-Encoding"] = []string{"deflate, gzip"}
}
req.Header = headers
}
// setQueryParam set http request query string param
func (client *HttpClient) setQueryParam(req *http.Request, reqUrl string, queryParam url.Values) error {
if queryParam != nil {
if !strings.Contains(reqUrl, "?") {
reqUrl = reqUrl + "?" + queryParam.Encode()
} else {
reqUrl = reqUrl + "&" + queryParam.Encode()
}
u, err := url.Parse(reqUrl)
if err != nil {
return err
}
req.URL = u
}
return nil
}
func (client *HttpClient) setFormData(req *http.Request, values url.Values) {
formData := []byte(values.Encode())
req.Body = ioutil.NopCloser(bytes.NewReader(formData))
req.ContentLength = int64(len(formData))
}
// validateRequest check if a request has url, and valid method.
func validateRequest(req *HttpRequest) error {
if req.RawURL == "" {
return errors.New("invalid request url")
}
// common HTTP methods
methods := []string{"GET", "POST", "PUT", "DELETE", "PATCH",
"HEAD", "CONNECT", "OPTIONS", "TRACE"}
if !slice.Contain(methods, strings.ToUpper(req.Method)) {
return errors.New("invalid request method")
}
return nil
}
// StructToUrlValues convert struct to url valuse,
// only convert the field which is exported and has `json` tag
func StructToUrlValues(targetStruct any) url.Values {
rv := reflect.ValueOf(targetStruct)
rt := reflect.TypeOf(targetStruct)
if rt.Kind() == reflect.Ptr {
rt = rt.Elem()
}
if rt.Kind() != reflect.Struct {
panic(fmt.Errorf("data type %T not support, shuld be struct or pointer to struct", targetStruct))
}
result := url.Values{}
fieldNum := rt.NumField()
pattern := `^[A-Z]`
regex := regexp.MustCompile(pattern)
for i := 0; i < fieldNum; i++ {
name := rt.Field(i).Name
tag := rt.Field(i).Tag.Get("json")
if regex.MatchString(name) && tag != "" {
result.Add(tag, fmt.Sprintf("%v", rv.Field(i).Interface()))
}
}
return result
}

Some files were not shown because too many files have changed in this diff Show More