Java原语封装选择的方式;避免“魔法”原语

发布于 2024-12-28 07:40:43 字数 502 浏览 1 评论 0原文

我正在编写一个程序,它创建大量大型数组来存储数据。所有这些数据都必须保存在 RAM 中,因此我避免使用对象,并且当前使用 Shorts 来节省空间。这些 Shorts 作为 ID 号,可以放入查找类中以按需获取相应的对象。我最近质疑我是否需要整个 2 个字节的 Short,所以我现在想知道是否有办法定义存储在代码中一个位置的数据类型,以便我可以轻松更改它,而无需寻找当前设置为短的每个演员、返回类型等。

如果我愿意使用对象,我可以轻松地

class MySmallNumber extends Short{}

在必要时更改父类。

如果这是 C/C++,我可以使用它

#define small short

来实现我正在寻找的效果。

我正在寻找一种在 java 中执行类似操作的方法,不需要在数组中存储 64 位对象引用。非常感谢任何帮助。现在我正在寻找一个非常混乱的 IDE 来替换所有内容来做到这一点。

I'm writing a program which creates a large number of large arrays to store data. All of this data has to held in RAM, so I'm avoiding objects and currently using shorts to save space. These shorts serve as ID numbers which can be put into a lookup class to get the corresponding object on demand. I have recently questioned whether I'll need the whole 2 bytes of a short, and so I'm now wondering if there's anyway to define the data type being stored in one place in my code so that I can change it easily without having to hunt down every cast, return type, etc. that is currently set to short.

If I were willing to use objects I could easily just do

class MySmallNumber extends Short{}

and change the parent class if necessary.

If this were C/C++, i could use

#define small short

for the effect I'm looking for.

I'm searching for a way to do something like this in java that won't require storing 64-bit object references in my arrays. Any help is greatly appreciated. Right now I'm looking at a really messy IDE replace all in order to do this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

瞳孔里扚悲伤 2025-01-04 07:40:43

您可以将数组封装在一些自定义类中。它不应该增加大量的空间开销,因为您使用大型数组。

在代码中的所有其他地方,您都可以使用 long。当您将这些长整型传递给数组自定义类时,您可以将其转换为您在其中使用的长整型。

最后,您只需在这一类中进行更改。

You can incapsulate you array in some custom class. It shouldn't add considerable space overhead because you work with large arrays.

In all other places in your code you can use long. When you pass these longs to you array custom class you can convert it to the one you use inside it.

Finally you have to make changes in this one class only.

征﹌骨岁月お 2025-01-04 07:40:43

我建议将所有依赖于 ID 值类型的代码分解到一个单独的类中。让该类处理所有依赖于 ID 值是短、字节还是其他值的操作(包括查找)。您可以将单个值作为 short 甚至 int 值传入和传出,即使它们在内部转换为 byte 也是如此。 (例如,java.io.DataOutputStream.writeByte(int) 的编写方式是这样的——它接受 int 参数并将其视为 byte< /代码> 值。)

I would suggest factoring out all code that depends on the type of your ID values into a separate class. Let that class handle all the operations (including lookup) that depend on whether the ID values are short, byte, or something else. You can pass individual values in and out as short or even int values, even if internally they are converted to byte. (This is, for instance, how java.io.DataOutputStream.writeByte(int) was written—it takes an int argument and treats it as a byte value.)

南风起 2025-01-04 07:40:43

不太确定你在这里追求什么,但这可能很有趣:

import java.util.Arrays;
interface Index {
    short getIndex(int i);
    void setIndex(int i, short value);
    int size();
}
class ShortIndexImpl implements Index {
    ShortIndexImpl(int n) {
        indices = new short[n];
    }
    @Override public short getIndex(int i) {
        return indices[i];
    }
    @Override public void setIndex(int i, short value) {
        indices[i] = value;
    }
    @Override public int size() {
        return indices.length;
    }
    final short[] indices;
}
class TenBitIndexImpl implements Index {
    TenBitIndexImpl(int n) {
        indices = new int[(n + 2) / 3];
    }
    @Override public short getIndex(int i) {
        int index = i / 3;
        int remainder = i % 3;
        int word = indices[index];
        return (short) (0x3ff & (word >> shifts[remainder]));
    }
    @Override public void setIndex(int i, short value) {
        int index = i / 3;
        int remainder = i % 3;
        int word = indices[index] & ~masks[remainder];
        int shiftedValue = ((int) value) << shifts[remainder];
        word |= shiftedValue;
        indices[index] = word;
    }
    @Override public int size() {
        return indices.length;
    }
    final int masks[] = new int[] { 0x3ff00000, 0xffc00, 0x3ff };
    final int shifts[] = new int[] { 20, 10, 0 };
    final int[] indices;
}
public class Main {
    static void test(Index index) {
        for (int i = 0; i < values.length; i++)
            index.setIndex(i, values[i]);
        for (int i = 0; i < values.length; i++) {
            System.out.println(values[i] + " " + index.getIndex(i));
            if (index.getIndex(i) != values[i])
                System.out.println("expected " + values[i] + " but got " + index.getIndex(i));
        }
    }
    public static void main(String[] args) {
        Index index = new ShortIndexImpl(values.length);
        test(index);
        index = new TenBitIndexImpl(values.length);
        test(index);
        System.out.println("indices");
        for (int i = 0; i < ((TenBitIndexImpl) index).indices.length; i++)
            System.out.println(((TenBitIndexImpl) index).indices[i]);
    }
    static short[] values = new short[] { 1, 2, 3, 4, 5, 6 };
}

not quite sure what you are after here, but this may be of interest:

import java.util.Arrays;
interface Index {
    short getIndex(int i);
    void setIndex(int i, short value);
    int size();
}
class ShortIndexImpl implements Index {
    ShortIndexImpl(int n) {
        indices = new short[n];
    }
    @Override public short getIndex(int i) {
        return indices[i];
    }
    @Override public void setIndex(int i, short value) {
        indices[i] = value;
    }
    @Override public int size() {
        return indices.length;
    }
    final short[] indices;
}
class TenBitIndexImpl implements Index {
    TenBitIndexImpl(int n) {
        indices = new int[(n + 2) / 3];
    }
    @Override public short getIndex(int i) {
        int index = i / 3;
        int remainder = i % 3;
        int word = indices[index];
        return (short) (0x3ff & (word >> shifts[remainder]));
    }
    @Override public void setIndex(int i, short value) {
        int index = i / 3;
        int remainder = i % 3;
        int word = indices[index] & ~masks[remainder];
        int shiftedValue = ((int) value) << shifts[remainder];
        word |= shiftedValue;
        indices[index] = word;
    }
    @Override public int size() {
        return indices.length;
    }
    final int masks[] = new int[] { 0x3ff00000, 0xffc00, 0x3ff };
    final int shifts[] = new int[] { 20, 10, 0 };
    final int[] indices;
}
public class Main {
    static void test(Index index) {
        for (int i = 0; i < values.length; i++)
            index.setIndex(i, values[i]);
        for (int i = 0; i < values.length; i++) {
            System.out.println(values[i] + " " + index.getIndex(i));
            if (index.getIndex(i) != values[i])
                System.out.println("expected " + values[i] + " but got " + index.getIndex(i));
        }
    }
    public static void main(String[] args) {
        Index index = new ShortIndexImpl(values.length);
        test(index);
        index = new TenBitIndexImpl(values.length);
        test(index);
        System.out.println("indices");
        for (int i = 0; i < ((TenBitIndexImpl) index).indices.length; i++)
            System.out.println(((TenBitIndexImpl) index).indices[i]);
    }
    static short[] values = new short[] { 1, 2, 3, 4, 5, 6 };
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文