Ada - 动态重新分配阵列

thi*_*him 2 arrays memory-management ada dynamic

我正在尝试编写一个基于数组的堆栈,可以动态地重新分配.我遇到的主要问题是如何实现调整数组大小的过程.在C++中它可能看起来像这样:

template<class T, int incr>
void Vector<T, incr>::inflate(int increase) {
    const int tsz = sizeof(T*);
    T** st = new T*[quantity + increase];
    memset(st, 0, (quantity + increase) * tsz);
    memcpy(st, storage, quantity * tsz);
    quantity += increase;
    delete []storage;
    storage = st;
}
Run Code Online (Sandbox Code Playgroud)

在哪里int quantity;T** storage;在私人部分宣布.

如果有人可以与我分享一些样品,我将非常感激.我试图通过Ada.Containers.Vectors的实现来看看,但是......它是big = P.

到目前为止,我已经做了这个Vector.ads任何人都可以帮忙吗?

thi*_*him 6

好的,案件解决了.我已经完成了我的Vector类(它实际上是一个数组的堆栈构建).感谢大家的帮助.

后代就是我的代码.希望有人能从中学到一些东西.代码 - > https://gist.github.com/496a50bc7f5cd93f8d91

如果您想看看并找到值得改变的东西,请发表评论.; d


tra*_*god 5

如果你一起去Ada.Containers.Vectors,在Ada 2005的基本原理中有一个有用的讨论:§8.2列表和向量.基本上,您使用Index_Type和实例化通用包Element_Type:

package Container is new Containers.Vectors (Natural, T);
Run Code Online (Sandbox Code Playgroud)

然后声明一个具有新类型的变量:

Stack : Container.Vector;
Run Code Online (Sandbox Code Playgroud)

然后Push程序变为Stack.Append,Pop函数返回Stack.Last_Element.请注意前缀表示法的可用性.