PageRenderTime 33ms CodeModel.GetById 16ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 19#import "GTMCalculatedRange.h"
 20
 21//  Our internal storage type. It keeps track of an item and it's
 22//  position.
 23@interface GTMCalculatedRangeStopPrivate : NSObject {
 24  id item_; // the item (STRONG)
 25  CGFloat position_; //
 26}
 27+ (id)stopWithObject:(id)item position:(CGFloat)inPosition;
 28- (id)initWithObject:(id)item position:(CGFloat)inPosition;
 29- (id)item;
 30- (CGFloat)position;
 31@end
 32
 33GTM_INLINE BOOL FPEqual(CGFloat a, CGFloat b) {
 34  return (fpclassify(a - b) == FP_ZERO);
 35}
 36
 37@implementation GTMCalculatedRangeStopPrivate
 38+ (id)stopWithObject:(id)item position:(CGFloat)inPosition {
 39  return [[[self alloc] initWithObject:item position:inPosition] autorelease];
 40}
 41
 42- (id)initWithObject:(id)item position:(CGFloat)inPosition {
 43  self = [super init];
 44  if (self != nil) {
 45    item_ = [item retain];
 46    position_ = inPosition;
 47  }
 48  return self;
 49}
 50
 51- (void)dealloc {
 52  [item_ release];
 53  [super dealloc];
 54}
 55
 56- (id)item {
 57  return item_;
 58}
 59
 60- (CGFloat)position {
 61  return position_;
 62}
 63
 64- (NSString *)description {
 65  return [NSString stringWithFormat: @"%f %@", position_, item_];
 66}
 67@end
 68
 69@implementation GTMCalculatedRange
 70- (id)init {
 71  self = [super init];
 72  if (self != nil) {
 73    storage_ = [[NSMutableArray arrayWithCapacity:0] retain]; 
 74  }
 75  return self;
 76}
 77- (void)dealloc {
 78  [storage_ release];
 79  [super dealloc];
 80}
 81
 82- (void)insertStop:(id)item atPosition:(CGFloat)position {
 83  NSUInteger positionIndex = 0;
 84  GTMCalculatedRangeStopPrivate *theStop;
 85  GTM_FOREACH_OBJECT(theStop, storage_) {
 86    if ([theStop position] < position) {
 87      positionIndex += 1;
 88    }
 89    else if (FPEqual([theStop position], position)) {
 90      // remove and stop the enum since we just modified the object
 91      [storage_ removeObjectAtIndex:positionIndex];
 92      break;
 93    }
 94  }
 95  [storage_ insertObject:[GTMCalculatedRangeStopPrivate stopWithObject:item position:position] 
 96                 atIndex:positionIndex];
 97}
 98
 99- (BOOL)removeStopAtPosition:(CGFloat)position {
100  NSUInteger positionIndex = 0;
101  BOOL foundStop = NO;
102  GTMCalculatedRangeStopPrivate *theStop;
103  GTM_FOREACH_OBJECT(theStop, storage_) {
104    if (FPEqual([theStop position], position)) {
105      break;
106    } else {
107       positionIndex += 1;
108    }
109  }
110  if (nil != theStop) {
111    [self removeStopAtIndex:positionIndex];
112    foundStop = YES;
113  }
114  return foundStop;
115}
116
117- (void)removeStopAtIndex:(NSUInteger)positionIndex {
118  [storage_ removeObjectAtIndex:positionIndex];
119}
120
121- (NSUInteger)stopCount {
122  return [storage_ count];
123}
124
125- (id)stopAtIndex:(NSUInteger)positionIndex position:(CGFloat*)outPosition {
126  GTMCalculatedRangeStopPrivate *theStop = [storage_ objectAtIndex:positionIndex];
127  if (nil != outPosition) {
128    *outPosition = [theStop position];
129  }
130  return [theStop item];
131}
132  
133- (id)valueAtPosition:(CGFloat)position {
134  id theValue = nil;
135  GTMCalculatedRangeStopPrivate *theStop;
136  GTM_FOREACH_OBJECT(theStop, storage_) {
137    if (FPEqual([theStop position], position)) {
138      theValue = [theStop item];
139      break;
140    }
141  }
142  return theValue;
143}
144
145- (NSString *)description {
146  return [storage_ description];
147}
148@end