56@protocol OOProbabilitySetEnumerable <NSObject>
58- (id)privObjectAtIndex:(NSUInteger)index;
62@interface OOProbabilitySet (OOPrivate)
81- (id)initWithObject:(
id)object weight:(
float)weight;
101- (id)initPrivWithObjectArray:(NSMutableArray*)objects weightsArray:(NSMutableArray*)weights sum:(
float)sumOfWeights;
111- (id)initWithEnumerable:(
id<OOProbabilitySetEnumerable>)enumerable;
126+ (id)probabilitySetWithObjects:(
id*)objects weights:(
float*)weights count:(NSUInteger)count
128 return [[[
self alloc] initWithObjects:objects weights:weights count:count] autorelease];
131+ (id)probabilitySetWithPropertyListRepresentation:(NSDictionary*)plist
133 return [[[
self alloc] initWithPropertyListRepresentation:plist] autorelease];
142- (id)initWithObjects:(
id*)objects weights:(
float*)weights count:(NSUInteger)count
144 NSZone* zone = [
self zone];
152 if (objects == NULL || weights == NULL) {
153 [
NSException raise:NSInvalidArgumentException format:@"Attempt to create %@ with non-zero count but nil objects or weights.", @"OOProbabilitySet"];
164- (id)initWithPropertyListRepresentation:(NSDictionary*)plist
166 NSArray* objects =
nil;
167 NSArray* weights =
nil;
168 NSUInteger i = 0,
count = 0;
169 id* rawObjects = NULL;
170 float* rawWeights = NULL;
172 objects = [
plist oo_arrayForKey:kObjectsKey];
173 weights = [
plist oo_arrayForKey:kWeightsKey];
176 if (objects ==
nil || weights ==
nil) {
187 rawObjects = malloc(
sizeof *rawObjects *
count);
188 rawWeights = malloc(
sizeof *rawWeights *
count);
190 if (rawObjects != NULL || rawWeights != NULL) {
192 [
objects getObjects:rawObjects];
195 for (i = 0; i <
count; ++i) {
196 rawWeights[
i] = fmax([weights oo_floatAtIndex:i], 0.0f);
219 return [
NSString stringWithFormat:@"count=%zu", [
self count]];
232- (float)weightForObject:(
id)object
252- (id)copyWithZone:(NSZone*)zone
254 if (zone == [
self zone]) {
255 return [
self retain];
261- (id)mutableCopyWithZone:(NSZone*)zone
268@implementation OOProbabilitySet (OOExtendedProbabilitySet)
270- (BOOL)containsObject:(
id)object
280- (float)probabilityForObject:(
id)object
306 NSArray* empty = [
NSArray array];
307 return [
NSDictionary dictionaryWithObjectsAndKeys:empty, kObjectsKey, empty, kWeightsKey, nil];
315- (float)weightForObject:(
id)object
335- (id)mutableCopyWithZone:(NSZone*)zone
343@implementation OOEmptyProbabilitySet (Singleton)
352+ (id)allocWithZone:(NSZone*)inZone
361- (id)copyWithZone:(NSZone*)inZone
389- (id)initWithObject:(
id)object weight:(
float)weight
414 [
NSArray arrayWithObject:_object], kObjectsKey,
415 [
NSArray arrayWithObject:[
NSNumber numberWithFloat:_weight]], kWeightsKey,
424- (float)weightForObject:(
id)object
444 return [
NSArray arrayWithObject:_object];
447- (id)mutableCopyWithZone:(NSZone*)zone
456- (id)initWithObjects:(
id*)objects weights:(
float*)weights count:(NSUInteger)count
459 float cuWeight = 0.0f;
461 assert(
count > 1 && objects != NULL && weights != NULL);
473 for (i = 0; i !=
count; ++i) {
475 cuWeight += weights[
i];
490 for (i = 0; i <
_count; ++i) {
491 [_objects[
i] release];
507 NSArray* objects =
nil;
508 NSMutableArray* weights =
nil;
509 float cuWeight = 0.0f, sum = 0.0f;
512 objects = [
NSArray arrayWithObjects:_objects count:_count];
514 for (i = 0; i <
_count; ++i) {
516 [
weights oo_addFloat:cuWeight - sum];
521 objects, kObjectsKey,
522 [[
weights copy] autorelease], kWeightsKey,
531- (id)privObjectForWeight:(
float)target
539 NSUInteger low = 0, high =
_count - 1, idx = 0;
543 idx = (low + high) / 2;
545 if (weight > target) {
549 }
else if (weight < target)
555 if (weight > target) {
575- (float)weightForObject:(
id)object
584 for (i = 0; i <
_count; ++i) {
602 return [
NSArray arrayWithObjects:_objects count:_count];
610- (id)privObjectAtIndex:(NSUInteger)index
615- (id)mutableCopyWithZone:(NSZone*)zone
618 float* weights = NULL;
620 float weight = 0.0f, sum = 0.0f;
623 weights = malloc(
sizeof *weights *
_count);
627 for (i = 0; i <
_count; ++i) {
629 weights[
i] = weight - sum;
650 NSZone* zone = [
self zone];
655- (id)initWithObjects:(
id*)objects weights:(
float*)weights count:(NSUInteger)count
657 NSZone* zone = [
self zone];
662- (id)initWithPropertyListRepresentation:(NSDictionary*)plist
664 NSZone* zone = [
self zone];
669- (id)copyWithZone:(NSZone*)zone
674- (void)setWeight:(
float)weight forObject:(
id)object
679- (void)removeObject:(
id)object
699- (id)initPrivWithObjectArray:(NSMutableArray*)objects weightsArray:(NSMutableArray*)weights sum:(
float)sumOfWeights
712- (id)initWithObjects:(
id*)objects weights:(
float*)weights count:(NSUInteger)count
717 if (
count != 0 && (objects == NULL || weights == NULL)) {
719 [
NSException raise:NSInvalidArgumentException format:@"Attempt to create %@ with non-zero count but nil objects or weights.", @"OOMutableProbabilitySet"];
724 for (i = 0; i !=
count; ++i) {
732- (id)initWithPropertyListRepresentation:(NSDictionary*)plist
735 NSArray* objects =
nil;
736 NSArray* weights =
nil;
737 NSUInteger i = 0,
count = 0;
743 objects = [
plist oo_arrayForKey:kObjectsKey];
744 weights = [
plist oo_arrayForKey:kWeightsKey];
747 if (objects ==
nil || weights ==
nil)
755 for (i = 0; i <
count; ++i) {
779 _objects, kObjectsKey,
780 _weights, kWeightsKey,
786 return [_objects count];
792 NSUInteger i = 0,
count = 0;
796 count = [_objects count];
800 for (i = 0; i <
count; ++i) {
801 sum += [_weights oo_floatAtIndex:i];
803 return [_objects objectAtIndex:i];
806 OOLog(
@"probabilitySet.broken",
@"%s fell off end, returning first object. Nominal sum = %f, target = %f, actual sum = %f, count = %zu. %@", __PRETTY_FUNCTION__,
sumOfWeights, target, sum,
count,
@"This is an internal error, please report it.");
807 return [_objects objectAtIndex:0];
810- (float)weightForObject:(
id)object
812 float result = -1.0f;
815 NSUInteger index = [_objects indexOfObject:object];
816 if (index != NSNotFound) {
817 result = [_weights oo_floatAtIndex:index];
819 result -= [_weights oo_floatAtIndex:index - 1];
832 for (i = 0; i <
count; ++i) {
841 return [[_objects copy] autorelease];
846 return [_objects objectEnumerator];
849- (void)setWeight:(
float)weight forObject:(
id)object
854 weight = fmax(weight, 0.0f);
855 NSUInteger index = [_objects indexOfObject:object];
856 if (index == NSNotFound) {
857 [_objects addObject:object];
858 [_weights oo_addFloat:weight];
865 [_weights replaceObjectAtIndex:index withObject:[
NSNumber numberWithFloat:weight]];
869- (void)removeObject:(
id)object
874 NSUInteger index = [_objects indexOfObject:object];
875 if (index != NSNotFound) {
876 [_objects removeObjectAtIndex:index];
878 [_weights removeObjectAtIndex:index];
882- (id)copyWithZone:(NSZone*)zone
886 float* weights = NULL;
887 NSUInteger i = 0,
count = 0;
889 count = [_objects count];
893 objects = malloc(
sizeof *objects *
count);
894 weights = malloc(
sizeof *weights *
count);
895 if (objects != NULL && weights != NULL) {
896 [_objects getObjects:objects];
898 for (i = 0; i <
count; ++i) {
899 weights[
i] = [_weights oo_floatAtIndex:i];
913- (id)mutableCopyWithZone:(NSZone*)zone
916 weightsArray:[[_weights mutableCopyWithZone:zone] autorelease]
924- (id)initWithEnumerable:(
id<OOProbabilitySetEnumerable>)enumerable
926 if ((
self = [super init])) {
935 [_enumerable release];
943 return [_enumerable privObjectAtIndex:_index++];
945 [_enumerable release];
955 [NSException raise:NSGenericException format:@"Attempt to use abstract class %@ - this indicates an incorrect initialization.", [obj class]];
#define OOLog(class, format,...)
static OOEmptyProbabilitySet * sOOEmptyProbabilitySetSingleton
static NSString *const kObjectsKey
static void ThrowAbstractionViolationException(id obj) GCC_ATTR((noreturn))
static NSString *const kWeightsKey
NSEnumerator * objectEnumerator()
NSMutableArray * _objects
NSDictionary * propertyListRepresentation()
NSMutableArray * _weights
NSEnumerator * objectEnumerator()
void setWeight:forObject:(float weight, [forObject] id object)
id privObjectForWeight:(float target)
NSDictionary * propertyListRepresentation()
NSEnumerator * objectEnumerator()
float * _cumulativeWeights
return sOOEmptyProbabilitySetSingleton
OOEmptyProbabilitySet * OO_RETURNS_RETAINED()
NSDictionary * propertyListRepresentation()
NSDictionary * propertyListRepresentation()
id probabilitySetWithObjects:weights:count:(id *objects,[weights] float *weights,[count] NSUInteger count)
id initWithObjects:weights:count:(id *objects,[weights] float *weights,[count] NSUInteger count)
NSString * descriptionComponents()
float weightForObject:(id object)
NSDictionary * propertyListRepresentation()