140 likes | 252 Views
Writing trees. Tutorial #4 CPSC 261. Swizzling. “the conversion of references based on name or position to direct pointer references” [Wikipedia] Performed when reading complex objects Unswizzling is the opposite Performed when writing complex objects. A tre e in memory. 0x1230. 37.
E N D
Writing trees Tutorial #4 CPSC 261
Swizzling • “the conversion of references based on name or position to direct pointer references” [Wikipedia] • Performed when reading complex objects • Unswizzling is the opposite • Performed when writing complex objects
A tree in memory 0x1230 37 0x1278 13 0x1238 left (0x1248) 0x1280 left (0x0) 0x1240 right (0x1260) 0x1288 right (0x0) 0x1248 25 0x1290 99 0x1250 left (0x1278) 0x1298 left (0x0) 0x1258 right (0x0) 0x12a0 right (0x0) 0x1260 52 0x1268 left (0x0) root 0x1230 0x1270 right (0x1290)
A file representation • Nodes have ids • Pointers have been converted to ids 5 1 3 37 52 99 left (2) left (0) left (0) right (3) right (5) right (0) 2 4 25 13 left (4) left (0) right (0) right (0)
Unswizzling, how to convert? 5 1 3 37 52 99 left (2) left (0) left (0) right (3) right (5) right (0) 2 4 0x1230 37 0x1278 13 25 0x1238 13 left (0x1248) 0x1280 left (0x0) left (4) left (0) 0x1240 right (0x1260) 0x1288 right (0x0) right (0) right (0) 0x1290 0x1248 25 99 0x1250 left (0x1278) 0x1298 left (0x0) 0x1258 0x12a0 right (0x0) right (0x0) 0x1260 52 0x1268 left (0x0) 0x1270 right (0x1290)
Basic idea - writing Given a tree (t) Write the id (x) to the file Write the value (t->value) to the file Write the id of (t->left) to the file Write the id of (t->right) to the file Recursively write t->left and t->right When writing data, you never swap bytes The key question is: what do we use as ids?
Option 1 – small integer ids • Initialize a global counter (nextid) to 1 • Given a tree (t) • If t is null, do nothing • Otherwise • remember the mapping t -> nextid • increment nextid • Recursively allocate ids for t->left and t->right
After allocating ids 0x1230 -> 1 0x1248 -> 2 0x1260 -> 3 0x1278 -> 4 0x1290 -> 5 0x1230 37 0x1278 13 0x1238 left (0x1248) 0x1280 left (0x0) 0x1240 right (0x1260) 0x1288 right (0x0) 0x1248 25 0x1290 99 0x1250 left (0x1278) 0x1298 left (0x0) 0x1258 right (0x0) 0x12a0 right (0x0) 0x1260 52 0x1268 left (0x0) root 0x1230 0x1270 right (0x1290)
Retrieving ids • When you want the id corresponding to a tree, just look up the tree pointer in the table 0x1230 -> 1 0x1248 -> 2 0x1260 -> 3 0x1278 -> 4 0x1290 -> 5
The resulting representation 5 1 3 37 52 99 left (2) left (0) left (0) right (3) right (5) right (0) 2 4 25 13 left (4) left (0) right (0) right (0)
Option 2 – arbitrary big integer ids • What do we need from our ids? • Every tree has a different one • They are no bigger than 64 bits • They are integer-like • The pointer to the tree, if interpreted as an integer, has all of these properties
Retrieving ids (option 2) • When you want the id corresponding to a tree, just look at the pointer as a long • Given a tree pointer t • The id is just (long)t
The resulting representation 0x1290 0x1230 0x1260 37 52 99 left (0x1248) left (0) left (0) right (0x1260) right (0x1290) right (0) 0x1248 0x1278 25 13 left (0x1278) left (0) right (0) right (0)
The original tree 0x1230 37 0x1278 13 0x1238 left (0x1248) 0x1280 left (0x0) 0x1240 right (0x1260) 0x1288 right (0x0) 0x1248 25 0x1290 99 0x1250 left (0x1278) 0x1298 left (0x0) 0x1258 right (0x0) 0x12a0 right (0x0) 0x1260 52 0x1268 left (0x0) root 0x1230 0x1270 right (0x1290)