Merge pull request #506 from andrewcsmith/patch-2
[supercollider.git] / SCClassLibrary / Common / Collections / ObjectTable.sc
blob4a320adc2a873f441db7db7e1febe3494e5c2275
1 TwoWayIdentityDictionary : Collection
3         var idToObj, objToID;
5         *new {
6                 ^super.new.init;
7         }
9         add { arg anAssociation;
10                 this.put(anAssociation.key, anAssociation.value);
11         }
12         put { arg key, obj;
13                 idToObj.put(key, obj);
14                 objToID.put(obj, key);
15         }
17         remove { arg obj;
18                 var key;
19                 key = this.getID(obj);
20                 idToObj.removeAt(key);
21                 objToID.removeAt(obj);
22         }
24         removeAt { arg key;
25                 var obj = this.at(key);
26                 idToObj.removeAt(key);
27                 objToID.removeAt(obj);
28         }
30         do { arg function;
31                 ^idToObj.do(function);
32         }
34         at { arg id;
35                 ^idToObj.at(id);
36         }
37         getID { arg obj;
38                 ^objToID.at(obj);
39         }
41         // PRIVATE
42         init {
43                 idToObj = IdentityDictionary.new;
44                 objToID = IdentityDictionary.new;
45         }
48 UniqueID {
49         classvar <id=1000;
50         *initClass { id = 1000; }
51         *next  { ^id = id + 1; }
54 ObjectTable : TwoWayIdentityDictionary
56         classvar <global;
59         *new {
60                 ^super.new;
61         }
63         add { arg obj;
64                 this.put(UniqueID.next, obj);
65         }
67         *initClass {
68                 global = this.new;
69         }
70         *add { arg obj;
71                 global.add(obj);
72                 ^UniqueID.id
73         }
74         *put { arg key, obj;
75                 global.put(key, obj);
76         }
77         *remove { arg obj;
78                 global.remove(obj);
79         }
80         *at { arg id;
81                 ^global.at(id);
82         }
83         *getID { arg obj;
84                 ^global.getID(obj);
85         }
86         *objPerform { arg id, selector ... args;
87                 var obj;
88                 obj = global.at(id);
89                 if (obj.notNil, {
90                         obj.performList(selector, args);
91                 });
92         }