/core/externals/update-engine/externals/google-toolbox-for-mac/Foundation/GTMCalculatedRange.m

http://macfuse.googlecode.com/ · Objective C · 148 lines · 111 code · 17 blank · 20 comment · 15 complexity · 6614bc7534329cdfe90622b8ba863bcf MD5 · raw file

  1. //
  2. // GTMCalculatedRange.m
  3. //
  4. // Copyright 2006-2008 Google Inc.
  5. //
  6. // Licensed under the Apache License, Version 2.0 (the "License"); you may not
  7. // use this file except in compliance with the License. You may obtain a copy
  8. // of the License at
  9. //
  10. // http://www.apache.org/licenses/LICENSE-2.0
  11. //
  12. // Unless required by applicable law or agreed to in writing, software
  13. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  14. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  15. // License for the specific language governing permissions and limitations under
  16. // the License.
  17. //
  18. #import "GTMCalculatedRange.h"
  19. // Our internal storage type. It keeps track of an item and it's
  20. // position.
  21. @interface GTMCalculatedRangeStopPrivate : NSObject {
  22. id item_; // the item (STRONG)
  23. CGFloat position_; //
  24. }
  25. + (id)stopWithObject:(id)item position:(CGFloat)inPosition;
  26. - (id)initWithObject:(id)item position:(CGFloat)inPosition;
  27. - (id)item;
  28. - (CGFloat)position;
  29. @end
  30. GTM_INLINE BOOL FPEqual(CGFloat a, CGFloat b) {
  31. return (fpclassify(a - b) == FP_ZERO);
  32. }
  33. @implementation GTMCalculatedRangeStopPrivate
  34. + (id)stopWithObject:(id)item position:(CGFloat)inPosition {
  35. return [[[self alloc] initWithObject:item position:inPosition] autorelease];
  36. }
  37. - (id)initWithObject:(id)item position:(CGFloat)inPosition {
  38. self = [super init];
  39. if (self != nil) {
  40. item_ = [item retain];
  41. position_ = inPosition;
  42. }
  43. return self;
  44. }
  45. - (void)dealloc {
  46. [item_ release];
  47. [super dealloc];
  48. }
  49. - (id)item {
  50. return item_;
  51. }
  52. - (CGFloat)position {
  53. return position_;
  54. }
  55. - (NSString *)description {
  56. return [NSString stringWithFormat: @"%f %@", position_, item_];
  57. }
  58. @end
  59. @implementation GTMCalculatedRange
  60. - (id)init {
  61. self = [super init];
  62. if (self != nil) {
  63. storage_ = [[NSMutableArray arrayWithCapacity:0] retain];
  64. }
  65. return self;
  66. }
  67. - (void)dealloc {
  68. [storage_ release];
  69. [super dealloc];
  70. }
  71. - (void)insertStop:(id)item atPosition:(CGFloat)position {
  72. NSUInteger positionIndex = 0;
  73. GTMCalculatedRangeStopPrivate *theStop;
  74. GTM_FOREACH_OBJECT(theStop, storage_) {
  75. if ([theStop position] < position) {
  76. positionIndex += 1;
  77. }
  78. else if (FPEqual([theStop position], position)) {
  79. // remove and stop the enum since we just modified the object
  80. [storage_ removeObjectAtIndex:positionIndex];
  81. break;
  82. }
  83. }
  84. [storage_ insertObject:[GTMCalculatedRangeStopPrivate stopWithObject:item position:position]
  85. atIndex:positionIndex];
  86. }
  87. - (BOOL)removeStopAtPosition:(CGFloat)position {
  88. NSUInteger positionIndex = 0;
  89. BOOL foundStop = NO;
  90. GTMCalculatedRangeStopPrivate *theStop;
  91. GTM_FOREACH_OBJECT(theStop, storage_) {
  92. if (FPEqual([theStop position], position)) {
  93. break;
  94. } else {
  95. positionIndex += 1;
  96. }
  97. }
  98. if (nil != theStop) {
  99. [self removeStopAtIndex:positionIndex];
  100. foundStop = YES;
  101. }
  102. return foundStop;
  103. }
  104. - (void)removeStopAtIndex:(NSUInteger)positionIndex {
  105. [storage_ removeObjectAtIndex:positionIndex];
  106. }
  107. - (NSUInteger)stopCount {
  108. return [storage_ count];
  109. }
  110. - (id)stopAtIndex:(NSUInteger)positionIndex position:(CGFloat*)outPosition {
  111. GTMCalculatedRangeStopPrivate *theStop = [storage_ objectAtIndex:positionIndex];
  112. if (nil != outPosition) {
  113. *outPosition = [theStop position];
  114. }
  115. return [theStop item];
  116. }
  117. - (id)valueAtPosition:(CGFloat)position {
  118. id theValue = nil;
  119. GTMCalculatedRangeStopPrivate *theStop;
  120. GTM_FOREACH_OBJECT(theStop, storage_) {
  121. if (FPEqual([theStop position], position)) {
  122. theValue = [theStop item];
  123. break;
  124. }
  125. }
  126. return theValue;
  127. }
  128. - (NSString *)description {
  129. return [storage_ description];
  130. }
  131. @end