This commit is contained in:
Federico Ponchio 2004-06-24 14:18:58 +00:00
parent 535a76ce2b
commit 298d7c14c6
4 changed files with 347 additions and 0 deletions

130
wrap/nexus/mfhash.cpp Normal file
View File

@ -0,0 +1,130 @@
/****************************************************************************
* VCGLib o o *
* Visual and Computer Graphics Library o o *
* _ O _ *
* Copyright(C) 2004 \/)\/ *
* Visual Computing Lab /\/| *
* ISTI - Italian National Research Council | *
* \ *
* All rights reserved. *
* *
* 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 (http://www.gnu.org/licenses/gpl.txt) *
* for more details. *
* *
****************************************************************************/
/****************************************************************************
History
$Log: not supported by cvs2svn $
****************************************************************************/
#include "mfhash.h"
using namespace std;
using namespace nxs;
bool MFHash::Create(const string &file, unsigned int reserved) {
if(!buffer.Create(file)) return false;
buffer.Resize(reserved);
Bucket empty;
for(unsigned int i = 0; i < buffer.Size(); i++)
buffer[i] = empty;
space = reserved;
return true;
}
bool MFHash::Load(const string &file, unsigned int used) {
if(!buffer.Load(file)) return false;
if(used != 0xffffffff) {
space = buffer.Size() - used;
} else {
space = 0;
for(unsigned int i = 0; i < buffer.Size(); i++)
if(buffer[i].Empty()) space++;
}
return true;
}
void MFHash::Resize(unsigned int n) {
assert(buffer.Size() - space <= n);
//lets dump actual content
FILE *fp = tmpfile();
unsigned int count = 0;
for(unsigned int i = 0; i < buffer.Size(); i++) {
if(!buffer[i].Empty()) {
fwrite(&buffer[i], sizeof(Bucket), 1, fp);
++count;
}
}
buffer.Resize(n);
Clear();
rewind(fp);
Bucket bucket;
for(unsigned int i = 0; i < count; i++) {
fread(&bucket, sizeof(Bucket), 1, fp);
Insert(bucket.key, bucket.value, false);
}
fclose(fp);
}
void MFHash::Insert(unsigned int key, unsigned int value, bool rehash) {
if(buffer.Size() < 5)
Resize(5);
assert(space > 0);
unsigned int hash_size = buffer.Size();
unsigned int j = key % hash_size;
while(!buffer[j].Empty()) {
if(buffer[j].key == key && buffer[j].value == value) //already here
return;
j++;
if(j >= hash_size) j = 0;
}
buffer[j] = Bucket(key, value);
space--;
if(rehash) {
float ratio = space / (float)buffer.Size();
if(ratio < 0.4) //need to resize
Resize(buffer.Size() * 2 - 1);
}
}
unsigned int MFHash::Count(unsigned int key) {
unsigned int count = 0;
unsigned int hash_size = buffer.Size();
unsigned int j = key % hash_size;
while(!buffer[j].Empty()) {
if(buffer[j].key == key)
count++;
j++;
if(j >= hash_size) j = 0;
}
return count;
}
void MFHash::Clear() {
Bucket empty;
for(unsigned int i = 0; i < buffer.Size(); i++)
buffer[i] = empty;
space = buffer.Size();
}
void MFHash::Close() {
buffer.Close();
}
unsigned int MFHash::Size() {
return buffer.Size() - space;
}

78
wrap/nexus/mfhash.h Normal file
View File

@ -0,0 +1,78 @@
/****************************************************************************
* VCGLib o o *
* Visual and Computer Graphics Library o o *
* _ O _ *
* Copyright(C) 2004 \/)\/ *
* Visual Computing Lab /\/| *
* ISTI - Italian National Research Council | *
* \ *
* All rights reserved. *
* *
* 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 (http://www.gnu.org/licenses/gpl.txt) *
* for more details. *
* *
****************************************************************************/
/****************************************************************************
History
$Log: not supported by cvs2svn $
****************************************************************************/
#ifndef NXS_MFHASH_H
#define NXS_MFHASH_H
#include "vfile.h"
#include <stdio.h>
#include <iostream>
namespace nxs {
class MFHash {
public:
struct Bucket {
unsigned int key;
unsigned int value;
Bucket(): key(0xffffffff) {}
Bucket(unsigned int k, unsigned int v): key(k), value(v) {}
bool Empty() { return key == 0xffffffff; }
};
MFHash() {}
bool Create(const std::string &file, unsigned int reserved = 32);
bool Load(const std::string &file, unsigned int used = 0xffffffff);
void Resize(unsigned int n);
void Insert(unsigned int key, unsigned int value, bool rehash = true);
template <class C> void GetValues(unsigned int key, C &container) {
container.clear();
unsigned int hash_size = buffer.Size();
unsigned int j = key % hash_size;
while(!buffer[j].Empty()) {
if(buffer[j].key == key) {
container.push_back(buffer[j].value);
}
j++;
if(j >= hash_size) j = 0;
}
}
unsigned int Count(unsigned int key);
void Clear();
unsigned int Size();
void Close();
private:
VFile<Bucket> buffer;
unsigned int space;
};
}//namespace
#endif

76
wrap/nexus/vert_remap.cpp Normal file
View File

@ -0,0 +1,76 @@
/****************************************************************************
* VCGLib o o *
* Visual and Computer Graphics Library o o *
* _ O _ *
* Copyright(C) 2004 \/)\/ *
* Visual Computing Lab /\/| *
* ISTI - Italian National Research Council | *
* \ *
* All rights reserved. *
* *
* 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 (http://www.gnu.org/licenses/gpl.txt) *
* for more details. *
* *
****************************************************************************/
/****************************************************************************
History
$Log: not supported by cvs2svn $
****************************************************************************/
#include <iostream>
#include "vert_remap.h"
using namespace std;
using namespace nxs;
bool VertRemap::Create(const std::string &file) {
if(all.Create(file)) return false;
if(!borders.Create(file)) return false;
return true;
}
bool VertRemap::Load(const std::string &file) {
if(!all.Load(file)) return false;
if(!borders.Create(file)) return false;
return true;
}
void VertRemap::Resize(unsigned int n_vert) {
all.Resize(n_vert);
for(unsigned int i = 0; i < n_vert; i++)
all[i] = 0xffffffff;
borders.Clear();
borders.Resize(n_vert/10);
}
unsigned int VertRemap::Size() {
return all.Size();
}
unsigned int VertRemap::Count(unsigned int key) {
assert(key < Size());
if(all[key] == 0xffffffff) return 0;
return 1 + borders.Count(key);
}
void VertRemap::Insert(unsigned int key, unsigned int value) {
if(all[key] == 0xffffffff)
all[key] = value;
else
borders.Insert(key, value);
}
unsigned int VertRemap::GetValue(unsigned int key) { //return first value
assert(key < Size());
return all[key];
}

63
wrap/nexus/vert_remap.h Normal file
View File

@ -0,0 +1,63 @@
/****************************************************************************
* VCGLib o o *
* Visual and Computer Graphics Library o o *
* _ O _ *
* Copyright(C) 2004 \/)\/ *
* Visual Computing Lab /\/| *
* ISTI - Italian National Research Council | *
* \ *
* All rights reserved. *
* *
* 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 (http://www.gnu.org/licenses/gpl.txt) *
* for more details. *
* *
****************************************************************************/
/****************************************************************************
History
$Log: not supported by cvs2svn $
****************************************************************************/
#ifndef NXS_VERTEX_REMAP_H
#define NXS_VERTEX_REMAP_H
#include <string>
#include "vfile.h"
#include "mfhash.h"
namespace nxs {
class VertRemap {
public:
bool Create(const std::string &file);
bool Load(const std::string &file);
void Resize(unsigned int n_vert);
unsigned int Size();
unsigned int Count(unsigned int key);
void Insert(unsigned int key, unsigned int value);
unsigned int GetValue(unsigned int key); //return first value
template <class C> void GetValues(unsigned int key,
C &container) {
assert(key < Size());
container.clear();
if(all[key] == 0xffffffff) return;
container.push_back(all[key]);
borders.GetValue(key, container);
}
private:
VFile<unsigned int> all;
MFHash borders;
};
} //namespace bmt
#endif