FValueIndex.m 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 "FValueIndex.h"
  17. #import "FNamedNode.h"
  18. #import "FSnapshotUtilities.h"
  19. #import "FUtilities.h"
  20. #import "FMaxNode.h"
  21. @implementation FValueIndex
  22. - (NSComparisonResult) compareKey:(NSString *)key1
  23. andNode:(id<FNode>)node1
  24. toOtherKey:(NSString *)key2
  25. andNode:(id<FNode>)node2
  26. {
  27. NSComparisonResult indexCmp = [node1 compare:node2];
  28. if (indexCmp == NSOrderedSame) {
  29. return [FUtilities compareKey:key1 toKey:key2];
  30. } else {
  31. return indexCmp;
  32. }
  33. }
  34. - (NSComparisonResult) compareKey:(NSString *)key1
  35. andNode:(id<FNode>)node1
  36. toOtherKey:(NSString *)key2
  37. andNode:(id<FNode>)node2
  38. reverse:(BOOL)reverse
  39. {
  40. if (reverse) {
  41. return [self compareKey:key2 andNode:node2 toOtherKey:key1 andNode:node1];
  42. } else {
  43. return [self compareKey:key1 andNode:node1 toOtherKey:key2 andNode:node2];
  44. }
  45. }
  46. - (NSComparisonResult) compareNamedNode:(FNamedNode *)namedNode1 toNamedNode:(FNamedNode *)namedNode2
  47. {
  48. return [self compareKey:namedNode1.name andNode:namedNode1.node toOtherKey:namedNode2.name andNode:namedNode2.node];
  49. }
  50. - (BOOL)isDefinedOn:(id<FNode>)node {
  51. return YES;
  52. }
  53. - (BOOL)indexedValueChangedBetween:(id<FNode>)oldNode and:(id<FNode>)newNode {
  54. return ![oldNode isEqual:newNode];
  55. }
  56. - (FNamedNode *)minPost {
  57. return FNamedNode.min;
  58. }
  59. - (FNamedNode *)maxPost {
  60. return FNamedNode.max;
  61. }
  62. - (FNamedNode *)makePost:(id<FNode>)indexValue name:(NSString*)name {
  63. return [[FNamedNode alloc] initWithName:name andNode:indexValue];
  64. }
  65. - (NSString *)queryDefinition {
  66. return @".value";
  67. }
  68. - (NSString *) description {
  69. return @"FValueIndex";
  70. }
  71. - (id)copyWithZone:(NSZone *)zone {
  72. return self;
  73. }
  74. - (BOOL) isEqual:(id)other {
  75. // since we're a singleton.
  76. return (other == self);
  77. }
  78. - (NSUInteger) hash {
  79. return [@".value" hash];
  80. }
  81. + (id<FIndex>) valueIndex {
  82. static id<FIndex> valueIndex;
  83. static dispatch_once_t once;
  84. dispatch_once(&once, ^{
  85. valueIndex = [[FValueIndex alloc] init];
  86. });
  87. return valueIndex;
  88. }
  89. @end