FPriorityIndex.m 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "FPriorityIndex.h"
  17. #import "FEmptyNode.h"
  18. #import "FLeafNode.h"
  19. #import "FMaxNode.h"
  20. #import "FNamedNode.h"
  21. #import "FNode.h"
  22. #import "FUtilities.h"
  23. // TODO: Abstract into some common base class?
  24. @implementation FPriorityIndex
  25. - (NSComparisonResult)compareKey:(NSString *)key1
  26. andNode:(id<FNode>)node1
  27. toOtherKey:(NSString *)key2
  28. andNode:(id<FNode>)node2 {
  29. id<FNode> child1 = [node1 getPriority];
  30. id<FNode> child2 = [node2 getPriority];
  31. NSComparisonResult indexCmp = [child1 compare:child2];
  32. if (indexCmp == NSOrderedSame) {
  33. return [FUtilities compareKey:key1 toKey:key2];
  34. } else {
  35. return indexCmp;
  36. }
  37. }
  38. - (NSComparisonResult)compareKey:(NSString *)key1
  39. andNode:(id<FNode>)node1
  40. toOtherKey:(NSString *)key2
  41. andNode:(id<FNode>)node2
  42. reverse:(BOOL)reverse {
  43. if (reverse) {
  44. return [self compareKey:key2
  45. andNode:node2
  46. toOtherKey:key1
  47. andNode:node1];
  48. } else {
  49. return [self compareKey:key1
  50. andNode:node1
  51. toOtherKey:key2
  52. andNode:node2];
  53. }
  54. }
  55. - (NSComparisonResult)compareNamedNode:(FNamedNode *)namedNode1
  56. toNamedNode:(FNamedNode *)namedNode2 {
  57. return [self compareKey:namedNode1.name
  58. andNode:namedNode1.node
  59. toOtherKey:namedNode2.name
  60. andNode:namedNode2.node];
  61. }
  62. - (BOOL)isDefinedOn:(id<FNode>)node {
  63. return !node.getPriority.isEmpty;
  64. }
  65. - (BOOL)indexedValueChangedBetween:(id<FNode>)oldNode and:(id<FNode>)newNode {
  66. id<FNode> oldValue = [oldNode getPriority];
  67. id<FNode> newValue = [newNode getPriority];
  68. return ![oldValue isEqual:newValue];
  69. }
  70. - (FNamedNode *)minPost {
  71. return FNamedNode.min;
  72. }
  73. - (FNamedNode *)maxPost {
  74. return [self makePost:[FMaxNode maxNode] name:[FUtilities maxName]];
  75. }
  76. - (FNamedNode *)makePost:(id<FNode>)indexValue name:(NSString *)name {
  77. id<FNode> node = [[FLeafNode alloc] initWithValue:@"[PRIORITY-POST]"
  78. withPriority:indexValue];
  79. return [[FNamedNode alloc] initWithName:name andNode:node];
  80. }
  81. - (NSString *)queryDefinition {
  82. return @".priority";
  83. }
  84. - (NSString *)description {
  85. return @"FPriorityIndex";
  86. }
  87. - (id)copyWithZone:(NSZone *)zone {
  88. // Safe since we're immutable.
  89. return self;
  90. }
  91. - (BOOL)isEqual:(id)other {
  92. return [other isKindOfClass:[FPriorityIndex class]];
  93. }
  94. - (NSUInteger)hash {
  95. // chosen by a fair dice roll. Guaranteed to be random
  96. return 3155577;
  97. }
  98. + (id<FIndex>)priorityIndex {
  99. static id<FIndex> index;
  100. static dispatch_once_t once;
  101. dispatch_once(&once, ^{
  102. index = [[FPriorityIndex alloc] init];
  103. });
  104. return index;
  105. }
  106. @end