简体   繁体   中英

Save/restore pointers in member variables

I have a data structure like (this is a gross simplification, it's not something that can be resolved by putting elements in a linked list collection):

null <-- [1] <==> [2] <==> [3] <==> [4] <==> [5] --> null

I need to pass, in a web worker postMessage() the sublist:

null <-- [2] <==> [3] <==> [4] --> null

So the easy way to do this is:

  1. Save 2.prev, 4.next
  2. set 2.prev & 4.next to null
  3. postMessage() the truncated list.
  4. set 2.prev & 4.next to their previous values.

I can do this with two lists, listPrev and listNext . Set the key as the variables 2 & 4 and the values are the pointer values. However, there are actually 6 different prev/next type member variables across different object types. So...

If this was C++ then I could store the address of the prev and next pointers as the key and have a single list of pointer addresses and values to set them to. Is there anything similar to this in javascript?

假设您已将其抽象为类似LinkedList的内容,那么我将使用诸如LinkedSublist之类的东西对其进行修饰,该LinkedSublist本身将存储第一个和最后一个节点的引用,因此您不会破坏性地更改原始列表。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM