1 /*
2  *
3  *  C++ Portable Types Library (PTypes)
4  *  Version 2.1.1  Released 27-Jun-2007
5  *
6  *  Copyright (C) 2001-2007 Hovik Melikyan
7  *
8  *  http://www.melikyan.com/ptypes/
9  *
10  */
11 
12 #include "ptypes.h"
13 
14 
15 PTYPES_BEGIN
16 
17 
_objlist()18 _objlist::_objlist()
19     : tpodlist<void*,true>()
20 {
21     memset(&config, 0, sizeof(config));
22 }
23 
24 
_objlist(bool ownobjects)25 _objlist::_objlist(bool ownobjects)
26     : tpodlist<void*,true>()
27 {
28     memset(&config, 0, sizeof(config));
29     config.ownobjects = ownobjects;
30 }
31 
32 
~_objlist()33 _objlist::~_objlist()
34 {
35 }
36 
37 
dofree(void *)38 void _objlist::dofree(void*)
39 {
40     fatal(CRIT_FIRST + 38, "ptrlist::dofree() not defined");
41 }
42 
43 
compare(const void *,const void *) const44 int _objlist::compare(const void*, const void*) const
45 {
46     fatal(CRIT_FIRST + 38, "ptrlist::compare() not defined");
47     return 0;
48 }
49 
50 
dofree(int index,int num)51 void _objlist::dofree(int index, int num)
52 {
53     void** p = (void**)list + index;
54     while (--num >= 0)
55         dofree(*p++);
56 }
57 
58 
doput(int index,void * obj)59 void _objlist::doput(int index, void* obj)
60 {
61     void** p = (void**)list + index;
62     if (config.ownobjects)
63         dofree(*p);
64     *p = obj;
65 }
66 
67 
dodel(int index)68 void _objlist::dodel(int index)
69 {
70     if (config.ownobjects)
71         dofree(doget(index));
72     tpodlist<void*, true>::dodel(index);
73 }
74 
75 
dodel(int index,int delcount)76 void _objlist::dodel(int index, int delcount)
77 {
78     if (config.ownobjects)
79     {
80         if (index + delcount > count)
81             delcount = count - index;
82         dofree(index, delcount);
83     }
84     tpodlist<void*, true>::dodel(index, delcount);
85 }
86 
87 
set_count(int newcount)88 void _objlist::set_count(int newcount)
89 {
90     if (newcount < count && config.ownobjects)
91     {
92         if (newcount < 0)
93             newcount = 0;
94         dofree(newcount, count - newcount);
95     }
96     _podlist::set_count(newcount, true);
97 }
98 
99 
dopop()100 void* _objlist::dopop()
101 {
102     void* t = doget(--count);
103     if (count == 0)
104         set_capacity(0);
105     return t;
106 }
107 
108 
search(const void * key,int & index) const109 bool _objlist::search(const void* key, int& index) const
110 {
111     int l, h, i, c;
112     bool ret = false;
113     l = 0;
114     h = count - 1;
115     while (l <= h)
116     {
117         i = (l + h) / 2;
118         c = compare(key, doget(i));
119         if (c > 0)
120             l = i + 1;
121         else
122         {
123             h = i - 1;
124             if (c == 0)
125             {
126                 ret = true;
127                 if (!config.duplicates)
128                     l = i;
129             }
130         }
131     }
132     index = l;
133     return ret;
134 }
135 
136 
indexof(void * obj) const137 int _objlist::indexof(void* obj) const
138 {
139     for (int i = 0; i < count; i++)
140         if (doget(i) == obj)
141             return i;
142     return -1;
143 }
144 
145 
146 #ifdef PTYPES19_COMPAT
147 
objlist(bool ownobjects)148 objlist::objlist(bool ownobjects): tobjlist<unknown>(ownobjects)  {}
149 
~objlist()150 objlist::~objlist()  {}
151 
152 #endif
153 
154 
155 PTYPES_END
156 
157