Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package common
import (
"strconv"
"ascend-common/common-utils/hwlog"
)
type int32Tool struct {
}
type int64Tool struct {
}
type stringTool struct {
}
var Int32Tool int32Tool
var Int64Tool int64Tool
var StringTool stringTool
func Contains[T comparable](sources []T, target T) bool {
for _, sourceNum := range sources {
if sourceNum == target {
return true
}
}
return false
}
func (i int32Tool) Contains(sources []int32, target int32) bool {
for _, sourceNum := range sources {
if sourceNum == target {
return true
}
}
return false
}
func (i int64Tool) SameElement(sources, targets []int64) bool {
for _, source := range sources {
for _, target := range targets {
if source == target {
return true
}
}
}
return false
}
func (i int64Tool) Remove(sources []int64, target int64) []int64 {
if len(sources) == 0 {
return sources
}
index := i.Index(sources, target)
if index == -1 {
return sources
}
return i.Remove(append(sources[:index], sources[index+1:]...), target)
}
func (i int64Tool) Index(sources []int64, target int64) int {
for index, source := range sources {
if source == target {
return index
}
}
return -1
}
func (i int64Tool) ToHexString(sources []int64) string {
var target string
for i, source := range sources {
if i == 0 {
target = FormatFaultCodeHex(source)
continue
}
target = target + "," + FormatFaultCodeHex(source)
}
return target
}
func (i int64Tool) Abs(var1, var2 int64) int64 {
if var1 > var2 {
return var1 - var2
}
return var2 - var1
}
func (i int64Tool) Contains(sources []int64, target int64) bool {
for _, sourceNum := range sources {
if sourceNum == target {
return true
}
}
return false
}
func (s stringTool) HexStringToInt(sources []string) []int64 {
intSlice := make([]int64, 0, len(sources))
for _, source := range sources {
num, err := strconv.ParseInt(source, Hex, 0)
if err != nil {
hwlog.RunLog.Errorf("parse hex int failed and skip it, string: %s", source)
continue
}
intSlice = append(intSlice, num)
}
return intSlice
}
func (s stringTool) Contains(sources []string, target string) bool {
for _, v := range sources {
if v == target {
return true
}
}
return false
}