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

Instance Methods

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

Private Attributes

NSUInteger _count
id * _objects
float * _cumulativeWeights
float _sumOfWeights

Additional Inherited Members

Class Methods inherited from OOProbabilitySet
(id) + probabilitySet
(id) + probabilitySetWithObjects:weights:count:
(id) + probabilitySetWithPropertyListRepresentation:

Detailed Description

Definition at line 85 of file OOProbabilitySet.m.

Method Documentation

◆ allObjects

- (NSArray *) allObjects
implementation

Reimplemented from OOProbabilitySet.

Definition at line 600 of file OOProbabilitySet.m.

601{
602 return [NSArray arrayWithObjects:_objects count:_count];
603}

References allObjects.

Referenced by allObjects.

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

◆ count

- (NSUInteger) count
implementation

Reimplemented from OOProbabilitySet.

Definition at line 526 of file OOProbabilitySet.m.

527{
528 return _count;
529}

References _count, and count.

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

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

◆ dealloc

- (void) dealloc
implementation

Definition at line 485 of file OOProbabilitySet.m.

486{
487 NSUInteger i = 0;
488
489 if (_objects != NULL) {
490 for (i = 0; i < _count; ++i) {
491 [_objects[i] release];
492 }
493 free(_objects);
494 _objects = NULL;
495 }
496
497 if (_cumulativeWeights != NULL) {
498 free(_cumulativeWeights);
499 _cumulativeWeights = NULL;
500 }
501
502 [super dealloc];
503}

References _count, _cumulativeWeights, _objects, and dealloc.

Referenced by dealloc.

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 
implementation

Reimplemented from OOProbabilitySet.

Definition at line 456 of file OOProbabilitySet.m.

456 :(id*)objects weights:(float*)weights count:(NSUInteger)count
457{
458 NSUInteger i = 0;
459 float cuWeight = 0.0f;
460
461 assert(count > 1 && objects != NULL && weights != NULL);
462
463 if ((self = [super initPriv])) {
464 // Allocate arrays
465 _objects = malloc(sizeof *objects * count);
466 _cumulativeWeights = malloc(sizeof *_cumulativeWeights * count);
467 if (_objects == NULL || _cumulativeWeights == NULL) {
468 [self release];
469 return nil;
470 }
471
472 // Fill in arrays, retain objects, add up weights.
473 for (i = 0; i != count; ++i) {
474 _objects[i] = [objects[i] retain];
475 cuWeight += weights[i];
476 _cumulativeWeights[i] = cuWeight;
477 }
478 _count = count;
479 _sumOfWeights = cuWeight;
480 }
481
482 return self;
483}
return nil

References _count, _cumulativeWeights, _objects, _sumOfWeights, count, OOProbabilitySet::initPriv, and nil.

Here is the call graph for this function:

◆ mutableCopyWithZone:

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

Reimplemented from OOProbabilitySet.

Definition at line 615 of file OOProbabilitySet.m.

615 :(NSZone*)zone
616{
617 id result = nil;
618 float* weights = NULL;
619 NSUInteger i = 0;
620 float weight = 0.0f, sum = 0.0f;
621
622 // Convert cumulative weights to "plain" weights.
623 weights = malloc(sizeof *weights * _count);
624 if (weights == NULL)
625 return nil;
626
627 for (i = 0; i < _count; ++i) {
628 weight = _cumulativeWeights[i];
629 weights[i] = weight - sum;
630 sum += weights[i];
631 }
632
633 result = [[OOConcreteMutableProbabilitySet allocWithZone:zone] initWithObjects:_objects weights:weights count:_count];
634 free(weights);
635
636 return result;
637}

References _count, _cumulativeWeights, and nil.

◆ objectEnumerator

- (NSEnumerator *) objectEnumerator
implementation

Reimplemented from OOProbabilitySet.

Definition at line 605 of file OOProbabilitySet.m.

606{
607 return [[[OOProbabilitySetEnumerator alloc] initWithEnumerable:self] autorelease];
608}

References objectEnumerator.

Referenced by objectEnumerator.

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

◆ privObjectAtIndex:

- (id) privObjectAtIndex: (NSUInteger) index
implementation

Reimplemented from <OOProbabilitySetEnumerable>.

Definition at line 610 of file OOProbabilitySet.m.

610 :(NSUInteger)index
611{
612 return (index < _count) ? _objects[index] : nil;
613}

References _count, _objects, and nil.

◆ privObjectForWeight:

- (id) privObjectForWeight: (float) target
implementation

Definition at line 531 of file OOProbabilitySet.m.

531 :(float)target
532{
533 /* Select an object at random. This is a binary search in the cumulative
534 weights array. Since weights of zero are allowed, there may be several
535 objects with the same cumulative weight, in which case we select the
536 leftmost, i.e. the one where the delta is non-zero.
537 */
538
539 NSUInteger low = 0, high = _count - 1, idx = 0;
540 float weight = 0.0f;
541
542 while (low < high) {
543 idx = (low + high) / 2;
544 weight = _cumulativeWeights[idx];
545 if (weight > target) {
546 if (EXPECT_NOT(idx == 0))
547 break;
548 high = idx - 1;
549 } else if (weight < target)
550 low = idx + 1;
551 else
552 break;
553 }
554
555 if (weight > target) {
556 while (idx > 0 && _cumulativeWeights[idx - 1] >= target)
557 --idx;
558 } else {
559 while (idx < (_count - 1) && _cumulativeWeights[idx] < target)
560 ++idx;
561 }
562
563 assert(idx < _count);
564 id result = _objects[idx];
565 return result;
566}
#define EXPECT_NOT(x)

References _count, _cumulativeWeights, _objects, and EXPECT_NOT.

Referenced by randomObject.

Here is the caller graph for this function:

◆ propertyListRepresentation

- (NSDictionary *) propertyListRepresentation
implementation

Reimplemented from OOProbabilitySet.

Definition at line 505 of file OOProbabilitySet.m.

506{
507 NSArray* objects = nil;
508 NSMutableArray* weights = nil;
509 float cuWeight = 0.0f, sum = 0.0f;
510 NSUInteger i = 0;
511
512 objects = [NSArray arrayWithObjects:_objects count:_count];
513 weights = [NSMutableArray arrayWithCapacity:_count];
514 for (i = 0; i < _count; ++i) {
515 cuWeight = _cumulativeWeights[i];
516 [weights oo_addFloat:cuWeight - sum];
517 sum = cuWeight;
518 }
519
520 return [NSDictionary dictionaryWithObjectsAndKeys:
521 objects, kObjectsKey,
522 [[weights copy] autorelease], kWeightsKey,
523 nil];
524}

References _count, _cumulativeWeights, nil, and 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 568 of file OOProbabilitySet.m.

569{
570 if (_sumOfWeights <= 0.0f)
571 return nil;
572 return [self privObjectForWeight:randf() * _sumOfWeights];
573}

References _sumOfWeights, nil, privObjectForWeight:, and randomObject.

Referenced by randomObject.

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

◆ sumOfWeights

- (float) sumOfWeights
implementation

Reimplemented from OOProbabilitySet.

Definition at line 595 of file OOProbabilitySet.m.

596{
597 return _sumOfWeights;
598}

References _sumOfWeights, and sumOfWeights.

Referenced by 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 575 of file OOProbabilitySet.m.

575 :(id)object
576{
577 NSUInteger i;
578
579 // Can't have nil in collection.
580 if (object == nil)
581 return -1.0f;
582
583 // Perform linear search, then get weight by subtracting cumulative weight from cumulative weight to left.
584 for (i = 0; i < _count; ++i) {
585 if ([_objects[i] isEqual:object]) {
586 float leftWeight = (i != 0) ? _cumulativeWeights[i - 1] : 0.0f;
587 return _cumulativeWeights[i] - leftWeight;
588 }
589 }
590
591 // If we got here, object not found.
592 return -1.0f;
593}

References _count, _cumulativeWeights, _objects, and nil.

Member Data Documentation

◆ _count

◆ _cumulativeWeights

- (float*) _cumulativeWeights
private

◆ _objects

- (id*) _objects
private

◆ _sumOfWeights

- (float) _sumOfWeights
private

Definition at line 90 of file OOProbabilitySet.m.

Referenced by initWithObjects:weights:count:, randomObject, and sumOfWeights.


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