Oolite 1.91.0.7604-240417-a536cbe
Loading...
Searching...
No Matches
OOProbabilitySet.h
Go to the documentation of this file.
1/*
2
3OOProbabilitySet.h
4
5A collection for selecting objects randomly, with probability weighting.
6Probability weights can be 0 - an object may be in the set but not selectable.
7Comes in mutable and immutable variants.
8
9Performance characteristics:
10 * -randomObject, the primary method, is O(log n) for immutable
11 OOProbabilitySets and O(n) for mutable ones.
12 * -containsObject: and -probabilityForObject: are O(n). This could be
13 optimized, but there's currently no need.
14
15
16Copyright (C) 2008-2013 Jens Ayton
17
18Permission is hereby granted, free of charge, to any person obtaining a copy
19of this software and associated documentation files (the "Software"), to deal
20in the Software without restriction, including without limitation the rights
21to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
22copies of the Software, and to permit persons to whom the Software is
23furnished to do so, subject to the following conditions:
24
25The above copyright notice and this permission notice shall be included in all
26copies or substantial portions of the Software.
27
28THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
29IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
30FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
31AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
32LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
33OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34SOFTWARE.
35
36*/
37
38#import "OOCocoa.h"
39
40
41@interface OOProbabilitySet: NSObject <NSCopying, NSMutableCopying>
42
43+ (id) probabilitySet;
44+ (id) probabilitySetWithObjects:(id *)objects weights:(float *)weights count:(NSUInteger)count;
45+ (id) probabilitySetWithPropertyListRepresentation:(NSDictionary *)plist;
46
47- (id) init;
48- (id) initWithObjects:(id *)objects weights:(float *)weights count:(NSUInteger)count;
49- (id) initWithPropertyListRepresentation:(NSDictionary *)plist;
50
51// propertyListRepresentation is only valid if objects are property list objects.
52- (NSDictionary *) propertyListRepresentation;
53
54- (NSUInteger) count;
55- (id) randomObject;
56
57- (float) weightForObject:(id)object; // Returns -1 for unknown objects.
58- (float) sumOfWeights;
59- (NSArray *) allObjects;
60
61@end
62
63
64@interface OOProbabilitySet (OOExtendedProbabilitySet)
65
66- (BOOL) containsObject:(id)object;
67- (NSEnumerator *) objectEnumerator;
68- (float) probabilityForObject:(id)object; // Returns -1 for unknown objects, or a value from 0 to 1 inclusive for known objects.
69
70@end
71
72
74
75- (void) setWeight:(float)weight forObject:(id)object; // Adds object if needed.
76- (void) removeObject:(id)object;
77
78@end
unsigned count
NSDictionary * propertyListRepresentation()