Oolite 1.91.0.7644-241112-7f5034b
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 41 of file OOProbabilitySet.h.

Method Documentation

◆ allObjects

- (NSArray *) allObjects

Reimplemented in OOConcreteMutableProbabilitySet, OOConcreteProbabilitySet, OOEmptyProbabilitySet, and OOSingleObjectProbabilitySet.

Definition at line 121 of file OOProbabilitySet.m.

279{
281}
282
static void ThrowAbstractionViolationException(id obj) GCC_ATTR((noreturn))

◆ containsObject:

- (BOOL) containsObject: (id) object

Provided by category OOProbabilitySet(OOExtendedProbabilitySet).

Definition at line 121 of file OOProbabilitySet.m.

308{
309 return [self weightForObject:object] >= 0.0f;
310}
311

◆ copyWithZone:

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

Reimplemented in OOConcreteMutableProbabilitySet, OOEmptyProbabilitySet, and OOMutableProbabilitySet.

Definition at line 121 of file OOProbabilitySet.m.

285{
286 if (zone == [self zone])
287 {
288 return [self retain];
289 }
290 else
291 {
292 return [[OOProbabilitySet allocWithZone:zone] initWithPropertyListRepresentation:[self propertyListRepresentation]];
293 }
294}
295

◆ count

- (NSUInteger) count

◆ descriptionComponents

- (NSString *) descriptionComponents
implementation

Definition at line 121 of file OOProbabilitySet.m.

244{
245 return [NSString stringWithFormat:@"count=%lu", [self count]];
246}
247

◆ init

- (id) init

Reimplemented in OOMutableProbabilitySet.

Definition at line 121 of file OOProbabilitySet.m.

154{
155 [self release];
156 return [OOEmptyProbabilitySet singleton];
157}
158

◆ initPriv

- (id) initPriv
implementation

Reimplemented in OOConcreteMutableProbabilitySet.

Provided by category OOProbabilitySet(OOPrivate).

Definition at line 121 of file OOProbabilitySet.m.

238{
239 return [super init];
240}
241

◆ initWithObjects:weights:count:

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

Reimplemented in OOConcreteMutableProbabilitySet, OOConcreteProbabilitySet, and OOMutableProbabilitySet.

Definition at line 121 of file OOProbabilitySet.m.

161{
162 NSZone *zone = [self zone];
163 DESTROY(self);
164
165 // Zero objects: return empty-set singleton.
166 if (count == 0) return [OOEmptyProbabilitySet singleton];
167
168 // If count is not zero and one of the paramters is nil, we've got us a programming error.
169 if (objects == NULL || weights == NULL)
170 {
171 [NSException raise:NSInvalidArgumentException format:@"Attempt to create %@ with non-zero count but nil objects or weights.", @"OOProbabilitySet"];
172 }
173
174 // Single object: simple one-object set. Expected to be quite common.
175 if (count == 1) return [[OOSingleObjectProbabilitySet allocWithZone:zone] initWithObject:objects[0] weight:weights[0]];
176
177 // Otherwise, use general implementation.
178 return [[OOConcreteProbabilitySet allocWithZone:zone] initWithObjects:objects weights:weights count:count];
179}
180
#define DESTROY(x)
Definition OOCocoa.h:77

◆ initWithPropertyListRepresentation:

- (id) initWithPropertyListRepresentation: (NSDictionary *) plist

Reimplemented in OOConcreteMutableProbabilitySet, and OOMutableProbabilitySet.

Definition at line 121 of file OOProbabilitySet.m.

183{
184 NSArray *objects = nil;
185 NSArray *weights = nil;
186 NSUInteger i = 0, count = 0;
187 id *rawObjects = NULL;
188 float *rawWeights = NULL;
189
190 objects = [plist oo_arrayForKey:kObjectsKey];
191 weights = [plist oo_arrayForKey:kWeightsKey];
192
193 // Validate
194 if (objects == nil || weights == nil)
195 {
196 [self release];
197 return nil;
198 }
199 count = [objects count];
200 if (count != [weights count])
201 {
202 [self release];
203 return nil;
204 }
205
206 // Extract contents.
207 rawObjects = malloc(sizeof *rawObjects * count);
208 rawWeights = malloc(sizeof *rawWeights * count);
209
210 if (rawObjects != NULL || rawWeights != NULL)
211 {
212 // Extract objects.
213 [objects getObjects:rawObjects];
214
215 // Extract and convert weights.
216 for (i = 0; i < count; ++i)
217 {
218 rawWeights[i] = fmax([weights oo_floatAtIndex:i], 0.0f);
219 }
220
221 self = [self initWithObjects:rawObjects weights:rawWeights count:count];
222 }
223 else
224 {
225 [self release];
226 self = nil;
227 }
228
229 // Clean up.
230 free(rawObjects);
231 free(rawWeights);
232
233 return self;
234}
235
return nil

◆ mutableCopyWithZone:

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

Reimplemented in OOConcreteMutableProbabilitySet, OOConcreteProbabilitySet, OOEmptyProbabilitySet, and OOSingleObjectProbabilitySet.

Definition at line 121 of file OOProbabilitySet.m.

298{
299 return [[OOMutableProbabilitySet allocWithZone:zone] initWithPropertyListRepresentation:[self propertyListRepresentation]];
300}
301

◆ objectEnumerator

- (NSEnumerator *) objectEnumerator

Reimplemented in OOConcreteMutableProbabilitySet, and OOConcreteProbabilitySet.

Provided by category OOProbabilitySet(OOExtendedProbabilitySet).

Definition at line 121 of file OOProbabilitySet.m.

314{
315 return [[self allObjects] objectEnumerator];
316}
317

◆ probabilityForObject:

- (float) probabilityForObject: (id) object

Provided by category OOProbabilitySet(OOExtendedProbabilitySet).

Definition at line 121 of file OOProbabilitySet.m.

320{
321 float weight = [self weightForObject:object];
322 if (weight > 0) weight /= [self sumOfWeights];
323
324 return weight;
325}
326

◆ probabilitySet

+ (id) probabilitySet

Reimplemented in OOMutableProbabilitySet.

Definition at line 121 of file OOProbabilitySet.m.

136{
137 return [[OOEmptyProbabilitySet singleton] autorelease];
138}
139

◆ probabilitySetWithObjects:weights:count:

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

Definition at line 121 of file OOProbabilitySet.m.

142{
143 return [[[self alloc] initWithObjects:objects weights:weights count:count] autorelease];
144}
145

◆ probabilitySetWithPropertyListRepresentation:

+ (id) probabilitySetWithPropertyListRepresentation: (NSDictionary *) plist

Definition at line 121 of file OOProbabilitySet.m.

148{
149 return [[[self alloc] initWithPropertyListRepresentation:plist] autorelease];
150}
151

◆ propertyListRepresentation

- (NSDictionary *) propertyListRepresentation

◆ randomObject

- (id) randomObject

◆ sumOfWeights

- (float) sumOfWeights

◆ weightForObject:

- (float) weightForObject: (id) object

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