Oolite 1.91.0.7644-241112-7f5034b
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:
 
- Instance Methods inherited from <OOProbabilitySetEnumerable>

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 92 of file OOProbabilitySet.m.

Method Documentation

◆ allObjects

- (NSArray *) allObjects
implementation

Reimplemented from OOProbabilitySet.

Definition at line 458 of file OOProbabilitySet.m.

679{
680 return [NSArray arrayWithObjects:_objects count:_count];
681}
682

◆ count

- (NSUInteger) count
implementation

Reimplemented from OOProbabilitySet.

Definition at line 458 of file OOProbabilitySet.m.

599{
600 return _count;
601}
602

◆ dealloc

- (void) dealloc
implementation

Definition at line 458 of file OOProbabilitySet.m.

553{
554 NSUInteger i = 0;
555
556 if (_objects != NULL)
557 {
558 for (i = 0; i < _count; ++i)
559 {
560 [_objects[i] release];
561 }
562 free(_objects);
563 _objects = NULL;
564 }
565
566 if (_cumulativeWeights != NULL)
567 {
568 free(_cumulativeWeights);
569 _cumulativeWeights = NULL;
570 }
571
572 [super dealloc];
573}
574

◆ initWithObjects:weights:count:

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

Reimplemented from OOProbabilitySet.

Definition at line 458 of file OOProbabilitySet.m.

520{
521 NSUInteger i = 0;
522 float cuWeight = 0.0f;
523
524 assert(count > 1 && objects != NULL && weights != NULL);
525
526 if ((self = [super initPriv]))
527 {
528 // Allocate arrays
529 _objects = malloc(sizeof *objects * count);
530 _cumulativeWeights = malloc(sizeof *_cumulativeWeights * count);
531 if (_objects == NULL || _cumulativeWeights == NULL)
532 {
533 [self release];
534 return nil;
535 }
536
537 // Fill in arrays, retain objects, add up weights.
538 for (i = 0; i != count; ++i)
539 {
540 _objects[i] = [objects[i] retain];
541 cuWeight += weights[i];
542 _cumulativeWeights[i] = cuWeight;
543 }
544 _count = count;
545 _sumOfWeights = cuWeight;
546 }
547
548 return self;
549}
550
return nil

◆ mutableCopyWithZone:

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

Reimplemented from OOProbabilitySet.

Definition at line 458 of file OOProbabilitySet.m.

697{
698 id result = nil;
699 float *weights = NULL;
700 NSUInteger i = 0;
701 float weight = 0.0f, sum = 0.0f;
702
703 // Convert cumulative weights to "plain" weights.
704 weights = malloc(sizeof *weights * _count);
705 if (weights == NULL) return nil;
706
707 for (i = 0; i < _count; ++i)
708 {
709 weight = _cumulativeWeights[i];
710 weights[i] = weight - sum;
711 sum += weights[i];
712 }
713
714 result = [[OOConcreteMutableProbabilitySet allocWithZone:zone] initWithObjects:_objects weights:weights count:_count];
715 free(weights);
716
717 return result;
718}
719

◆ objectEnumerator

- (NSEnumerator *) objectEnumerator
implementation

Reimplemented from OOProbabilitySet.

Definition at line 458 of file OOProbabilitySet.m.

685{
686 return [[[OOProbabilitySetEnumerator alloc] initWithEnumerable:self] autorelease];
687}
688

◆ privObjectAtIndex:

- (id) privObjectAtIndex: (NSUInteger) index
implementation

Reimplemented from <OOProbabilitySetEnumerable>.

Definition at line 458 of file OOProbabilitySet.m.

691{
692 return (index < _count) ? _objects[index] : nil;
693}
694

◆ privObjectForWeight:

- (id) privObjectForWeight: (float) target
implementation

Definition at line 458 of file OOProbabilitySet.m.

605{
606 /* Select an object at random. This is a binary search in the cumulative
607 weights array. Since weights of zero are allowed, there may be several
608 objects with the same cumulative weight, in which case we select the
609 leftmost, i.e. the one where the delta is non-zero.
610 */
611
612 NSUInteger low = 0, high = _count - 1, idx = 0;
613 float weight = 0.0f;
614
615 while (low < high)
616 {
617 idx = (low + high) / 2;
618 weight = _cumulativeWeights[idx];
619 if (weight > target)
620 {
621 if (EXPECT_NOT(idx == 0)) break;
622 high = idx - 1;
623 }
624 else if (weight < target) low = idx + 1;
625 else break;
626 }
627
628 if (weight > target)
629 {
630 while (idx > 0 && _cumulativeWeights[idx - 1] >= target) --idx;
631 }
632 else
633 {
634 while (idx < (_count - 1) && _cumulativeWeights[idx] < target) ++idx;
635 }
636
637 assert(idx < _count);
638 id result = _objects[idx];
639 return result;
640}
641
#define EXPECT_NOT(x)

◆ propertyListRepresentation

- (NSDictionary *) propertyListRepresentation
implementation

Reimplemented from OOProbabilitySet.

Definition at line 458 of file OOProbabilitySet.m.

577{
578 NSArray *objects = nil;
579 NSMutableArray *weights = nil;
580 float cuWeight = 0.0f, sum = 0.0f;
581 NSUInteger i = 0;
582
583 objects = [NSArray arrayWithObjects:_objects count:_count];
584 weights = [NSMutableArray arrayWithCapacity:_count];
585 for (i = 0; i < _count; ++i)
586 {
587 cuWeight = _cumulativeWeights[i];
588 [weights oo_addFloat:cuWeight - sum];
589 sum = cuWeight;
590 }
591
592 return [NSDictionary dictionaryWithObjectsAndKeys:
593 objects, kObjectsKey,
594 [[weights copy] autorelease], kWeightsKey,
595 nil];
596}
597

◆ randomObject

- (id) randomObject
implementation

Reimplemented from OOProbabilitySet.

Definition at line 458 of file OOProbabilitySet.m.

644{
645 if (_sumOfWeights <= 0.0f) return nil;
646 return [self privObjectForWeight:randf() * _sumOfWeights];
647}
648

◆ sumOfWeights

- (float) sumOfWeights
implementation

Reimplemented from OOProbabilitySet.

Definition at line 458 of file OOProbabilitySet.m.

673{
674 return _sumOfWeights;
675}
676

◆ weightForObject:

- (float) weightForObject: (id) object
implementation

Reimplemented from OOProbabilitySet.

Definition at line 458 of file OOProbabilitySet.m.

651{
652 NSUInteger i;
653
654 // Can't have nil in collection.
655 if (object == nil) return -1.0f;
656
657 // Perform linear search, then get weight by subtracting cumulative weight from cumulative weight to left.
658 for (i = 0; i < _count; ++i)
659 {
660 if ([_objects[i] isEqual:object])
661 {
662 float leftWeight = (i != 0) ? _cumulativeWeights[i - 1] : 0.0f;
663 return _cumulativeWeights[i] - leftWeight;
664 }
665 }
666
667 // If we got here, object not found.
668 return -1.0f;
669}
670

Member Data Documentation

◆ _count

- (NSUInteger) _count
private

Definition at line 96 of file OOProbabilitySet.m.

◆ _cumulativeWeights

- (float*) _cumulativeWeights
private

Definition at line 98 of file OOProbabilitySet.m.

◆ _objects

- (id*) _objects
private

Definition at line 97 of file OOProbabilitySet.m.

◆ _sumOfWeights

- (float) _sumOfWeights
private

Definition at line 99 of file OOProbabilitySet.m.


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