创建自定义图形数据结构是否违反任何原则?
我正在研究一个与使用连续方法进行电路测试相关的小型科学课程项目。该程序将解析电路定义文件,然后构建一个表示电路的易于修改的图形结构。然后对该图形进行某些修改,并按拓扑排序。排序后,图被转换为由数组列表组成的静态结构,其中每个数组对应于一定的拓扑排序程度。之后,可以轻松地模拟电路,因为您可以依靠排序顺序并按顺序处理模型。
现在这一切都很好而且符合逻辑,但是我提到的两个图表是自定义数据结构,它们:
1)不完全按照 STL 规范构建(无论如何对我来说都会很长而且很困难 - 图表比向量和列表复杂得多)
2)对于第二张图,我假设它不可修改,并使用向量的向量或向量列表来提高速度。
3)可用于我的图表的操作集是有限的,反映了我的项目的需求。
4)代码简单。
现在我只是一名三年级的 IT 学生,在完成软件设计课程并阅读了一些现实生活中的代码后,我想知道:
1)代码可以(甚至可能)那么简单吗?
2)我对数据结构做出假设是否违反了软件设计的数千条原则?
3)我真的应该始终遵守所有数据的 STL 规范吗?我在这个项目和未来的项目中创建的结构?
这个项目使用 C++。
感谢您的关注!我希望得到这些问题的基本和理论答案,以及解决该问题的实际方法的示例。
I'm working on a small scientific course project related to circuit testing using continuous methods. The program would parse circuit definition files and then build an easily modifiable graph structure representing the circuit.Then certain modifications are done to that graph, and it is topologically sorted. After sorting, the graph is converted into a static structure that consists of a list of arrays, where every array corresponds to a certain topological sorting degree. After that, the circuit can be simulated easily as you can rely on the sorting order and process the model sequentially.
Now that is all nice and logical, but the two graphs I mentioned are custom data structures, which:
1)Are not built quite to the STL specifications (would be long and difficult for me anyway - graphs are way more complex than vectors and lists)
2)For the second graph, I assume it's not modifiable and use a vector of vectors or a list of vectors for speed.
3)The set of operations available for my graphs is limited and reflects the needs of my project.
4)The code is simple.
Now I'm just a 3rd year IT student, and after having done a course in Software Design and after reading some real life code, I wonder:
1)Can (or even may) the code be as simple?
2)Don't I violate any of the thousands of principles of Software Design with making assumptions about the data structures?
3)Should I really always conform to STL specifications for all the data structures I create in this and future projects?
This project uses C++.
Thank you for your attention! I'd appreciate a fundamental and theoretical answer to these questions, as well as an example of practical approach to this problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
1)代码可以(甚至可以)如此简单吗?
不仅可以,而且应该如此。代码不需要太复杂。
2)我对数据结构做出假设是否违反了软件设计的数千条原则中的任何一条?
嗯,否则你还能怎么做。这个问题对我来说没有意义。
3) 我真的应该始终遵守我在本项目和未来项目中创建的所有数据结构的 STL 规范吗?
不。例如,如果您不需要迭代结构,则不要提供迭代器。仅实施您实际需要的内容。
1)Can (or even may) the code be as simple?
Not only can it be, it should be. There is no need for code to be complicated.
2)Don't I violate any of the thousands of principles of Software Design with making assumptions about the data structures?
Um, how can you do otherwise. This question doesn't make sense to me.
3)Should I really always conform to STL specifications for all the data structures I create in this and future projects?
Nope. For example, if you don't need to iterate a structure, don't provide iterators. Only implement what you actually need.
我认为这样的简单设计没有任何问题:
它
保持简单、模块化,并允许您在其上使用标准库。它允许您迭代节点和边。如果您想要顶点的邻接列表,则必须遍历整个边列表,但这是设计使然(如果您想快速完成此操作,则需要更好的结构)。
例如,定义(不幸的是,这不是标准的)
,现在您可以
迭代
some_node
的邻接列表。使用 C++0x,有更优雅的方法来编码此类算法(使用 lambda)。
I would see nothing wrong with a simple design like this:
where
It stays simple, modular, and allows you to use the standard library on it. It allows you to iterate on nodes and edges. If you want the adjacency list of a vertex, you will have to loop over the whole edge list, but this is by design (you want a better structure if you want to do this fast).
For instance, define (this one is unfortunately not standard)
and now you can do
to iterate over the adjacency list of
some_node
.With C++0x there are more elegant ways to code such algorithms (using lambdas).