This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
deb-goldedplus/goldlib/gall/gutltag.cpp

323 lines
7.5 KiB
C++
Raw Normal View History

2000-02-25 10:15:17 +00:00
// This may look like C code, but it is really -*- C++ -*-
// ------------------------------------------------------------------
// The Goldware Library
// Copyright (C) 1990-1999 Odinn Sorensen
// ------------------------------------------------------------------
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library 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
// Library General Public License for more details.
//
// You should have received a copy of the GNU Library General Public
// License along with this program; if not, write to the Free
// Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
// MA 02111-1307, USA
// ------------------------------------------------------------------
// $Id$
// ------------------------------------------------------------------
// Tag number types and set class.
// ------------------------------------------------------------------
#include <cstdlib>
#include <cstring>
#include <gmemdbg.h>
#include <gutltag.h>
// ------------------------------------------------------------------
// Constructor.
GTag::GTag() {
granularity = 10;
tag = NULL;
allocated = tags = count = 0;
}
// ------------------------------------------------------------------
// Destructor.
GTag::~GTag() {
Reset();
}
// ------------------------------------------------------------------
// Deallocate and reset internal data.
void GTag::Reset() {
throw_xrelease(tag);
allocated = tags = 0;
// NOTE: Does and must NOT reset the count!
}
// ------------------------------------------------------------------
// Resize tag array.
// Returns NULL if realloc failed.
2005-10-07 12:41:11 +00:00
uint32_t* GTag::Resize(uint __tags) {
2000-02-25 10:15:17 +00:00
register uint newsize = 0;
if(__tags >= allocated)
newsize = __tags + granularity;
else if(__tags < allocated) {
if((allocated-__tags) > granularity)
newsize = __tags + granularity;
}
if(newsize) {
2005-10-07 12:41:11 +00:00
tag = (uint32_t*)throw_realloc(tag, newsize*sizeof(uint32_t));
2000-02-25 10:15:17 +00:00
allocated = newsize;
}
count = tags = __tags;
return tag;
}
// ------------------------------------------------------------------
// Appends a new tag number to the end of the tag array.
// NOTE - Does not check for duplicates or correct sequence!
2005-10-07 12:41:11 +00:00
uint32_t* GTag::Append(uint32_t __tagn) {
2000-02-25 10:15:17 +00:00
Resize(tags+1);
tag[tags-1] = __tagn;
return tag;
}
// ------------------------------------------------------------------
// Add a new tag number at the correct position of the tag array.
//
// Algorithm dry test situations:
//
// [] 0123456
// tag 345789x tags = 7 (after resize)
// _reln 1234567
//
// Case 1 - New tag is smaller than first tag in array:
// __tagn = 2
// _reln = 1 (changed to 0)
// tags-_reln-1 = 7-0-1 = 6
//
// Case 2 - Normal insert in middle:
// __tagn = 6
// _reln = 3
// tags-_reln-1 = 7-3-1 = 3
//
// Case 3 - New tag is larger than last tag in array:
// __tagn = 10
// _reln = 6
// tags-_reln-1 = 7-6-1 = 0
//
// Dry test result: Works!
2005-10-07 12:41:11 +00:00
uint32_t* GTag::Add(uint32_t __tagn) {
2000-02-25 10:15:17 +00:00
// Find closest tag number
uint _reln = ToReln(__tagn, TAGN_CLOSEST);
// Do we have it already?
2001-04-15 19:24:44 +00:00
if((_reln == RELN_INVALID) or (tag[_reln-1] != __tagn)) {
2000-02-25 10:15:17 +00:00
// Resize tag array to make room for the new number
Resize(tags+1);
// General rule:
// - tag[_reln-1] is smaller than __tagn
//
// The exceptions to the rule:
// - no tags in array (if _reln == RELN_INVALID)
// - tag[_reln-1] is larger than __tagn
2001-04-15 19:24:44 +00:00
if(_reln and (tag[_reln-1] > __tagn))
2000-02-25 10:15:17 +00:00
_reln--;
// Move data to make room for the new tag number
2005-10-07 12:41:11 +00:00
memmove(tag+_reln+1, tag+_reln, (tags-_reln-1)*sizeof(uint32_t));
2000-02-25 10:15:17 +00:00
// Copy the new tag number to the insert position
tag[_reln] = __tagn;
}
return tag;
}
// ------------------------------------------------------------------
// Delete a tag number from the tag array.
// Returns the relative tag number or RELN_INVALID if missing.
// NOTE - Does not resize the array.
2005-10-07 12:41:11 +00:00
uint GTag::Del(uint32_t __tagn) {
2000-02-25 10:15:17 +00:00
return DelReln(ToReln(__tagn));
}
// ------------------------------------------------------------------
uint GTag::DelReln(uint __reln) {
if(__reln) {
2005-10-07 12:41:11 +00:00
memmove(tag+__reln-1, tag+__reln, (tags-__reln)*sizeof(uint32_t));
2000-02-25 10:15:17 +00:00
count--;
tags--;
}
return __reln;
}
// ------------------------------------------------------------------
2005-10-07 12:41:11 +00:00
uint GTag::DelResize(uint32_t __tagn) {
2000-02-25 10:15:17 +00:00
uint _reln = Del(__tagn);
Resize(tags);
return _reln;
}
// ------------------------------------------------------------------
2005-10-07 12:41:11 +00:00
static int TagnCmp(const uint32_t* __a, const uint32_t* __b) {
2000-02-25 10:15:17 +00:00
return CmpV(*__a, *__b);
}
// ------------------------------------------------------------------
void GTag::Sort() {
2005-10-07 12:41:11 +00:00
qsort(tag, tags, sizeof(uint32_t), (StdCmpCP)TagnCmp);
2000-02-25 10:15:17 +00:00
}
// ------------------------------------------------------------------
void GTag::ElimDups() {
if(tags > 1) {
uint _before = tags;
2005-10-07 12:41:11 +00:00
uint32_t _last = tag[0];
2000-02-25 10:15:17 +00:00
for(uint n=1; n<tags; n++) {
if(_last == tag[n]) {
2005-10-07 12:41:11 +00:00
memmove(&tag[n-1], &tag[n], (tags-n)*sizeof(uint32_t));
2000-02-25 10:15:17 +00:00
tags--;
n--;
}
_last = tag[n];
}
if(_before != tags)
Resize(tags);
}
}
// ------------------------------------------------------------------
// Return the tag number corresponding to a relative tag number.
// Returns TAGN_INVALID, if the requested number did not exist.
2005-10-07 12:41:11 +00:00
uint32_t GTag::CvtReln(uint __reln) {
2000-02-25 10:15:17 +00:00
if(tag and __reln and (__reln <= tags))
return tag[__reln-1];
else
return TAGN_INVALID;
}
// ------------------------------------------------------------------
// Return the relative tag number corresponding to a tag number.
// Returns RELN_INVALID if the requested number did not exist.
// If __closest is true, the closest smaller tag number is returned.
// If no tags exist at all, RELN_INVALID is returned.
2005-10-07 12:41:11 +00:00
uint GTag::ToReln(uint32_t __tagn, int __closest) {
2000-02-25 10:15:17 +00:00
uint _lastreln = RELN_INVALID;
if(tag) {
if(__tagn and tags)
if(__tagn > tag[tags-1])
return __closest ? tags : RELN_INVALID;
if(tags and __tagn) {
2005-10-07 12:41:11 +00:00
int _mid;
int _left = 0;
int _right = tags;
2000-02-25 10:15:17 +00:00
do {
_mid = (_left+_right)/2;
if(__tagn < tag[(uint)_mid])
_right = _mid - 1;
else if(__tagn > tag[(uint)_mid])
_left = _mid + 1;
else
return (uint)(_mid + 1);
} while(_left < _right);
_lastreln = (uint)(_left + 1);
if(__tagn == tag[(uint)_left])
return _lastreln;
}
}
// Not found
return __closest ? _lastreln : RELN_INVALID;
}
// ------------------------------------------------------------------
2005-10-07 12:41:11 +00:00
uint GTag::ToReln(uint32_t __tagn) {
2000-02-25 10:15:17 +00:00
return ToReln(__tagn, TAGN_EXACT);
}
// ------------------------------------------------------------------
2006-04-24 16:38:44 +00:00
void GTag::Load(gfile& fp)
{
2000-02-25 10:15:17 +00:00
dword val;
2006-04-24 16:38:44 +00:00
fp.Fread(&val, sizeof(dword));
2000-02-25 10:15:17 +00:00
count = (uint) val;
2006-04-24 16:38:44 +00:00
if (count)
{
2000-02-25 10:15:17 +00:00
Resize(count);
2006-04-24 16:38:44 +00:00
fp.Fread(tag, sizeof(uint32_t), count);
2000-02-25 10:15:17 +00:00
}
}
// ------------------------------------------------------------------
2006-04-24 16:38:44 +00:00
void GTag::Save(gfile& fp)
{
dword val = (dword) count;
fp.Fwrite(&val, sizeof(dword));
2000-02-25 10:15:17 +00:00
2006-04-24 16:38:44 +00:00
if (tag and count)
fp.Fwrite(tag, sizeof(uint32_t), count);
2000-02-25 10:15:17 +00:00
}
// ------------------------------------------------------------------