ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

Simple Object Pool Manager

Lua

Goal -- WPM

Ready
Exercise Algorithm Area
1local ObjectPool = {}
2
3function ObjectPool:new(object_factory, initial_size)
4local self = {
5pool = {},
6object_factory = object_factory, -- Function to create new objects
7active_objects = 0
8}
9setmetatable(self, {__index = ObjectPool})
10
11-- Pre-populate the pool
12for i = 1, initial_size do
13local obj = self.object_factory()
14table.insert(self.pool, obj)
15end
16
17return self
18end
19
20function ObjectPool:acquire()
21local obj
22if #self.pool > 0 then
23-- Take an object from the pool
24obj = table.remove(self.pool, 1)
25else
26-- Pool is empty, create a new object
27obj = self.object_factory()
28end
29self.active_objects = self.active_objects + 1
30return obj
31end
32
33function ObjectPool:release(obj)
34if not obj then
35print("Warning: Attempted to release a nil object.")
36return
37end
38
39-- Optional: Reset object state here before returning to pool
40-- For example: obj.reset_state()
41
42table.insert(self.pool, obj)
43self.active_objects = self.active_objects - 1
44if self.active_objects < 0 then
45print("Warning: Released more objects than acquired.")
46self.active_objects = 0 -- Prevent negative count
47end
48end
49
50function ObjectPool:get_pool_size()
51return #self.pool
52end
53
54function ObjectPool:get_active_objects()
55return self.active_objects
56end
57
58-- Example Usage:
59-- local function create_particle()
60-- return { x = 0, y = 0, life = 0, active = false }
61-- end
62--
63-- local particle_pool = ObjectPool:new(create_particle, 10)
64--
65-- -- Acquire an object
66-- local p1 = particle_pool:acquire()
67-- p1.x = 10
68-- print("Acquired p1. Pool size: " .. particle_pool:get_pool_size() .. ", Active: " .. particle_pool:get_active_objects())
69--
70-- -- Acquire another
71-- local p2 = particle_pool:acquire()
72-- print("Acquired p2. Pool size: " .. particle_pool:get_pool_size() .. ", Active: " .. particle_pool:get_active_objects())
73--
74-- -- Release an object
75-- particle_pool:release(p1)
76-- print("Released p1. Pool size: " .. particle_pool:get_pool_size() .. ", Active: " .. particle_pool:get_active_objects())
77--
78-- -- Acquire again, should reuse p1
79-- local p3 = particle_pool:acquire()
80-- print("Acquired p3. Pool size: " .. particle_pool:get_pool_size() .. ", Active: " .. particle_pool:get_active_objects())
81--
82-- -- Release nil object (edge case)
83-- particle_pool:release(nil)
Algorithm description viewbox

Simple Object Pool Manager

Algorithm description:

This Lua code implements a simple object pool. An object pool is a collection of pre-initialized objects that can be reused to avoid the overhead of creating and destroying objects frequently. The `acquire` method retrieves an object from the pool, creating a new one if the pool is empty. The `release` method returns an object to the pool for future use. This pattern is common in game development for managing entities like bullets or particles, and in high-performance systems.

Algorithm explanation:

The `ObjectPool` uses a Lua table (`self.pool`) to store available objects. The `object_factory` function is responsible for creating new instances of the desired object type. When `acquire` is called, it first checks if `self.pool` has any objects. If so, it removes and returns the first one (`table.remove(self.pool, 1)`), simulating a FIFO behavior. If the pool is empty, it calls `self.object_factory()` to create a new object. `release` adds the object back to the pool using `table.insert` and decrements the `active_objects` count. Edge cases include releasing `nil` objects and ensuring `active_objects` doesn't go below zero. The time complexity for `acquire` and `release` is O(1) on average, assuming `table.remove` and `table.insert` at the ends of a table are amortized O(1). Space complexity is O(N), where N is the maximum number of objects in the pool.

Pseudocode:

Initialize pool with a factory function and initial size.
Pre-populate pool by calling factory function `initial_size` times.
Acquire method:
  If pool is not empty:
    Remove and return the first object from the pool.
  Else:
    Create and return a new object using the factory function.
  Increment active object count.
Release method:
  If object is nil, print warning and return.
  Add object back to the pool.
  Decrement active object count.
  If active object count is negative, reset to 0.