如何用按位操作替换此String操作? [英] How to replace this String operations with Bitwise operations?

查看:129
本文介绍了如何用按位操作替换此String操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑到我在Base对象中具有以下字段:

Considering I have this fields in a Base object:

//these fields have their bits constantly permuted
//and have their values defined to "011233455677..."
//to integers acts like Strings

var ul = 0x011233
var ur = 0x455677
var dl = 0x998bba
var dr = 0xddcffe

考虑到我有这种方法可以将这些字段作为字符串值进行操作:

And considering I have this method which operates those fields as String values:

private fun Cubo.isSolved(): Boolean {             
    val solved = Base()

    //function to append "leading zero" to the hex integer
    fun fill(s: String) = if (s.length == 5) "0$s" else s

    //all these fields are declared at the global escope

    //converted ul, ur, dl, dr from solved obj to hex string
    a1 = fill(Integer.toHexString(solved.ul))
    b1 = fill(Integer.toHexString(solved.ur))
    c1 = fill(Integer.toHexString(solved.dl))
    d1 = fill(Integer.toHexString(solved.dr))

    //concats the converteds ul and ur into a new one
    ab1 = a1 + b1
    //concats the converteds dl and dr into a new one
    cd1 = c1 + d1

    //do the same with fields of THIS object
    a2 = fill(Integer.toHexString(this.ul))
    b2 = fill(Integer.toHexString(this.ur))
    c2 = fill(Integer.toHexString(this.dl))
    d2 = fill(Integer.toHexString(this.dr))
    ab2 = a2 + b2
    cd2 = c2 + d2

    //checks if concatenated fields from THIS object exists inside the 
    //duplicated [solved] object fields.
    //This will help me to check if fields from THIS object are 
    //cyclic/circular permutations of the [solved] object.
    return (ab1 + ab1).contains(ab2) && (cd1 + cd1).contains(cd2)
}

我的目标是一旦字段为整数,就知道如何用按位运算替换该运算?

My goal is to know how to replace that operations with bitwise operations, once the fields are integers?

我之所以这样做,是因为我的应用程序运行缓慢,并且一旦被调用数千次,该方法就会降低其性能,然后意识到可以通过使用位操作来提高应用程序性能.

I'm trying this because my app is so slow and this method is reducing its performance once it is called thousand times in loop, then realized a way of improve my application performance could be using the bitwising operations.

为简化此方法的思想,它仅用于验证字段THIS对象是否对应于"SOLVED"对象的字段,但这是考虑到测试对象的字段为循环排列.

And to simplify the idea of ​​this method, it used just to verify that the field THIS object corresponds to the fields of a "SOLVED" object, but this is done considering the possibility that the fields of the test object are cyclically permuted.

推荐答案

假设您的目标状态是固定的,并且固定大小为12个十六进制字符,那么我会做一些不同的事情.我首先要预先计算solved的所有旋转,然后使用简单的equals对照它们进行检查.我不确定将其存储在您的应用程序中何处有意义.在这段代码中,我将其作为target参数传递.

Assuming your target state is fixed and has a fixed size of 12 hex chars, I'd do it a bit differently. I'd first pre-calculated all the rotations of the solved and then checked against them using simple equals. I'm not sure where it makes sense to store that in your application. In this code I pass that as the target parameter.

class Cubo() {

    var ul: Int = 0x011233
    var ur: Int = 0x455677
    var dl: Int = 0x998bba
    var dr: Int = 0xddcffe

    constructor(ul: Int, ur: Int, dl: Int, dr: Int) : this() {
        this.ul = ul
        this.ur = ur
        this.dl = dl
        this.dr = dr
    }

    fun isSolved(target: TargetState): Boolean {
        return target.checkSolved(this)
    }
}


class TargetState(val uRot: AllRotations, val dRot: AllRotations) {
    constructor(c: Cubo) : this(AllRotations(concat(c.ul, c.ur)), AllRotations(concat(c.dl, c.dr))) {
    }

    fun checkSolved(c: Cubo): Boolean {
        val u = concat(c.ul, c.ur)
        val d = concat(c.dl, c.dr)
        return uRot.contains(u) and dRot.contains(d)
    }

    companion object {
        fun concat(l: Int, r: Int): Long {
            return l.toLong().shl(AllRotations.singleBitsCount).or(r.toLong())
        }

    }
}


class AllRotations(value: Long) {

    val rotatedValues = Array<Long>(doubleDigitsCount) { i -> rotate(value, digitSize * i) }

    fun contains(test: Long): Boolean {
        for (v in rotatedValues) {
            if (v == test)
                return true
        }
        return false
    }


    companion object {
        const val singleDigitsCount = 6
        const val doubleDigitsCount = 2 * singleDigitsCount
        const val digitSize = 4
        const val singleBitsCount = digitSize * singleDigitsCount
        const val doubleBitsCount = digitSize * doubleDigitsCount
        const val mask = 1L.shl(doubleBitsCount) - 1


        fun rotate(value: Long, shift: Int): Long {
            val hi = value.shl(shift).and(mask)
            val lo = value.shr(doubleBitsCount - shift)
            return hi.or(lo)
        }
    }
}

这是一个有效的简单测试:

Here is a simple test that it works:

val solved = Cubo(0x011233, 0x455677, 0, 0)
val targetState = TargetState(solved)

val c1 = Cubo(0x233455, 0x677011, 0, 0)
val c2 = Cubo(0x455677, 0x011233, 0, 0)
val cbad = Cubo(0x455677, 0x011233, 1, 0)

println(c1.isSolved(targetState))
println(c2.isSolved(targetState))
println(cbad.isSolved(targetState))

这篇关于如何用按位操作替换此String操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆