Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
pa4
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Nilesh Jagdish
pa4
Commits
158e764c
Commit
158e764c
authored
Nov 09, 2020
by
Nilesh Jagdish
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Made changes to cache
parent
67c23ef2
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
25 additions
and
6 deletions
+25
-6
cache.cpp
cache.cpp
+25
-6
No files found.
cache.cpp
View file @
158e764c
...
...
@@ -3,6 +3,7 @@
#include <iterator>
#include <unordered_map>
#include <string>
#include "cs_thread.h"
#include "cache.h"
using
namespace
std
;
/*
...
...
@@ -37,17 +38,19 @@ unordered_map<string, list <cacheNode> :: iterator> hashTable;
* Returns 1 if key-value pair is present in cache and 0 otherwise
* If key is present, brings correspoding node to front of the cache(To identify recently used element)
*/
int
search
(
string
key
)
{
string
search
(
string
key
)
{
unordered_map
<
string
,
list
<
cacheNode
>
::
iterator
>
::
iterator
it
=
hashTable
.
find
(
key
);
if
(
it
==
hashTable
.
end
())
{
return
0
;
string
ans
=
""
;
return
ans
;
}
else
{
list
<
cacheNode
>
::
iterator
cnode_it
=
it
->
second
;
cache
.
push_front
(
*
cnode_it
);
cache
.
erase
(
cnode_it
);
hashTable
[
key
]
=
cache
.
begin
();
return
1
;
string
ans
=
cnode_it
->
value
;
return
ans
;
}
}
...
...
@@ -89,13 +92,29 @@ void insert(string key, string value) {
/*
* Delete from cache
*/
void
del
(
string
key
)
{
void
DEL
(
string
key
)
{
unordered_map
<
string
,
list
<
cacheNode
>
::
iterator
>
::
iterator
it
=
hashTable
.
find
(
key
);
list
<
cacheNode
>
::
iterator
cnode_it
=
it
->
second
;
list
<
cacheNode
>
::
iterator
cnode_it
=
it
->
second
;
cache
.
erase
(
cnode_it
);
hashTable
.
erase
(
key
);
}
string
GET
(
string
key
)
{
string
ans
=
search
(
key
);
return
ans
;
}
string
PUT
(
string
key
,
string
value
)
{
string
ans
=
search
(
key
);
if
(
ans
==
""
)
{
insert
(
key
,
value
);
}
else
{
unordered_map
<
string
,
list
<
cacheNode
>
::
iterator
>
::
iterator
it
=
hashTable
.
find
(
key
);
it
->
second
->
value
=
value
;
}
}
/*
* Testing code
*/
...
...
@@ -141,7 +160,7 @@ int main() {
cout
<<
p
.
second
->
key
<<
" "
<<
p
.
second
->
value
<<
" "
<<
p
.
second
->
dirtyBit
<<
endl
;
}
printf
(
"-----------------------------
\n
"
);
del
(
key5
);
DEL
(
key5
);
for
(
auto
p
:
hashTable
)
{
cout
<<
p
.
first
<<
" : "
;
cout
<<
p
.
second
->
key
<<
" "
<<
p
.
second
->
value
<<
" "
<<
p
.
second
->
dirtyBit
<<
endl
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment