c# - Optimal data structure for lookup within nested scopes -


say, there's tree data structure, each leaf of defines set of keys lookup:

* |- = 1, b = 2 |- *    |- c = 4    |- *       |- d = 5       |- d = 6, e = 7 

i need way of finding value of key given leaf while tree being traversed depth-first.

there 2 approaches have thought of:

  1. if value not found in current leaf, check dictionary of parent , on root of tree.

  2. there global dictionary , each leaf inserts/removes keys when being traversed. lookup in performed in global dictionary.

it's there many leafs few keys in each, , 3-4 lookups each key.

which approach more efficient? or, maybe, there's way of doing better both?

the programming language implementing sure define exact rules name resolution. don't think lead depth-first search. name resolution rules, fruequently, somethink this:

  1. search current scope, considering has been declared “up” current position in source code;
  2. when specific rules met, e.g. there form of using / import or whatever other construct linking other scope, perform search in other scope (all of such scopes, sequentially), , recurse within it:
    1. search given scope,
    2. recurse relevant nested scopes;
  3. go immediatelly enclosing scope;
  4. repeat (1), 'current' scope determined in (3).

in other words, gradually go in tree of enclosing scopes, , decide whether search 'foreign' referenced scopes. statements such using / import lead references among scopes, in turn cause viewed tree of scopes in fact directed graph.

regarding lookup table construction, i'd start simple hashtable. prefix trees (tries) work these scenarios, too.

last not least, wouldn't care of lookup performance unless i'd face real performance problem when compiling tens or maybe hundreds of thousands of lines of code.


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -