Oolite
Loading...
Searching...
No Matches
OOConcreteMutableProbabilitySet Class Reference
Inheritance diagram for OOConcreteMutableProbabilitySet:
Collaboration diagram for OOConcreteMutableProbabilitySet:

Instance Methods

(id) - initPrivWithObjectArray:weightsArray:sum:
(id) - initPriv [implementation]
(id) - initWithObjects:weights:count: [implementation]
(id) - initWithPropertyListRepresentation: [implementation]
(void) - dealloc [implementation]
(NSDictionary *) - propertyListRepresentation [implementation]
(NSUInteger) - count [implementation]
(id) - randomObject [implementation]
(float) - weightForObject: [implementation]
(float) - sumOfWeights [implementation]
(NSArray *) - allObjects [implementation]
(NSEnumerator *) - objectEnumerator [implementation]
(void) - setWeight:forObject: [implementation]
(void) - removeObject: [implementation]
(id) - copyWithZone: [implementation]
(id) - mutableCopyWithZone: [implementation]
Instance Methods inherited from OOMutableProbabilitySet
(id) - init [implementation]
Instance Methods inherited from OOProbabilitySet
(NSString *) - descriptionComponents [implementation]
(BOOL) - containsObject:
(float) - probabilityForObject:

Private Attributes

NSMutableArray * _objects
NSMutableArray * _weights
float _sumOfWeights

Additional Inherited Members

Class Methods inherited from OOMutableProbabilitySet
(id) + probabilitySet [implementation]
Class Methods inherited from OOProbabilitySet
(id) + probabilitySetWithObjects:weights:count:
(id) + probabilitySetWithPropertyListRepresentation:

Detailed Description

Definition at line 94 of file OOProbabilitySet.m.

Method Documentation

◆ allObjects

- (NSArray *) allObjects
implementation

Reimplemented from OOProbabilitySet.

Definition at line 839 of file OOProbabilitySet.m.

840{
841 return [[_objects copy] autorelease];
842}

References allObjects.

Referenced by allObjects.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ copyWithZone:

- (id) copyWithZone: (NSZone*) zone
implementation

Reimplemented from OOMutableProbabilitySet.

Definition at line 882 of file OOProbabilitySet.m.

882 :(NSZone*)zone
883{
884 id result = nil;
885 id* objects = NULL;
886 float* weights = NULL;
887 NSUInteger i = 0, count = 0;
888
889 count = [_objects count];
890 if (EXPECT_NOT(count == 0))
891 return [OOEmptyProbabilitySet singleton];
892
893 objects = malloc(sizeof *objects * count);
894 weights = malloc(sizeof *weights * count);
895 if (objects != NULL && weights != NULL) {
896 [_objects getObjects:objects];
897
898 for (i = 0; i < count; ++i) {
899 weights[i] = [_weights oo_floatAtIndex:i];
900 }
901
902 result = [[OOProbabilitySet probabilitySetWithObjects:objects weights:weights count:count] retain];
903 }
904
905 if (objects != NULL)
906 free(objects);
907 if (weights != NULL)
908 free(weights);
909
910 return result;
911}
#define EXPECT_NOT(x)
return nil
id probabilitySetWithObjects:weights:count:(id *objects,[weights] float *weights,[count] NSUInteger count)

References count, EXPECT_NOT, nil, and OOProbabilitySet::probabilitySetWithObjects:weights:count:.

Here is the call graph for this function:

◆ count

- (NSUInteger) count
implementation

Reimplemented from OOProbabilitySet.

Definition at line 784 of file OOProbabilitySet.m.

785{
786 return [_objects count];
787}

References count.

Referenced by copyWithZone:, count, initPrivWithObjectArray:weightsArray:sum:, initWithObjects:weights:count:, initWithPropertyListRepresentation:, randomObject, and sumOfWeights.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ dealloc

- (void) dealloc
implementation

Definition at line 768 of file OOProbabilitySet.m.

769{
770 [_objects release];
771 [_weights release];
772
773 [super dealloc];
774}

References dealloc.

Referenced by dealloc.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ initPriv

- (id) initPriv
implementation

Reimplemented from OOProbabilitySet.

Definition at line 688 of file OOProbabilitySet.m.

689{
690 if ((self = [super initPriv])) {
691 _objects = [[NSMutableArray alloc] init];
692 _weights = [[NSMutableArray alloc] init];
693 }
694
695 return self;
696}

References _objects, _weights, and initPriv.

Referenced by initPriv, initPrivWithObjectArray:weightsArray:sum:, initWithObjects:weights:count:, and initWithPropertyListRepresentation:.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ initPrivWithObjectArray:weightsArray:sum:

- (id) initPrivWithObjectArray: (NSMutableArray*) objects
weightsArray: (NSMutableArray*) weights
sum: (float) sumOfWeights 

Definition at line 699 of file OOProbabilitySet.m.

699 :(NSMutableArray*)objects weightsArray:(NSMutableArray*)weights sum:(float)sumOfWeights
700{
701 assert(objects != nil && weights != nil && [objects count] == [weights count] && sumOfWeights >= 0.0f);
702
703 if ((self = [super initPriv])) {
704 _objects = [objects retain];
705 _weights = [weights retain];
707 }
708
709 return self;
710}

References _objects, _sumOfWeights, _weights, count, initPriv, nil, and sumOfWeights.

Here is the call graph for this function:

◆ initWithObjects:weights:count:

- (id) initWithObjects: (id*) objects
weights: (float*) weights
count: (NSUInteger) count 
implementation

Reimplemented from OOMutableProbabilitySet.

Definition at line 712 of file OOProbabilitySet.m.

712 :(id*)objects weights:(float*)weights count:(NSUInteger)count
713{
714 NSUInteger i = 0;
715
716 // Validate parameters.
717 if (count != 0 && (objects == NULL || weights == NULL)) {
718 [self release];
719 [NSException raise:NSInvalidArgumentException format:@"Attempt to create %@ with non-zero count but nil objects or weights.", @"OOMutableProbabilitySet"];
720 }
721
722 // Set up & go.
723 if ((self = [self initPriv])) {
724 for (i = 0; i != count; ++i) {
725 [self setWeight:fmax(weights[i], 0.0f) forObject:objects[i]];
726 }
727 }
728
729 return self;
730}

References count, initPriv, and setWeight:forObject:.

Here is the call graph for this function:

◆ initWithPropertyListRepresentation:

- (id) initWithPropertyListRepresentation: (NSDictionary*) plist
implementation

Reimplemented from OOMutableProbabilitySet.

Definition at line 732 of file OOProbabilitySet.m.

732 :(NSDictionary*)plist
733{
734 BOOL OK = YES;
735 NSArray* objects = nil;
736 NSArray* weights = nil;
737 NSUInteger i = 0, count = 0;
738
739 if (!(self = [super initPriv]))
740 OK = NO;
741
742 if (OK) {
743 objects = [plist oo_arrayForKey:kObjectsKey];
744 weights = [plist oo_arrayForKey:kWeightsKey];
745
746 // Validate
747 if (objects == nil || weights == nil)
748 OK = NO;
749 count = [objects count];
750 if (count != [weights count])
751 OK = NO;
752 }
753
754 if (OK) {
755 for (i = 0; i < count; ++i) {
756 [self setWeight:[weights oo_floatAtIndex:i] forObject:[objects objectAtIndex:i]];
757 }
758 }
759
760 if (!OK) {
761 [self release];
762 self = nil;
763 }
764
765 return self;
766}

References count, initPriv, nil, and setWeight:forObject:.

Here is the call graph for this function:

◆ mutableCopyWithZone:

- (id) mutableCopyWithZone: (NSZone*) zone
implementation

Reimplemented from OOProbabilitySet.

Definition at line 913 of file OOProbabilitySet.m.

913 :(NSZone*)zone
914{
915 return [[OOConcreteMutableProbabilitySet alloc] initPrivWithObjectArray:[[_objects mutableCopyWithZone:zone] autorelease]
916 weightsArray:[[_weights mutableCopyWithZone:zone] autorelease]
917 sum:_sumOfWeights];
918}

◆ objectEnumerator

- (NSEnumerator *) objectEnumerator
implementation

Reimplemented from OOProbabilitySet.

Definition at line 844 of file OOProbabilitySet.m.

845{
846 return [_objects objectEnumerator];
847}

References objectEnumerator.

Referenced by objectEnumerator.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ propertyListRepresentation

- (NSDictionary *) propertyListRepresentation
implementation

Reimplemented from OOProbabilitySet.

Definition at line 776 of file OOProbabilitySet.m.

777{
778 return [NSDictionary dictionaryWithObjectsAndKeys:
779 _objects, kObjectsKey,
780 _weights, kWeightsKey,
781 nil];
782}

References propertyListRepresentation.

Referenced by propertyListRepresentation.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ randomObject

- (id) randomObject
implementation

Reimplemented from OOProbabilitySet.

Definition at line 789 of file OOProbabilitySet.m.

790{
791 float target = 0.0f, sum = 0.0f, sumOfWeights;
792 NSUInteger i = 0, count = 0;
793
794 sumOfWeights = [self sumOfWeights];
795 target = randf() * sumOfWeights;
796 count = [_objects count];
797 if (count == 0 || sumOfWeights <= 0.0f)
798 return nil;
799
800 for (i = 0; i < count; ++i) {
801 sum += [_weights oo_floatAtIndex:i];
802 if (sum >= target)
803 return [_objects objectAtIndex:i];
804 }
805
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];
808}
#define OOLog(class, format,...)
Definition OOLogging.h:84
float randf(void)

References count, nil, OOLog, randf(), randomObject, and sumOfWeights.

Referenced by randomObject.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ removeObject:

- (void) removeObject: (id) object
implementation

Reimplemented from OOMutableProbabilitySet.

Definition at line 869 of file OOProbabilitySet.m.

869 :(id)object
870{
871 if (object == nil)
872 return;
873
874 NSUInteger index = [_objects indexOfObject:object];
875 if (index != NSNotFound) {
876 [_objects removeObjectAtIndex:index];
877 _sumOfWeights = -1.0f; // Simply subtracting the relevant weight doesn't work if the weight is large, due to floating-point precision issues.
878 [_weights removeObjectAtIndex:index];
879 }
880}

References _sumOfWeights, and nil.

◆ setWeight:forObject:

- (void) setWeight: (float) weight
forObject: (id) object 
implementation

Reimplemented from OOMutableProbabilitySet.

Definition at line 849 of file OOProbabilitySet.m.

849 :(float)weight forObject:(id)object
850{
851 if (object == nil)
852 return;
853
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];
859 if (_sumOfWeights >= 0) {
860 _sumOfWeights += weight;
861 }
862 // Else, _sumOfWeights is invalid and will need to be recalculated on demand.
863 } else {
864 _sumOfWeights = -1.0f; // Simply subtracting the relevant weight doesn't work if the weight is large, due to floating-point precision issues.
865 [_weights replaceObjectAtIndex:index withObject:[NSNumber numberWithFloat:weight]];
866 }
867}

References _sumOfWeights, and nil.

Referenced by initWithObjects:weights:count:, and initWithPropertyListRepresentation:.

Here is the caller graph for this function:

◆ sumOfWeights

- (float) sumOfWeights
implementation

Reimplemented from OOProbabilitySet.

Definition at line 825 of file OOProbabilitySet.m.

826{
827 if (_sumOfWeights < 0.0f) {
828 NSUInteger i, count;
829 count = [self count];
830
831 _sumOfWeights = 0.0f;
832 for (i = 0; i < count; ++i) {
833 _sumOfWeights += [_weights oo_floatAtIndex:i];
834 }
835 }
836 return _sumOfWeights;
837}

References _sumOfWeights, count, and sumOfWeights.

Referenced by initPrivWithObjectArray:weightsArray:sum:, randomObject, and sumOfWeights.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ weightForObject:

- (float) weightForObject: (id) object
implementation

Reimplemented from OOProbabilitySet.

Definition at line 810 of file OOProbabilitySet.m.

810 :(id)object
811{
812 float result = -1.0f;
813
814 if (object != nil) {
815 NSUInteger index = [_objects indexOfObject:object];
816 if (index != NSNotFound) {
817 result = [_weights oo_floatAtIndex:index];
818 if (index != 0)
819 result -= [_weights oo_floatAtIndex:index - 1];
820 }
821 }
822 return result;
823}

References nil.

Member Data Documentation

◆ _objects

- (NSMutableArray*) _objects
private

Definition at line 96 of file OOProbabilitySet.m.

Referenced by initPriv, and initPrivWithObjectArray:weightsArray:sum:.

◆ _sumOfWeights

- (float) _sumOfWeights
private

◆ _weights

- (NSMutableArray*) _weights
private

Definition at line 97 of file OOProbabilitySet.m.

Referenced by initPriv, and initPrivWithObjectArray:weightsArray:sum:.


The documentation for this class was generated from the following file: