|
1 /* |
|
2 * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * System Includes |
|
16 * |
|
17 */ |
|
18 |
|
19 |
|
20 |
|
21 #include <assert.h> |
|
22 |
|
23 |
|
24 /******************************************************************************* |
|
25 * |
|
26 * Local Includes |
|
27 * |
|
28 ******************************************************************************/ |
|
29 #include "CIntegerAllocator.h" |
|
30 |
|
31 |
|
32 /******************************************************************************* |
|
33 * |
|
34 * Implementation |
|
35 * |
|
36 ******************************************************************************/ |
|
37 |
|
38 /******************************************************************************* |
|
39 * |
|
40 * PUBLIC METHOD: AddToIntegerPool |
|
41 * |
|
42 ******************************************************************************/ |
|
43 int CIntegerAllocator::AddToIntegerPool( int aStart, int aEnd ) |
|
44 { |
|
45 int i, err; |
|
46 |
|
47 // numbers must be positive |
|
48 if( aStart > aEnd ) { |
|
49 return -1; |
|
50 } |
|
51 if( aStart < 0 ) { |
|
52 return -1; |
|
53 } |
|
54 |
|
55 // add each integer in the range, as long as it doesn't already |
|
56 // exist in the list |
|
57 for( i = aStart; i <= aEnd; i++ ) { |
|
58 err = DoesIntegerAlreadyAppear( i ); |
|
59 if( err == 0 ) { |
|
60 iList.push_back( i ); |
|
61 } |
|
62 } |
|
63 |
|
64 // done |
|
65 return 0; |
|
66 } |
|
67 |
|
68 |
|
69 /******************************************************************************* |
|
70 * |
|
71 * PUBLIC METHOD: AllocateInteger |
|
72 * |
|
73 ******************************************************************************/ |
|
74 int CIntegerAllocator::AllocateInteger() |
|
75 { |
|
76 int rv; |
|
77 vector<int>::iterator iter; |
|
78 |
|
79 // check that there is a free integer |
|
80 if( iList.empty() ) { |
|
81 return -1; |
|
82 } |
|
83 |
|
84 // get the integer and delete it |
|
85 iter = iList.begin(); |
|
86 rv = *iter; |
|
87 iList.erase( iter ); |
|
88 |
|
89 // return the allocated integer |
|
90 return rv; |
|
91 } |
|
92 |
|
93 |
|
94 /******************************************************************************* |
|
95 * |
|
96 * PUBLIC METHOD: FreeInteger |
|
97 * |
|
98 ******************************************************************************/ |
|
99 int CIntegerAllocator::FreeInteger( int aFreeInteger ) |
|
100 { |
|
101 // check that the number isn't already there |
|
102 assert( DoesIntegerAlreadyAppear(aFreeInteger) == 0 ); |
|
103 |
|
104 // add the integer back to the list |
|
105 iList.push_back( aFreeInteger ); |
|
106 return 0; |
|
107 } |
|
108 |
|
109 |
|
110 /******************************************************************************* |
|
111 * |
|
112 * PRIVATE METHOD: DoesIntegerAlreadyAppear |
|
113 * |
|
114 ******************************************************************************/ |
|
115 int CIntegerAllocator::DoesIntegerAlreadyAppear( int aInteger ) |
|
116 { |
|
117 vector<int>::iterator iter; |
|
118 |
|
119 // search the vector for a match |
|
120 for( iter = iList.begin(); iter != iList.end(); iter++ ) { |
|
121 if( *iter == aInteger ) { |
|
122 return 1; |
|
123 } |
|
124 } |
|
125 |
|
126 // done - not found |
|
127 return 0; |
|
128 } |