How should I set up tag files for a multi-level directory hierarchy?

本文探讨了在多级目录结构中设置标签文件的不同方法,包括局部标签文件、全局标签文件及其组合使用,并针对不同编辑器特性进行了优劣分析。

How should I set up tag files for a multi-level directory hierarchy?

There are a few ways of approaching this:

 

  1. A local tag file in each directory containing only the tags for source files in that directory.
  2. One single big, global tag file present in the root directory of your hierarchy, containing all tags present in all source files in the hierarchy.
  3. A local tag file in each directory containing only the tags for source files in that directory, in addition to one single global tag file present in the root directory of your hierarchy, containing all non-static tags present in all source files in the hierarchy.
  4. A local tag file in each directory of the hierarchy, each one containing all tags present in source files in that directory and all non-static tags in every directory below it (note that this implies also having one big tag file in the root directory of the hierarchy).
Each of these approaches has its own set of advantages disadvantages, depending upon your particular conditions. Which approach is deemed best depends upon the following factors:

 

  1. The ability of your editor to use multiple tag files.

    If your editor cannot make use of multiple tag files (original vi implementations could not), then one large tag file is the only way to go if you ever desire to jump to tags located in other directories. If you never need to jump to tags in another directory (i.e. the source in each directory is entirely self-contained), then a local tag file in each directory will fit your needs.

     

  2. The time is takes for your editor to look up a tag in the tag file.

    The significance of this factor depends upon the size of your source tree and on whether the source files are located on a local or remote file system. For source and tag files located on a local file system, looking up a tag is not as big a hit as one might first imagine, since vi implementations typically perform a binary search on a sorted tag file. This may or may not be true for the editor you use. For files located on a remote file system, reading a large file is an expensive operation.

     

  3. Whether or not you expect the source code to change and the time it takes to rebuild a tag file to account for changes to the source code.

    While Exuberant Ctags is particularly fast in scanning source code (around 1-2 MB/sec), a large project may still result in objectionable delays if one wishes to keep their tag file(s) up to date on a frequent basis, or if the files are located on a remote file system.

     

  4. The presence of duplicate tags in the source code and the ability to handle them. The impact of this factor is influenced by the following three issues:

    1. How common are duplicate tags in your project?

    2. Does your editor provide any facilities for dealing with duplicate tags?

    While standard vi does not, many modern vi implementations, such as Vim have good facilities for selecting the desired match from the list of duplicates. If your editor does not support duplicate tags, then it will typically send you to only one of them, whether or not that is the one you wanted (and not even notifying you that there are other potential matches).

    3. What is the significance of duplicate tags?

    For example, if you have two tags of the same name from entirely isolated software components, jumping first to the match found in component B while working in component A may be entirely misleading, distracting or inconvenient (to keep having to choose which one if your editor provides you with a list of matches). However, if you have two tags of the same name for parallel builds (say two initialization routines for different hosts), you may always want to specify which one you want.

Of the approaches listed above, I tend to favor Approach 3. My editor of choice is Vim, which provides a rich set of features for handling multiple tag files, which partly influences my choice. If you are working with source files on a remote file system, then I would recommend either Approach 3 or Approach 4, depending upon the hit when reading the global tag file.

The advantages of Approach 3 are many (assuming that your editor has the ability to support both multiple tag files and duplicate tags). All lookups of tag located in the currect directory are fast and the local tag file can be quickly and easily regenerated in one second or less (I have even mapped a keystroke to do this easily). A lookup of a (necessarily non-static) tag found in another directory fails a lookup in the local tag file, but is found in the global tag file, which satisfies all cross-directory lookups. The global tag file can be automatically regenerated periodically with a cron job (and perhaps the local tag files also).

Now I give an example of how you would implement Approach 3. Means of implementing the other approaches can be performed in a similar manner.

Here is a visual representation of an example directory hierarchy:

project
  `-----misccomp
  |       `...
  `-----sysint
          `-----client
          |       `-----hdrs
          |       `-----lib
          |       `-----src
          |       `-----test
          `-----common
          |       `-----hdrs
          |       `-----lib
          |       `-----src
          |       `-----test
          `-----server
                  `-----hdrs
                  `-----lib
                  `-----src
                  `-----test
Here is a recommended solution (conceptually) to build the tag files:

 

  1. Within each of the leaf nodes (i.e. hdrs, lib, src, test) build a tag file using "ctags *.[ch]". This can be easily be done for the whole hierarchy by making a shell script, call it "dirtags", containing the following lines:
            #!/bin/sh
    	cd $1
    	ctags *
    
    Now execute the following command:
            find * -type d -exec dirtags {} /;
    
    These tag files are trivial (and extremely quick) to rebuild while making changes within a directory. The following Vim key mapping is quite useful to rebuild the tag file in the directory of the current source file:
            :nmap ,t :!(cd %:p:h;ctags *.[ch])&
    
  2. Build the global tag file:
            cd ~/project
            ctags --file-scope=no -R
    
    thus constructing a tag file containing only non-static tags for all source files in all descendent directories.
  3. Configure your editor to read the local tag file first, then consult the global tag file when not found in the local tag file. In Vim, this is done as follows: :set tags=./tags,tags,~/project/tags
If you wish to implement Approach 4, you would need to replace the "dirtags" script of step 1 with the following:

        #!/bin/sh
	cd $1
	ctags *
	# Now append the non-static tags from descendent directories
	find * -type d -prune -print | ctags -aR --file-scope=no -L-
And replace the configuration of step 3 with this:

        :set tags=./tags,./../tags,./../../tags,./../../../tags,tags
As a caveat, it should be noted that step 2 builds a global tag file whose file names will be relative to the directory in which the global tag file is being built. This takes advantage of the Vim 'tagrelative' option, which causes the path to be interpreted a relative to the location of the tag file instead of the current directory. For standard vi, which always interprets the paths as relative to the current directory, we need to build the global tag file with absolute path names. This can be accomplished by replacing step 2 with the following:

        cd ~/project
	ctags --file-scope=no -R `pwd`
基于可靠性评估序贯蒙特卡洛模拟法的配电网可靠性评估研究(Matlab代码实现)内容概要:本文围绕基于序贯蒙特卡洛模拟法的配电网可靠性评估展开研究,重点介绍了利用Matlab代码实现该方法的技术路径。文中详细阐述了序贯蒙特卡洛模拟的基本原理及其在配电网可靠性分析中的应用,包括系统状态抽样、时序模拟、故障判断与修复过程等核心环节。通过构建典型配电网模型,结合元件故障率、修复时间等参数进行大量仿真,获取系统可靠性指标如停电频率、停电持续时间等,进而评估不同运行条件或规划方案下的配电网可靠性水平。研究还可能涉及对含分布式电源、储能等新型元件的复杂配电网的适应性分析,展示了该方法在现代电力系统评估中的实用性与扩展性。; 适合人群:具备电力系统基础知识和Matlab编程能力的高校研究生、科研人员及从事电网规划与运行的技术工程师。; 使用场景及目标:①用于教学与科研中理解蒙特卡洛模拟在电力系统可靠性评估中的具体实现;②为实际配电网的可靠性优化设计、设备配置与运维策略制定提供仿真工具支持;③支撑学术论文复现与算法改进研究; 阅读建议:建议读者结合提供的Matlab代码逐段理解算法流程,重点关注状态转移逻辑与时间序列模拟的实现细节,并尝试在IEEE标准测试系统上进行验证与扩展实验,以深化对方法机理的理解。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值