summaryrefslogtreecommitdiff
path: root/apps/soreduce/Sig_List.cpp
blob: 4327a14bdc45f92f40907e87048252b922a5ea6d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184

// $Id$

// File: Sig_List.cpp

// Author: Phil Mesnier

#include "Sig_List.h"

ACE_RCSID(src, Sig_List, "$Id$")

//-----------------------------------------------------------------------------

Sig_List::Sig_List (int cap)
  : size_(0),
    capacity_(cap),
    index_(0),
    has_nulls_(0),
    modified_(0),
    array_(0)
{
  array_ = new Signature*[capacity_];
}

Sig_List::~Sig_List ()
{
  for (int i = 0; i < size_; i++)
    if (array_[i]) array_[i]->release();
  delete [] array_;
}

void
Sig_List::add (const ACE_CString &s)
{
  if (this->index_of (s) != -1)
    return;
  modified_ = 1;
  if (has_nulls_)
    for (int i = 0; i < size_; i++) 
      if (array_[i] == 0) {
        array_[i] = new Signature (s);
        has_nulls_ --;
        return;
      }
  if (size_ == capacity_) {
    int ncap = capacity_ * 2;
    Signature ** narray = new Signature *[ncap];
    ACE_OS::memcpy (narray,array_,capacity_ * sizeof(Signature*));
    delete [] array_;
    array_ = narray;
    capacity_ = ncap;
  }
  array_[size_++] = new Signature(s);
}

void
Sig_List::add (const Sig_List &other)
{
  if (capacity_ < size_ + other.capacity_) {
    int ncap = size_ + other.capacity_ + 50;
    Signature ** narray = new Signature *[ncap];
    ACE_OS::memcpy (narray,array_,capacity_ * sizeof(Signature*));
    delete [] array_;
    array_ = narray;
    capacity_ = ncap;
  }
  modified_ = 1;
  for (int i = 0; i < other.size_; i++)
    if (other.array_[i] != 0 && 
        this->index_of (other.array_[i]->name()) == -1) 
      {
        if (!has_nulls_) 
          array_[size_++] = other.array_[i]->dup();
        else
          for (int i = 0; i < size_; i++) 
            if (array_[i] == 0) 
              {
                array_[i] = other.array_[i]->dup();
                has_nulls_ --;
                break;
              }
      }
}

void
Sig_List::remove (const Signature &s)
{
  for (int i = 0; i < size_; i++)
    if (array_[i] && array_[i]->name() == s.name()) {
      array_[i]->release();
      array_[i] = 0;
      modified_ = 1;
      if (i == size_ - 1)
        size_ --;
      else
        has_nulls_ ++;
      break;
    }
}

void
Sig_List::remove_current ()
{
  array_[index_]->release();
  array_[index_] = 0;
  modified_ = 1;
  if (index_ == size_ - 1) 
    size_--;
  else
    has_nulls_++;
}

int
Sig_List::index_of (const Signature *s)
{
  for (int i = 0; i < size_; i++)
    if (array_[i] && array_[i]->name() == s->name()) {
      array_[i]->used();
      return i;
    }
  return -1;
}

int
Sig_List::index_of (const ACE_CString &s)
{
  for (int i = 0; i < size_; i++)
    if (array_[i] && array_[i]->name() == s) {
      return i;
    }
  return -1;
}


const Signature *
Sig_List::first()
{
  for (index_ = 0; index_ < size_; index_++)
    if (array_[index_] != 0)
      return array_[index_];
  return 0;
}

const Signature *
Sig_List::next()
{
  for (++index_; index_ < size_; index_++)
    if (array_[index_] != 0)
      return array_[index_];
  return 0;
}

int 
Sig_List::hasmore ()
{
  return index_ < size_;
}

int
Sig_List::size()
{
  return size_;
}

int
Sig_List::modified()
{
  int rtn = modified_;
  modified_ = 0;
  int insert = 0;
  if (has_nulls_) {
    for (int i = 0; i < size_; i++)
      if (array_[i] != 0) {
        if (i != insert) {
          array_[insert] = array_[i];
          array_[i] = 0;
        } 
        insert++;
      }
    size_ = insert+1;
    has_nulls_ = 0;
  }
  return rtn;
}