File: delta-table.cc

package info (click to toggle)
cssc 1.0.1-4
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny
  • size: 3,620 kB
  • ctags: 1,424
  • sloc: cpp: 13,500; sh: 4,759; ansic: 2,971; perl: 342; makefile: 342; awk: 11
file content (251 lines) | stat: -rw-r--r-- 5,180 bytes parent folder | download | duplicates (3)
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
 * delta-table.cc: Part of GNU CSSC.
 * 
 * 
 *    Copyright (C) 1997,1998,1999 Free Software Foundation, Inc. 
 * 
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 * 
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 * 
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
 * 
 * CSSC was originally Based on MySC, by Ross Ridge, which was 
 * placed in the Public Domain.
 *
 */

#include "cssc.h"
#include "sccsfile.h"
#include "delta.h"
#include "delta-table.h"
#include "delta-iterator.h"

const bool
cssc_delta_table::delta_at_seq_exists(seq_no seq)
{
  ASSERT(0 != this);
  ASSERT(seq > 0 && seq <= high_seqno);
  if (seq_table == NULL)
    {
      build_seq_table();
    }
  return seq_table[seq] != -1;
}

const delta &
cssc_delta_table::delta_at_seq(seq_no seq)
{
  ASSERT(0 != this);
  ASSERT(seq > 0 && seq <= high_seqno);
  if (seq_table == NULL)
    {
      build_seq_table();
    }
  return l.select(seq_table[seq]);
}



cssc_delta_table::~cssc_delta_table()
{
  ASSERT(0 != this);
  if (seq_table)
    delete [] seq_table;
  seq_table = 0;
}



seq_no cssc_delta_table::next_seqno() const
{
  ASSERT(0 != this);
  return highest_seqno() + 1u;
}


/* Builds the seq_no to delta table index table. */

void
cssc_delta_table::build_seq_table()
{
  ASSERT(0 != this);

  seq_table = new int[high_seqno + 1];

  int i;
  for(i = 0; i < high_seqno + 1; i++)
    {
      seq_table[i] = -1;
    }
  
  delta_iterator iter(this);
  while (iter.next(1))
    {
      seq_no seq = iter->seq;
      if (seq_table[seq] != -1)
	{
          /* ignore duplicate sequence number: some old sccs files
           * contain removed deltas with the same sequence number as
           * existing delta
           */
          continue;
	  s_corrupt_quit("Sequence number %u is duplicated"
	       " in delta table [build].", seq);
	}
      seq_table[seq] = iter.index();
    }
}

void
cssc_delta_table::update_highest(const delta &it) 
{
  ASSERT(0 != this);
  seq_no seq = it.seq;
  
  if (seq > high_seqno) 
    {
      if (seq_table != NULL) 
	{
//
// Create a temporary array to hold seq_table
//
	  int *temp_seq_table = new int[seq + 1];
	  memcpy( temp_seq_table, seq_table, (high_seqno+1)*sizeof( int));
	  delete [] seq_table;
	  seq_table = temp_seq_table;
	  for(int i = high_seqno + 1; i < seq + 1; i++) 
	    {
	      seq_table[i] = -1;
	    }
	}
      high_seqno = it.seq;
    }

  /* high_release is initialised to {0} so 
   * any greater-than comparison always fails since 
   * operator> decides it's not comparable with it.id.
   */
  if (!it.removed()) /* this conditional fixes SF bug #450900 */
    {
      if (high_release.is_null())
	{
	  if (it.id.on_trunk())
	    high_release = it.id;
	}
      else if (it.id > high_release)
	{
	  high_release = it.id;
	}
    }
  
  if (seq_table) 
    {
      if (seq_table[seq] != -1) 
	{
	  sid sid_using_this_seqno = delta_at_seq(seq).id;

	  sid_using_this_seqno.print(stderr);
	  it.id.print(stderr);

	  if (it.id != sid_using_this_seqno)
	    {
	      // diagnose...
	      for (int i = 1; i < high_seqno + 1; i++) 
		fprintf(stderr, "%u ", (unsigned)seq_table[i]);

	      fprintf(stderr,
		      "Sequence number %u is duplicated"
		      " in delta table [update] "
		      "(already used by entry %u: ", seq, seq_table[seq]);
	      sid_using_this_seqno.print(stderr);
	      fprintf(stderr, ")\n");
	  
	      s_corrupt_quit("Sequence number %u is duplicated"
			     " in delta table [update]", seq);
	    }
	}
      seq_table[seq] = l.length() - 1;
    }
}



/* Adds a delta to the end of the delta_table. */

void
cssc_delta_table::add(const delta &it)
{
  ASSERT(0 != this);
  
  l.add(it);
  update_highest(it);
}

/* for the prepend() operation, see sf-add.cc. */


/* Finds a delta in the delta table by its SID. */

delta const * cssc_delta_table::
find(sid id) const
{
  ASSERT(0 != this);
  const_delta_iterator iter(this);
  
  while (iter.next())
    {
      if (iter->id == id)
	{
	  return iter.operator->();
	}
    }
  return NULL;
}

/* Finds a delta in the delta table by its SID.
 * Removed deltas are counted.
 */

delta const * cssc_delta_table::
find_any(sid id) const
{
  ASSERT(0 != this);
  const_delta_iterator iter(this);
  
  while (iter.next(1))
    {
      if (iter->id == id)
	{
	  return iter.operator->();
	}
    }
  return NULL;
}

// This non-const variety is used by sf-cdc.cc.
delta * cssc_delta_table::
find(sid id)
{
  ASSERT(0 != this);
  delta_iterator iter(this);
  
  while (iter.next())
    {
      if (iter->id == id)
	{
	  return iter.operator->();
	}
    }
  return NULL;
}