FPriorityIndex.m 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 "FNode.h"
  18. #import "FUtilities.h"
  19. #import "FNamedNode.h"
  20. #import "FEmptyNode.h"
  21. #import "FLeafNode.h"
  22. #import "FMaxNode.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. {
  30. id<FNode> child1 = [node1 getPriority];
  31. id<FNode> child2 = [node2 getPriority];
  32. NSComparisonResult indexCmp = [child1 compare:child2];
  33. if (indexCmp == NSOrderedSame) {
  34. return [FUtilities compareKey:key1 toKey:key2];
  35. } else {
  36. return indexCmp;
  37. }
  38. }
  39. - (NSComparisonResult) compareKey:(NSString *)key1
  40. andNode:(id<FNode>)node1
  41. toOtherKey:(NSString *)key2
  42. andNode:(id<FNode>)node2
  43. reverse:(BOOL)reverse
  44. {
  45. if (reverse) {
  46. return [self compareKey:key2 andNode:node2 toOtherKey:key1 andNode:node1];
  47. } else {
  48. return [self compareKey:key1 andNode:node1 toOtherKey:key2 andNode:node2];
  49. }
  50. }
  51. - (NSComparisonResult) compareNamedNode:(FNamedNode *)namedNode1 toNamedNode:(FNamedNode *)namedNode2
  52. {
  53. return [self compareKey:namedNode1.name andNode:namedNode1.node toOtherKey:namedNode2.name andNode:namedNode2.node];
  54. }
  55. - (BOOL)isDefinedOn:(id <FNode>)node {
  56. return !node.getPriority.isEmpty;
  57. }
  58. - (BOOL)indexedValueChangedBetween:(id <FNode>)oldNode and:(id <FNode>)newNode {
  59. id<FNode> oldValue = [oldNode getPriority];
  60. id<FNode> newValue = [newNode getPriority];
  61. return ![oldValue isEqual:newValue];
  62. }
  63. - (FNamedNode *)minPost {
  64. return FNamedNode.min;
  65. }
  66. - (FNamedNode *)maxPost {
  67. return [self makePost:[FMaxNode maxNode] name:[FUtilities maxName]];
  68. }
  69. - (FNamedNode*)makePost:(id<FNode>)indexValue name:(NSString*)name {
  70. id<FNode> node = [[FLeafNode alloc] initWithValue:@"[PRIORITY-POST]" withPriority:indexValue];
  71. return [[FNamedNode alloc] initWithName:name andNode:node];
  72. }
  73. - (NSString *)queryDefinition {
  74. return @".priority";
  75. }
  76. - (NSString *)description {
  77. return @"FPriorityIndex";
  78. }
  79. - (id)copyWithZone:(NSZone *)zone {
  80. // Safe since we're immutable.
  81. return self;
  82. }
  83. - (BOOL) isEqual:(id)other {
  84. return [other isKindOfClass:[FPriorityIndex class]];
  85. }
  86. - (NSUInteger) hash {
  87. // chosen by a fair dice roll. Guaranteed to be random
  88. return 3155577;
  89. }
  90. + (id<FIndex>) priorityIndex {
  91. static id<FIndex> index;
  92. static dispatch_once_t once;
  93. dispatch_once(&once, ^{
  94. index = [[FPriorityIndex alloc] init];
  95. });
  96. return index;
  97. }
  98. @end