import '../../../assert/js/assert.js';
import '../../../globals.js';
import { alpenglow } from '../imports.js';
export default class ParallelWorkgroupArray {
    constructor(data, indeterminateValue) {
        this.data = data;
        this.indeterminateValue = indeterminateValue;
        this.writeLocalIDs = data.map(() => []);
    }
    async get(context, index) {
        let value;
        if (!isFinite(index) || index < 0 || index >= this.data.length) {
            value = this.indeterminateValue;
        }
        else {
            // Ensure that we're the only ones who have written to this
            assert && assert(this.writeLocalIDs[index].every(id => id.equals(context.localId)));
            value = this.data[index];
        }
        await context.afterGet();
        return value;
    }
    async set(context, index, value) {
        if (isFinite(index) && index >= 0 && index < this.data.length) {
            // Ensure that we're the only ones who have written to this
            assert && assert(this.writeLocalIDs[index].every(id => id.equals(context.localId)));
            this.writeLocalIDs[index].push(context.localId);
            this.data[index] = value;
        }
        await context.afterSet();
    }
    synchronize(workgroupId) {
        // TODO: assert that we're in the same workgroup?
        // Clear IDs that were written
        this.writeLocalIDs.forEach(ids => {
            ids.length = 0;
        });
    }
}
alpenglow.register('ParallelWorkgroupArray', ParallelWorkgroupArray);
//# sourceMappingURL=ParallelWorkgroupArray.js.map