1%%% Copyright (c) 2009 Nomasystems, S.L., All Rights Reserved 2%%% 3%%% This file contains Original Code and/or Modifications of Original Code as 4%%% defined in and that are subject to the Nomasystems Public License version 5%%% 1.0 (the 'License'). You may not use this file except in compliance with 6%%% the License. BY USING THIS FILE YOU AGREE TO ALL TERMS AND CONDITIONS OF 7%%% THE LICENSE. A copy of the License is provided with the Original Code and 8%%% Modifications, and is also available at www.nomasystems.com/license.txt. 9%%% 10%%% The Original Code and all software distributed under the License are 11%%% distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 12%%% EXPRESS OR IMPLIED, AND NOMASYSTEMS AND ALL CONTRIBUTORS HEREBY DISCLAIM 13%%% ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF 14%%% MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR 15%%% NON-INFRINGEMENT. Please see the License for the specific language 16%%% governing rights and limitations under the License. 17-module(cl_pool). 18 19%%% EXTERNAL EXPORTS 20-export([add/2, del/2, items/1, new/1, next/1]). 21 22%%% MACROS 23-define(CL_POOL_TAB, cl_pool_tab). 24 25%%%----------------------------------------------------------------------------- 26%%% EXTERNAL EXPORTS 27%%%----------------------------------------------------------------------------- 28add(Name, Item) -> 29 Items = ets:lookup_element(Name, items, 2), 30 true = ets:insert(Name, {items, [Item | Items]}), 31 ok. 32 33 34del(Name, Item) -> 35 Items = ets:lookup_element(Name, items, 2), 36 true = ets:insert(Name, {items, lists:delete(Item, Items)}), 37 ok. 38 39 40items(Name) -> 41 ets:lookup_element(Name, items, 2). 42 43 44new(Name) -> 45 Name = ets:new(Name, [public, named_table]), 46 true = ets:insert(Name, {next, -1}), 47 true = ets:insert(Name, {items, []}), 48 ok. 49 50 51next(Name) -> 52 try 53 Items = ets:lookup_element(Name, items, 2), 54 Next = (ets:update_counter(Name, next, 1) rem length(Items)) + 1, 55 lists:nth(Next, Items) 56 catch 57 _Class:_Reason -> 58 erlang:throw(not_found) 59 end. 60 61