Oolite 1.91.0.7645-241119-222d325
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 104 of file OOProbabilitySet.m.

Method Documentation

◆ allObjects

- (NSArray *) allObjects
implementation

Reimplemented from OOProbabilitySet.

Definition at line 458 of file OOProbabilitySet.m.

947{
948 return [[_objects copy] autorelease];
949}
950

◆ copyWithZone:

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

Reimplemented from OOMutableProbabilitySet.

Definition at line 458 of file OOProbabilitySet.m.

997{
998 id result = nil;
999 id *objects = NULL;
1000 float *weights = NULL;
1001 NSUInteger i = 0, count = 0;
1002
1003 count = [_objects count];
1004 if (EXPECT_NOT(count == 0)) return [OOEmptyProbabilitySet singleton];
1005
1006 objects = malloc(sizeof *objects * count);
1007 weights = malloc(sizeof *weights * count);
1008 if (objects != NULL && weights != NULL)
1009 {
1010 [_objects getObjects:objects];
1011
1012 for (i = 0; i < count; ++i)
1013 {
1014 weights[i] = [_weights oo_floatAtIndex:i];
1015 }
1016
1017 result = [[OOProbabilitySet probabilitySetWithObjects:objects weights:weights count:count] retain];
1018 }
1019
1020 if (objects != NULL) free(objects);
1021 if (weights != NULL) free(weights);
1022
1023 return result;
1024}
1025
#define EXPECT_NOT(x)
return nil
id probabilitySetWithObjects:weights:count:(id *objects,[weights] float *weights,[count] NSUInteger count)

◆ count

- (NSUInteger) count
implementation

Reimplemented from OOProbabilitySet.

Definition at line 458 of file OOProbabilitySet.m.

886{
887 return [_objects count];
888}
889

◆ dealloc

- (void) dealloc
implementation

Definition at line 458 of file OOProbabilitySet.m.

868{
869 [_objects release];
870 [_weights release];
871
872 [super dealloc];
873}
874

◆ initPriv

- (id) initPriv
implementation

Reimplemented from OOProbabilitySet.

Definition at line 458 of file OOProbabilitySet.m.

778{
779 if ((self = [super initPriv]))
780 {
781 _objects = [[NSMutableArray alloc] init];
782 _weights = [[NSMutableArray alloc] init];
783 }
784
785 return self;
786}
787

◆ initPrivWithObjectArray:weightsArray:sum:

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

Definition at line 458 of file OOProbabilitySet.m.

791{
792 assert(objects != nil && weights != nil && [objects count] == [weights count] && sumOfWeights >= 0.0f);
793
794 if ((self = [super initPriv]))
795 {
796 _objects = [objects retain];
797 _weights = [weights retain];
799 }
800
801 return self;
802}
803

◆ initWithObjects:weights:count:

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

Reimplemented from OOMutableProbabilitySet.

Definition at line 458 of file OOProbabilitySet.m.

806{
807 NSUInteger i = 0;
808
809 // Validate parameters.
810 if (count != 0 && (objects == NULL || weights == NULL))
811 {
812 [self release];
813 [NSException raise:NSInvalidArgumentException format:@"Attempt to create %@ with non-zero count but nil objects or weights.", @"OOMutableProbabilitySet"];
814 }
815
816 // Set up & go.
817 if ((self = [self initPriv]))
818 {
819 for (i = 0; i != count; ++i)
820 {
821 [self setWeight:fmax(weights[i], 0.0f) forObject:objects[i]];
822 }
823 }
824
825 return self;
826}
827

◆ initWithPropertyListRepresentation:

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

Reimplemented from OOMutableProbabilitySet.

Definition at line 458 of file OOProbabilitySet.m.

830{
831 BOOL OK = YES;
832 NSArray *objects = nil;
833 NSArray *weights = nil;
834 NSUInteger i = 0, count = 0;
835
836 if (!(self = [super initPriv])) OK = NO;
837
838 if (OK)
839 {
840 objects = [plist oo_arrayForKey:kObjectsKey];
841 weights = [plist oo_arrayForKey:kWeightsKey];
842
843 // Validate
844 if (objects == nil || weights == nil) OK = NO;
845 count = [objects count];
846 if (count != [weights count]) OK = NO;
847 }
848
849 if (OK)
850 {
851 for (i = 0; i < count; ++i)
852 {
853 [self setWeight:[weights oo_floatAtIndex:i] forObject:[objects objectAtIndex:i]];
854 }
855 }
856
857 if (!OK)
858 {
859 [self release];
860 self = nil;
861 }
862
863 return self;
864}
865

◆ mutableCopyWithZone:

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

Reimplemented from OOProbabilitySet.

Definition at line 458 of file OOProbabilitySet.m.

1028{
1029 return [[OOConcreteMutableProbabilitySet alloc] initPrivWithObjectArray:[[_objects mutableCopyWithZone:zone] autorelease]
1030 weightsArray:[[_weights mutableCopyWithZone:zone] autorelease]
1031 sum:_sumOfWeights];
1032}
1033

◆ objectEnumerator

- (NSEnumerator *) objectEnumerator
implementation

Reimplemented from OOProbabilitySet.

Definition at line 458 of file OOProbabilitySet.m.

953{
954 return [_objects objectEnumerator];
955}
956

◆ propertyListRepresentation

- (NSDictionary *) propertyListRepresentation
implementation

Reimplemented from OOProbabilitySet.

Definition at line 458 of file OOProbabilitySet.m.

877{
878 return [NSDictionary dictionaryWithObjectsAndKeys:
879 _objects, kObjectsKey,
880 _weights, kWeightsKey,
881 nil];
882}
883

◆ randomObject

- (id) randomObject
implementation

Reimplemented from OOProbabilitySet.

Definition at line 458 of file OOProbabilitySet.m.

892{
893 float target = 0.0f, sum = 0.0f, sumOfWeights;
894 NSUInteger i = 0, count = 0;
895
896 sumOfWeights = [self sumOfWeights];
897 target = randf() * sumOfWeights;
898 count = [_objects count];
899 if (count == 0 || sumOfWeights <= 0.0f) return nil;
900
901 for (i = 0; i < count; ++i)
902 {
903 sum += [_weights oo_floatAtIndex:i];
904 if (sum >= target) return [_objects objectAtIndex:i];
905 }
906
907 OOLog(@"probabilitySet.broken", @"%s fell off end, returning first object. Nominal sum = %f, target = %f, actual sum = %f, count = %lu. %@", __PRETTY_FUNCTION__, sumOfWeights, target, sum, count,@"This is an internal error, please report it.");
908 return [_objects objectAtIndex:0];
909}
910
#define OOLog(class, format,...)
Definition OOLogging.h:88
float randf(void)

◆ removeObject:

- (void) removeObject: (id) object
implementation

Reimplemented from OOMutableProbabilitySet.

Definition at line 458 of file OOProbabilitySet.m.

983{
984 if (object == nil) return;
985
986 NSUInteger index = [_objects indexOfObject:object];
987 if (index != NSNotFound)
988 {
989 [_objects removeObjectAtIndex:index];
990 _sumOfWeights = -1.0f; // Simply subtracting the relevant weight doesn't work if the weight is large, due to floating-point precision issues.
991 [_weights removeObjectAtIndex:index];
992 }
993}
994

◆ setWeight:forObject:

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

Reimplemented from OOMutableProbabilitySet.

Definition at line 458 of file OOProbabilitySet.m.

959{
960 if (object == nil) return;
961
962 weight = fmax(weight, 0.0f);
963 NSUInteger index = [_objects indexOfObject:object];
964 if (index == NSNotFound)
965 {
966 [_objects addObject:object];
967 [_weights oo_addFloat:weight];
968 if (_sumOfWeights >= 0)
969 {
970 _sumOfWeights += weight;
971 }
972 // Else, _sumOfWeights is invalid and will need to be recalculated on demand.
973 }
974 else
975 {
976 _sumOfWeights = -1.0f; // Simply subtracting the relevant weight doesn't work if the weight is large, due to floating-point precision issues.
977 [_weights replaceObjectAtIndex:index withObject:[NSNumber numberWithFloat:weight]];
978 }
979}
980

◆ sumOfWeights

- (float) sumOfWeights
implementation

Reimplemented from OOProbabilitySet.

Definition at line 458 of file OOProbabilitySet.m.

930{
931 if (_sumOfWeights < 0.0f)
932 {
933 NSUInteger i, count;
934 count = [self count];
935
936 _sumOfWeights = 0.0f;
937 for (i = 0; i < count; ++i)
938 {
939 _sumOfWeights += [_weights oo_floatAtIndex:i];
940 }
941 }
942 return _sumOfWeights;
943}
944

◆ weightForObject:

- (float) weightForObject: (id) object
implementation

Reimplemented from OOProbabilitySet.

Definition at line 458 of file OOProbabilitySet.m.

913{
914 float result = -1.0f;
915
916 if (object != nil)
917 {
918 NSUInteger index = [_objects indexOfObject:object];
919 if (index != NSNotFound)
920 {
921 result = [_weights oo_floatAtIndex:index];
922 if (index != 0) result -= [_weights oo_floatAtIndex:index - 1];
923 }
924 }
925 return result;
926}
927

Member Data Documentation

◆ _objects

- (NSMutableArray*) _objects
private

Definition at line 107 of file OOProbabilitySet.m.

◆ _sumOfWeights

- (float) _sumOfWeights
private

Definition at line 109 of file OOProbabilitySet.m.

◆ _weights

- (NSMutableArray*) _weights
private

Definition at line 108 of file OOProbabilitySet.m.


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