-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathint.go
462 lines (430 loc) · 11 KB
/
int.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
package goutils
import (
"errors"
"strconv"
"sync/atomic"
"unsafe"
)
/**
@author : Jerbe - The porter from Earth
@time : 2023/8/18 00:35
@describe :
*/
func IntLen(val int) int {
return Int64Len(int64(val))
}
func Int8Len(val int8) int {
return Int64Len(int64(val))
}
func Int16Len(val int16) int {
return Int64Len(int64(val))
}
func Int32Len(val int32) int {
return Int64Len(int64(val))
}
func Int64Len(val int64) int {
return len(strconv.FormatInt(val, 10))
}
//----------------------------------------------------------
//------------------- SORT ---------------------------------
//----------------------------------------------------------
// IntSort 排序整型
// 返回的数据总是 a <= b
func IntSort(a, b int) (int, int) {
if a > b {
a, b = b, a
}
return a, b
}
// Int8Sort 排序整型
// 返回的数据总是 a <= b
func Int8Sort(a, b int8) (int8, int8) {
if a > b {
a, b = b, a
}
return a, b
}
// Int16Sort 排序整型
// 返回的数据总是 a <= b
func Int16Sort(a, b int16) (int16, int16) {
if a > b {
a, b = b, a
}
return a, b
}
// Int32Sort 排序整型
// 返回的数据总是 a <= b
func Int32Sort(a, b int32) (int32, int32) {
if a > b {
a, b = b, a
}
return a, b
}
// Int64Sort 排序整型
// 返回的数据总是 a <= b
func Int64Sort(a, b int64) (int64, int64) {
if a > b {
a, b = b, a
}
return a, b
}
// IntBetween dest在指定范围内
// 是一个闭区间 from <= dest <= to
func IntBetween(dest, from, to int) bool {
if from <= dest && dest <= to {
return true
}
return false
}
// Int8Between dest在指定范围内
// 是一个闭区间 from <= dest <= to
func Int8Between(dest, from, to int8) bool {
if from <= dest && dest <= to {
return true
}
return false
}
// Int16Between dest在指定范围内
// 是一个闭区间 from <= dest <= to
func Int16Between(dest, from, to int16) bool {
if from <= dest && dest <= to {
return true
}
return false
}
// Int32Between dest在指定范围内
// 是一个闭区间 from <= dest <= to
func Int32Between(dest, from, to int32) bool {
if from <= dest && dest <= to {
return true
}
return false
}
// Int64Between dest在指定范围内
// 是一个闭区间 from <= dest <= to
func Int64Between(dest, from, to int64) bool {
if from <= dest && dest <= to {
return true
}
return false
}
//----------------------------------------------------------
//----------------- ATOMIC ---------------------------------
//----------------------------------------------------------
// IntBitSet 填充设置某一位的数值为1,从右到左方向
func IntBitSet(val interface{}, position int) interface{} {
switch v := val.(type) {
case int:
return v | (1 << position)
case uint:
return v | (1 << position)
case int8:
return v | (1 << position)
case uint8:
return v | (1 << position)
case int16:
return v | (1 << position)
case uint16:
return v | (1 << position)
case int32:
return v | (1 << position)
case uint32:
return v | (1 << position)
case int64:
return v | (1 << position)
case uint64:
return v | (1 << position)
case *int:
return *v | (1 << position)
case *uint:
return *v | (1 << position)
case *int8:
return *v | (1 << position)
case *uint8:
return *v | (1 << position)
case *int16:
return *v | (1 << position)
case *uint16:
return *v | (1 << position)
case *int32:
return *v | (1 << position)
case *uint32:
return *v | (1 << position)
case *int64:
return *v | (1 << position)
case *uint64:
return *v | (1 << position)
default:
panic("no int")
}
}
// IntBitClear 清理设置某一位的数值为0,从右到左方向
func IntBitClear(val interface{}, position int) interface{} {
switch v := val.(type) {
case int:
return v &^ (1 << position)
case uint:
return v &^ (1 << position)
case int8:
return v &^ (1 << position)
case uint8:
return v &^ (1 << position)
case int16:
return v &^ (1 << position)
case uint16:
return v &^ (1 << position)
case int32:
return v &^ (1 << position)
case uint32:
return v &^ (1 << position)
case int64:
return v &^ (1 << position)
case uint64:
return v &^ (1 << position)
case *int:
return *v &^ (1 << position)
case *uint:
return *v &^ (1 << position)
case *int8:
return *v &^ (1 << position)
case *uint8:
return *v &^ (1 << position)
case *int16:
return *v &^ (1 << position)
case *uint16:
return *v &^ (1 << position)
case *int32:
return *v &^ (1 << position)
case *uint32:
return *v &^ (1 << position)
case *int64:
return *v &^ (1 << position)
case *uint64:
return *v &^ (1 << position)
default:
panic("no int")
}
}
// IntBitCheck 检测某一位的数值是否为1,从右到左方向
func IntBitCheck(val interface{}, position int) bool {
switch v := val.(type) {
case int:
return v&(1<<position) != 0
case uint:
return v&(1<<position) != 0
case int8:
return v&(1<<position) != 0
case uint8:
return v&(1<<position) != 0
case int16:
return v&(1<<position) != 0
case uint16:
return v&(1<<position) != 0
case int32:
return v&(1<<position) != 0
case uint32:
return v&(1<<position) != 0
case int64:
return v&(1<<position) != 0
case uint64:
return v&(1<<position) != 0
case *int:
return *v&(1<<position) != 0
case *uint:
return *v&(1<<position) != 0
case *int8:
return *v&(1<<position) != 0
case *uint8:
return *v&(1<<position) != 0
case *int16:
return *v&(1<<position) != 0
case *uint16:
return *v&(1<<position) != 0
case *int32:
return *v&(1<<position) != 0
case *uint32:
return *v&(1<<position) != 0
case *int64:
return *v&(1<<position) != 0
case *uint64:
return *v&(1<<position) != 0
default:
panic("no int")
}
}
//----------------------------------------------------------
//----------------- ATOMIC ---------------------------------
//----------------------------------------------------------
// AtomicAddInt 原子增加
func AtomicAddInt(dst interface{}, delta int64) interface{} {
switch d := dst.(type) {
case *int64:
return atomic.AddInt64(d, int64(delta))
case *int32:
return atomic.AddInt32(d, int32(delta))
case *int16:
n := (*int32)(unsafe.Pointer(d))
return int16(atomic.AddInt32(n, int32(delta)))
case *int8:
n := (*int32)(unsafe.Pointer(d))
return int8(atomic.AddInt32(n, int32(delta)))
case *int:
n := (*int32)(unsafe.Pointer(d))
return int(atomic.AddInt32(n, int32(delta)))
case *uint:
n := (*uint32)(unsafe.Pointer(d))
return uint(atomic.AddUint32(n, uint32(delta)))
case *uint8:
n := (*uint32)(unsafe.Pointer(d))
return uint8(atomic.AddUint32(n, uint32(delta)))
case *uint16:
n := (*uint32)(unsafe.Pointer(d))
return uint16(atomic.AddUint32(n, uint32(delta)))
case *uint32:
return atomic.AddUint32(d, uint32(delta))
case *uint64:
return atomic.AddUint64(d, uint64(delta))
default:
panic(errors.New("unsupported type"))
}
}
// AtomicStoreInt 原子存储
func AtomicStoreInt(dst interface{}, val int64) {
switch d := dst.(type) {
case *int64:
atomic.StoreInt64(d, int64(val))
case *int32:
atomic.StoreInt32(d, int32(val))
case *int16:
n := (*int32)(unsafe.Pointer(d))
atomic.StoreInt32(n, int32(val))
case *int8:
n := (*int32)(unsafe.Pointer(d))
atomic.StoreInt32(n, int32(val))
case *int:
n := (*int32)(unsafe.Pointer(d))
atomic.StoreInt32(n, int32(val))
case *uint:
n := (*uint32)(unsafe.Pointer(d))
atomic.StoreUint32(n, uint32(val))
case *uint8:
n := (*uint32)(unsafe.Pointer(d))
atomic.StoreUint32(n, uint32(val))
case *uint16:
n := (*uint32)(unsafe.Pointer(d))
atomic.StoreUint32(n, uint32(val))
case *uint32:
atomic.StoreUint32(d, uint32(val))
case *uint64:
atomic.StoreUint64(d, uint64(val))
default:
panic(errors.New("unsupported type"))
}
}
// AtomicLoadInt 原子加载
func AtomicLoadInt(dst interface{}) interface{} {
switch d := dst.(type) {
case *int64:
return atomic.LoadInt64(d)
case *int32:
return atomic.LoadInt32(d)
case *int16:
n := (*int32)(unsafe.Pointer(d))
return int16(atomic.LoadInt32(n))
case *int8:
n := (*int32)(unsafe.Pointer(d))
return int8(atomic.LoadInt32(n))
case *int:
n := (*int32)(unsafe.Pointer(d))
return int(atomic.LoadInt32(n))
case *uint:
n := (*uint32)(unsafe.Pointer(d))
return uint(atomic.LoadUint32(n))
case *uint8:
n := (*uint32)(unsafe.Pointer(d))
return uint8(atomic.LoadUint32(n))
case *uint16:
n := (*uint32)(unsafe.Pointer(d))
return uint16(atomic.LoadUint32(n))
case *uint32:
return atomic.LoadUint32(d)
case *uint64:
return atomic.LoadUint64(d)
default:
panic(errors.New("unsupported type"))
}
}
// AtomicCompareAndSwapInt 原子对比并替换
func AtomicCompareAndSwapInt(dst interface{}, oldVal, newVal interface{}) bool {
switch d := dst.(type) {
case *int64:
o, n := oldVal.(int64), newVal.(int64)
return atomic.CompareAndSwapInt64(d, o, n)
case *int32:
o, n := oldVal.(int32), newVal.(int32)
return atomic.CompareAndSwapInt32(d, o, n)
case *int16:
o, n, nd := int32(oldVal.(int16)), int32(newVal.(int16)), (*int32)(unsafe.Pointer(d))
return atomic.CompareAndSwapInt32(nd, o, n)
case *int8:
o, n, nd := int32(oldVal.(int8)), int32(newVal.(int8)), (*int32)(unsafe.Pointer(d))
return atomic.CompareAndSwapInt32(nd, o, n)
case *int:
o, n, nd := int32(oldVal.(int)), int32(newVal.(int)), (*int32)(unsafe.Pointer(d))
return atomic.CompareAndSwapInt32(nd, o, n)
case *uint:
o, n, nd := uint32(oldVal.(uint)), uint32(newVal.(uint)), (*uint32)(unsafe.Pointer(d))
return atomic.CompareAndSwapUint32(nd, o, n)
case *uint8:
o, n, nd := uint32(oldVal.(uint8)), uint32(newVal.(uint8)), (*uint32)(unsafe.Pointer(d))
return atomic.CompareAndSwapUint32(nd, o, n)
case *uint16:
o, n, nd := uint32(oldVal.(uint16)), uint32(newVal.(uint16)), (*uint32)(unsafe.Pointer(d))
return atomic.CompareAndSwapUint32(nd, o, n)
case *uint32:
o, n := oldVal.(uint32), newVal.(uint32)
return atomic.CompareAndSwapUint32(d, o, n)
case *uint64:
o, n := oldVal.(uint64), newVal.(uint64)
return atomic.CompareAndSwapUint64(d, o, n)
default:
panic(errors.New("unsupported type"))
}
}
// AtomicSwapInt 原子替换
func AtomicSwapInt(dst interface{}, newVal interface{}) interface{} {
switch d := dst.(type) {
case *int64:
n := newVal.(int64)
return atomic.SwapInt64(d, n)
case *int32:
n := newVal.(int32)
return atomic.SwapInt32(d, n)
case *int16:
n, nd := int32(newVal.(int16)), (*int32)(unsafe.Pointer(d))
return atomic.SwapInt32(nd, n)
case *int8:
n, nd := int32(newVal.(int8)), (*int32)(unsafe.Pointer(d))
return atomic.SwapInt32(nd, n)
case *int:
n, nd := int32(newVal.(int)), (*int32)(unsafe.Pointer(d))
return atomic.SwapInt32(nd, n)
case *uint:
n, nd := uint32(newVal.(uint)), (*uint32)(unsafe.Pointer(d))
return atomic.SwapUint32(nd, n)
case *uint8:
n, nd := uint32(newVal.(uint8)), (*uint32)(unsafe.Pointer(d))
return atomic.SwapUint32(nd, n)
case *uint16:
n, nd := uint32(newVal.(uint16)), (*uint32)(unsafe.Pointer(d))
return atomic.SwapUint32(nd, n)
case *uint32:
n := newVal.(uint32)
return atomic.SwapUint32(d, n)
case *uint64:
n := newVal.(uint64)
return atomic.SwapUint64(d, n)
default:
panic(errors.New("unsupported type"))
}
}