Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PREVIEW] deprecate last and pop, implement insert, erase, capacity #1

Open
wants to merge 3 commits into
base: parser_string_view
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions python/pylibvw.cc
Original file line number Diff line number Diff line change
Expand Up @@ -381,11 +381,12 @@ void ex_push_dictionary(example_ptr ec, vw_ptr vw, py::dict& dict)

bool ex_pop_feature(example_ptr ec, unsigned char ns)
{ if (ec->feature_space[ns].size() == 0) return false;
float val = ec->feature_space[ns].values.pop();
float val = ec->feature_space[ns].values.back();
ec->feature_space[ns].values.pop_back();
if (ec->feature_space[ns].indicies.size()> 0)
ec->feature_space[ns].indicies.pop();
ec->feature_space[ns].indicies.pop_back();
if (ec->feature_space[ns].space_names.size()> 0)
ec->feature_space[ns].space_names.pop();
ec->feature_space[ns].space_names.pop_back();
ec->num_features--;
ec->feature_space[ns].sum_feat_sq -= val * val;
ec->total_sum_feat_sq -= val * val;
Expand All @@ -401,7 +402,8 @@ void ex_erase_namespace(example_ptr ec, unsigned char ns)

bool ex_pop_namespace(example_ptr ec)
{ if (ec->indices.size() == 0) return false;
unsigned char ns = ec->indices.pop();
unsigned char ns = ec->indices.back();
ec->indices.pop_back();
ex_erase_namespace(ec, ns);
return true;
}
Expand Down Expand Up @@ -446,7 +448,7 @@ void unsetup_example(vw_ptr vwP, example_ptr ae)
if (hit_constant >= 0)
{ for (size_t i=hit_constant; i<N-1; i++)
ae->indices[i] = ae->indices[i+1];
ae->indices.pop();
ae->indices.pop_back();
}
}

Expand Down
2 changes: 1 addition & 1 deletion vowpalwabbit/autolink.cc
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ void VW::autolink::reset_example(example& ec)
features& fs = ec.feature_space[autolink_namespace];
ec.total_sum_feat_sq -= fs.sum_feat_sq;
fs.clear();
ec.indices.pop();
ec.indices.pop_back();
}

template <bool is_learn>
Expand Down
2 changes: 1 addition & 1 deletion vowpalwabbit/comp_io.cc
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ bool comp_io_buf::close_file()
gzclose(gz_files.back());
gz_files.pop_back();
if (!files.empty())
files.pop();
files.pop_back();
return true;
}
return false;
Expand Down
8 changes: 4 additions & 4 deletions vowpalwabbit/conditional_contextual_bandit.cc
Original file line number Diff line number Diff line change
Expand Up @@ -250,13 +250,13 @@ void inject_slot_id(ccb& data, example* shared, size_t id)
template <bool audit>
void remove_slot_id(example* shared)
{
shared->feature_space[ccb_id_namespace].indicies.pop();
shared->feature_space[ccb_id_namespace].values.pop();
shared->indices.pop();
shared->feature_space[ccb_id_namespace].indicies.pop_back();
shared->feature_space[ccb_id_namespace].values.pop_back();
shared->indices.pop_back();

if (audit)
{
shared->feature_space[ccb_id_namespace].space_names.pop();
shared->feature_space[ccb_id_namespace].space_names.pop_back();
}
}

Expand Down
4 changes: 2 additions & 2 deletions vowpalwabbit/csoaa.cc
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ void unsubtract_example(example* ec)
return;
}

if (ec->indices.last() != wap_ldf_namespace)
if (ec->indices.back() != wap_ldf_namespace)
{
std::cerr
<< "internal error (bug): trying to unsubtract_example, but either it wasn't added, or something was added "
Expand All @@ -241,7 +241,7 @@ void unsubtract_example(example* ec)
ec->num_features -= fs.size();
ec->total_sum_feat_sq -= fs.sum_feat_sq;
fs.clear();
ec->indices.decr();
ec->indices.pop_back();
}

void make_single_prediction(ldf& data, single_learner& base, example& ec)
Expand Down
2 changes: 1 addition & 1 deletion vowpalwabbit/ect.cc
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ size_t create_circuit(ect& e, uint64_t max_label, uint64_t eliminations)
e.directions[direction_index].loser = 0;
}
if (tournaments[t].size() % 2 == 1)
new_tournaments[t].push_back(tournaments[t].last());
new_tournaments[t].push_back(tournaments[t].back());
}
e.all_levels.push_back(new_tournaments);
level++;
Expand Down
2 changes: 1 addition & 1 deletion vowpalwabbit/example.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ inline int example_is_newline(example const& ec)
{ // if only index is constant namespace or no index
if (!ec.tag.empty())
return false;
return ((ec.indices.empty()) || ((ec.indices.size() == 1) && (ec.indices.last() == constant_namespace)));
return ((ec.indices.empty()) || ((ec.indices.size() == 1) && (ec.indices.back() == constant_namespace)));
}

inline bool valid_ns(char c) { return !(c == '|' || c == ':'); }
Expand Down
2 changes: 1 addition & 1 deletion vowpalwabbit/ezexample.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ class ezexample

current_seed = past_seeds.back();
past_seeds.pop_back();
ec->indices.pop();
ec->indices.pop_back();
example_changed_since_prediction = true;
}
}
Expand Down
18 changes: 8 additions & 10 deletions vowpalwabbit/interact.cc
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,13 @@ void predict_or_learn(interact& in, LEARNER::single_learner& base, example& ec)
std::cout<< std::endl;*/

// remove 2nd namespace
int n2_i = -1;
for (size_t i = 0; i < ec.indices.size(); i++)
size_t n2_i = 0;
size_t indices_original_size = ec.indices.size();
for (; n2_i < indices_original_size; ++n2_i)
{
if (ec.indices[i] == in.n2)
if (ec.indices[n2_i] == in.n2)
{
n2_i = (int)i;
memmove(&ec.indices[n2_i], &ec.indices[n2_i + 1], sizeof(unsigned char) * (ec.indices.size() - n2_i - 1));
ec.indices.decr();
ec.indices.erase(n2_i);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It just works? It's that easy?!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see its a member function, still great!

break;
}
}
Expand All @@ -139,10 +138,9 @@ void predict_or_learn(interact& in, LEARNER::single_learner& base, example& ec)
if (is_learn)
base.learn(ec);

// re-insert namespace into the right position
ec.indices.incr();
memmove(&ec.indices[n2_i + 1], &ec.indices[n2_i], sizeof(unsigned char) * (ec.indices.size() - n2_i - 1));
ec.indices[n2_i] = in.n2;
// re-insert namespace into the right position (if one was removed)
if (n2_i < indices_original_size)
ec.indices.insert(n2_i, in.n2);

f1.deep_copy_from(in.feat_store);
ec.total_sum_feat_sq = in.total_sum_feat_sq;
Expand Down
2 changes: 1 addition & 1 deletion vowpalwabbit/io_buf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ void io_buf::buf_write(char*& pointer, size_t n)
flush();
else // Array is short, so increase size.
{
space.resize(2 * (space.end_array - space.begin()));
space.resize(2 * space.capacity());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, thankyou!

space.end() = space.begin();
head = space.begin();
}
Expand Down
5 changes: 3 additions & 2 deletions vowpalwabbit/io_buf.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ class io_buf
if (space.end_array - space.end() == 0)
{ // reallocate to twice as much space
size_t head_loc = head - space.begin();
space.resize(2 * (space.end_array - space.begin()));
space.resize(2 * space.capacity());
head = space.begin() + head_loc;
}
// read more bytes from file up to the remaining allocated space
Expand Down Expand Up @@ -204,7 +204,8 @@ class io_buf
{
if (!files.empty())
{
close_file_or_socket(files.pop());
close_file_or_socket(files.back());
files.pop_back();
return true;
}
return false;
Expand Down
8 changes: 4 additions & 4 deletions vowpalwabbit/kernel_svm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -523,9 +523,9 @@ int remove(svm_params& params, size_t svi)
}
svi_e->~svm_example();
free(svi_e);
model->support_vec.pop();
model->alpha.pop();
model->delta.pop();
model->support_vec.pop_back();
model->alpha.pop_back();
model->delta.pop_back();
model->num_support--;
// shift cache
int alloc = 0;
Expand All @@ -536,7 +536,7 @@ int remove(svm_params& params, size_t svi)
if (svi < rowsize)
{
for (size_t i = svi; i < rowsize - 1; i++) e->krow[i] = e->krow[i + 1];
e->krow.pop();
e->krow.pop_back();
alloc -= 1;
}
}
Expand Down
4 changes: 2 additions & 2 deletions vowpalwabbit/label_dictionary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ void del_example_namespace(example& ec, namespace_index ns, features& fs)
features& del_target = ec.feature_space[(size_t)ns];
assert(del_target.size() >= fs.size());
assert(ec.indices.size() > 0);
if (ec.indices.last() == ns && ec.feature_space[(size_t)ns].size() == fs.size())
ec.indices.pop();
if (ec.indices.back() == ns && ec.feature_space[(size_t)ns].size() == fs.size())
ec.indices.pop_back();
ec.total_sum_feat_sq -= fs.sum_feat_sq;
ec.num_features -= fs.size();
del_target.truncate_to(del_target.size() - fs.size());
Expand Down
4 changes: 2 additions & 2 deletions vowpalwabbit/lda_core.cc
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,7 @@ void learn_batch(lda &l)
eta = l.all->eta * l.powf((float)l.example_t, -l.all->power_t);
minuseta = 1.0f - eta;
eta *= l.lda_D / batch_size;
l.decay_levels.push_back(l.decay_levels.last() + log(minuseta));
l.decay_levels.push_back(l.decay_levels.back() + log(minuseta));

l.digammas.clear();
float additional = (float)(l.all->length()) * l.lda_rho;
Expand Down Expand Up @@ -1259,7 +1259,7 @@ void end_examples(lda &l, T &weights)
for (typename T::iterator iter = weights.begin(); iter != weights.end(); ++iter)
{
float decay_component =
l.decay_levels.last() - l.decay_levels.end()[(int)(-1 - l.example_t + (&(*iter))[l.all->lda])];
l.decay_levels.back() - l.decay_levels.end()[(int)(-1 - l.example_t + (&(*iter))[l.all->lda])];
float decay = fmin(1.f, correctedExp(decay_component));

weight *wp = &(*iter);
Expand Down
4 changes: 2 additions & 2 deletions vowpalwabbit/memory_tree.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ void remove_at_index(v_array<T>& array, uint32_t index)
}
if (index == array.size() - 1)
{
array.pop();
array.pop_back();
return;
}
for (size_t i = index + 1; i < array.size(); i++)
{
array[i - 1] = array[i];
}
array.pop();
array.pop_back();
return;
}

Expand Down
3 changes: 2 additions & 1 deletion vowpalwabbit/mwt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ void predict_or_learn(mwt& c, single_learner& base, example& ec)
if (exclude || learn)
while (!c.indices.empty())
{
unsigned char ns = c.indices.pop();
unsigned char ns = c.indices.back();
c.indices.pop_back();
std::swap(c.feature_space[ns], ec.feature_space[ns]);
}

Expand Down
2 changes: 1 addition & 1 deletion vowpalwabbit/nn.cc
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ void predict_or_learn_multi(nn& n, single_learner& base, example& ec)
ec.total_sum_feat_sq -= tmp_sum_feat_sq;
ec.feature_space[nn_output_namespace].sum_feat_sq = 0;
std::swap(ec.feature_space[nn_output_namespace], save_nn_output_namespace);
ec.indices.pop();
ec.indices.pop_back();
}
else
{
Expand Down
9 changes: 5 additions & 4 deletions vowpalwabbit/parse_example.cc
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ class TC_parser
d = '.';
else
d = '#';
// if ((spelling.size() == 0) || (spelling.last() != d))
// if ((spelling.size() == 0) || (spelling.back() != d))
_spelling.push_back(d);
}

Expand Down Expand Up @@ -444,10 +444,11 @@ void substring_to_example(vw* all, example* ae, VW::string_view example)
std::vector<VW::string_view> tokenized;
tokenize(' ', label_space, all->p->words);
if (all->p->words.size() > 0 &&
(all->p->words.last().end() == label_space.end() ||
all->p->words.last().front() == '\'')) // The last field is a tag, so record and strip it off
(all->p->words.back().end() == label_space.end() ||
all->p->words.back().front() == '\'')) // The last field is a tag, so record and strip it off
{
VW::string_view tag = all->p->words.pop();
VW::string_view tag = all->p->words.back();
all->p->words.pop_back();
if (tag.front() == '\'')
tag.remove_prefix(1);
push_many(ae->tag, tag.begin(), tag.size());
Expand Down
13 changes: 7 additions & 6 deletions vowpalwabbit/parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ void reset_source(vw& all, size_t numbits)
input->close_file();
else
{
int fd = input->files.pop();
int fd = input->files.back();
input->files.pop_back();
const auto& fps = all.final_prediction_sink;

// If the current popped file is not in the list of final predictions sinks, close it.
Expand Down Expand Up @@ -208,7 +209,7 @@ void finalize_source(parser* p)
#else
int f = fileno(stdin);
#endif
while (!p->input->files.empty() && p->input->files.last() == f) p->input->files.pop();
while (!p->input->files.empty() && p->input->files.back() == f) p->input->files.pop_back();
p->input->close_files();

delete p->input;
Expand Down Expand Up @@ -586,9 +587,9 @@ void set_done(vw& all)
void addgrams(vw& all, size_t ngram, size_t skip_gram, features& fs, size_t initial_length, v_array<size_t>& gram_mask,
size_t skips)
{
if (ngram == 0 && gram_mask.last() < initial_length)
if (ngram == 0 && gram_mask.back() < initial_length)
{
size_t last = initial_length - gram_mask.last();
size_t last = initial_length - gram_mask.back();
for (size_t i = 0; i < last; i++)
{
uint64_t new_index = fs.indicies[i];
Expand All @@ -610,9 +611,9 @@ void addgrams(vw& all, size_t ngram, size_t skip_gram, features& fs, size_t init
}
if (ngram > 0)
{
gram_mask.push_back(gram_mask.last() + 1 + skips);
gram_mask.push_back(gram_mask.back() + 1 + skips);
addgrams(all, ngram - 1, skip_gram, fs, initial_length, gram_mask, 0);
gram_mask.pop();
gram_mask.pop_back();
}
if (skip_gram > 0 && ngram > 0)
addgrams(all, ngram, skip_gram - 1, fs, initial_length, gram_mask, skips + 1);
Expand Down
2 changes: 1 addition & 1 deletion vowpalwabbit/recall_tree.cc
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ void remove_node_id_feature(recall_tree& /* b */, uint32_t /* cn */, example& ec
{
features& fs = ec.feature_space[node_id_namespace];
fs.clear();
ec.indices.pop();
ec.indices.pop_back();
}

uint32_t oas_predict(recall_tree& b, single_learner& base, uint32_t cn, example& ec)
Expand Down
14 changes: 7 additions & 7 deletions vowpalwabbit/search.cc
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ void add_new_feature(search_private& priv, float val, uint64_t idx)
uint64_t idx2 = ((idx & mask) >> ss) & mask;
features& fs = priv.dat_new_feature_ec->feature_space[priv.dat_new_feature_namespace];
fs.push_back(val * priv.dat_new_feature_value, ((priv.dat_new_feature_idx + idx2) << ss));
cdbg << "adding: " << fs.indicies.last() << ':' << fs.values.last() << endl;
cdbg << "adding: " << fs.indicies.back() << ':' << fs.values.back() << endl;
if (priv.all->audit)
{
std::stringstream temp;
Expand All @@ -628,17 +628,17 @@ void add_new_feature(search_private& priv, float val, uint64_t idx)

void del_features_in_top_namespace(search_private& /* priv */, example& ec, size_t ns)
{
if ((ec.indices.size() == 0) || (ec.indices.last() != ns))
if ((ec.indices.size() == 0) || (ec.indices.back() != ns))
{
return;
// if (ec.indices.size() == 0)
//{ THROW("internal error (bug): expecting top namespace to be '" << ns << "' but it was empty"); }
// else
//{ THROW("internal error (bug): expecting top namespace to be '" << ns << "' but it was " <<
//(size_t)ec.indices.last()); }
//(size_t)ec.indices.back()); }
}
features& fs = ec.feature_space[ns];
ec.indices.decr();
ec.indices.pop_back();
ec.num_features -= fs.size();
ec.total_sum_feat_sq -= fs.sum_feat_sq;
fs.clear();
Expand Down Expand Up @@ -895,7 +895,7 @@ void add_example_conditioning(search_private& priv, example& ec, size_t conditio

void del_example_conditioning(search_private& priv, example& ec)
{
if ((ec.indices.size() > 0) && (ec.indices.last() == conditioning_namespace))
if ((ec.indices.size() > 0) && (ec.indices.back() == conditioning_namespace))
del_features_in_top_namespace(priv, ec, conditioning_namespace);
}

Expand Down Expand Up @@ -1080,7 +1080,7 @@ void allowed_actions_to_label(search_private& priv, size_t ec_cnt, const action*
template <class T>
void ensure_size(v_array<T>& A, size_t sz)
{
if ((size_t)(A.end_array - A.begin()) < sz)
if (A.capacity() < sz)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

A.resize(sz * 2 + 1);
A.end() = A.begin() + sz;
}
Expand Down Expand Up @@ -3484,7 +3484,7 @@ action predictor::predict()
: sch.predict(*ec, my_tag, orA, oracle_actions.size(), cOn, cNa, alA, numAlA, alAcosts, learner_id, weight);

if (condition_on_names.size() > 0)
condition_on_names.pop(); // un-null-terminate
condition_on_names.pop_back(); // un-null-terminate
return p;
}
} // namespace Search
Expand Down
Loading