File: SkipList.cpp

package info (click to toggle)
fastdds 2.9.1%2Bds-1%2Bdeb12u2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 31,412 kB
  • sloc: cpp: 378,073; xml: 7,623; ansic: 4,596; python: 2,545; sh: 189; makefile: 36
file content (172 lines) | stat: -rw-r--r-- 3,614 bytes parent folder | download | duplicates (2)
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
// Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
 * @file SkipList.cpp
 *
 */

#include "SkipList.h"

#include <cstdlib>
#include <ctime>

namespace eprosima {
namespace fastrtps{
namespace rtps {


template<class Obj>
SkipNode<Obj>::~SkipNode()
{
	delete [] mp_fwdNodes;
}

template<class Obj>
SkipNode<Obj>::SkipNode(Obj* obj,uint32_t level):m_height(level),
mp_data(obj)
{
	mp_fwdNodes = new SkipNode<Obj>* [m_height];
	for(int x = 0;x<m_height;++x)
		mp_fwdNodes[x] = nullptr;
}

template <class Obj>
SkipNode<Obj>::SkipNode(uint32_t level):m_height(level),
mp_data(nullptr)
{
	mp_fwdNodes = new SkipNode<Obj>* [m_height];
	for(int x = 0;x<m_height;++x)
		mp_fwdNodes[x] = nullptr;
}

//SKIP LIST

template <class Obj>
SkipList<Obj>::SkipList(float probability,uint32_t maxHeight): m_maxHeight(maxHeight),
m_currHeight(0),
m_probability(probability),
mp_head(nullptr),
mp_tails(nullptr)
{
	srand (time(NULL));
	mp_head = new SkipNode<Obj>(m_maxHeight);
	mp_tails = new SkipNode<Obj>* [m_maxHeight];
	for ( int x = 0; x < m_maxHeight; x++ )
	{
		mp_head->mp_fwdNodes[x] = nullptr;
		mp_tails[x] = mp_head;
	}

}

template <class Obj>
SkipList<Obj>::~SkipList() {
	// Walk 0 level nodes and delete all
	SkipNode<Obj>* tmp;
	SkipNode<Obj>* nxt;
	tmp = mp_head;
	while ( tmp )
	{
		nxt = tmp->mp_fwdNodes[0];
		delete tmp;
		tmp = nxt;
	}
	delete [] mp_tails;
}

template <class Obj>
bool SkipList<Obj>::insert_at_end(Obj* obj)
{
	SkipNode<Obj>* tmp = new SkipNode<Obj>(obj,newLevel());
	for(int x = 0;x<tmp->m_height;++x)
	{
		mp_tails[x]->mp_fwdNodes[x] = tmp;
		mp_tails[x] = tmp;
	}
	return true;
}

template <class Obj>
bool SkipList<Obj>::insert(Obj* obj)
{
	int lvl = 0, h = 0;
	SkipNode<Obj>** updateVec =	new SkipNode<Obj>* [m_maxHeight];
	SkipNode<Obj>* tmp = mp_head;
	// Figure out where new node goes
	for ( h = m_currHeight; h >= 0; h-- )
	{
		while (tmp->mp_fwdNodes[h] != nullptr && *tmp->mp_fwdNodes[h]->mp_data < *obj )
		{
			tmp = tmp->fwdNodes[h];
		}
		updateVec[h] = tmp;
	}
	tmp = tmp->fwdNodes[0]; //Element just before the one we are inserting.
	// If dup, return false
	if ( *tmp->mp_data == *obj )
	{
		return false;
	}
	else
	{
		// Perform an insert
		lvl = newLevel();
		if ( lvl > m_currHeight )
		{
			for ( int i = m_currHeight; i <= lvl; i++ )
				updateVec[i] = mp_head;
			m_currHeight = lvl;
		}
		// Insert new element
		tmp = new SkipNode<Obj>(obj, lvl);
		for ( int i = 0; i < lvl; i++ )
		{
			tmp->fwdNodes[i] = updateVec[i]->fwdNodes[i];
			updateVec[i]->fwdNodes[i] = tmp;
		}
	}
	return true;
}


template <class Obj>
bool SkipList<Obj>::remove(Obj* obj)
{

}



template <class Obj>
uint32_t SkipList<Obj>::newLevel()
{
	uint32_t tmpLvl = 1;
	// Develop a random number between 1 and maxLvl (node height).
	while ((rand01() < m_probability) &&
			(tmpLvl < m_maxHeight))
		tmpLvl++;

	return tmpLvl;
}

template <class Obj>
float SkipList<Obj>::rand01()
{
	return static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
}

}
} /* namespace rtps */
} /* namespace eprosima */