多维原始整数数组
Multi-dimensional primitive integer array
我知道这段代码不起作用,但我怎么才能真正正确地初始化它呢?:
NSUInteger highestModelID = 34605;
NSUInteger highestColorID = 328;
NSUInteger** modelColors[highestModelID][highestColorID] = malloc(highestModelID * highestColorID * sizeof(NSUInteger));
所以有 2 个动态深度。我在多维 NSMutableDictionary
中有这个巨大的缓冲区,它占用了内存。我真的很想做这个原始的。
我想如果能用它制作一个 class 以便能够在更多 Objective-C 字典甚至 NSMutableArray
实在是太过分的地方使用它会更令人惊奇.随着时间的推移,我真的越来越讨厌使用 NSNumber
来做一些我在 golang
中习惯使用的超轻量级的东西,突然间它成为减慢我的应用程序速度的主要因素。 .
创建它的方式与创建 NSArrays 的 NSArray 并没有什么不同。您需要先分配 NSUInteger* 数组,然后分配其每个元素。
NSUInteger **modelColors;
modelColors = malloc(highestModelID * sizeof(NSUInteger*));
for (int i = 0; i < highestModelID; i++) {
modelColors[i] = malloc(highestColorID * sizeof(NSUInteger));
}
你需要这个:
NSUInteger* modelColors = malloc(highestModelID * highestColorID * sizeof(NSUInteger));
你可以这样使用:
NSUInteger getModelColor(int modelID, int colorID, int highestModelID, NSUInteger* modelColors) {
return modelColors[colorID * highestModelID + modelID];
}
void setModelColor(NSUInteger color, int modelID, int colorID, int highestModelID, NSUInteger* modelColors) {
modelColors[colorID * highestModelID + modelID] = color;
}
基本上这是一个二维数组,其中 modelID
索引行,colorID
索引列(假设行优先布局)。
我知道这段代码不起作用,但我怎么才能真正正确地初始化它呢?:
NSUInteger highestModelID = 34605;
NSUInteger highestColorID = 328;
NSUInteger** modelColors[highestModelID][highestColorID] = malloc(highestModelID * highestColorID * sizeof(NSUInteger));
所以有 2 个动态深度。我在多维 NSMutableDictionary
中有这个巨大的缓冲区,它占用了内存。我真的很想做这个原始的。
我想如果能用它制作一个 class 以便能够在更多 Objective-C 字典甚至 NSMutableArray
实在是太过分的地方使用它会更令人惊奇.随着时间的推移,我真的越来越讨厌使用 NSNumber
来做一些我在 golang
中习惯使用的超轻量级的东西,突然间它成为减慢我的应用程序速度的主要因素。 .
创建它的方式与创建 NSArrays 的 NSArray 并没有什么不同。您需要先分配 NSUInteger* 数组,然后分配其每个元素。
NSUInteger **modelColors;
modelColors = malloc(highestModelID * sizeof(NSUInteger*));
for (int i = 0; i < highestModelID; i++) {
modelColors[i] = malloc(highestColorID * sizeof(NSUInteger));
}
你需要这个:
NSUInteger* modelColors = malloc(highestModelID * highestColorID * sizeof(NSUInteger));
你可以这样使用:
NSUInteger getModelColor(int modelID, int colorID, int highestModelID, NSUInteger* modelColors) {
return modelColors[colorID * highestModelID + modelID];
}
void setModelColor(NSUInteger color, int modelID, int colorID, int highestModelID, NSUInteger* modelColors) {
modelColors[colorID * highestModelID + modelID] = color;
}
基本上这是一个二维数组,其中 modelID
索引行,colorID
索引列(假设行优先布局)。