StrIdx 0.1.4 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/thread_pool.hpp CHANGED
@@ -21,14 +21,21 @@ public:
21
21
 
22
22
  // Creating worker threads
23
23
  for (size_t i = 0; i < num_threads; ++i) {
24
- workerThreads.emplace_back([this] {
24
+ workDone.push_back(false);
25
+ workerThreads.emplace_back([this, i] {
25
26
  while (true) {
27
+
28
+ // std::lock_guard<std::mutex) mu_guard(mu_done);
26
29
  std::function<void()> task;
27
30
  {
28
31
  std::unique_lock<std::mutex> lock(mu_queue);
29
32
 
30
33
  // Waiting until there is a task to execute or the pool is stopped
31
- cv_.wait(lock, [this] { return !taskQueue.empty() || stop_; });
34
+
35
+ workDone[i] = true;
36
+ cv_.wait(lock, [this, i] { return !taskQueue.empty() || stop_; });
37
+
38
+ workDone[i] = false;
32
39
 
33
40
  // Exit the thread in case the pool is stopped and there are no tasks
34
41
  if (stop_ && taskQueue.empty()) {
@@ -39,7 +46,6 @@ public:
39
46
  task = std::move(taskQueue.front());
40
47
  taskQueue.pop();
41
48
  }
42
-
43
49
  task();
44
50
  }
45
51
  });
@@ -68,11 +74,18 @@ public:
68
74
  while (true) {
69
75
  {
70
76
  std::lock_guard<std::mutex> guard(mu_queue);
71
- if (taskQueue.empty()) {
77
+ bool done = true;
78
+
79
+ for (auto x : workDone) {
80
+ if (x == false) {
81
+ done = false;
82
+ }
83
+ }
84
+ if (done && taskQueue.empty()) {
72
85
  return;
73
86
  }
74
87
  }
75
- std::this_thread::sleep_for(std::chrono::milliseconds(50));
88
+ std::this_thread::sleep_for(std::chrono::milliseconds(10));
76
89
  }
77
90
  }
78
91
 
@@ -89,6 +102,8 @@ private:
89
102
  std::vector<std::thread> workerThreads;
90
103
  std::queue<std::function<void()>> taskQueue;
91
104
  std::mutex mu_queue;
105
+ std::vector<bool> workDone;
106
+ std::mutex mu_done;
92
107
 
93
108
  // Condition variable to signal changes in the state of the tasks queue
94
109
  std::condition_variable cv_;
data/unittest.cpp CHANGED
@@ -3,6 +3,8 @@
3
3
  #include "stridx.hpp"
4
4
  #include <cmath>
5
5
  #include <memory>
6
+ #include <chrono>
7
+ #include <thread>
6
8
 
7
9
  TEST(SplitString, MatchSize) {
8
10
  std::vector<std::string> svec = StrIdx::splitString("foo/bar/test1.txt", '/');
@@ -13,7 +15,8 @@ TEST(SplitString, MatchSize) {
13
15
  }
14
16
  }
15
17
 
16
- std::vector<std::string> flist{"./drivers/char/hw_random/nomadik-rng.c",
18
+ std::vector<std::string> flist{
19
+ "./drivers/char/hw_random/nomadik-rng.c",
17
20
  "./drivers/pinctrl/nomadik",
18
21
  "./drivers/clk/clk-nomadik.c",
19
22
  "./drivers/gpio/gpio-nomadik.c",
@@ -24,17 +27,37 @@ std::vector<std::string> flist{"./drivers/char/hw_random/nomadik-rng.c",
24
27
  "./drivers/pinctrl/nomadik/pinctrl-nomadik.c",
25
28
  "./drivers/input/keyboard/nomadik-ske-keypad.c",
26
29
  "./drivers/pinctrl/nomadik/pinctrl-nomadik-db8500.c",
27
- "./drivers/pinctrl/nomadik/pinctrl-nomadik-stn8815.c",
30
+ "./drivers/pinctrl/nomadik/pinctrl-nomadik-stn8816.c",
28
31
  "./drivers/char/hw_random/omap-rng.c",
29
32
  "./drivers/char/hw_random/omap3-rom-rng.c",
30
33
  "./include/dt-bindings/pinctrl/nomadik.h",
31
34
  "./Documentation/devicetree/bindings/arm/ste-nomadik.txt"};
32
35
 
33
- std::vector<float> target_scores{0.342944, 0.271396, 0.271126, 0.270893, 0.270431, 0.270355,
36
+ std::vector<float> target_scores{0.342944, 0.271396, 0.271126, 0.270893, 0.270431, 0.270355,
34
37
  0.270088, 0.270088, 0.26987, 0.269776, 0.269574, 0.269538,
35
38
  0.236358, 0.236074, 0.224804, 0.224238};
36
39
 
37
- void scoreTest(bool threaded) {
40
+ void createIndex(bool threaded) {
41
+
42
+ StrIdx::StringIndex idx('/'); // Separate directories using unix style "/" char
43
+ std::string query = "rngnomadriv";
44
+
45
+ int i = 1;
46
+ for (const auto &str : flist) {
47
+ if (threaded && i > 2) {
48
+ idx.addStrToIndexThreaded(str, i);
49
+ } else {
50
+ idx.addStrToIndex(str, i);
51
+ }
52
+ i++;
53
+ }
54
+
55
+ idx.waitUntilReady();
56
+
57
+ EXPECT_EQ(idx.size(), 16);
58
+ }
59
+
60
+ void scoreTest(bool threaded, bool runSearch) {
38
61
 
39
62
  StrIdx::StringIndex idx('/'); // Separate directories using unix style "/" char
40
63
  std::string query = "rngnomadriv";
@@ -48,22 +71,41 @@ void scoreTest(bool threaded) {
48
71
  }
49
72
  i++;
50
73
  }
51
- const std::vector<std::pair<float, int>> &results = idx.findSimilar(query);
52
-
53
- std::cout << results[0].first;
54
- EXPECT_EQ(results[0].second, 1);
55
- if (results.size() == 16) {
56
- int i = 0;
57
- for (const auto &res : results) {
58
- // Check if first five digits of the scores match
59
- EXPECT_EQ(std::floor(res.first * 1e5), std::floor(1e5 * target_scores[i]));
60
- i++;
74
+
75
+ idx.waitUntilReady();
76
+ std::this_thread::sleep_for(std::chrono::milliseconds(1000));
77
+ std::cout << "s:" << idx.size() << ":";
78
+ EXPECT_EQ(idx.size(), 16);
79
+
80
+ if (runSearch) {
81
+ const std::vector<std::pair<float, int>> &results = idx.findSimilar(query);
82
+
83
+ std::cout << results[0].first;
84
+ EXPECT_EQ(results[0].second, 1);
85
+ if (results.size() == 16) {
86
+ int i = 0;
87
+ for (const auto &res : results) {
88
+ // Check if first five digits of the scores match
89
+ std::cout<< "{" << res.first << " " << target_scores[i] << "\n";
90
+ EXPECT_EQ(std::floor(res.first * 1e5), std::floor(1e5 * target_scores[i]));
91
+ i++;
92
+ }
61
93
  }
62
94
  }
95
+
96
+ // EXPECT_EQ(0,1);
63
97
  }
64
98
 
65
- TEST(IndexSearch, MatchingScoresSingleThread) { scoreTest(false); }
66
- TEST(IndexSearch, MatchingScoresThreaded) { scoreTest(true); }
99
+ TEST(Index, Create) { createIndex(false); }
100
+ TEST(Index, CreateThreaded) { createIndex(true); }
101
+ TEST(IndexSearch, MatchingScoresSingleThread) {
102
+ scoreTest(false, true);
103
+ }
104
+ TEST(IndexSearch, MatchingScoresThreaded) {
105
+ for (int i = 0; i < 3; i++) {
106
+ scoreTest(true, true);
107
+ }
108
+ }
67
109
 
68
110
  class IndexTest : public testing::Test {
69
111
  protected:
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: StrIdx
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sami Sieranoja
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-06-18 00:00:00.000000000 Z
11
+ date: 2025-06-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -178,7 +178,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
178
178
  - !ruby/object:Gem::Version
179
179
  version: '0'
180
180
  requirements: []
181
- rubygems_version: 3.3.26
181
+ rubygems_version: 3.4.20
182
182
  signing_key:
183
183
  specification_version: 4
184
184
  summary: StrIdx