forked from dotnet/machinelearning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhybrid_map.cpp
More file actions
142 lines (134 loc) · 3.86 KB
/
Copy pathhybrid_map.cpp
File metadata and controls
142 lines (134 loc) · 3.86 KB
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#include <fstream>
#include <iostream>
#include <vector>
#include "utils.hpp"
#include <stdlib.h>
#include "hybrid_map.h"
namespace lda
{
hybrid_map::hybrid_map()
:memory_(nullptr),
is_dense_(1),
capacity_(0),
empty_key_(0),
deleted_key_(-1),
key_(nullptr),
value_(nullptr),
num_deleted_key_(0),
external_rehash_buf_(nullptr)
{
}
hybrid_map::hybrid_map(int32_t *memory, int32_t is_dense, int32_t capacity, int32_t num_deleted_key
, int32_t *external_rehash_buf_)
: memory_(memory),
is_dense_(is_dense),
capacity_(capacity),
empty_key_(0),
deleted_key_(-1),
key_(nullptr),
value_(nullptr),
num_deleted_key_(num_deleted_key),
external_rehash_buf_(external_rehash_buf_)
{
if (is_dense_ == 0) {
key_ = memory_;
value_ = memory_ + capacity_;
}
}
hybrid_map::hybrid_map(const hybrid_map &other)
{
this->memory_ = other.memory_;
this->is_dense_ = other.is_dense_;
this->capacity_ = other.capacity_;
empty_key_ = other.empty_key_;
deleted_key_ = other.deleted_key_;
num_deleted_key_ = other.num_deleted_key_;
external_rehash_buf_ = other.external_rehash_buf_;
if (this->is_dense_)
{
this->key_ = nullptr;
this->value_ = nullptr;
}
else
{
this->key_ = this->memory_;
this->value_ = this->memory_ + capacity_;
}
}
hybrid_map& hybrid_map::operator=(const hybrid_map &other)
{
this->memory_ = other.memory_;
this->is_dense_ = other.is_dense_;
this->capacity_ = other.capacity_;
empty_key_ = other.empty_key_;
deleted_key_ = other.deleted_key_;
num_deleted_key_ = other.num_deleted_key_;
external_rehash_buf_ = other.external_rehash_buf_;
if (this->is_dense_)
{
this->key_ = nullptr;
this->value_ = nullptr;
}
else
{
this->key_ = this->memory_;
this->value_ = this->memory_ + capacity_;
}
return *this;
}
void hybrid_map::clear()
{
int32_t memory_size = is_dense_ ? capacity_ : 2 * capacity_;
memset(memory_, 0, memory_size * sizeof(int32_t));
}
std::string hybrid_map::DumpString() const
{
if (is_dense_)
{
std::string result;
for (int i = 0; i < capacity_; ++i)
{
if (memory_[i] != 0)
{
result += std::to_string(i) + ":" + std::to_string(memory_[i]) + " ";
}
}
return result;
}
else
{
std::string result;
for (int i = 0; i < capacity_; ++i)
{
if (key_[i] > 0)
{
result += std::to_string(key_[i] - 1) + ":" + std::to_string(value_[i]) + " ";
}
}
return result;
}
}
void hybrid_map::sorted_rehashing()
{
if (!is_dense_)
{
std::map<int32_t, int32_t> rehash_buffer;
for (int i = 0; i < capacity_; ++i)
{
if (key_[i] > 0)
{
rehash_buffer[key_[i] - 1] = value_[i];
}
}
memset(memory_, 0, 2 * capacity_ * sizeof(int32_t));
for (auto it = rehash_buffer.begin();
it != rehash_buffer.end(); ++it)
{
inc(it->first, it->second);
}
}
}
}