Implement Basic Shared Ownership
Problem statement
Implement a small shared-owner class for a mutable resource. An owner holds either no resource or one shared control block. Copies share the same resource. Releasing the final owner must dispose of that resource exactly once. Do not use a library shared-pointer implementation.
The function runs a single-threaded trace over slotCount owner slots, initially empty. Each operation is [kind,a,b]:
0: replace slot a with a newly created resource whose value is b.1: copy slot b into slot a, releasing a's previous resource first. Copying a slot to itself does nothing. Copying an empty slot makes a empty.2: write b into a's resource; an empty slot ignores the write.3: read a's value, or return -2147483648 if a is empty.4: close a, making it empty. Closing an empty slot does nothing.5: return the number of nonempty owner slots sharing a's resource, or 0 if a is empty.6: return how many resources have been disposed of so far.
Return one integer per operation. Kinds 0, 1, 2 and 4 return 0. All live slots are closed after the trace; that final cleanup is not included in an earlier kind-6 result. Implement actual resource, owner and control-block objects. In C++, implement copying, assignment and a destructor that releases ownership through RAII. In garbage-collected languages, explicit close/dispose releases the logical resource and its owned references; this exercise does not claim when the language physically reclaims heap memory. Thread-safe reference counting, weak pointers, moves and custom deleters are outside this exercise.
Function
runSharedOwners(slotCount: int, operations: int[][]) → int[]Examples
Example 1
slotCount = 3operations = [[0,0,7],[1,1,0],[2,1,9],[3,0,0],[5,1,0],[4,0,0],[6,0,0],[4,1,0],[6,0,0]]return = [0,0,0,9,2,0,0,0,1]Slots 0 and 1 share one resource. Writing 9 through slot 1 changes the value read through slot 0. The owner count is 2. Closing slot 0 disposes of nothing; closing slot 1 disposes of the resource once.
Example 2
slotCount = 2operations = [[0,0,4],[1,0,0],[5,0,0],[1,0,1],[6,0,0],[3,0,0],[4,0,0],[6,0,0]]return = [0,0,1,0,1,-2147483648,0,1]Self-copy keeps one owner. Copying empty slot 1 over slot 0 releases the only resource, so the disposed count becomes 1 and reading slot 0 returns the empty sentinel. Closing it again does not dispose of anything.
Constraints
1 <= slotCount <= 32.0 <= operations.length <= 400.- Each operation has exactly three integers
[kind,a,b]. -1000000 <= operations[i][j] <= 1000000.- The kind is in 0..6. For kinds 0..5, a is a slot index in 0..slotCount-1; for kind 1, b is also a slot index. Other b values are payloads for kinds 0 and 2, and ignored for kinds 3..6. Both a and b are ignored for kind 6.