1 / 14

Writing trees

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.

Download Presentation

Writing trees

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Writing trees Tutorial #4 CPSC 261

  2. 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

  3. 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)

  4. 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)

  5. 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)

  6. 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?

  7. 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

  8. 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)

  9. 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

  10. 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)

  11. 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

  12. 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

  13. 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)

  14. 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)

More Related