|
1 | 1 | package org.ton.sdk.cell.internal |
2 | 2 |
|
| 3 | +import kotlinx.io.bytestring.unsafe.UnsafeByteStringApi |
| 4 | +import kotlinx.io.bytestring.unsafe.UnsafeByteStringOperations |
| 5 | +import org.ton.sdk.bitstring.BitString |
| 6 | +import org.ton.sdk.bitstring.unsafe.UnsafeBitStringApi |
| 7 | +import org.ton.sdk.bitstring.unsafe.UnsafeBitStringOperations |
3 | 8 | import org.ton.sdk.cell.Cell |
4 | 9 | import org.ton.sdk.cell.CellDescriptor |
5 | 10 | import org.ton.sdk.crypto.HashBytes |
6 | 11 |
|
7 | 12 | internal class PrunedCell( |
8 | 13 | override val descriptor: CellDescriptor, |
9 | 14 | val hash: HashBytes, |
10 | | - val data: ByteArray |
| 15 | + val data: BitString |
11 | 16 | ) : Cell { |
12 | | - override fun virtualize(offset: Int): Cell { |
13 | | - TODO("Not yet implemented") |
14 | | - } |
| 17 | + override fun virtualize(offset: Int): Cell = VirtualCell(this, offset) |
15 | 18 |
|
| 19 | + @OptIn(UnsafeByteStringApi::class, UnsafeBitStringApi::class) |
16 | 20 | override fun hash(level: Int): HashBytes { |
17 | 21 | val hashIndex = descriptor.levelMask.apply(level).hashIndex |
18 | | - if (hashIndex == this.level) { |
| 22 | + val cellLevel = this.level |
| 23 | + if (hashIndex == cellLevel) { |
19 | 24 | return hash |
20 | 25 | } |
21 | | - |
22 | | - TODO() |
| 26 | + val offset = 2 + hashIndex * 32 |
| 27 | + return UnsafeBitStringOperations.withByteArrayUnsafe(data) { |
| 28 | + HashBytes( |
| 29 | + UnsafeByteStringOperations.wrapUnsafe(it.copyOfRange(offset, offset + 32)) |
| 30 | + ) |
| 31 | + } |
23 | 32 | } |
24 | 33 |
|
| 34 | + @Suppress("OPT_IN_USAGE") |
25 | 35 | override fun depth(level: Int): Int { |
26 | | - TODO("Not yet implemented") |
| 36 | + val hashIndex = descriptor.levelMask.apply(level).hashIndex |
| 37 | + val cellLevel = this.level |
| 38 | + if (hashIndex == cellLevel) { |
| 39 | + return 0 |
| 40 | + } |
| 41 | + val offset = 2 + cellLevel * 32 + hashIndex * 2 |
| 42 | + return UnsafeBitStringOperations.withByteArrayUnsafe(data) { |
| 43 | + (it[offset].toInt() and 0xFF shl 8) or |
| 44 | + it[offset + 1].toInt() and 0xFF |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + override fun toString(): String { |
| 49 | + return "PrunedCell(descriptor=$descriptor, hash=$hash, data=$data)" |
| 50 | + } |
| 51 | + |
| 52 | + override fun equals(other: Any?): Boolean { |
| 53 | + if (this === other) return true |
| 54 | + if (other == null || this::class != other::class) return false |
| 55 | + |
| 56 | + other as PrunedCell |
| 57 | + |
| 58 | + if (descriptor != other.descriptor) return false |
| 59 | + if (hash != other.hash) return false |
| 60 | + if (data != other.data) return false |
| 61 | + |
| 62 | + return true |
| 63 | + } |
| 64 | + |
| 65 | + override fun hashCode(): Int { |
| 66 | + var result = descriptor.hashCode() |
| 67 | + result = 31 * result + hash.hashCode() |
| 68 | + result = 31 * result + data.hashCode() |
| 69 | + return result |
27 | 70 | } |
28 | 71 | } |
0 commit comments