Oolite
Loading...
Searching...
No Matches
OOProbabilitySet Class Reference

#include <OOProbabilitySet.h>

Inheritance diagram for OOProbabilitySet:
Collaboration diagram for OOProbabilitySet:

Instance Methods

(id) - init
(id) - initWithObjects:weights:count:
(id) - initWithPropertyListRepresentation:
(NSDictionary *) - propertyListRepresentation
(NSUInteger) - count
(id) - randomObject
(float) - weightForObject:
(float) - sumOfWeights
(NSArray *) - allObjects
(id) - initPriv [implementation]
(NSString *) - descriptionComponents [implementation]
(id) - copyWithZone: [implementation]
(id) - mutableCopyWithZone: [implementation]
(BOOL) - containsObject:
(NSEnumerator *) - objectEnumerator
(float) - probabilityForObject:

Class Methods

(id) + probabilitySet
(id) + probabilitySetWithObjects:weights:count:
(id) + probabilitySetWithPropertyListRepresentation:

Detailed Description

Definition at line 40 of file OOProbabilitySet.h.

Method Documentation

◆ allObjects

- (NSArray *) allObjects

Reimplemented in OOConcreteMutableProbabilitySet, OOConcreteProbabilitySet, OOEmptyProbabilitySet, and OOSingleObjectProbabilitySet.

Definition at line 247 of file OOProbabilitySet.m.

248{
250}
static void ThrowAbstractionViolationException(id obj) GCC_ATTR((noreturn))

References allObjects, and ThrowAbstractionViolationException().

Referenced by allObjects, OOProbabilitySet(OOExtendedProbabilitySet)::objectEnumerator, and OOShipRegistry(OOConveniences)::shipKeysWithRole:.

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

◆ containsObject:

- (BOOL) containsObject: (id) object

Provided by category OOProbabilitySet(OOExtendedProbabilitySet).

Definition at line 270 of file OOProbabilitySet.m.

270 :(id)object
271{
272 return [self weightForObject:object] >= 0.0f;
273}

◆ copyWithZone:

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

Reimplemented in OOConcreteMutableProbabilitySet, OOEmptyProbabilitySet, and OOMutableProbabilitySet.

Definition at line 252 of file OOProbabilitySet.m.

252 :(NSZone*)zone
253{
254 if (zone == [self zone]) {
255 return [self retain];
256 } else {
257 return [[OOProbabilitySet allocWithZone:zone] initWithPropertyListRepresentation:[self propertyListRepresentation]];
258 }
259}

References propertyListRepresentation.

Here is the call graph for this function:

◆ count

- (NSUInteger) count

Reimplemented in OOConcreteMutableProbabilitySet, OOConcreteProbabilitySet, OOEmptyProbabilitySet, and OOSingleObjectProbabilitySet.

Definition at line 242 of file OOProbabilitySet.m.

243{
245}

References count, and ThrowAbstractionViolationException().

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

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

◆ descriptionComponents

- (NSString *) descriptionComponents
implementation

Definition at line 217 of file OOProbabilitySet.m.

218{
219 return [NSString stringWithFormat:@"count=%zu", [self count]];
220}

References count, and descriptionComponents.

Referenced by descriptionComponents.

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

◆ init

- (id) init

Reimplemented in OOMutableProbabilitySet.

Definition at line 136 of file OOProbabilitySet.m.

137{
138 [self release];
139 return [OOEmptyProbabilitySet singleton];
140}

References init.

Referenced by init.

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

◆ initPriv

- (id) initPriv
implementation

Reimplemented in OOConcreteMutableProbabilitySet.

Provided by category OOProbabilitySet(OOPrivate).

Definition at line 212 of file OOProbabilitySet.m.

213{
214 return [super init];
215}

References OOProbabilitySet(OOPrivate)::initPriv.

Referenced by OOSingleObjectProbabilitySet::if, OOProbabilitySet(OOPrivate)::initPriv, and OOConcreteProbabilitySet::initWithObjects:weights:count:.

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

◆ initWithObjects:weights:count:

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

Reimplemented in OOConcreteMutableProbabilitySet, OOConcreteProbabilitySet, and OOMutableProbabilitySet.

Definition at line 142 of file OOProbabilitySet.m.

142 :(id*)objects weights:(float*)weights count:(NSUInteger)count
143{
144 NSZone* zone = [self zone];
145 DESTROY(self);
146
147 // Zero objects: return empty-set singleton.
148 if (count == 0)
149 return [OOEmptyProbabilitySet singleton];
150
151 // If count is not zero and one of the paramters is nil, we've got us a programming error.
152 if (objects == NULL || weights == NULL) {
153 [NSException raise:NSInvalidArgumentException format:@"Attempt to create %@ with non-zero count but nil objects or weights.", @"OOProbabilitySet"];
154 }
155
156 // Single object: simple one-object set. Expected to be quite common.
157 if (count == 1)
158 return [[OOSingleObjectProbabilitySet allocWithZone:zone] initWithObject:objects[0] weight:weights[0]];
159
160 // Otherwise, use general implementation.
161 return [[OOConcreteProbabilitySet allocWithZone:zone] initWithObjects:objects weights:weights count:count];
162}
#define DESTROY(x)

References count, and DESTROY.

Referenced by initWithPropertyListRepresentation:.

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

◆ initWithPropertyListRepresentation:

- (id) initWithPropertyListRepresentation: (NSDictionary*) plist

Reimplemented in OOConcreteMutableProbabilitySet, and OOMutableProbabilitySet.

Definition at line 164 of file OOProbabilitySet.m.

164 :(NSDictionary*)plist
165{
166 NSArray* objects = nil;
167 NSArray* weights = nil;
168 NSUInteger i = 0, count = 0;
169 id* rawObjects = NULL;
170 float* rawWeights = NULL;
171
172 objects = [plist oo_arrayForKey:kObjectsKey];
173 weights = [plist oo_arrayForKey:kWeightsKey];
174
175 // Validate
176 if (objects == nil || weights == nil) {
177 [self release];
178 return nil;
179 }
180 count = [objects count];
181 if (count != [weights count]) {
182 [self release];
183 return nil;
184 }
185
186 // Extract contents.
187 rawObjects = malloc(sizeof *rawObjects * count);
188 rawWeights = malloc(sizeof *rawWeights * count);
189
190 if (rawObjects != NULL || rawWeights != NULL) {
191 // Extract objects.
192 [objects getObjects:rawObjects];
193
194 // Extract and convert weights.
195 for (i = 0; i < count; ++i) {
196 rawWeights[i] = fmax([weights oo_floatAtIndex:i], 0.0f);
197 }
198
199 self = [self initWithObjects:rawObjects weights:rawWeights count:count];
200 } else {
201 [self release];
202 self = nil;
203 }
204
205 // Clean up.
206 free(rawObjects);
207 free(rawWeights);
208
209 return self;
210}
return nil

References count, initWithObjects:weights:count:, and nil.

Here is the call graph for this function:

◆ mutableCopyWithZone:

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

Reimplemented in OOConcreteMutableProbabilitySet, OOConcreteProbabilitySet, OOEmptyProbabilitySet, and OOSingleObjectProbabilitySet.

Definition at line 261 of file OOProbabilitySet.m.

261 :(NSZone*)zone
262{
263 return [[OOMutableProbabilitySet allocWithZone:zone] initWithPropertyListRepresentation:[self propertyListRepresentation]];
264}

References propertyListRepresentation.

Here is the call graph for this function:

◆ objectEnumerator

- (NSEnumerator *) objectEnumerator

Reimplemented in OOConcreteMutableProbabilitySet, and OOConcreteProbabilitySet.

Provided by category OOProbabilitySet(OOExtendedProbabilitySet).

Definition at line 275 of file OOProbabilitySet.m.

276{
277 return [[self allObjects] objectEnumerator];
278}

◆ probabilityForObject:

- (float) probabilityForObject: (id) object

Provided by category OOProbabilitySet(OOExtendedProbabilitySet).

Definition at line 280 of file OOProbabilitySet.m.

280 :(id)object
281{
282 float weight = [self weightForObject:object];
283 if (weight > 0)
284 weight /= [self sumOfWeights];
285
286 return weight;
287}

◆ probabilitySet

+ (id) probabilitySet

Reimplemented in OOMutableProbabilitySet.

Definition at line 121 of file OOProbabilitySet.m.

122{
123 return [[OOEmptyProbabilitySet singleton] autorelease];
124}

References probabilitySet.

Referenced by probabilitySet.

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

◆ probabilitySetWithObjects:weights:count:

+ (id) probabilitySetWithObjects: (id*) objects
weights: (float*) weights
count: (NSUInteger) count 

Definition at line 126 of file OOProbabilitySet.m.

126 :(id*)objects weights:(float*)weights count:(NSUInteger)count
127{
128 return [[[self alloc] initWithObjects:objects weights:weights count:count] autorelease];
129}

Referenced by OOConcreteMutableProbabilitySet::copyWithZone:.

Here is the caller graph for this function:

◆ probabilitySetWithPropertyListRepresentation:

+ (id) probabilitySetWithPropertyListRepresentation: (NSDictionary*) plist

Definition at line 131 of file OOProbabilitySet.m.

131 :(NSDictionary*)plist
132{
133 return [[[self alloc] initWithPropertyListRepresentation:plist] autorelease];
134}

Referenced by OOShipRegistry(OODataLoader)::loadCachedRoleProbabilitySets.

Here is the caller graph for this function:

◆ propertyListRepresentation

- (NSDictionary *) propertyListRepresentation

Reimplemented in OOConcreteMutableProbabilitySet, OOConcreteProbabilitySet, OOEmptyProbabilitySet, and OOSingleObjectProbabilitySet.

Definition at line 222 of file OOProbabilitySet.m.

223{
225}

References propertyListRepresentation, and ThrowAbstractionViolationException().

Referenced by OOShipRegistry(OODataLoader)::buildRoleProbabilitySets, OOMutableProbabilitySet::copyWithZone:, copyWithZone:, mutableCopyWithZone:, and propertyListRepresentation.

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

◆ randomObject

- (id) randomObject

Reimplemented in OOConcreteMutableProbabilitySet, OOConcreteProbabilitySet, OOEmptyProbabilitySet, and OOSingleObjectProbabilitySet.

Definition at line 227 of file OOProbabilitySet.m.

228{
230}

References randomObject, and ThrowAbstractionViolationException().

Referenced by randomObject, OOShipRegistry(OOConveniences)::randomShipKeyForRole:, and Universe::randomShipKeyForRoleRespectingConditions:.

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

◆ sumOfWeights

- (float) sumOfWeights

Reimplemented in OOConcreteMutableProbabilitySet, OOConcreteProbabilitySet, OOEmptyProbabilitySet, and OOSingleObjectProbabilitySet.

Definition at line 237 of file OOProbabilitySet.m.

238{
240}

References sumOfWeights, and ThrowAbstractionViolationException().

Referenced by OOProbabilitySet(OOExtendedProbabilitySet)::probabilityForObject:, and sumOfWeights.

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

◆ weightForObject:

- (float) weightForObject: (id) object

Reimplemented in OOConcreteMutableProbabilitySet, OOConcreteProbabilitySet, OOEmptyProbabilitySet, and OOSingleObjectProbabilitySet.

Definition at line 232 of file OOProbabilitySet.m.

232 :(id)object
233{
235}

References ThrowAbstractionViolationException().

Referenced by OOProbabilitySet(OOExtendedProbabilitySet)::containsObject:, and OOProbabilitySet(OOExtendedProbabilitySet)::probabilityForObject:.

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

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