diff --git a/assets/figure-svg/css/9f909651eeceaf5c7dda140d.css.gz b/assets/figure-svg/css/9f909651eeceaf5c7dda140d.css.gz deleted file mode 100644 index 552914137..000000000 Binary files a/assets/figure-svg/css/9f909651eeceaf5c7dda140d.css.gz and /dev/null differ diff --git a/assets/figure-svg/css/9f909651eeceaf5c7dda140d.css b/assets/figure-svg/css/f41655ef506ff5ce3cf0fd42.css similarity index 62% rename from assets/figure-svg/css/9f909651eeceaf5c7dda140d.css rename to assets/figure-svg/css/f41655ef506ff5ce3cf0fd42.css index e3804a76e..9fe9c4627 100644 --- a/assets/figure-svg/css/9f909651eeceaf5c7dda140d.css +++ b/assets/figure-svg/css/f41655ef506ff5ce3cf0fd42.css @@ -5,6 +5,14 @@ body[data-md-color-scheme="slate"] ha-svg-figure > object[data-theme="light"]:ha body[data-md-color-scheme="slate"] ha-svg-figure > object[data-theme="dark"]{display:block} @media print{ha-svg-figure{break-inside:avoid}} [data-figure-sequence][aria-busy="true"] .animation-controls{opacity:.55} +.ha-sr-only{position:absolute!important;width:1px!important;height:1px!important;padding:0!important;margin:-1px!important;overflow:hidden!important;clip-path:inset(50%)!important;border:0!important} +/* Keep clipped text on whole lines; compact text extents also avoid Orca + combining the last hidden paragraph with the following visible line. */ +.figure-description,.figure-description *{white-space:nowrap!important;font-size:1px!important;line-height:1px!important} +.figure-description *{margin:0!important;padding:0!important;border:0!important;min-width:0!important} +/* Site table styles must not turn clipped tables into keyboard-focusable scrollers. */ +.figure-description table{display:table!important;max-width:none!important;overflow:visible!important;border-collapse:collapse!important;border-spacing:0!important} +ha-svg-figure:focus-visible{outline:2px solid var(--md-accent-fg-color,currentColor);outline-offset:2px} body[data-md-color-scheme="slate"]{--ha-figure-background:#171717} body[data-md-color-scheme="slate"] ha-svg-figure{background:#171717;color-scheme:dark} diff --git a/assets/figure-svg/css/f41655ef506ff5ce3cf0fd42.css.gz b/assets/figure-svg/css/f41655ef506ff5ce3cf0fd42.css.gz new file mode 100644 index 000000000..cfd655c66 Binary files /dev/null and b/assets/figure-svg/css/f41655ef506ff5ce3cf0fd42.css.gz differ diff --git a/assets/figure-svg/js/99a707e9f0a14d8280f16808.js.gz b/assets/figure-svg/js/99a707e9f0a14d8280f16808.js.gz deleted file mode 100644 index aa433c7a8..000000000 Binary files a/assets/figure-svg/js/99a707e9f0a14d8280f16808.js.gz and /dev/null differ diff --git a/assets/figure-svg/js/99a707e9f0a14d8280f16808.js b/assets/figure-svg/js/a704d8e80a80ddc71f2a03a7.js similarity index 98% rename from assets/figure-svg/js/99a707e9f0a14d8280f16808.js rename to assets/figure-svg/js/a704d8e80a80ddc71f2a03a7.js index 134de8b2f..6516ddaad 100644 --- a/assets/figure-svg/js/99a707e9f0a14d8280f16808.js +++ b/assets/figure-svg/js/a704d8e80a80ddc71f2a03a7.js @@ -2,7 +2,7 @@ (() => { 'use strict'; const NS = 'http://www.w3.org/2000/svg'; - function mount(svg, model) { + function mount(svg, model, options = {}) { if (svg.reading) return svg.reading; const doc = svg.ownerDocument, win = doc.defaultView, config = model.config.motion; const sources = new Map([...svg.querySelectorAll('[data-source]')].map(n => [n.dataset.source, n])); @@ -11,7 +11,7 @@ const originalAttrs = new Map(['tabindex', 'data-reading-root'].map(k => [k, svg.getAttribute(k)])); const scope = 'ha-flow-' + Array.from(win.crypto.getRandomValues(new Uint32Array(4)), n => n.toString(16)).join(''); svg.setAttribute('data-reading-root', scope); - if (model.arrows.length) svg.setAttribute('tabindex', '0'); + if (model.arrows.length && options.focusable !== false) svg.setAttribute('tabindex', '0'); function node(tag, attrs, parent) { const n = doc.createElementNS(NS, tag); for (const [k, v] of Object.entries(attrs)) n.setAttribute(k, v); diff --git a/assets/figure-svg/js/a704d8e80a80ddc71f2a03a7.js.gz b/assets/figure-svg/js/a704d8e80a80ddc71f2a03a7.js.gz new file mode 100644 index 000000000..a4102daea Binary files /dev/null and b/assets/figure-svg/js/a704d8e80a80ddc71f2a03a7.js.gz differ diff --git a/assets/figure-svg/js/b789dc8b01e43e756fc89964.js.gz b/assets/figure-svg/js/b789dc8b01e43e756fc89964.js.gz deleted file mode 100644 index a08f883da..000000000 Binary files a/assets/figure-svg/js/b789dc8b01e43e756fc89964.js.gz and /dev/null differ diff --git a/assets/figure-svg/js/b789dc8b01e43e756fc89964.js b/assets/figure-svg/js/bff17b8259339758cb1c4a06.js similarity index 80% rename from assets/figure-svg/js/b789dc8b01e43e756fc89964.js rename to assets/figure-svg/js/bff17b8259339758cb1c4a06.js index 384d3c3ef..f3e5f0de6 100644 --- a/assets/figure-svg/js/b789dc8b01e43e756fc89964.js +++ b/assets/figure-svg/js/bff17b8259339758cb1c4a06.js @@ -4,6 +4,7 @@ if (customElements.get('ha-svg-figure')) return; const {resource, decode, fonts, theme} = FigureWeb; const sequences = new WeakMap(); + const motionStates = new WeakMap(); function sequence(group) { if (sequences.has(group)) return sequences.get(group); let pending = null, pendingTheme, intent = null; @@ -25,9 +26,10 @@ return pending; }; group.addEventListener('click', event => { - const target = event.target.closest('.animation-controls button, .tabbed-labels label'); + const target = event.target.closest('.animation-controls button, .tabbed-labels label, input[type="radio"]'); if (!target || target.closest('.tabbed-set') !== group) return; if (group.dataset.figureState === 'ready') return; + group.dispatchEvent(new Event('figureinteractionpending')); event.preventDefault(); event.stopImmediatePropagation(); intent = target; prepare(); }, true); @@ -79,7 +81,14 @@ const staging = stage.attachShadow({mode:'open'}); staging.append(style, svg); document.body.append(stage); try { - if (model) HelloAlgoReading.mount(svg, model); + const accessible = host.hasAttribute('data-figure-accessible'); + if (model) HelloAlgoReading.mount(svg, model, {focusable: !accessible}); + if (accessible) { + svg.setAttribute('aria-hidden', 'true'); + svg.removeAttribute('tabindex'); + for (const node of svg.querySelectorAll('[tabindex],a')) node.setAttribute('tabindex', '-1'); + if (model?.arrows?.length) host.motionControl(svg); + } if (svg.reading?.state().issues.length) throw Error(svg.reading.state().issues.join('; ')); const shadow = host.shadowRoot || host.attachShadow({mode:'open'}); shadow.replaceChildren(style, svg); @@ -94,7 +103,10 @@ const group = this.closest('[data-figure-sequence]'); this.unsubscribe = FigureWeb.subscribe(() => { if (!this.active) return; - if (group) sequence(group)(); + if (group) { + group.dispatchEvent(new Event('figureinteractionpending')); + sequence(group)(); + } else this.prepare(); }); if (group) { @@ -113,6 +125,29 @@ this.showStatic(); this.observer?.disconnect(); this.unsubscribe?.(); + this.motionState?.hosts.delete(this); + } + motionControl(svg) { + const owner = this.closest('[data-figure-sequence]') || this; + if (!motionStates.has(owner)) motionStates.set(owner, {paused:false, hosts:new Set()}); + const state = this.motionState = motionStates.get(owner); state.hosts.add(this); + this.tabIndex = 0; + this.setAttribute('aria-keyshortcuts', 'Space'); + this.setAttribute('aria-description', document.documentElement.lang.toLowerCase() === 'zh-hant' + ? '空白鍵暫停或繼續箭頭動效。' : '空格键暂停或继续箭头动效。'); + if (!this.motionKeyHandler) { + this.motionKeyHandler = event => { + if (event.target !== this || event.code !== 'Space' || event.ctrlKey || event.metaKey || event.altKey) return; + event.preventDefault(); + const shared = this.motionState; + shared.paused = !shared.paused; + for (const host of shared.hosts) { + host.shadowRoot?.querySelector('svg')?.reading?.configure({motion: !shared.paused}); + } + }; + this.addEventListener('keydown', this.motionKeyHandler); + } + svg.reading.configure({motion: !state.paused}); } prepare() { this.active = true; diff --git a/assets/figure-svg/js/bff17b8259339758cb1c4a06.js.gz b/assets/figure-svg/js/bff17b8259339758cb1c4a06.js.gz new file mode 100644 index 000000000..92334c7d5 Binary files /dev/null and b/assets/figure-svg/js/bff17b8259339758cb1c4a06.js.gz differ diff --git a/chapter_array_and_linkedlist/array/index.html b/chapter_array_and_linkedlist/array/index.html index dda504f9c..68bb36058 100644 --- a/chapter_array_and_linkedlist/array/index.html +++ b/chapter_array_and_linkedlist/array/index.html @@ -47,7 +47,7 @@ set(key, value, storage, key === "__palette" ? shared : scope); })(); - +
@@ -2093,8 +2093,8 @@数组(array)是一种线性数据结构,其将相同类型的元素存储在连续的内存空间中。我们将元素在数组中的位置称为该元素的索引(index)。图 4-1 展示了数组的主要概念和存储方式。
-图 4-1 数组定义与存储方式
+图 4-1 数组定义与存储方式
数组通过从 0 开始的索引定位元素,所有元素在内存中连续存放且占用相同大小的空间。
左侧列出数组、索引和内存地址;右侧网格的第二行有 5 个连续绿色单元格,表示划分给数组的空间,其余浅灰格表示可用空间。
| 索引 | 元素值 | 起始内存地址 |
|---|---|---|
| 0 | 1 | 00 |
| 1 | 3 | 04 |
| 2 | 2 | 08 |
| 3 | 5 | 12 |
| 4 | 4 | 16 |
本例每个元素占 4 字节,所以相邻元素的起始地址固定相差 4。地址中的前导 0 仅用于对齐位数,不表示八进制。
索引表示元素在数组中的位置序号,元素值是该位置存放的数据,内存地址则是存储单元的起始编号,三者概念不同。
我们可以根据需求选用数组的两种初始化方式:无初始值、给定初始值。在未指定初始值的情况下,大多数编程语言会将数组元素初始化为 \(0\) :
@@ -2199,8 +2199,8 @@数组元素被存储在连续的内存空间中,这意味着计算数组元素的内存地址非常容易。给定数组内存地址(首元素内存地址)和某个元素的索引,我们可以使用图 4-2 所示的公式计算得到该元素的内存地址,从而直接访问该元素。
-图 4-2 数组元素的内存地址计算
+图 4-2 数组元素的内存地址计算
利用首地址、固定元素长度和索引,直接算出元素位置。
图上方排列数组、索引和内存地址,突出索引 3 这一列;下方给出地址公式和代入示例。每个元素占 4 字节,上方地址行用两位写法(如 00、12),下方算式用三位写法(000、012);前导零仅用于对齐,均按十进制解释。
| 索引 | 元素值 | 内存地址 |
|---|---|---|
| 0 | 1 | 00 |
| 1 | 3 | 04 |
| 2 | 2 | 08 |
| 3 | 5 | 12 |
| 4 | 4 | 16 |
元素地址 = 首元素地址 + 元素长度 × 索引。索引从 0 起算,所以索引 3 是第 4 个元素,值为 5;相对首元素跨过 3 个元素,字节偏移为 4 × 3 = 12,地址为 000 + 4 × 3 = 012。这里按索引定位,并不是搜索值为 3 的元素。
连续存储且元素等长时,用一次乘法和加法即可求出地址,支持 O(1) 随机访问。
观察图 4-2 ,我们发现数组首个元素的索引为 \(0\) ,这似乎有些反直觉,因为从 \(1\) 开始计数会更自然。但从地址计算公式的角度看,索引本质上是内存地址的偏移量。首个元素的地址偏移量是 \(0\) ,因此它的索引为 \(0\) 是合理的。
在数组中访问元素非常高效,我们可以在 \(O(1)\) 时间内随机访问数组中的任意一个元素。
数组元素在内存中是“紧挨着的”,它们之间没有空间再存放任何数据。如图 4-3 所示,如果想在数组中间插入一个元素,则需要将该元素之后的所有元素都向后移动一位,之后再把元素赋值给该索引。
-图 4-3 数组插入元素示例
+图 4-3 数组插入元素示例
在索引 1 处插入元素 3:先从末端开始将原有元素依次向右复制一位,再写入新元素。
数组 nums 共有 5 个槽位,索引为 0 至 4。初始存储为 [1, 2, 5, 4, 0],其中只有前 4 个元素 [1, 2, 5, 4] 有效,末尾索引 4 的 0 是无意义的占位值。目标是在索引 1 处插入 3,并保持原有有效元素的相对顺序。
| 操作 | 索引0 | 索引1 | 索引2 | 索引3 | 索引4 |
|---|---|---|---|---|---|
| 初始状态 | 1 | 2 | 5 | 4 | 0 |
| nums[4] = nums[3] | 1 | 2 | 5 | 4 | 4 |
| nums[3] = nums[2] | 1 | 2 | 5 | 5 | 4 |
| nums[2] = nums[1] | 1 | 2 | 2 | 5 | 4 |
| nums[1] = 3 | 1 | 3 | 2 | 5 | 4 |
从末端向插入点倒序复制,保证每个即将被覆盖、仍需保留的元素已复制到右侧相邻格。复制不会清空源槽位,因此中间状态会出现重复值。在数组中间插入,平均需要移动 O(n) 个元素。
插入后 5 个槽位全部有效;调用方据此将有效长度更新为 5,示例 insert 本身只执行槽位赋值。后文所说的定长数组尾部元素“丢失”,在本例中是末槽原有的占位值 0 被覆盖,四个原有有效元素均未丢失。若原本 5 个槽位全部有效,这种覆盖就会丢失末尾有效元素,需要先扩容或拒绝插入。
值得注意的是,由于数组的长度是固定的,因此插入一个元素必定会导致数组尾部元素“丢失”。我们将这个问题的解决方案留在“列表”章节中讨论。
同理,如图 4-4 所示,若想删除索引 \(i\) 处的元素,则需要把索引 \(i\) 之后的元素都向前移动一位。
-图 4-4 数组删除元素示例
+图 4-4 数组删除元素示例
覆盖待删除位置后保持其余元素顺序,并区分有效长度与存储槽位数。
数组 nums 有 5 个槽位,初始为 [1, 3, 2, 5, 4],有效长度为 5,索引从 0 起。目标是删除索引 1 的 3,并保持其余元素的次序。
| 状态或赋值 | 存储内容 | 有效长度或含义 |
|---|---|---|
| 初始 | [1,3,2,5,4] | 5,索引1的3待删除 |
| 第 1 次复制:nums[1]=nums[2] | [1,2,2,5,4] | 尚未完成,记录长度5 |
| 第 2 次复制:nums[2]=nums[3] | [1,2,5,5,4] | 尚未完成,记录长度5 |
| 第 3 次复制:nums[3]=nums[4];调用方将有效长度改为 4 | [1,2,5,4,4] | 4;仅前4项有效,末项4为残留 |
从删除位置向右依次读取相邻元素、向左覆盖,确保每个源值都在被覆盖前读出。复制只改写目标槽位,不清除源槽位,所以过程中会出现相邻重复值。
三次复制后,由调用方把有效长度从 5 改为 4,仅索引 0 至 3 的 [1, 2, 5, 4] 有效,末槽的 4 是无效残留,不必清零。示例 remove 只覆盖赋值,不会自动缩短 Python 列表;这类中间删除的平均时间复杂度为 O(n)。
请注意,删除元素完成后,原先末尾的元素变得“无意义”了,所以我们无须特意去修改它。