盘点前端、后端工具包

1、视频API或者播放器API

API地址:https://h5player.bytedance.com/api/

开源地址:https://github.com/bytedance/xgplayer

安装: npm install xgplayer
在这里插入图片描述
2、位运算工具类

import java.math.BigDecimal;
import java.math.RoundingMode;

public class BitUtils {

    /**
     * 获取运算数指定位置的值<br>
     * 例如: 0000 1011 获取其第 0 位的值为 1, 第 2 位 的值为 0<br>
     *
     * @param source 需要运算的数
     * @param pos 指定位置 (0<=pos<=7)
     * @return 指定位置的值(0 or 1)
     */
    public static byte getBitValue(byte source, int pos) {
        return (byte) ((source >> pos) & 1);
    }

    /**
     * 将运算数指定位置的值置为指定值<br>
     * 例: 0000 1011 需要更新为 0000 1111, 即第 2 位的值需要置为 1<br>
     *
     * @param source  需要运算的数
     * @param pos 指定位置 (0<=pos<=7)
     * @param value  只能取值为 0, 或 1, 所有大于0的值作为1处理, 所有小于0的值作为0处理
     * @return 运算后的结果数
     */
    public static byte setBitValue(byte source, int pos, byte value) {
        byte mask = (byte) (1 << pos);
        if (value > 0) {
            source |= mask;
        } else {
            source &= (~mask);
        }
        return source;
    }

    /**
     * 将运算数指定位置取反值<br>
     * 例: 0000 1011 指定第 3 位取反, 结果为 0000 0011; 指定第2位取反, 结果为 0000 1111<br>
     *
     * @param source 指定位置 (0<=pos<=7)
     * @param pos
     * @return 运算后的结果数
     */
    public static byte reverseBitValue(byte source, int pos) {
        byte mask = (byte) (1 << pos);
        return (byte) (source ^ mask);
    }

    /**
     * 检查运算数的指定位置是否为1<br>
     *
     * @param source 需要运算的数
     * @param pos 指定位置 (0<=pos<=7)
     * @return true 表示指定位置值为1, false 表示指定位置值为 0
     */
    public static boolean checkBitValue(byte source, int pos) {
        source = (byte) (source >>> pos);
        return (source & 1) == 1;
    }


    public static int turnOffRightmost1(final int input) {
        return input & (input - 1);
    }

    public static int turnOnRightmost0(final int input) {
        return input | (input + 1);
    }

    public static int turnOffTrailing1s(final int input) {
        return input & (input + 1);
    }

    public static int turnOnTrailing0s(final int input) {
        return input | (input - 1);
    }

    public static int markPositionOfRightmost0With1(final int input) {
        return ~input & (input + 1);
    }

    public static int markPositionOfRightmost1With0(final int input) {
        return ~input | (input - 1);
    }

    public static int createWordWith1sAtPositionsOfTrailing0s(final int input) {
        return ~input & (input - 1);
    }

    public static int createWordWith0sAtPositionsOfTrailing1s(final int input) {
        return ~input | (input + 1);
    }

    public static int markPositionOfRightmost1BitWith1(final int input) {
        return input & (input * -1);
    }

    public static int markRightmost1WithContiguous1s(final int input) {
        return input ^ (input - 1);
    }

    public static int markRightmost0WithContiguous1s(final int input) {
        return input ^ (input + 1);
    }

    public static int clearRightmostContiguousStringOf1s(final int input) {
        return (markPositionOfRightmost1BitWith1(input) + input) & input;
    }

    public static boolean isBaseTwoAMultipleOfBaseTwo(final int baseTwoB, final int baseTwoA) {
        if (baseTwoB < baseTwoA) {
            throw new IllegalArgumentException();
        }
        if (baseTwoA < 0) {
            throw new IllegalArgumentException();
        }
        if ((clearRightmostContiguousStringOf1s(baseTwoB) | clearRightmostContiguousStringOf1s(baseTwoA)) == 0){
            return true;
        }

        return false;
    }

    public static int abs(final int input) {
        int y = input >> 31;
        return (input ^ y) - y;
    }

    public static int nabs(final int input) {
        int y = input >> 31;
        return y - (input ^ y);
    }


    /**
     * 计算两个无符号整数的四舍五入平均值
     */
    public static int floorAvg(final int x, final int y) {
        return ((x & y) + ((x ^ y) >>> 1));
    }

    /**
     * 计算两个无符号整数的四舍五入平均值
     */
    public static int ceilingAvg(final int x, final int y) {
        return ((x | y) - ((x ^ y) >>> 1));
    }

    public static int threeValueCompare(final int x, final int y) {
        return (lt(y, x) - lt(x, y));
    }

    public static int eq(final int x, final int y) {
        return (abs(x - y) - 1) >>> 31;
    }

    public static int ne(final int x, final int y) {
        return nabs(x - y) >>> 31;
    }

    public static int lt(final int x, final int y) {
        return ((x - y) ^ ((x ^ y) & ((x - y) ^ x))) >>> 31;
    }

    public static int gt(final int x, final int y) {
        return lt(y, x);
    }

    public static int le(final int x, final int y) {
        return ((x | ~y) & ((x ^ y) | (~(y - x)))) >>> 31;
    }

    public static int ge(final int x, final int y) {
        return le(y, x);
    }

    public static int doz(final int x, final int y) {
        if (x >= y ) {
            return x - y;
        }
        return 0;
    }

    /**
     * 正负数都包含在里面,不用分开处理
     * @param a
     * @param b
     * @return
     */
    private static int binaryAdd(int a, int b) {
        // 不考虑进位的和
        int s = a ^ b;
        // 进位
        int jw = a & b;
        // 下面是 s + (jw<<1) 的计算
        while (jw != 0) {
            // 保存s + (jw<<1)的进位
            int jw_temp = s & (jw << 1);
            // 保存s + (jw<<1)的和,不包含进位
            s = s ^ (jw << 1);
            // 赋值之后,还是计算s+(jw<<1),依旧是计算:进位以及不进位的和,当进位为0时,不进位的和就是最终的计算结果
            jw = jw_temp;
        }
        return s;
    }

    /**
     * 计算a*b
     * @param a
     * @param b
     * @return
     */
    private static int binaryMulti(int a, int b) {
        if (a == 0 || b == 0) {
            return 0;
        }
        int res = 0;
        int base = a;
        while (b != 0) {
            if ((b & 1) != 0) {
                res = binaryAdd(res, base);
            }
            b >>= 1;
            base <<= 1;
        }

        return res;
    }

    /**
     * 计算a*b
     * @param a
     * @param b
     * @return
     */
    private static int binaryMulti2(int a, int b) {
        if (a == 0 || b == 0) {
            return 0;
        }
        if(b>a) {
            int tmp = a;
            a = b;
            b = tmp;
        }
        int res = 0;
        int shift = 0;
        while(b!=0) {
            if((b&1)!=0) {
                res += (a<<shift);
            }
            shift += 1;
            b >>= 1;
        }
        return res;
    }

    /**
     *  计算a/b
     * @param a
     * @param b
     * @return
     * @throws Exception
     */
    private static int binaryDiv(int a, int b) throws Exception {
        if (b == 0) {
            throw new Exception("分母不能为0");
        }
        boolean flag = false;
        if ((a ^ b) < 0) {
            // 表示a,b异号;
            flag = true;
        }
        a = a >= 0 ? a : -a;
        b = b >= 0 ? b : -b;

        int res = 0;
        //依次获取a的最高位
        int aux = 0;
        // 用来依次获取分母的最高位bit
        int mask = 0x40_00_00_00;
        while (mask != 0) {
            // 这里注意处理,尤其是后半部分表达式,很容易写成aux
            aux = (aux << 1) | ((a & mask) == 0 ? 0 : 1);
            // = (aux<<1) |
            // (a&mask);
            if (aux >= b) {
                res = (res << 1) | 1;
                aux -= b;
            } else {
                res = (res << 1) | 0;
            }
            mask >>= 1;
        }
        return flag ? -res : res;
    }

    /**
     * 加
     * @param a
     * @param b
     * @return
     */
    public static int add(int a, int b) {
        if (b == 0) {
            return a;
        } else {
            return add(a ^ b, (a & b) << 1);
        }
    }

    /**
     * 加
     * @param a
     * @param b
     * @return
     */
    public static int add2(int a, int b) {
        int sum = a;
        while (b != 0) {
            sum = a ^ b;
            b = (a & b) << 1;
            a = sum;
        }
        return sum;
    }

    /**
     * 加
     * @param a
     * @param b
     * @return
     */
    public int add3(int a, int b) {
        while (b != 0) {
            int j = a ^ b;
            int k = (a & b) << 1;
            a = j;
            b = k;
        }
        return a;
    }
    /**
     * 减
     * @param a
     * @param b
     * @return
     */
    public static int minus(int a, int b) {
        return add(a, add(~b, 1));
    }

    /**
     * 乘
     * @param a
     * @param b
     * @return
     */
    public static int multiply(int a, int b) {
        //将乘数和被乘数都取绝对值
        int A = a < 0 ? add(~a, 1) : a;
        int B = b < 0 ? add(~b, 1) : b;
        //计算绝对值的乘积
        int P = 0;
        while (B != 0) {
            //取乘数的二进制的最后一位,0 or 1
            if ((B & 1) != 0) {
                P = add(P, A);
            }
            A = A << 1;
            B = B >> 1;
        }
        //计算乘积的符号
        if ((a ^ b) < 0) {
            P = add(~P, 1);
        }
        return P;
    }

    /**
     * 除
     * @param a
     * @param b
     * @return
     */
    public static int[] divide(int a, int b) {
        //对被除数和除数取绝对值
        int A = a < 0 ? add(~a, 1) : a;
        int B = b < 0 ? add(~b, 1) : b;
        //对被除数和除数的绝对值求商
        // 余数C
        int C = A;
        // 商N
        int N = 0;
        while (C >= B) {
            // C-B
            C = minus(C, B);
            // N+1
            N = add(N, 1);
        }
        // 求商的符号
        if ((a ^ b) < 0) {
            N = add(~N, 1);
        }
        // 求余数的符合
        if (a < 0) {
            C = add(~C, 1);
        }
        return new int[]{N, C};
    }

    /**
     * 除
     * @param a
     * @param b
     * @return
     */
    public static int[] divide2(int a, int b) {
        // 对被除数和除数取绝对值
        int A = a < 0 ? add(~a, 1) : a;
        int B = b < 0 ? add(~b, 1) : b;
        // 商 N
        int N = 0;
        for (int i = 31; i >= 0; i--) {
            // 未使用A>=(B<<i)进行判断,因为只有左移B时舍弃的高位不包含1,才相当于该数乘以2的i次方.
            // A ÷ 2^i >= B
            if ((A >> i) >= B) {
                // N = N + 2^i
                N += (1 << i);
                // A = A - B*2^i
                A -= (B << i);
            }
        }
        // 余数C
        int C = A;
        // 求商的符号
        if ((a ^ b) < 0) {
            N = add(~N, 1);
        }
        // 求余数的符号
        if (a < 0) {
            C = add(~C, 1);
        }
        return new int[]{N, C};
    }


    /**
     * 除法计算
     *
     * @param x 被除数
     * @param y 除数
     * @return 结果,保留两位小数
     */
    public static BigDecimal divide(long x, long y) {
        if (y == 0) {
            return BigDecimal.ZERO;
        }
        return BigDecimal.valueOf(x).divide(BigDecimal.valueOf(y), 2, RoundingMode.HALF_UP);
    }

    /**
     * 除法计算
     *
     * @param x     被除数
     * @param y     除数
     * @param scale 精确位数
     * @return 结果,保留两位小数
     */
    public static BigDecimal divide(long x, long y, int scale) {
        if (y == 0) {
            return BigDecimal.ZERO;
        }
        return BigDecimal.valueOf(x).divide(BigDecimal.valueOf(y), scale, RoundingMode.HALF_UP);
    }

    /**
     * 除法计算
     *
     * @param x     被除数
     * @param y     除数
     * @param scale 精确位数
     * @return 结果,保留两位小数
     */
    public static BigDecimal divide(BigDecimal x, BigDecimal y, int scale) {
        if (y.intValue() == 0) {
            return BigDecimal.ZERO;
        }
        return x.divide(y, scale, RoundingMode.HALF_UP);
    }

    /**
     * 毫秒数转换成秒
     *
     * @param val 毫秒数
     * @return 对应的秒
     */
    public static BigDecimal millis2Seconds(BigDecimal val) {
        return val.divide(BigDecimal.valueOf(1000), 2, RoundingMode.HALF_UP);
    }

    /**
     * 获取百分比值
     *
     * @param val 值
     * @return 乘以100后的值
     */
    public static BigDecimal getPercent(BigDecimal val) {
        if (val == null) {
            return BigDecimal.ZERO;
        }
        return val.multiply(BigDecimal.valueOf(100));
    }
    
}

3、封装了大多数nlp项目中常用工具

开源地址:https://github.com/NLPchina/nlp-lang

依赖:

    <dependency>
        <groupId>org.nlpcn</groupId>
        <artifactId>nlp-lang</artifactId>
        <version>1.7.6</version>
    </dependency>

4、待续。。。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值