SPB Git

Repository Public

ResultPages.swift 5.6 KB 141 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : SP Search iOS9// File             : Core/Sources/SPSearchKit/ResultPages.swift10// Description      : Accumulation of result pages behind a cursor:11//                    deduplication, notices, end of list.12// Module           : SPSearchKit13// Repository       : https://git.spboucher.ai/sp-search-ios14// Project Website  : https://search.spboucher.ai15// Created          : 2026-12-0116// Last Modified    : 2026-12-0117//18// =============================================================================19// AUTHOR & PROFESSIONAL AFFILIATION20// =============================================================================21//22// Author           : Simon-Pierre Boucher23// Position         : Professor of Finance24// Institution      : Université du Québec en Outaouais (UQO)25// Department       : Département des sciences administratives26// Organization     : SP Boucher27//28// Website          : https://spboucher.ai29// Git              : https://git.spboucher.ai30//31// =============================================================================32// OFFICIAL CONTACT DIRECTORY33// =============================================================================34//35// General          : info@spboucher.ai36// Professional     : contact@spboucher.ai37// Direct           : simon@spboucher.ai38// Research         : research@spboucher.ai39// Development      : dev@spboucher.ai40// Support          : support@spboucher.ai41// Security/Admin   : admin@spboucher.ai42// Notifications    : notifications@spboucher.ai43//44// Preferred Contact: support@spboucher.ai45//46// =============================================================================47// COPYRIGHT & INTELLECTUAL PROPERTY48// =============================================================================49//50// Copyright (c) 202651// Simon-Pierre Boucher52//53// All rights reserved where applicable.54//55// This file is governed by the applicable project license,56// copyright notices, institutional agreements and relevant law.57//58// Unless expressly authorized by the applicable license or59// rights holder, copying, redistribution, sublicensing,60// publication and commercial exploitation are not permitted.61//62// Existing third-party rights and notices remain unaffected.63//64// =============================================================================65// CONFIDENTIALITY & SECURITY66// =============================================================================67//68// Classification   : Public69// Distribution     : Public repository; use is governed by the project license70// Security Contact : admin@spboucher.ai71//72// Access to this source does not independently grant permission73// to redistribute or disclose confidential materials.74//75// Do not embed credentials, private keys, access tokens,76// certificates or confidential personal information.77//78// =============================================================================79// LICENSE & MAINTENANCE80// =============================================================================81//82// License          : None specified (no license file in this repository)83// Maintainer       : Simon-Pierre Boucher84// Technical Contact: dev@spboucher.ai85//86// =============================================================================87// END OF SP BOUCHER SOURCE HEADER88// =============================================================================8990import Foundation9192/// The pages received so far for one search. One page is requested at a time, each with the cursor of the previous one.93public struct ResultPages: Sendable {94    public private(set) var results: [SearchResult] = []95    /// Header figures (total, coverage, facets, applied region) come from the first page.96    public private(set) var head: SearchResponse?97    public private(set) var nextCursor: String?98    public private(set) var notices: [SearchNotice] = []99    public private(set) var partial = false100    public private(set) var pageCount = 0101    private var ids = Set<String>()102103    public init() {}104    public init(first: SearchResponse) { reset(with: first) }105106    public var canLoadMore: Bool { nextCursor != nil && !notices.contains(.depthLimit) }107    public var isEmpty: Bool { results.isEmpty }108109    public mutating func reset(with first: SearchResponse) {110        results = []; ids = []; notices = []; partial = false; pageCount = 0111        head = first112        add(first)113    }114115    /// Adds a following page and returns how many new results it brought. A page answered with `cursor_expired`116    /// is the first page again: the list restarts from it.117    @discardableResult118    public mutating func append(_ page: SearchResponse) -> Int {119        if page.notices.contains(.cursorExpired) {120            reset(with: page)121            return results.count122        }123        return add(page)124    }125126    @discardableResult127    private mutating func add(_ page: SearchResponse) -> Int {128        var added = 0129        for r in page.results where ids.insert(r.id).inserted {130            results.append(r)131            added += 1132        }133        for n in page.notices where !notices.contains(n) { notices.append(n) }134        partial = partial || page.partial135        pageCount += 1136        // A page that brings nothing new ends the list: asking again would loop.137        nextCursor = (added == 0 && pageCount > 1) ? nil : page.nextCursor138        return added139    }140}141
Files main
Menu

Theme