数据结构与算法篇--结构不变式--动态数组

动态数据列表类的不变式

重点:形式化定义转换成 checkInvariants 代码

DArrayList

DArrayListTest

形式化定义

txt 复制代码
Invariant:
  (1) data != null
  (2) capacity > 0
  (3) 0 <= size <= capacity
  (4) ∀ i ∈ [0, size): data[i] != null
  (5) ∀ i ∈ [size, capacity): data[i] == null
  (6) capacity 仅通过 resize() 成倍增长

checkInvariants源码

java 复制代码
public void checkInvariants() {
    // --- 1. 容量与大小约束检查 ---

    // C1: 容量必须大于 0
    if (capacity <= 0) {
        throw new AssertionError("Invariant Broken: Capacity must be positive (capacity=" + capacity + ")");
    }

    // C2: Size 必须在 [0, capacity] 范围内
    if (size < 0 || size > capacity) {
        throw new AssertionError("Invariant Broken: Size must be between 0 and capacity (size=" + size + ", capacity=" + capacity + ")");
    }

    // --- 2. 数组和数据完整性检查 ---

    // D1: 底层数组必须存在
    if (data == null) {
        throw new AssertionError("Invariant Broken: Internal array 'data' must not be null.");
    }

    // D2: 数组的实际长度必须等于记录的 capacity
    if (data.length != capacity) {
        throw new AssertionError("Invariant Broken: data.length (" + data.length + ") does not match recorded capacity (" + capacity + ")");
    }

    // --- 3. 数据填充和空值约束检查 ---

    // 验证有效数据范围 [0, size-1]
    for (int i = 0; i < size; i++) {
        // D3: 有效数据区域 [0, size-1] 不得包含 null
        if (data[i] == null) {
            // 根据您的 add 方法,data[i] 应该是非空的
            throw new AssertionError("Invariant Broken: Element at index " + i + " must not be null (size=" + size + ")");
        }
    }

    // 验证闲置空间范围 [size, capacity-1]
    for (int j = size; j < capacity; j++) {
        // D4: 闲置空间 [size, capacity-1] 必须为 null (防止内存泄漏)
        if (data[j] != null) {
            throw new AssertionError("Invariant Broken: Element at index " + j + " must be null to prevent memory leak (size=" + size + ", capacity=" + capacity + ")");
        }
    }

    InternalIterator it = new InternalIterator();
    int elementsFound = 0;
    while(it.hasNext()) {
        try {
            it.next();
            elementsFound++;
        } catch (NoSuchElementException e) {
            throw new AssertionError("Iterator failed unexpectedly near index " + elementsFound);
        }
    }
    if (elementsFound != size) {
        throw new AssertionError("Iterator found " + elementsFound + " elements, but size is " + size);
    }
}

使用指南

  1. 整合代码: 将 checkInvariants() 方法添加到您的 DArrayList 类定义中。
  2. 插入调用: 在每个修改数组状态的方法(DArrayList 的 add 和 remove 以及私有方法 resize)的末尾调用 this.checkInvariants();。

通过这种方式,每次修改数组后,程序都会进行一次快速的自我验证,确保您的动态扩容和元素移动逻辑没有破坏任何核心不变式。

相关推荐
天威?*7 小时前
团体程序设计天梯练习集L3-001 凑零钱
数据结构·c++
龙砚序8 小时前
【数据结构|栈实战 03-2】不带头结点链栈实现:文本编辑器撤销 Undo 模块(C 语言)
数据结构
hai3152475438 小时前
阶乘分层关系模型存储文本与表格
数据结构·算法·rust·排序算法·线性回归
凌冰_9 小时前
数据结构 - 数组(八)
数据结构·算法·矩阵
Shaoshing10 小时前
LeetCode 1798. 你能构造出连续值的最大数目
java·数据结构·算法
YSL07012410 小时前
C++string类详解
开发语言·数据结构·c++
大今野10 小时前
江苏专转本计算机大类基础理论考点:算法的概念与复杂度分析
数据结构·算法·专转本
郝学胜-神的一滴10 小时前
量化交易从入门到精通 04:量化生意的四大特点
开发语言·数据结构·c++·python·算法·pycharm
会周易的程序员12 小时前
ASX 标准库 API 文档增加标准容器list, queue,map , unordered_map
数据结构·c++·物联网·list·虚拟机·字节码·anglescript
龙砚序13 小时前
【数据结构|线性表实战 01】基于顺序表的地铁站点管理:查找、增删、有序二路归并(C 语言实现)
c语言·数据结构·算法