首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何从NSIndexSet筛选和创建范围?

如何从NSIndexSet筛选和创建范围?
EN

Stack Overflow用户
提问于 2018-11-13 20:45:14
回答 2查看 245关注 0票数 1

我创建了一个NSIndexSet

代码语言:javascript
运行
复制
NSIndexSet* groups = [self.specifiers indexesOfObjectsPassingTest:^(PSSpecifier*specifier, NSUInteger idx, BOOL *stop) {
return [(NSString*)[specifier.properties objectForKey:@"cell"] isEqualToString:@"KBGroupCell"]; }];

可能是这样的:

(0 3 5 8-9 12 14 17-19)和给定的最大值为25。

我希望过滤并创建范围,以便实现如下所示的输出:

代码语言:javascript
运行
复制
0-2
3-4
5-7
8-11
12-13
14-16
17-25

所使用的数组由用户填充,而不是由我自己填充。KBGroupCells用于填充表中的头和页脚,因此不应该连续识别。当我稍后填充表时,我需要记住忽略的数字(9、18和19)。

通过使用rangeAtIndex,我可以识别集合中包含多个值的范围,因此可能用于查找要忽略的数字。

我怎样才能做到这一点?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-11-13 23:38:34

我猜你有NSIndexSet:

代码语言:javascript
运行
复制
    let original =   IndexSet(arrayLiteral: 0, 3, 5).union(IndexSet(integersIn: 8...9)).union( IndexSet.init(arrayLiteral: 12,14)).union(IndexSet(integersIn: 17...19)).union(IndexSet.init(arrayLiteral: 26,27))
    let nsIndex = original as! NSIndexSet

在将其转换为IndexSet之后,您可以轻松地获得两个数组。一个是NSRange,另一个是closedRange

代码语言:javascript
运行
复制
    let index = nsIndex as! IndexSet
    let rangeView = index.rangeView
    print  ( Array(rangeView.enumerated().map{
      NSRange.init(location:($0.element.first!) , length: ((rangeView[$0.offset + 1].first!  - ($0.element.first!))))
    }.dropLast()))

    print  ( Array(rangeView.enumerated().map{
                ($0.element.first!)...(rangeView[$0.offset + 1].first!) - 1
        }.dropLast()))

如果是客观的-C。就像这样:

代码语言:javascript
运行
复制
NSMutableIndexSet * nsIndex =    [[NSMutableIndexSet alloc] init];
[nsIndex addIndex: 0];
[nsIndex addIndex: 3];
[nsIndex addIndex: 5];
[nsIndex addIndexesInRange:NSMakeRange(8, 2)];
[nsIndex addIndex: 12];
[nsIndex addIndex: 14];
[nsIndex addIndexesInRange:NSMakeRange(17, 3)];
[nsIndex addIndex: 26];
__block int count = 0;
__block NSMutableArray * ranges = [NSMutableArray array];
[nsIndex enumerateRangesUsingBlock:^(NSRange range, BOOL * _Nonnull stop) {
    count ++;
    [ranges addObject:[NSValue valueWithRange: range]];
}];
NSMutableArray * result  = [NSMutableArray array];
for (NSUInteger location = 0 ; location < count - 1 ; location++) {
    NSUInteger loc = ((NSValue *) ranges[location]).rangeValue.location;
    [result addObject:[NSValue valueWithRange: (NSMakeRange( loc,  ((NSValue *) ranges[location + 1]).rangeValue.location -  loc))]];
}
NSLog(result.description);

或者用一轮:

代码语言:javascript
运行
复制
__block NSUInteger temp = nsIndex.firstIndex;
__block NSMutableArray * result = [NSMutableArray array];
[nsIndex enumerateRangesWithOptions:NSEnumerationReverse usingBlock:^(NSRange range, BOOL * _Nonnull stop) {
    [result insertObject: [NSValue valueWithRange: NSMakeRange(range.location, temp - range.location)] atIndex:0];
    temp = range.location ;
}];
  result =  [result subarrayWithRange:NSMakeRange(0, result.count - 1)];
  NSLog(result.description);
票数 1
EN

Stack Overflow用户

发布于 2018-11-13 22:12:07

这是一个可能的算法,编码和一些错误检查是留作练习!

代码语言:javascript
运行
复制
cursor <- theSet first contained index
result <- empty array

while cursor <= maxIndex
   start <- cursor
   while cursor <= maxIndex and theSet contains cursor do increment cursor
   while cursor <= maxIndex and theSet does not contain cursor do increment cursor
   newRange <- start to cursor-1
   add newRange to result
end

return result

该算法仅使用两种NSIndexSet方法--一种是查找集合中的第一个索引,另一种是测试索引是否在集合中。有一些“更高层次”的方法可以加快算法的速度,但是在0-25的范围内通过索引就足够了。

HTH

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53289230

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档