github.com/wangyougui/gf/v2@v2.6.5/container/gmap/gmap_z_example_int_int_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with gm file, 5 // You can obtain one at https://github.com/wangyougui/gf. 6 7 package gmap_test 8 9 import ( 10 "fmt" 11 12 "github.com/wangyougui/gf/v2/container/gmap" 13 "github.com/wangyougui/gf/v2/frame/g" 14 "github.com/wangyougui/gf/v2/internal/json" 15 "github.com/wangyougui/gf/v2/util/gconv" 16 ) 17 18 func ExampleIntIntMap_Iterator() { 19 m := gmap.NewIntIntMap() 20 for i := 0; i < 10; i++ { 21 m.Set(i, i*2) 22 } 23 24 var totalKey, totalValue int 25 m.Iterator(func(k int, v int) bool { 26 totalKey += k 27 totalValue += v 28 29 return totalKey < 10 30 }) 31 32 fmt.Println("totalKey:", totalKey) 33 fmt.Println("totalValue:", totalValue) 34 35 // May Output: 36 // totalKey: 11 37 // totalValue: 22 38 } 39 40 func ExampleIntIntMap_Clone() { 41 m := gmap.NewIntIntMap() 42 43 m.Set(1, 1) 44 fmt.Println(m) 45 46 n := m.Clone() 47 fmt.Println(n) 48 49 // Output: 50 // {"1":1} 51 // {"1":1} 52 } 53 54 func ExampleIntIntMap_Map() { 55 // non concurrent-safety, a pointer to the underlying data 56 m1 := gmap.NewIntIntMap() 57 m1.Set(1, 1) 58 fmt.Println("m1:", m1) 59 60 n1 := m1.Map() 61 fmt.Println("before n1:", n1) 62 m1.Set(1, 2) 63 fmt.Println("after n1:", n1) 64 65 // concurrent-safety, copy of underlying data 66 m2 := gmap.New(true) 67 m2.Set(1, "1") 68 fmt.Println("m2:", m2) 69 70 n2 := m2.Map() 71 fmt.Println("before n2:", n2) 72 m2.Set(1, "2") 73 fmt.Println("after n2:", n2) 74 75 // Output: 76 // m1: {"1":1} 77 // before n1: map[1:1] 78 // after n1: map[1:2] 79 // m2: {"1":"1"} 80 // before n2: map[1:1] 81 // after n2: map[1:1] 82 } 83 84 func ExampleIntIntMap_MapCopy() { 85 m := gmap.NewIntIntMap() 86 87 m.Set(1, 1) 88 m.Set(2, 2) 89 fmt.Println(m) 90 91 n := m.MapCopy() 92 fmt.Println(n) 93 94 // Output: 95 // {"1":1,"2":2} 96 // map[1:1 2:2] 97 } 98 99 func ExampleIntIntMap_MapStrAny() { 100 m := gmap.NewIntIntMap() 101 m.Set(1001, 1) 102 m.Set(1002, 2) 103 104 n := m.MapStrAny() 105 fmt.Printf("%#v", n) 106 107 // Output: 108 // map[string]interface {}{"1001":1, "1002":2} 109 } 110 111 func ExampleIntIntMap_FilterEmpty() { 112 m := gmap.NewIntIntMapFrom(g.MapIntInt{ 113 1: 0, 114 2: 1, 115 }) 116 m.FilterEmpty() 117 fmt.Println(m.Map()) 118 119 // Output: 120 // map[2:1] 121 } 122 123 func ExampleIntIntMap_Set() { 124 m := gmap.NewIntIntMap() 125 126 m.Set(1, 1) 127 fmt.Println(m) 128 129 // Output: 130 // {"1":1} 131 } 132 133 func ExampleIntIntMap_Sets() { 134 m := gmap.NewIntIntMap() 135 136 addMap := make(map[int]int) 137 addMap[1] = 1 138 addMap[2] = 12 139 addMap[3] = 123 140 141 m.Sets(addMap) 142 fmt.Println(m) 143 144 // Output: 145 // {"1":1,"2":12,"3":123} 146 } 147 148 func ExampleIntIntMap_Search() { 149 m := gmap.NewIntIntMap() 150 151 m.Set(1, 1) 152 153 value, found := m.Search(1) 154 if found { 155 fmt.Println("find key1 value:", value) 156 } 157 158 value, found = m.Search(2) 159 if !found { 160 fmt.Println("key2 not find") 161 } 162 163 // Output: 164 // find key1 value: 1 165 // key2 not find 166 } 167 168 func ExampleIntIntMap_Get() { 169 m := gmap.NewIntIntMap() 170 171 m.Set(1, 1) 172 173 fmt.Println("key1 value:", m.Get(1)) 174 fmt.Println("key2 value:", m.Get(2)) 175 176 // Output: 177 // key1 value: 1 178 // key2 value: 0 179 } 180 181 func ExampleIntIntMap_Pop() { 182 var m gmap.IntIntMap 183 m.Sets(g.MapIntInt{ 184 1: 1, 185 2: 2, 186 3: 3, 187 4: 4, 188 }) 189 190 fmt.Println(m.Pop()) 191 192 // May Output: 193 // 1 1 194 } 195 196 func ExampleIntIntMap_Pops() { 197 var m gmap.IntIntMap 198 m.Sets(g.MapIntInt{ 199 1: 1, 200 2: 2, 201 3: 3, 202 4: 4, 203 }) 204 fmt.Println(m.Pops(-1)) 205 fmt.Println("size:", m.Size()) 206 207 m.Sets(g.MapIntInt{ 208 1: 1, 209 2: 2, 210 3: 3, 211 4: 4, 212 }) 213 fmt.Println(m.Pops(2)) 214 fmt.Println("size:", m.Size()) 215 216 // May Output: 217 // map[1:1 2:2 3:3 4:4] 218 // size: 0 219 // map[1:1 2:2] 220 // size: 2 221 } 222 223 func ExampleIntIntMap_GetOrSet() { 224 m := gmap.NewIntIntMap() 225 m.Set(1, 1) 226 227 fmt.Println(m.GetOrSet(1, 0)) 228 fmt.Println(m.GetOrSet(2, 2)) 229 230 // Output: 231 // 1 232 // 2 233 } 234 235 func ExampleIntIntMap_GetOrSetFunc() { 236 m := gmap.NewIntIntMap() 237 m.Set(1, 1) 238 239 fmt.Println(m.GetOrSetFunc(1, func() int { 240 return 0 241 })) 242 fmt.Println(m.GetOrSetFunc(2, func() int { 243 return 0 244 })) 245 246 // Output: 247 // 1 248 // 0 249 } 250 251 func ExampleIntIntMap_GetOrSetFuncLock() { 252 m := gmap.NewIntIntMap() 253 m.Set(1, 1) 254 255 fmt.Println(m.GetOrSetFuncLock(1, func() int { 256 return 0 257 })) 258 fmt.Println(m.GetOrSetFuncLock(2, func() int { 259 return 0 260 })) 261 262 // Output: 263 // 1 264 // 0 265 } 266 267 func ExampleIntIntMap_SetIfNotExist() { 268 var m gmap.IntIntMap 269 fmt.Println(m.SetIfNotExist(1, 1)) 270 fmt.Println(m.SetIfNotExist(1, 2)) 271 fmt.Println(m.Map()) 272 273 // Output: 274 // true 275 // false 276 // map[1:1] 277 } 278 279 func ExampleIntIntMap_SetIfNotExistFunc() { 280 var m gmap.IntIntMap 281 fmt.Println(m.SetIfNotExistFunc(1, func() int { 282 return 1 283 })) 284 fmt.Println(m.SetIfNotExistFunc(1, func() int { 285 return 2 286 })) 287 fmt.Println(m.Map()) 288 289 // Output: 290 // true 291 // false 292 // map[1:1] 293 } 294 295 func ExampleIntIntMap_SetIfNotExistFuncLock() { 296 var m gmap.IntIntMap 297 fmt.Println(m.SetIfNotExistFuncLock(1, func() int { 298 return 1 299 })) 300 fmt.Println(m.SetIfNotExistFuncLock(1, func() int { 301 return 2 302 })) 303 fmt.Println(m.Map()) 304 305 // Output: 306 // true 307 // false 308 // map[1:1] 309 } 310 311 func ExampleIntIntMap_Remove() { 312 var m gmap.IntIntMap 313 m.Set(1, 1) 314 315 fmt.Println(m.Remove(1)) 316 fmt.Println(m.Remove(2)) 317 fmt.Println(m.Size()) 318 319 // Output: 320 // 1 321 // 0 322 // 0 323 } 324 325 func ExampleIntIntMap_Removes() { 326 var m gmap.IntIntMap 327 m.Sets(g.MapIntInt{ 328 1: 1, 329 2: 2, 330 3: 3, 331 4: 4, 332 }) 333 334 removeList := make([]int, 2) 335 removeList = append(removeList, 1) 336 removeList = append(removeList, 2) 337 338 m.Removes(removeList) 339 340 fmt.Println(m.Map()) 341 342 // Output: 343 // map[3:3 4:4] 344 } 345 346 func ExampleIntIntMap_Keys() { 347 var m gmap.IntIntMap 348 m.Sets(g.MapIntInt{ 349 1: 1, 350 2: 2, 351 3: 3, 352 4: 4, 353 }) 354 fmt.Println(m.Keys()) 355 356 // May Output: 357 // [1 2 3 4] 358 } 359 360 func ExampleIntIntMap_Values() { 361 var m gmap.IntIntMap 362 m.Sets(g.MapIntInt{ 363 1: 1, 364 2: 2, 365 3: 3, 366 4: 4, 367 }) 368 fmt.Println(m.Values()) 369 370 // May Output: 371 // [1 v2 v3 4] 372 } 373 374 func ExampleIntIntMap_Contains() { 375 var m gmap.IntIntMap 376 m.Sets(g.MapIntInt{ 377 1: 1, 378 2: 2, 379 3: 3, 380 4: 4, 381 }) 382 383 fmt.Println(m.Contains(1)) 384 fmt.Println(m.Contains(5)) 385 386 // Output: 387 // true 388 // false 389 } 390 391 func ExampleIntIntMap_Size() { 392 var m gmap.IntIntMap 393 m.Sets(g.MapIntInt{ 394 1: 1, 395 2: 2, 396 3: 3, 397 4: 4, 398 }) 399 400 fmt.Println(m.Size()) 401 402 // Output: 403 // 4 404 } 405 406 func ExampleIntIntMap_IsEmpty() { 407 var m gmap.IntIntMap 408 fmt.Println(m.IsEmpty()) 409 410 m.Set(1, 1) 411 fmt.Println(m.IsEmpty()) 412 413 // Output: 414 // true 415 // false 416 } 417 418 func ExampleIntIntMap_Clear() { 419 var m gmap.IntIntMap 420 m.Sets(g.MapIntInt{ 421 1: 1, 422 2: 2, 423 3: 3, 424 4: 4, 425 }) 426 427 m.Clear() 428 429 fmt.Println(m.Map()) 430 431 // Output: 432 // map[] 433 } 434 435 func ExampleIntIntMap_Replace() { 436 var m gmap.IntIntMap 437 m.Sets(g.MapIntInt{ 438 1: 1, 439 }) 440 441 var n gmap.IntIntMap 442 n.Sets(g.MapIntInt{ 443 2: 2, 444 }) 445 446 fmt.Println(m.Map()) 447 448 m.Replace(n.Map()) 449 fmt.Println(m.Map()) 450 451 n.Set(2, 1) 452 fmt.Println(m.Map()) 453 454 // Output: 455 // map[1:1] 456 // map[2:2] 457 // map[2:1] 458 } 459 460 func ExampleIntIntMap_LockFunc() { 461 var m gmap.IntIntMap 462 m.Sets(g.MapIntInt{ 463 1: 1, 464 2: 2, 465 3: 3, 466 4: 4, 467 }) 468 469 m.LockFunc(func(m map[int]int) { 470 totalValue := 0 471 for _, v := range m { 472 totalValue += v 473 } 474 fmt.Println("totalValue:", totalValue) 475 }) 476 477 // Output: 478 // totalValue: 10 479 } 480 481 func ExampleIntIntMap_RLockFunc() { 482 var m gmap.IntIntMap 483 m.Sets(g.MapIntInt{ 484 1: 1, 485 2: 2, 486 3: 3, 487 4: 4, 488 }) 489 490 m.RLockFunc(func(m map[int]int) { 491 totalValue := 0 492 for _, v := range m { 493 totalValue += v 494 } 495 fmt.Println("totalValue:", totalValue) 496 }) 497 498 // Output: 499 // totalValue: 10 500 } 501 502 func ExampleIntIntMap_Flip() { 503 var m gmap.IntIntMap 504 m.Sets(g.MapIntInt{ 505 1: 10, 506 }) 507 m.Flip() 508 fmt.Println(m.Map()) 509 510 // Output: 511 // map[10:1] 512 } 513 514 func ExampleIntIntMap_Merge() { 515 var m1, m2 gmap.Map 516 m1.Set(1, "1") 517 m2.Set(2, "2") 518 m1.Merge(&m2) 519 fmt.Println(m1.Map()) 520 521 // May Output: 522 // map[key1:1 key2:2] 523 } 524 525 func ExampleIntIntMap_String() { 526 var m gmap.IntIntMap 527 m.Sets(g.MapIntInt{ 528 1: 1, 529 }) 530 531 fmt.Println(m.String()) 532 533 var m1 *gmap.IntIntMap = nil 534 fmt.Println(len(m1.String())) 535 536 // Output: 537 // {"1":1} 538 // 0 539 } 540 541 func ExampleIntIntMap_MarshalJSON() { 542 var m gmap.IntIntMap 543 m.Sets(g.MapIntInt{ 544 1: 1, 545 2: 2, 546 3: 3, 547 4: 4, 548 }) 549 550 bytes, err := json.Marshal(&m) 551 if err == nil { 552 fmt.Println(gconv.String(bytes)) 553 } 554 555 // Output: 556 // {"1":1,"2":2,"3":3,"4":4} 557 } 558 559 func ExampleIntIntMap_UnmarshalJSON() { 560 var m gmap.IntIntMap 561 m.Sets(g.MapIntInt{ 562 1: 1, 563 2: 2, 564 3: 3, 565 4: 4, 566 }) 567 568 var n gmap.Map 569 570 err := json.Unmarshal(gconv.Bytes(m.String()), &n) 571 if err == nil { 572 fmt.Println(n.Map()) 573 } 574 575 // Output: 576 // map[1:1 2:2 3:3 4:4] 577 } 578 579 func ExampleIntIntMap_UnmarshalValue() { 580 var m gmap.IntIntMap 581 582 n := map[int]int{ 583 1: 1001, 584 2: 1002, 585 3: 1003, 586 } 587 588 if err := gconv.Scan(n, &m); err == nil { 589 fmt.Printf("%#v", m.Map()) 590 } 591 // Output: 592 // map[int]int{1:1001, 2:1002, 3:1003} 593 }